From ad291040a3be5b084d4acd32ea8283eb43bda283 Mon Sep 17 00:00:00 2001 From: Jared Wasinger Date: Tue, 13 Feb 2024 21:27:50 -0800 Subject: [PATCH] fix some things that broke in the latest rebase. fix the cross-validator --- cmd/stateless/main.go | 2 +- cmd/stateless/server.go | 10 +++++++++- cmd/utils/stateless.go | 9 +++++---- consensus/beacon/consensus.go | 1 - core/blockchain.go | 2 +- core/state/statedb.go | 4 ++-- tests/block_test.go | 8 ++++---- tests/block_test_util.go | 15 ++++++++++----- trie/secure_trie.go | 10 ++++------ 9 files changed, 36 insertions(+), 25 deletions(-) diff --git a/cmd/stateless/main.go b/cmd/stateless/main.go index fd226bdf17..a8233ea9c7 100644 --- a/cmd/stateless/main.go +++ b/cmd/stateless/main.go @@ -81,7 +81,7 @@ var ( Usage: "", ArgsUsage: "server --chain-config /path/to/chain-config.json", Flags: []cli.Flag{ChainConfigFlag}, - Description: `Runs an HTTP server which provides an API endpoint for stateless block verification`, + Description: `Runs an HTTP server (temporarily hard-coded to listen on the local address at port 8080) which provides an API endpoint for stateless block verification`, } ) diff --git a/cmd/stateless/server.go b/cmd/stateless/server.go index 079d358741..5b900244c0 100644 --- a/cmd/stateless/server.go +++ b/cmd/stateless/server.go @@ -2,6 +2,7 @@ package main import ( "github.com/ethereum/go-ethereum/cmd/utils" + "github.com/ethereum/go-ethereum/params" "github.com/urfave/cli/v2" "os" "os/signal" @@ -9,7 +10,14 @@ import ( ) func server(ctx *cli.Context) error { - chainConfig := loadChainConfig(ctx.String(ChainConfigFlag.Name)) + var chainConfig *params.ChainConfig + if chainConfigFlagVal := ctx.String(ChainConfigFlag.Name); chainConfigFlagVal != "" { + chainConfig = loadChainConfig(ctx.String(ChainConfigFlag.Name)) + } else { + // TODO: instead of assuming mainnet configuration in absence of chain config + // val, accept known chain configurations via network preset flag. + chainConfig = params.MainnetChainConfig + } closeCh, _, err := utils.RunLocalServer(chainConfig, 8080) if err != nil { return err diff --git a/cmd/utils/stateless.go b/cmd/utils/stateless.go index 61a9ddafd4..5465298237 100644 --- a/cmd/utils/stateless.go +++ b/cmd/utils/stateless.go @@ -12,7 +12,7 @@ import ( "github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/params" - "github.com/ethereum/go-ethereum/trie" + "github.com/ethereum/go-ethereum/triedb" "io" "net" "net/http" @@ -25,7 +25,7 @@ func StatelessExecute(logOutput io.Writer, chainCfg *params.ChainConfig, witness } _, prestateRoot := rawdb.ReadAccountTrieNode(rawDb, nil) - db, err := state.New(prestateRoot, state.NewDatabaseWithConfig(rawDb, trie.PathDefaults), nil) + db, err := state.New(prestateRoot, state.NewDatabaseWithConfig(rawDb, triedb.PathDefaults), nil) if err != nil { return common.Hash{}, err } @@ -49,8 +49,9 @@ func StatelessExecute(logOutput io.Writer, chainCfg *params.ChainConfig, witness return root, nil } -// RunLocalServer runs an http server at the specified port (or 0 to use a random port). -// The server provides a POST endpoint /verify_block which takes input as an RLP-encoded +// RunLocalServer runs an http server on the local address at the specified +// port (or 0 to use a random port). +// The server provides a POST endpoint /verify_block which takes input as an octet-stream RLP-encoded // block witness proof in the body, executes the block proof and returns the computed state root. func RunLocalServer(chainConfig *params.ChainConfig, port int) (closeChan chan<- struct{}, actualPort int, err error) { mux := http.NewServeMux() diff --git a/consensus/beacon/consensus.go b/consensus/beacon/consensus.go index 469d0b2ce4..457e57fda8 100644 --- a/consensus/beacon/consensus.go +++ b/consensus/beacon/consensus.go @@ -30,7 +30,6 @@ import ( "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/rpc" "github.com/ethereum/go-ethereum/trie" - "github.com/holiman/uint256" ) // Proof-of-stake protocol constants. diff --git a/core/blockchain.go b/core/blockchain.go index a5e579afbd..14c457eedb 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -268,7 +268,7 @@ type BlockChain struct { // remote endpoint. If validation fails, they are dumped to the folder specified at witnessRecordingPath func NewBlockchainWithCrossValidator(endpoint string, witnessRecordingPath string, db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis, overrides *ChainOverrides, engine consensus.Engine, vmConfig vm.Config, shouldPreserve func(header *types.Header) bool, txLookupLimit *uint64) (*BlockChain, error) { bc, err := NewBlockChain(db, cacheConfig, genesis, overrides, engine, vmConfig, shouldPreserve, txLookupLimit) - bc.crossValidator = &crossValidator{witnessRecordingPath, endpoint} + bc.crossValidator = &crossValidator{endpoint, witnessRecordingPath} return bc, err } diff --git a/core/state/statedb.go b/core/state/statedb.go index 49399280f3..53d194aba5 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -1255,8 +1255,8 @@ func (s *StateDB) Witness() *Witness { func (s *StateDB) ApplyWithdrawals(withdrawals types.Withdrawals) { for _, w := range withdrawals { // Convert amount from gwei to wei. - amount := new(big.Int).SetUint64(w.Amount) - amount = amount.Mul(amount, big.NewInt(params.GWei)) + amount := new(uint256.Int).SetUint64(w.Amount) + amount = amount.Mul(amount, uint256.NewInt(params.GWei)) s.AddBalance(w.Address, amount) } diff --git a/tests/block_test.go b/tests/block_test.go index 138fabfd47..744b588f0b 100644 --- a/tests/block_test.go +++ b/tests/block_test.go @@ -163,19 +163,19 @@ func TestExecutionSpec(t *testing.T) { } func execBlockTest(t *testing.T, bt *testMatcher, test *BlockTest) { - if err := bt.checkFailure(t, test.Run(false, rawdb.HashScheme, nil)); err != nil { + if err := bt.checkFailure(t, test.Run(false, rawdb.HashScheme, nil, nil)); err != nil { t.Errorf("test in hash mode without snapshotter failed: %v", err) return } - if err := bt.checkFailure(t, test.Run(true, rawdb.HashScheme, nil)); err != nil { + if err := bt.checkFailure(t, test.Run(true, rawdb.HashScheme, nil, nil)); err != nil { t.Errorf("test in hash mode with snapshotter failed: %v", err) return } - if err := bt.checkFailure(t, test.Run(false, rawdb.PathScheme, nil)); err != nil { + if err := bt.checkFailure(t, test.Run(false, rawdb.PathScheme, nil, nil)); err != nil { t.Errorf("test in path mode without snapshotter failed: %v", err) return } - if err := bt.checkFailure(t, test.Run(true, rawdb.PathScheme, nil)); err != nil { + if err := bt.checkFailure(t, test.Run(true, rawdb.PathScheme, nil, nil)); err != nil { t.Errorf("test in path mode with snapshotter failed: %v", err) return } diff --git a/tests/block_test_util.go b/tests/block_test_util.go index 8e222df02f..4481145e76 100644 --- a/tests/block_test_util.go +++ b/tests/block_test_util.go @@ -112,15 +112,15 @@ type btHeaderMarshaling struct { ExcessBlobGas *math.HexOrDecimal64 } -func (t *BlockTest) Run(snapshotter bool, scheme string, tracer vm.EVMLogger) error { - return t.run(false, snapshotter, scheme, tracer) +func (t *BlockTest) Run(snapshotter bool, scheme string, tracer vm.EVMLogger, postCheck func(error, *core.BlockChain)) error { + return t.run(false, snapshotter, scheme, tracer, postCheck) } -func (t *BlockTest) RunStateless(snapshotter bool, scheme string, tracer vm.EVMLogger) error { - return t.run(true, snapshotter, scheme, tracer) +func (t *BlockTest) RunStateless(snapshotter bool, scheme string, tracer vm.EVMLogger, postCheck func(error, *core.BlockChain)) error { + return t.run(true, snapshotter, scheme, tracer, postCheck) } -func (t *BlockTest) run(stateless bool, snapshotter bool, scheme string, tracer vm.EVMLogger) error { +func (t *BlockTest) run(stateless bool, snapshotter bool, scheme string, tracer vm.EVMLogger, postCheck func(error, *core.BlockChain)) (result error) { config, ok := Forks[t.json.Network] if !ok { return UnsupportedForkError{t.json.Network} @@ -191,6 +191,11 @@ func (t *BlockTest) run(stateless bool, snapshotter bool, scheme string, tracer if err != nil { return err } + // Import succeeded: regardless of whether the _test_ succeeds or not, schedule + // the post-check to run + if postCheck != nil { + defer postCheck(result, chain) + } cmlast := chain.CurrentBlock().Hash() if common.Hash(t.json.BestBlock) != cmlast { return fmt.Errorf("last block hash validation mismatch: want: %x, have: %x", t.json.BestBlock, cmlast) diff --git a/trie/secure_trie.go b/trie/secure_trie.go index d7bac514d5..1bd37d36ed 100644 --- a/trie/secure_trie.go +++ b/trie/secure_trie.go @@ -220,13 +220,11 @@ func (t *StateTrie) AccessList() map[string][]byte { func (t *StateTrie) CommitAndObtainAccessList(collectLeaf bool) (common.Hash, *trienode.NodeSet, map[string][]byte, error) { // Write all the pre-images to the actual disk database if len(t.getSecKeyCache()) > 0 { - if t.preimages != nil { - preimages := make(map[common.Hash][]byte) - for hk, key := range t.secKeyCache { - preimages[common.BytesToHash([]byte(hk))] = key - } - t.preimages.insertPreimage(preimages) + preimages := make(map[common.Hash][]byte) + for hk, key := range t.secKeyCache { + preimages[common.BytesToHash([]byte(hk))] = key } + t.db.InsertPreimage(preimages) t.secKeyCache = make(map[string][]byte) } // Commit the trie and return its modified nodeset.