-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Add eth_getProof support for historical blocks #7115
Changes from 5 commits
0290cef
b0f0e58
0f74ead
0ddb7a0
5220b5b
28f061b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,24 +5,29 @@ import ( | |
"errors" | ||
"fmt" | ||
"math/big" | ||
"os" | ||
|
||
"github.com/holiman/uint256" | ||
libcommon "github.com/ledgerwatch/erigon-lib/common" | ||
"github.com/ledgerwatch/erigon-lib/common/datadir" | ||
"github.com/ledgerwatch/erigon-lib/gointerfaces" | ||
txpool_proto "github.com/ledgerwatch/erigon-lib/gointerfaces/txpool" | ||
"github.com/ledgerwatch/erigon-lib/kv" | ||
"github.com/ledgerwatch/erigon-lib/kv/memdb" | ||
types2 "github.com/ledgerwatch/erigon-lib/types" | ||
"github.com/ledgerwatch/log/v3" | ||
"google.golang.org/grpc" | ||
|
||
"github.com/ledgerwatch/erigon/common" | ||
"github.com/ledgerwatch/erigon/common/hexutil" | ||
"github.com/ledgerwatch/erigon/core" | ||
"github.com/ledgerwatch/erigon/core/rawdb" | ||
"github.com/ledgerwatch/erigon/core/state" | ||
"github.com/ledgerwatch/erigon/core/types" | ||
"github.com/ledgerwatch/erigon/core/types/accounts" | ||
"github.com/ledgerwatch/erigon/core/vm" | ||
"github.com/ledgerwatch/erigon/crypto" | ||
"github.com/ledgerwatch/erigon/eth/stagedsync" | ||
"github.com/ledgerwatch/erigon/eth/tracers/logger" | ||
"github.com/ledgerwatch/erigon/params" | ||
"github.com/ledgerwatch/erigon/rpc" | ||
|
@@ -303,7 +308,19 @@ func (api *APIImpl) EstimateGas(ctx context.Context, argsOrNil *ethapi2.CallArgs | |
return hexutil.Uint64(hi), nil | ||
} | ||
|
||
// GetProof is partially implemented; no Storage proofs; only for the latest block | ||
// maxGetProofRewindBlockCount limits the number of blocks into the past that | ||
// GetProof will allow computing proofs. Because we must rewind the hash state | ||
// and re-compute the state trie, the further back in time the request, the more | ||
// computationally intensive the operation becomes. The staged sync code | ||
// assumes that if more than 100_000 blocks are skipped, that the entire trie | ||
// should be re-computed. Re-computing the entire trie will currently take ~15 | ||
// minutes on mainnet. The current limit has been chosen arbitrarily as | ||
// 'useful' without likely being overly computationally intense. This parameter | ||
// could possibly be made configurable in the future if needed. | ||
var maxGetProofRewindBlockCount uint64 = 1_000 | ||
|
||
// GetProof is partially implemented; no Storage proofs, and proofs must be for | ||
// blocks within maxGetProofRewindBlockCount blocks of the head. | ||
func (api *APIImpl) GetProof(ctx context.Context, address libcommon.Address, storageKeys []libcommon.Hash, blockNrOrHash rpc.BlockNumberOrHash) (*accounts.AccProofResult, error) { | ||
|
||
tx, err := api.db.BeginRo(ctx) | ||
|
@@ -312,59 +329,101 @@ func (api *APIImpl) GetProof(ctx context.Context, address libcommon.Address, sto | |
} | ||
defer tx.Rollback() | ||
|
||
blockNr, _, _, err := rpchelper.GetBlockNumber(blockNrOrHash, tx, api.filters) | ||
blockNr, blockHash, _, err := rpchelper.GetBlockNumber(blockNrOrHash, tx, api.filters) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
header := rawdb.ReadHeader(tx, blockHash, blockNr) | ||
if header == nil { | ||
return nil, err | ||
} | ||
|
||
latestBlock, err := rpchelper.GetLatestBlockNumber(tx) | ||
if err != nil { | ||
return nil, err | ||
} else if blockNr != latestBlock { | ||
return nil, fmt.Errorf(NotImplemented, "eth_getProof for block != latest") | ||
} else if len(storageKeys) != 0 { | ||
return nil, fmt.Errorf(NotImplemented, "eth_getProof with storageKeys") | ||
} else { | ||
addrHash, err := common.HashData(address[:]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
if latestBlock < blockNr { | ||
// shouldn't happen, but check anyway | ||
return nil, fmt.Errorf("block number is in the future") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's add more info to the error: values of latestBlock, blockNr There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. I don't think it's actually reachable, but, can't hurt to have more context in the error. |
||
} | ||
|
||
rl := trie.NewRetainList(0) | ||
rl.AddKey(addrHash[:]) | ||
if len(storageKeys) != 0 { | ||
return nil, fmt.Errorf(NotImplemented, "eth_getProof with storageKeys") | ||
} | ||
|
||
loader := trie.NewFlatDBTrieLoader("getProof") | ||
if err := loader.Reset(rl, nil, nil, false); err != nil { | ||
return nil, err | ||
} | ||
addrHash, err := common.HashData(address[:]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var accProof accounts.AccProofResult | ||
accProof.Address = address | ||
rl := trie.NewRetainList(0) | ||
rl.AddKey(addrHash[:]) | ||
|
||
// Fill in the Account fields here to reduce the code changes | ||
// needed in turbo/trie/hashbuilder.go | ||
reader, err := rpchelper.CreateStateReader(ctx, tx, blockNrOrHash, 0, api.filters, api.stateCache, api.historyV3(tx), "") | ||
if err != nil { | ||
return nil, err | ||
var loader *trie.FlatDBTrieLoader | ||
if blockNr < latestBlock { | ||
if latestBlock-blockNr > maxGetProofRewindBlockCount { | ||
return nil, fmt.Errorf("requested block is too old, block must be within %d blocks of the head block number (currently %d)", maxGetProofRewindBlockCount, latestBlock) | ||
} | ||
a, err := reader.ReadAccountData(address) | ||
tmpDir, err := os.MkdirTemp("", "eth_getHash") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i advise youse existing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't like this part of the PR, but, wasn't really sure of the best way to do it and didn't see any of the other RPC daemon commands referencing a tempdir. My best guess is that you're referring to the datadir temp dir? I see there is a datadir on the CLI flags, so, I've pushed a commit on top that wires the I'm not sure if that's what you were hoping for, so please let me know if you had something different in mind, happy to take a different approach. |
||
if err != nil { | ||
return nil, err | ||
} | ||
if a != nil { | ||
accProof.Balance = (*hexutil.Big)(a.Balance.ToBig()) | ||
accProof.CodeHash = a.CodeHash | ||
accProof.Nonce = hexutil.Uint64(a.Nonce) | ||
accProof.StorageHash = a.Root | ||
defer os.RemoveAll(tmpDir) | ||
dirs := datadir.New(tmpDir) | ||
|
||
batch := memdb.NewMemoryBatch(tx, dirs.Tmp) | ||
defer batch.Rollback() | ||
|
||
unwindState := &stagedsync.UnwindState{UnwindPoint: blockNr} | ||
stageState := &stagedsync.StageState{BlockNumber: latestBlock} | ||
|
||
hashStageCfg := stagedsync.StageHashStateCfg(nil, dirs, api.historyV3(batch), api._agg) | ||
if err := stagedsync.UnwindHashStateStage(unwindState, stageState, batch, hashStageCfg, ctx); err != nil { | ||
return nil, err | ||
} | ||
|
||
loader.SetProofReturn(&accProof) | ||
_, err = loader.CalcTrieRoot(tx, nil, nil) | ||
interHashStageCfg := stagedsync.StageTrieCfg(nil, false, false, false, dirs.Tmp, api._blockReader, nil, api.historyV3(batch), api._agg) | ||
loader, err = stagedsync.UnwindIntermediateHashesForTrieLoader("eth_getProof", rl, unwindState, stageState, batch, interHashStageCfg, nil, nil, ctx.Done()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &accProof, nil | ||
tx = batch | ||
} else { | ||
loader = trie.NewFlatDBTrieLoader("eth_getProof", rl, nil, nil, false) | ||
} | ||
|
||
var accProof accounts.AccProofResult | ||
accProof.Address = address | ||
|
||
// Fill in the Account fields here to reduce the code changes | ||
// needed in turbo/trie/hashbuilder.go | ||
reader, err := rpchelper.CreateStateReader(ctx, tx, blockNrOrHash, 0, api.filters, api.stateCache, api.historyV3(tx), "") | ||
if err != nil { | ||
return nil, err | ||
} | ||
a, err := reader.ReadAccountData(address) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if a != nil { | ||
accProof.Balance = (*hexutil.Big)(a.Balance.ToBig()) | ||
accProof.CodeHash = a.CodeHash | ||
accProof.Nonce = hexutil.Uint64(a.Nonce) | ||
accProof.StorageHash = a.Root | ||
} | ||
|
||
loader.SetProofReturn(&accProof) | ||
root, err := loader.CalcTrieRoot(tx, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if root != header.Root { | ||
return nil, fmt.Errorf("mismatch in expected state root computed %v vs %v indicates bug in proof implementation", root, header.Root) | ||
} | ||
return &accProof, nil | ||
} | ||
|
||
func (api *APIImpl) tryBlockFromLru(hash libcommon.Hash) *types.Block { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use
api._blockReader.HeaderByNumber
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done