Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

graphql: fix transaction API #23052

Merged
merged 1 commit into from
Jun 22, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions graphql/graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/eth/filters"
Expand Down Expand Up @@ -94,7 +93,11 @@ func (a *Account) Balance(ctx context.Context) (hexutil.Big, error) {
if err != nil {
return hexutil.Big{}, err
}
return hexutil.Big(*state.GetBalance(a.address)), nil
balance := state.GetBalance(a.address)
if balance == nil {
return hexutil.Big{}, fmt.Errorf("failed to load balance %x", a.address)
}
return hexutil.Big(*balance), nil
}

func (a *Account) TransactionCount(ctx context.Context) (hexutil.Uint64, error) {
Expand Down Expand Up @@ -179,18 +182,20 @@ type Transaction struct {
// resolve returns the internal transaction object, fetching it if needed.
func (t *Transaction) resolve(ctx context.Context) (*types.Transaction, error) {
if t.tx == nil {
tx, blockHash, _, index := rawdb.ReadTransaction(t.backend.ChainDb(), t.hash)
if tx != nil {
// Try to return an already finalized transaction
tx, blockHash, _, index, err := t.backend.GetTransaction(ctx, t.hash)
if err == nil && tx != nil {
t.tx = tx
blockNrOrHash := rpc.BlockNumberOrHashWithHash(blockHash, false)
t.block = &Block{
backend: t.backend,
numberOrHash: &blockNrOrHash,
}
t.index = index
} else {
t.tx = t.backend.GetPoolTransaction(t.hash)
return t.tx, nil
}
// No finalized transaction, try to retrieve it from the pool
t.tx = t.backend.GetPoolTransaction(t.hash)
}
return t.tx, nil
}
Expand Down Expand Up @@ -271,6 +276,9 @@ func (t *Transaction) Value(ctx context.Context) (hexutil.Big, error) {
if err != nil || tx == nil {
return hexutil.Big{}, err
}
if tx.Value() == nil {
return hexutil.Big{}, fmt.Errorf("invalid transaction value %x", t.hash)
}
return hexutil.Big(*tx.Value()), nil
}

Expand Down Expand Up @@ -706,7 +714,11 @@ func (b *Block) TotalDifficulty(ctx context.Context) (hexutil.Big, error) {
}
h = header.Hash()
}
return hexutil.Big(*b.backend.GetTd(ctx, h)), nil
td := b.backend.GetTd(ctx, h)
if td == nil {
return hexutil.Big{}, fmt.Errorf("total difficulty not found %x", b.hash)
}
return hexutil.Big(*td), nil
}

// BlockNumberArgs encapsulates arguments to accessors that specify a block number.
Expand Down