Added sendRawTransaction mutation

This commit is contained in:
Nick Johnson 2018-10-16 11:51:06 +01:00
parent 75aeee1c30
commit 42bbab1b03
2 changed files with 38 additions and 18 deletions

View file

@ -34,6 +34,7 @@ import (
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/node" "github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/rpc" "github.com/ethereum/go-ethereum/rpc"
graphql "github.com/graph-gophers/graphql-go" graphql "github.com/graph-gophers/graphql-go"
"github.com/graph-gophers/graphql-go/relay" "github.com/graph-gophers/graphql-go/relay"
@ -764,7 +765,7 @@ func (b *Block) OmmerAt(ctx context.Context, args ArrayIndexArgs) (*Block, error
}, nil }, nil
} }
type Query struct { type Resolver struct {
node *node.Node node *node.Node
} }
@ -773,23 +774,23 @@ type BlockArgs struct {
Hash *Bytes32 Hash *Bytes32
} }
func (q *Query) Block(ctx context.Context, args BlockArgs) (*Block, error) { func (r *Resolver) Block(ctx context.Context, args BlockArgs) (*Block, error) {
var block *Block var block *Block
if args.Number != nil { if args.Number != nil {
num := rpc.BlockNumber(uint64(*args.Number)) num := rpc.BlockNumber(uint64(*args.Number))
block = &Block{ block = &Block{
node: q.node, node: r.node,
num: &num, num: &num,
} }
} else if args.Hash != nil { } else if args.Hash != nil {
block = &Block{ block = &Block{
node: q.node, node: r.node,
hash: args.Hash.Hash, hash: args.Hash.Hash,
} }
} else { } else {
num := rpc.LatestBlockNumber num := rpc.LatestBlockNumber
block = &Block{ block = &Block{
node: q.node, node: r.node,
num: &num, num: &num,
} }
} }
@ -809,8 +810,8 @@ type BlocksArgs struct {
To *int32 To *int32
} }
func (q *Query) Blocks(ctx context.Context, args BlocksArgs) ([]*Block, error) { func (r *Resolver) Blocks(ctx context.Context, args BlocksArgs) ([]*Block, error) {
be, err := getBackend(q.node) be, err := getBackend(r.node)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -832,7 +833,7 @@ func (q *Query) Blocks(ctx context.Context, args BlocksArgs) ([]*Block, error) {
for i := from; i <= to; i++ { for i := from; i <= to; i++ {
num := i num := i
ret = append(ret, &Block{ ret = append(ret, &Block{
node: q.node, node: r.node,
num: &num, num: &num,
}) })
} }
@ -844,14 +845,14 @@ type AccountArgs struct {
BlockNumber *int32 BlockNumber *int32
} }
func (q *Query) Account(ctx context.Context, args AccountArgs) *Account { func (r *Resolver) Account(ctx context.Context, args AccountArgs) *Account {
blockNumber := rpc.LatestBlockNumber blockNumber := rpc.LatestBlockNumber
if args.BlockNumber != nil { if args.BlockNumber != nil {
blockNumber = rpc.BlockNumber(*args.BlockNumber) blockNumber = rpc.BlockNumber(*args.BlockNumber)
} }
return &Account{ return &Account{
node: q.node, node: r.node,
address: args.Address.Address, address: args.Address.Address,
blockNumber: blockNumber, blockNumber: blockNumber,
} }
@ -861,9 +862,9 @@ type TransactionArgs struct {
Hash Bytes32 Hash Bytes32
} }
func (q *Query) Transaction(ctx context.Context, args TransactionArgs) (*Transaction, error) { func (r *Resolver) Transaction(ctx context.Context, args TransactionArgs) (*Transaction, error) {
tx := &Transaction{ tx := &Transaction{
node: q.node, node: r.node,
hash: args.Hash.Hash, hash: args.Hash.Hash,
} }
@ -877,8 +878,22 @@ func (q *Query) Transaction(ctx context.Context, args TransactionArgs) (*Transac
return tx, nil return tx, nil
} }
func (r *Resolver) SendRawTransaction(ctx context.Context, args struct{ Data HexBytes }) (Bytes32, error) {
be, err := getBackend(r.node)
if err != nil {
return Bytes32{}, err
}
tx := new(types.Transaction)
if err := rlp.DecodeBytes(args.Data.Bytes, tx); err != nil {
return Bytes32{}, err
}
hash, err := ethapi.SubmitTransaction(ctx, be, tx)
return Bytes32{hash}, err
}
func NewHandler(n *node.Node) (http.Handler, error) { func NewHandler(n *node.Node) (http.Handler, error) {
q := Query{n} q := Resolver{n}
s := ` s := `
scalar Bytes32 scalar Bytes32
@ -888,6 +903,7 @@ func NewHandler(n *node.Node) (http.Handler, error) {
schema { schema {
query: Query query: Query
mutation: Mutation
} }
type Account { type Account {
@ -957,6 +973,10 @@ func NewHandler(n *node.Node) (http.Handler, error) {
blocks(from: Int!, to: Int): [Block!]! blocks(from: Int!, to: Int): [Block!]!
transaction(hash: Bytes32!): Transaction transaction(hash: Bytes32!): Transaction
} }
type Mutation {
sendRawTransaction(data: HexBytes!): Bytes32!
}
` `
schema, err := graphql.ParseSchema(s, &q) schema, err := graphql.ParseSchema(s, &q)
if err != nil { if err != nil {

View file

@ -378,7 +378,7 @@ func (s *PrivateAccountAPI) SendTransaction(ctx context.Context, args SendTxArgs
log.Warn("Failed transaction send attempt", "from", args.From, "to", args.To, "value", args.Value.ToInt(), "err", err) log.Warn("Failed transaction send attempt", "from", args.From, "to", args.To, "value", args.Value.ToInt(), "err", err)
return common.Hash{}, err return common.Hash{}, err
} }
return submitTransaction(ctx, s.b, signed) return SubmitTransaction(ctx, s.b, signed)
} }
// SignTransaction will create a transaction from the given arguments and // SignTransaction will create a transaction from the given arguments and
@ -1181,8 +1181,8 @@ func (args *SendTxArgs) toTransaction() *types.Transaction {
return types.NewTransaction(uint64(*args.Nonce), *args.To, (*big.Int)(args.Value), uint64(*args.Gas), (*big.Int)(args.GasPrice), input) return types.NewTransaction(uint64(*args.Nonce), *args.To, (*big.Int)(args.Value), uint64(*args.Gas), (*big.Int)(args.GasPrice), input)
} }
// submitTransaction is a helper function that submits tx to txPool and logs a message. // SubmitTransaction is a helper function that submits tx to txPool and logs a message.
func submitTransaction(ctx context.Context, b Backend, tx *types.Transaction) (common.Hash, error) { func SubmitTransaction(ctx context.Context, b Backend, tx *types.Transaction) (common.Hash, error) {
if err := b.SendTx(ctx, tx); err != nil { if err := b.SendTx(ctx, tx); err != nil {
return common.Hash{}, err return common.Hash{}, err
} }
@ -1234,7 +1234,7 @@ func (s *PublicTransactionPoolAPI) SendTransaction(ctx context.Context, args Sen
if err != nil { if err != nil {
return common.Hash{}, err return common.Hash{}, err
} }
return submitTransaction(ctx, s.b, signed) return SubmitTransaction(ctx, s.b, signed)
} }
// SendRawTransaction will add the signed transaction to the transaction pool. // SendRawTransaction will add the signed transaction to the transaction pool.
@ -1244,7 +1244,7 @@ func (s *PublicTransactionPoolAPI) SendRawTransaction(ctx context.Context, encod
if err := rlp.DecodeBytes(encodedTx, tx); err != nil { if err := rlp.DecodeBytes(encodedTx, tx); err != nil {
return common.Hash{}, err return common.Hash{}, err
} }
return submitTransaction(ctx, s.b, tx) return SubmitTransaction(ctx, s.b, tx)
} }
// Sign calculates an ECDSA signature for: // Sign calculates an ECDSA signature for: