Refactored to use hexutil.Bytes instead of HexBytes

This commit is contained in:
Nick Johnson 2018-10-16 13:31:36 +01:00
parent c6c6a7a68f
commit 59e2a7f2c8
2 changed files with 47 additions and 51 deletions

View file

@ -72,6 +72,23 @@ func (b Bytes) String() string {
return Encode(b) return Encode(b)
} }
func (b Bytes) ImplementsGraphQLType(name string) bool { return name == "Bytes" }
func (b *Bytes) UnmarshalGraphQL(input interface{}) error {
var err error
switch input := input.(type) {
case string:
data, err := Decode(input)
if err != nil {
return err
}
*b = data
default:
err = fmt.Errorf("Unexpected type for Bytes: %v", input)
}
return err
}
// UnmarshalFixedJSON decodes the input as a string with 0x prefix. The length of out // UnmarshalFixedJSON decodes the input as a string with 0x prefix. The length of out
// determines the required input length. This function is commonly used to implement the // determines the required input length. This function is commonly used to implement the
// UnmarshalJSON method for fixed-size types. // UnmarshalJSON method for fixed-size types.

View file

@ -50,27 +50,6 @@ func getBackend(n *node.Node) (ethapi.Backend, error) {
return ethereum.APIBackend, nil return ethereum.APIBackend, nil
} }
type HexBytes struct {
hexutil.Bytes
}
func (h HexBytes) ImplementsGraphQLType(name string) bool { return name == "HexBytes" }
func (h *HexBytes) UnmarshalGraphQL(input interface{}) error {
var err error
switch input := input.(type) {
case string:
data, err := hexutil.Decode(input)
if err != nil {
return err
}
*h = HexBytes{data}
default:
err = fmt.Errorf("Unexpected type for HexBytes: %v", input)
}
return err
}
type Bytes32 struct { type Bytes32 struct {
common.Hash common.Hash
} }
@ -168,13 +147,13 @@ func (a *Account) TransactionCount(ctx context.Context) (int32, error) {
return int32(state.GetNonce(a.address)), nil return int32(state.GetNonce(a.address)), nil
} }
func (a *Account) Code(ctx context.Context) (HexBytes, error) { func (a *Account) Code(ctx context.Context) (hexutil.Bytes, error) {
state, err := a.getState(ctx) state, err := a.getState(ctx)
if err != nil { if err != nil {
return HexBytes{}, err return hexutil.Bytes{}, err
} }
return HexBytes{state.GetCode(a.address)}, nil return hexutil.Bytes(state.GetCode(a.address)), nil
} }
type StorageSlotArgs struct { type StorageSlotArgs struct {
@ -220,8 +199,8 @@ func (l *Log) Topics(ctx context.Context) []Bytes32 {
return ret return ret
} }
func (l *Log) Data(ctx context.Context) HexBytes { func (l *Log) Data(ctx context.Context) hexutil.Bytes {
return HexBytes{l.log.Data} return hexutil.Bytes(l.log.Data)
} }
type Transaction struct { type Transaction struct {
@ -258,12 +237,12 @@ func (tx *Transaction) Hash(ctx context.Context) Bytes32 {
return Bytes32{tx.hash} return Bytes32{tx.hash}
} }
func (t *Transaction) InputData(ctx context.Context) (HexBytes, error) { func (t *Transaction) InputData(ctx context.Context) (hexutil.Bytes, error) {
tx, err := t.resolve(ctx) tx, err := t.resolve(ctx)
if err != nil || tx == nil { if err != nil || tx == nil {
return HexBytes{}, err return hexutil.Bytes{}, err
} }
return HexBytes{tx.Data()}, nil return hexutil.Bytes(tx.Data()), nil
} }
func (t *Transaction) Gas(ctx context.Context) (int32, error) { func (t *Transaction) Gas(ctx context.Context) (int32, error) {
@ -637,20 +616,20 @@ func (b *Block) Ommers(ctx context.Context) ([]*Block, error) {
return ret, nil return ret, nil
} }
func (b *Block) ExtraData(ctx context.Context) (HexBytes, error) { func (b *Block) ExtraData(ctx context.Context) (hexutil.Bytes, error) {
block, err := b.resolve(ctx) block, err := b.resolve(ctx)
if err != nil { if err != nil {
return HexBytes{}, err return hexutil.Bytes{}, err
} }
return HexBytes{block.Extra()}, nil return hexutil.Bytes(block.Extra()), nil
} }
func (b *Block) LogsBloom(ctx context.Context) (HexBytes, error) { func (b *Block) LogsBloom(ctx context.Context) (hexutil.Bytes, error) {
block, err := b.resolve(ctx) block, err := b.resolve(ctx)
if err != nil { if err != nil {
return HexBytes{}, err return hexutil.Bytes{}, err
} }
return HexBytes{block.Bloom().Bytes()}, nil return hexutil.Bytes(block.Bloom().Bytes()), nil
} }
func (b *Block) TotalDifficulty(ctx context.Context) (BigNum, error) { func (b *Block) TotalDifficulty(ctx context.Context) (BigNum, error) {
@ -880,14 +859,14 @@ func (r *Resolver) Transaction(ctx context.Context, args TransactionArgs) (*Tran
return tx, nil return tx, nil
} }
func (r *Resolver) SendRawTransaction(ctx context.Context, args struct{ Data HexBytes }) (Bytes32, error) { func (r *Resolver) SendRawTransaction(ctx context.Context, args struct{ Data hexutil.Bytes }) (Bytes32, error) {
be, err := getBackend(r.node) be, err := getBackend(r.node)
if err != nil { if err != nil {
return Bytes32{}, err return Bytes32{}, err
} }
tx := new(types.Transaction) tx := new(types.Transaction)
if err := rlp.DecodeBytes(args.Data.Bytes, tx); err != nil { if err := rlp.DecodeBytes(args.Data, tx); err != nil {
return Bytes32{}, err return Bytes32{}, err
} }
hash, err := ethapi.SubmitTransaction(ctx, be, tx) hash, err := ethapi.SubmitTransaction(ctx, be, tx)
@ -900,17 +879,17 @@ type CallData struct {
Gas *int32 Gas *int32
GasPrice *BigNum GasPrice *BigNum
Value *BigNum Value *BigNum
Data *HexBytes Data *hexutil.Bytes
BlockNumber *int32 BlockNumber *int32
} }
type CallResult struct { type CallResult struct {
data HexBytes data hexutil.Bytes
gasUsed int32 gasUsed int32
status int32 status int32
} }
func (c *CallResult) Data() HexBytes { func (c *CallResult) Data() hexutil.Bytes {
return c.data return c.data
} }
@ -941,7 +920,7 @@ func convertCallData(data CallData) (ethapi.CallArgs, rpc.BlockNumber) {
callArgs.Value = hexutil.Big(*data.Value.Int) callArgs.Value = hexutil.Big(*data.Value.Int)
} }
if data.Data != nil { if data.Data != nil {
callArgs.Data = data.Data.Bytes callArgs.Data = *data.Data
} }
blockNumber := rpc.LatestBlockNumber blockNumber := rpc.LatestBlockNumber
@ -966,7 +945,7 @@ func (r *Resolver) Call(ctx context.Context, args struct{ Data CallData }) (*Cal
status = 0 status = 0
} }
return &CallResult{ return &CallResult{
data: HexBytes{result}, data: hexutil.Bytes(result),
gasUsed: int32(gas), gasUsed: int32(gas),
status: status, status: status,
}, err }, err
@ -990,7 +969,7 @@ func NewHandler(n *node.Node) (http.Handler, error) {
s := ` s := `
scalar Bytes32 scalar Bytes32
scalar Address scalar Address
scalar HexBytes scalar Bytes
scalar BigNum scalar BigNum
schema { schema {
@ -1002,7 +981,7 @@ func NewHandler(n *node.Node) (http.Handler, error) {
address: Address! address: Address!
balance: BigNum! balance: BigNum!
transactionCount: Int! transactionCount: Int!
code: HexBytes! code: Bytes!
storage(slot: Bytes32!): Bytes32! storage(slot: Bytes32!): Bytes32!
} }
@ -1010,7 +989,7 @@ func NewHandler(n *node.Node) (http.Handler, error) {
index: Int! index: Int!
account(block: Int): Account! account(block: Int): Account!
topics: [Bytes32!]! topics: [Bytes32!]!
data: HexBytes! data: Bytes!
transaction: Transaction! transaction: Transaction!
} }
@ -1023,7 +1002,7 @@ func NewHandler(n *node.Node) (http.Handler, error) {
value: BigNum! value: BigNum!
gasPrice: BigNum! gasPrice: BigNum!
gas: Int! gas: Int!
inputData: HexBytes! inputData: Bytes!
block: Block block: Block
status: Int status: Int
@ -1043,11 +1022,11 @@ func NewHandler(n *node.Node) (http.Handler, error) {
stateRoot: Bytes32! stateRoot: Bytes32!
receiptsRoot: Bytes32! receiptsRoot: Bytes32!
miner(block: Int): Account! miner(block: Int): Account!
extraData: HexBytes! extraData: Bytes!
gasLimit: Int! gasLimit: Int!
gasUsed: Int! gasUsed: Int!
timestamp: BigNum! timestamp: BigNum!
logsBloom: HexBytes! logsBloom: Bytes!
mixHash: Bytes32! mixHash: Bytes32!
difficulty: BigNum! difficulty: BigNum!
totalDifficulty: BigNum! totalDifficulty: BigNum!
@ -1065,12 +1044,12 @@ func NewHandler(n *node.Node) (http.Handler, error) {
gas: Int gas: Int
gasPrice: BigNum gasPrice: BigNum
value: BigNum value: BigNum
data: HexBytes data: Bytes
blockNumber: Int blockNumber: Int
} }
type CallResult { type CallResult {
data: HexBytes! data: Bytes!
gasUsed: Int! gasUsed: Int!
status: Int! status: Int!
} }
@ -1085,7 +1064,7 @@ func NewHandler(n *node.Node) (http.Handler, error) {
} }
type Mutation { type Mutation {
sendRawTransaction(data: HexBytes!): Bytes32! sendRawTransaction(data: Bytes!): Bytes32!
} }
` `
schema, err := graphql.ParseSchema(s, &q) schema, err := graphql.ParseSchema(s, &q)