Skip to content

Commit bef78ef

Browse files
authored
graphql: fix transaction API (ethereum#23052)
1 parent ddf1025 commit bef78ef

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

graphql/graphql.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
"github.com/ethereum/go-ethereum/common"
3030
"github.com/ethereum/go-ethereum/common/hexutil"
3131
"github.com/ethereum/go-ethereum/common/math"
32-
"github.com/ethereum/go-ethereum/core/rawdb"
3332
"github.com/ethereum/go-ethereum/core/state"
3433
"github.com/ethereum/go-ethereum/core/types"
3534
"github.com/ethereum/go-ethereum/eth/filters"
@@ -94,7 +93,11 @@ func (a *Account) Balance(ctx context.Context) (hexutil.Big, error) {
9493
if err != nil {
9594
return hexutil.Big{}, err
9695
}
97-
return hexutil.Big(*state.GetBalance(a.address)), nil
96+
balance := state.GetBalance(a.address)
97+
if balance == nil {
98+
return hexutil.Big{}, fmt.Errorf("failed to load balance %x", a.address)
99+
}
100+
return hexutil.Big(*balance), nil
98101
}
99102

100103
func (a *Account) TransactionCount(ctx context.Context) (hexutil.Uint64, error) {
@@ -179,18 +182,20 @@ type Transaction struct {
179182
// resolve returns the internal transaction object, fetching it if needed.
180183
func (t *Transaction) resolve(ctx context.Context) (*types.Transaction, error) {
181184
if t.tx == nil {
182-
tx, blockHash, _, index := rawdb.ReadTransaction(t.backend.ChainDb(), t.hash)
183-
if tx != nil {
185+
// Try to return an already finalized transaction
186+
tx, blockHash, _, index, err := t.backend.GetTransaction(ctx, t.hash)
187+
if err == nil && tx != nil {
184188
t.tx = tx
185189
blockNrOrHash := rpc.BlockNumberOrHashWithHash(blockHash, false)
186190
t.block = &Block{
187191
backend: t.backend,
188192
numberOrHash: &blockNrOrHash,
189193
}
190194
t.index = index
191-
} else {
192-
t.tx = t.backend.GetPoolTransaction(t.hash)
195+
return t.tx, nil
193196
}
197+
// No finalized transaction, try to retrieve it from the pool
198+
t.tx = t.backend.GetPoolTransaction(t.hash)
194199
}
195200
return t.tx, nil
196201
}
@@ -286,6 +291,9 @@ func (t *Transaction) Value(ctx context.Context) (hexutil.Big, error) {
286291
if err != nil || tx == nil {
287292
return hexutil.Big{}, err
288293
}
294+
if tx.Value() == nil {
295+
return hexutil.Big{}, fmt.Errorf("invalid transaction value %x", t.hash)
296+
}
289297
return hexutil.Big(*tx.Value()), nil
290298
}
291299

@@ -721,7 +729,11 @@ func (b *Block) TotalDifficulty(ctx context.Context) (hexutil.Big, error) {
721729
}
722730
h = header.Hash()
723731
}
724-
return hexutil.Big(*b.backend.GetTd(ctx, h)), nil
732+
td := b.backend.GetTd(ctx, h)
733+
if td == nil {
734+
return hexutil.Big{}, fmt.Errorf("total difficulty not found %x", b.hash)
735+
}
736+
return hexutil.Big(*td), nil
725737
}
726738

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

0 commit comments

Comments
 (0)