From 6baa56bc257811b833f8b85e60f94925668b89af Mon Sep 17 00:00:00 2001 From: Shivam Sharma Date: Wed, 12 Oct 2022 20:06:00 +0530 Subject: [PATCH 01/20] add : Add state sync transaction to debug_traceBlock --- consensus/bor/statefull/processor.go | 52 +++++++-- core/vm/interface.go | 2 + eth/tracers/api.go | 159 +++++++++++++++++++++------ eth/tracers/api_test.go | 11 +- 4 files changed, 178 insertions(+), 46 deletions(-) diff --git a/consensus/bor/statefull/processor.go b/consensus/bor/statefull/processor.go index c87d4f4ff4..8ac633bb3d 100644 --- a/consensus/bor/statefull/processor.go +++ b/consensus/bor/statefull/processor.go @@ -31,22 +31,22 @@ func (c ChainContext) GetHeader(hash common.Hash, number uint64) *types.Header { } // callmsg implements core.Message to allow passing it as a transaction simulator. -type callmsg struct { +type Callmsg struct { ethereum.CallMsg } -func (m callmsg) From() common.Address { return m.CallMsg.From } -func (m callmsg) Nonce() uint64 { return 0 } -func (m callmsg) CheckNonce() bool { return false } -func (m callmsg) To() *common.Address { return m.CallMsg.To } -func (m callmsg) GasPrice() *big.Int { return m.CallMsg.GasPrice } -func (m callmsg) Gas() uint64 { return m.CallMsg.Gas } -func (m callmsg) Value() *big.Int { return m.CallMsg.Value } -func (m callmsg) Data() []byte { return m.CallMsg.Data } +func (m Callmsg) From() common.Address { return m.CallMsg.From } +func (m Callmsg) Nonce() uint64 { return 0 } +func (m Callmsg) CheckNonce() bool { return false } +func (m Callmsg) To() *common.Address { return m.CallMsg.To } +func (m Callmsg) GasPrice() *big.Int { return m.CallMsg.GasPrice } +func (m Callmsg) Gas() uint64 { return m.CallMsg.Gas } +func (m Callmsg) Value() *big.Int { return m.CallMsg.Value } +func (m Callmsg) Data() []byte { return m.CallMsg.Data } // get system message -func GetSystemMessage(toAddress common.Address, data []byte) callmsg { - return callmsg{ +func GetSystemMessage(toAddress common.Address, data []byte) Callmsg { + return Callmsg{ ethereum.CallMsg{ From: systemAddress, Gas: math.MaxUint64 / 2, @@ -61,7 +61,7 @@ func GetSystemMessage(toAddress common.Address, data []byte) callmsg { // apply message func ApplyMessage( _ context.Context, - msg callmsg, + msg Callmsg, state *state.StateDB, header *types.Header, chainConfig *params.ChainConfig, @@ -92,4 +92,32 @@ func ApplyMessage( gasUsed := initialGas - gasLeft return gasUsed, nil + +} + +func ApplyBorMessage(vmenv vm.EVM, msg Callmsg) (*core.ExecutionResult, error) { + + initialGas := msg.Gas() + + // Apply the transaction to the current state (included in the env) + ret, gasLeft, err := vmenv.Call( + vm.AccountRef(msg.From()), + *msg.To(), + msg.Data(), + msg.Gas(), + msg.Value(), + ) + // Update the state with pending changes + if err != nil { + vmenv.StateDB.Finalise(true) + } + + gasUsed := initialGas - gasLeft + + return &core.ExecutionResult{ + UsedGas: gasUsed, + Err: err, + ReturnData: ret, + }, nil + } diff --git a/core/vm/interface.go b/core/vm/interface.go index ad9b05d666..1064adf590 100644 --- a/core/vm/interface.go +++ b/core/vm/interface.go @@ -74,6 +74,8 @@ type StateDB interface { AddPreimage(common.Hash, []byte) ForEachStorage(common.Address, func(common.Hash, common.Hash) bool) error + + Finalise(bool) } // CallContext provides a basic interface for the EVM calling conventions. The EVM diff --git a/eth/tracers/api.go b/eth/tracers/api.go index 08c17601e4..12b1411ae7 100644 --- a/eth/tracers/api.go +++ b/eth/tracers/api.go @@ -28,9 +28,11 @@ import ( "sync" "time" + "github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/consensus" + "github.com/ethereum/go-ethereum/consensus/bor/statefull" "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/core/state" @@ -80,6 +82,9 @@ type Backend interface { // so this method should be called with the parent. StateAtBlock(ctx context.Context, block *types.Block, reexec uint64, base *state.StateDB, checkLive, preferDisk bool) (*state.StateDB, error) StateAtTransaction(ctx context.Context, block *types.Block, txIndex int, reexec uint64) (core.Message, vm.BlockContext, *state.StateDB, error) + + // Bor related APIs + GetBorBlockTransactionWithBlockHash(ctx context.Context, txHash common.Hash, blockHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error) } // API is the collection of tracing APIs exposed over the private debugging endpoint. @@ -164,6 +169,25 @@ func (api *API) blockByNumberAndHash(ctx context.Context, number rpc.BlockNumber return api.blockByHash(ctx, hash) } +// returns block transactions along with state-sync transaction if present +func (api *API) getAllBlockTransactions(ctx context.Context, block *types.Block) (types.Transactions, bool) { + txs := block.Transactions() + + stateSyncPresent := false + + borReceipt := rawdb.ReadBorReceipt(api.backend.ChainDb(), block.Hash(), block.NumberU64()) + if borReceipt != nil { + txHash := types.GetDerivedBorTxHash(types.BorReceiptKey(block.Number().Uint64(), block.Hash())) + if txHash != (common.Hash{}) { + borTx, _, _, _, _ := api.backend.GetBorBlockTransactionWithBlockHash(ctx, txHash, block.Hash()) + txs = append(txs, borTx) + stateSyncPresent = true + } + } + + return txs, stateSyncPresent +} + // TraceConfig holds extra parameters to trace functions. type TraceConfig struct { *logger.Config @@ -274,19 +298,30 @@ func (api *API) traceChain(ctx context.Context, start, end *types.Block, config signer := types.MakeSigner(api.backend.ChainConfig(), task.block.Number()) blockCtx := core.NewEVMBlockContext(task.block.Header(), api.chainContext(localctx), nil) // Trace all the transactions contained within - for i, tx := range task.block.Transactions() { + txs, stateSyncPresent := api.getAllBlockTransactions(ctx, task.block) + for i, tx := range txs { msg, _ := tx.AsMessage(signer, task.block.BaseFee()) txctx := &Context{ BlockHash: task.block.Hash(), TxIndex: i, TxHash: tx.Hash(), } - res, err := api.traceTx(localctx, msg, txctx, blockCtx, task.statedb, config) + + var res interface{} + var err error + + if stateSyncPresent && i == len(txs)-1 { + res, err = api.traceTx(localctx, msg, txctx, blockCtx, task.statedb, config, true) + } else { + res, err = api.traceTx(localctx, msg, txctx, blockCtx, task.statedb, config, false) + } + if err != nil { task.results[i] = &txTraceResult{Error: err.Error()} log.Warn("Tracing failed", "hash", tx.Hash(), "block", task.block.NumberU64(), "err", err) break } + // Only delete empty objects if EIP158/161 (a.k.a Spurious Dragon) is in effect task.statedb.Finalise(api.backend.ChainConfig().IsEIP158(task.block.Number())) task.results[i] = &txTraceResult{Result: res} @@ -492,6 +527,23 @@ func (api *API) StandardTraceBlockToFile(ctx context.Context, hash common.Hash, return api.standardTraceBlockToFile(ctx, block, config) } +func prepareCallMessage(msg core.Message) statefull.Callmsg { + + return statefull.Callmsg{ + ethereum.CallMsg{ + From: msg.From(), + To: msg.To(), + Gas: msg.Gas(), + GasPrice: msg.GasPrice(), + GasFeeCap: msg.GasFeeCap(), + GasTipCap: msg.GasTipCap(), + Value: msg.Value(), + Data: msg.Data(), + AccessList: msg.AccessList(), + }} + +} + // IntermediateRoots executes a block (bad- or canon- or side-), and returns a list // of intermediate roots: the stateroot after each transaction. func (api *API) IntermediateRoots(ctx context.Context, hash common.Hash, config *TraceConfig) ([]common.Hash, error) { @@ -525,23 +577,42 @@ func (api *API) IntermediateRoots(ctx context.Context, hash common.Hash, config vmctx = core.NewEVMBlockContext(block.Header(), api.chainContext(ctx), nil) deleteEmptyObjects = chainConfig.IsEIP158(block.Number()) ) - for i, tx := range block.Transactions() { + txs, stateSyncPresent := api.getAllBlockTransactions(ctx, block) + for i, tx := range txs { var ( msg, _ = tx.AsMessage(signer, block.BaseFee()) txContext = core.NewEVMTxContext(msg) vmenv = vm.NewEVM(vmctx, txContext, statedb, chainConfig, vm.Config{}) ) statedb.Prepare(tx.Hash(), i) - if _, err := core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(msg.Gas())); err != nil { - log.Warn("Tracing intermediate roots did not complete", "txindex", i, "txhash", tx.Hash(), "err", err) - // We intentionally don't return the error here: if we do, then the RPC server will not - // return the roots. Most likely, the caller already knows that a certain transaction fails to - // be included, but still want the intermediate roots that led to that point. - // It may happen the tx_N causes an erroneous state, which in turn causes tx_N+M to not be - // executable. - // N.B: This should never happen while tracing canon blocks, only when tracing bad blocks. - return roots, nil + if stateSyncPresent && i == len(txs)-1 { + callmsg := prepareCallMessage(msg) + + if _, err := statefull.ApplyMessage(ctx, callmsg, statedb, block.Header(), api.backend.ChainConfig(), api.chainContext(ctx)); err != nil { + log.Warn("Tracing intermediate roots did not complete", "txindex", i, "txhash", tx.Hash(), "err", err) + // We intentionally don't return the error here: if we do, then the RPC server will not + // return the roots. Most likely, the caller already knows that a certain transaction fails to + // be included, but still want the intermediate roots that led to that point. + // It may happen the tx_N causes an erroneous state, which in turn causes tx_N+M to not be + // executable. + // N.B: This should never happen while tracing canon blocks, only when tracing bad blocks. + return roots, nil + } + + } else { + if _, err := core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(msg.Gas())); err != nil { + log.Warn("Tracing intermediate roots did not complete", "txindex", i, "txhash", tx.Hash(), "err", err) + // We intentionally don't return the error here: if we do, then the RPC server will not + // return the roots. Most likely, the caller already knows that a certain transaction fails to + // be included, but still want the intermediate roots that led to that point. + // It may happen the tx_N causes an erroneous state, which in turn causes tx_N+M to not be + // executable. + // N.B: This should never happen while tracing canon blocks, only when tracing bad blocks. + return roots, nil + } + } + // calling IntermediateRoot will internally call Finalize on the state // so any modifications are written to the trie roots = append(roots, statedb.IntermediateRoot(deleteEmptyObjects)) @@ -581,9 +652,9 @@ func (api *API) traceBlock(ctx context.Context, block *types.Block, config *Trac } // Execute all the transaction contained within the block concurrently var ( - signer = types.MakeSigner(api.backend.ChainConfig(), block.Number()) - txs = block.Transactions() - results = make([]*txTraceResult, len(txs)) + signer = types.MakeSigner(api.backend.ChainConfig(), block.Number()) + txs, stateSyncPresent = api.getAllBlockTransactions(ctx, block) + results = make([]*txTraceResult, len(txs)) pend = new(sync.WaitGroup) jobs = make(chan *txTraceTask, len(txs)) @@ -606,7 +677,13 @@ func (api *API) traceBlock(ctx context.Context, block *types.Block, config *Trac TxIndex: task.index, TxHash: txs[task.index].Hash(), } - res, err := api.traceTx(ctx, msg, txctx, blockCtx, task.statedb, config) + var res interface{} + var err error + if stateSyncPresent && task.index == len(txs)-1 { + res, err = api.traceTx(ctx, msg, txctx, blockCtx, task.statedb, config, true) + } else { + res, err = api.traceTx(ctx, msg, txctx, blockCtx, task.statedb, config, false) + } if err != nil { results[task.index] = &txTraceResult{Error: err.Error()} continue @@ -650,7 +727,7 @@ func (api *API) traceBlock(ctx context.Context, block *types.Block, config *Trac func (api *API) standardTraceBlockToFile(ctx context.Context, block *types.Block, config *StdTraceConfig) ([]string, error) { // If we're tracing a single transaction, make sure it's present if config != nil && config.TxHash != (common.Hash{}) { - if !containsTx(block, config.TxHash) { + if !api.containsTx(ctx, block, config.TxHash) { return nil, fmt.Errorf("transaction %#x not found in block", config.TxHash) } } @@ -705,7 +782,8 @@ func (api *API) standardTraceBlockToFile(ctx context.Context, block *types.Block canon = false } } - for i, tx := range block.Transactions() { + txs, stateSyncPresent := api.getAllBlockTransactions(ctx, block) + for i, tx := range txs { // Prepare the trasaction for un-traced execution var ( msg, _ = tx.AsMessage(signer, block.BaseFee()) @@ -739,10 +817,19 @@ func (api *API) standardTraceBlockToFile(ctx context.Context, block *types.Block // Execute the transaction and flush any traces to disk vmenv := vm.NewEVM(vmctx, txContext, statedb, chainConfig, vmConf) statedb.Prepare(tx.Hash(), i) - _, err = core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(msg.Gas())) - if writer != nil { - writer.Flush() + if stateSyncPresent && i == len(txs)-1 { + callmsg := prepareCallMessage(msg) + _, err = statefull.ApplyBorMessage(*vmenv, callmsg) + if writer != nil { + writer.Flush() + } + } else { + _, err = core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(msg.Gas())) + if writer != nil { + writer.Flush() + } } + if dump != nil { dump.Close() log.Info("Wrote standard trace", "file", dump.Name()) @@ -764,8 +851,9 @@ func (api *API) standardTraceBlockToFile(ctx context.Context, block *types.Block // containsTx reports whether the transaction with a certain hash // is contained within the specified block. -func containsTx(block *types.Block, hash common.Hash) bool { - for _, tx := range block.Transactions() { +func (api *API) containsTx(ctx context.Context, block *types.Block, hash common.Hash) bool { + txs, _ := api.getAllBlockTransactions(ctx, block) + for _, tx := range txs { if tx.Hash() == hash { return true } @@ -775,7 +863,7 @@ func containsTx(block *types.Block, hash common.Hash) bool { // TraceTransaction returns the structured logs created during the execution of EVM // and returns them as a JSON object. -func (api *API) TraceTransaction(ctx context.Context, hash common.Hash, config *TraceConfig) (interface{}, error) { +func (api *API) TraceTransaction(ctx context.Context, hash common.Hash, config *TraceConfig, borTx bool) (interface{}, error) { tx, blockHash, blockNumber, index, err := api.backend.GetTransaction(ctx, hash) if tx == nil { // For BorTransaction, there will be no trace available @@ -811,14 +899,14 @@ func (api *API) TraceTransaction(ctx context.Context, hash common.Hash, config * TxIndex: int(index), TxHash: hash, } - return api.traceTx(ctx, msg, txctx, vmctx, statedb, config) + return api.traceTx(ctx, msg, txctx, vmctx, statedb, config, borTx) } // TraceCall lets you trace a given eth_call. It collects the structured logs // created during the execution of EVM if the given transaction was added on // top of the provided block and returns them as a JSON object. // You can provide -2 as a block number to trace on top of the pending block. -func (api *API) TraceCall(ctx context.Context, args ethapi.TransactionArgs, blockNrOrHash rpc.BlockNumberOrHash, config *TraceCallConfig) (interface{}, error) { +func (api *API) TraceCall(ctx context.Context, args ethapi.TransactionArgs, blockNrOrHash rpc.BlockNumberOrHash, config *TraceCallConfig, borTx bool) (interface{}, error) { // Try to retrieve the specified block var ( err error @@ -865,13 +953,13 @@ func (api *API) TraceCall(ctx context.Context, args ethapi.TransactionArgs, bloc Reexec: config.Reexec, } } - return api.traceTx(ctx, msg, new(Context), vmctx, statedb, traceConfig) + return api.traceTx(ctx, msg, new(Context), vmctx, statedb, traceConfig, borTx) } // traceTx configures a new tracer according to the provided configuration, and // executes the given message in the provided environment. The return value will // be tracer dependent. -func (api *API) traceTx(ctx context.Context, message core.Message, txctx *Context, vmctx vm.BlockContext, statedb *state.StateDB, config *TraceConfig) (interface{}, error) { +func (api *API) traceTx(ctx context.Context, message core.Message, txctx *Context, vmctx vm.BlockContext, statedb *state.StateDB, config *TraceConfig, borTx bool) (interface{}, error) { // Assemble the structured logger or the JavaScript tracer var ( tracer vm.EVMLogger @@ -911,9 +999,18 @@ func (api *API) traceTx(ctx context.Context, message core.Message, txctx *Contex // Call Prepare to clear out the statedb access list statedb.Prepare(txctx.TxHash, txctx.TxIndex) - result, err := core.ApplyMessage(vmenv, message, new(core.GasPool).AddGas(message.Gas())) - if err != nil { - return nil, fmt.Errorf("tracing failed: %w", err) + var result *core.ExecutionResult + if borTx { + callmsg := prepareCallMessage(message) + if result, err = statefull.ApplyBorMessage(*vmenv, callmsg); err != nil { + return nil, fmt.Errorf("tracing failed: %w", err) + } + + } else { + result, err = core.ApplyMessage(vmenv, message, new(core.GasPool).AddGas(message.Gas())) + if err != nil { + return nil, fmt.Errorf("tracing failed: %w", err) + } } // Depending on the tracer type, format and return the output. diff --git a/eth/tracers/api_test.go b/eth/tracers/api_test.go index d2ed9c2179..68335239e8 100644 --- a/eth/tracers/api_test.go +++ b/eth/tracers/api_test.go @@ -176,6 +176,11 @@ func (b *testBackend) StateAtTransaction(ctx context.Context, block *types.Block return nil, vm.BlockContext{}, nil, fmt.Errorf("transaction index %d out of range for block %#x", txIndex, block.Hash()) } +func (b *testBackend) GetBorBlockTransactionWithBlockHash(ctx context.Context, txHash common.Hash, blockHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error) { + tx, blockHash, blockNumber, index := rawdb.ReadBorTransactionWithBlockHash(b.ChainDb(), txHash, blockHash) + return tx, blockHash, blockNumber, index, nil +} + func TestTraceCall(t *testing.T) { t.Parallel() @@ -285,7 +290,7 @@ func TestTraceCall(t *testing.T) { }, } for _, testspec := range testSuite { - result, err := api.TraceCall(context.Background(), testspec.call, rpc.BlockNumberOrHash{BlockNumber: &testspec.blockNumber}, testspec.config) + result, err := api.TraceCall(context.Background(), testspec.call, rpc.BlockNumberOrHash{BlockNumber: &testspec.blockNumber}, testspec.config, false) if testspec.expectErr != nil { if err == nil { t.Errorf("Expect error %v, get nothing", testspec.expectErr) @@ -325,7 +330,7 @@ func TestTraceTransaction(t *testing.T) { b.AddTx(tx) target = tx.Hash() })) - result, err := api.TraceTransaction(context.Background(), target, nil) + result, err := api.TraceTransaction(context.Background(), target, nil, false) if err != nil { t.Errorf("Failed to trace transaction %v", err) } @@ -508,7 +513,7 @@ func TestTracingWithOverrides(t *testing.T) { }, } for i, tc := range testSuite { - result, err := api.TraceCall(context.Background(), tc.call, rpc.BlockNumberOrHash{BlockNumber: &tc.blockNumber}, tc.config) + result, err := api.TraceCall(context.Background(), tc.call, rpc.BlockNumberOrHash{BlockNumber: &tc.blockNumber}, tc.config, false) if tc.expectErr != nil { if err == nil { t.Errorf("test %d: want error %v, have nothing", i, tc.expectErr) From 002717ad236018e8b5c3ac9a4c217e987f225e32 Mon Sep 17 00:00:00 2001 From: Shivam Sharma Date: Thu, 13 Oct 2022 15:36:21 +0530 Subject: [PATCH 02/20] chg : major fix --- eth/tracers/api.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/eth/tracers/api.go b/eth/tracers/api.go index 12b1411ae7..c6398d1a8a 100644 --- a/eth/tracers/api.go +++ b/eth/tracers/api.go @@ -702,11 +702,22 @@ func (api *API) traceBlock(ctx context.Context, block *types.Block, config *Trac // Generate the next state snapshot fast without tracing msg, _ := tx.AsMessage(signer, block.BaseFee()) statedb.Prepare(tx.Hash(), i) + vmenv := vm.NewEVM(blockCtx, core.NewEVMTxContext(msg), statedb, api.backend.ChainConfig(), vm.Config{}) - if _, err := core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(msg.Gas())); err != nil { - failed = err - break + + if stateSyncPresent && i == len(txs)-1 { + callmsg := prepareCallMessage(msg) + if _, err := statefull.ApplyBorMessage(*vmenv, callmsg); err != nil { + failed = err + break + } + } else { + if _, err := core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(msg.Gas())); err != nil { + failed = err + break + } } + // Finalize the state so any modifications are written to the trie // Only delete empty objects if EIP158/161 (a.k.a Spurious Dragon) is in effect statedb.Finalise(vmenv.ChainConfig().IsEIP158(block.Number())) From 0918cf73437d6bea349bd56ec1a50d773f8643b0 Mon Sep 17 00:00:00 2001 From: Shivam Sharma Date: Thu, 13 Oct 2022 15:55:45 +0530 Subject: [PATCH 03/20] chg : support state-sync in newcli server debugBorBlock --- eth/tracers/api_bor.go | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/eth/tracers/api_bor.go b/eth/tracers/api_bor.go index f42c7a27f7..b93baae432 100644 --- a/eth/tracers/api_bor.go +++ b/eth/tracers/api_bor.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/consensus/bor/statefull" "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/vm" @@ -68,14 +69,14 @@ func (api *API) traceBorBlock(ctx context.Context, block *types.Block, config *T // Execute all the transaction contained within the block concurrently var ( - signer = types.MakeSigner(api.backend.ChainConfig(), block.Number()) - txs = block.Transactions() - deleteEmptyObjects = api.backend.ChainConfig().IsEIP158(block.Number()) + signer = types.MakeSigner(api.backend.ChainConfig(), block.Number()) + txs, stateSyncPresent = api.getAllBlockTransactions(ctx, block) + deleteEmptyObjects = api.backend.ChainConfig().IsEIP158(block.Number()) ) blockCtx := core.NewEVMBlockContext(block.Header(), api.chainContext(ctx), nil) - traceTxn := func(indx int, tx *types.Transaction) *TxTraceResult { + traceTxn := func(indx int, tx *types.Transaction, borTx bool) *TxTraceResult { message, _ := tx.AsMessage(signer, block.BaseFee()) txContext := core.NewEVMTxContext(message) @@ -88,7 +89,15 @@ func (api *API) traceBorBlock(ctx context.Context, block *types.Block, config *T // Not sure if we need to do this statedb.Prepare(tx.Hash(), indx) - execRes, err := core.ApplyMessage(vmenv, message, new(core.GasPool).AddGas(message.Gas())) + var execRes *core.ExecutionResult + + if borTx { + callmsg := prepareCallMessage(message) + execRes, err = statefull.ApplyBorMessage(*vmenv, callmsg) + } else { + execRes, err = core.ApplyMessage(vmenv, message, new(core.GasPool).AddGas(message.Gas())) + } + if err != nil { return &TxTraceResult{ Error: err.Error(), @@ -115,7 +124,11 @@ func (api *API) traceBorBlock(ctx context.Context, block *types.Block, config *T } for indx, tx := range txs { - res.Transactions = append(res.Transactions, traceTxn(indx, tx)) + if stateSyncPresent && indx == len(txs)-1 { + res.Transactions = append(res.Transactions, traceTxn(indx, tx, true)) + } else { + res.Transactions = append(res.Transactions, traceTxn(indx, tx, false)) + } } return res, nil From d0508e7dc0a3a348b2cf838a74212e77f3623558 Mon Sep 17 00:00:00 2001 From: Shivam Sharma Date: Fri, 14 Oct 2022 12:53:46 +0530 Subject: [PATCH 04/20] chg : lint files --- consensus/bor/statefull/processor.go | 3 --- eth/tracers/api.go | 17 ++++++++++++----- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/consensus/bor/statefull/processor.go b/consensus/bor/statefull/processor.go index 8ac633bb3d..0fe9baeeba 100644 --- a/consensus/bor/statefull/processor.go +++ b/consensus/bor/statefull/processor.go @@ -92,11 +92,9 @@ func ApplyMessage( gasUsed := initialGas - gasLeft return gasUsed, nil - } func ApplyBorMessage(vmenv vm.EVM, msg Callmsg) (*core.ExecutionResult, error) { - initialGas := msg.Gas() // Apply the transaction to the current state (included in the env) @@ -119,5 +117,4 @@ func ApplyBorMessage(vmenv vm.EVM, msg Callmsg) (*core.ExecutionResult, error) { Err: err, ReturnData: ret, }, nil - } diff --git a/eth/tracers/api.go b/eth/tracers/api.go index c6398d1a8a..baf9417b82 100644 --- a/eth/tracers/api.go +++ b/eth/tracers/api.go @@ -308,6 +308,7 @@ func (api *API) traceChain(ctx context.Context, start, end *types.Block, config } var res interface{} + var err error if stateSyncPresent && i == len(txs)-1 { @@ -528,9 +529,8 @@ func (api *API) StandardTraceBlockToFile(ctx context.Context, hash common.Hash, } func prepareCallMessage(msg core.Message) statefull.Callmsg { - return statefull.Callmsg{ - ethereum.CallMsg{ + CallMsg: ethereum.CallMsg{ From: msg.From(), To: msg.To(), Gas: msg.Gas(), @@ -541,7 +541,6 @@ func prepareCallMessage(msg core.Message) statefull.Callmsg { Data: msg.Data(), AccessList: msg.AccessList(), }} - } // IntermediateRoots executes a block (bad- or canon- or side-), and returns a list @@ -577,6 +576,7 @@ func (api *API) IntermediateRoots(ctx context.Context, hash common.Hash, config vmctx = core.NewEVMBlockContext(block.Header(), api.chainContext(ctx), nil) deleteEmptyObjects = chainConfig.IsEIP158(block.Number()) ) + txs, stateSyncPresent := api.getAllBlockTransactions(ctx, block) for i, tx := range txs { var ( @@ -585,6 +585,7 @@ func (api *API) IntermediateRoots(ctx context.Context, hash common.Hash, config vmenv = vm.NewEVM(vmctx, txContext, statedb, chainConfig, vm.Config{}) ) statedb.Prepare(tx.Hash(), i) + if stateSyncPresent && i == len(txs)-1 { callmsg := prepareCallMessage(msg) @@ -598,7 +599,6 @@ func (api *API) IntermediateRoots(ctx context.Context, hash common.Hash, config // N.B: This should never happen while tracing canon blocks, only when tracing bad blocks. return roots, nil } - } else { if _, err := core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(msg.Gas())); err != nil { log.Warn("Tracing intermediate roots did not complete", "txindex", i, "txhash", tx.Hash(), "err", err) @@ -677,7 +677,9 @@ func (api *API) traceBlock(ctx context.Context, block *types.Block, config *Trac TxIndex: task.index, TxHash: txs[task.index].Hash(), } + var res interface{} + var err error if stateSyncPresent && task.index == len(txs)-1 { res, err = api.traceTx(ctx, msg, txctx, blockCtx, task.statedb, config, true) @@ -793,6 +795,7 @@ func (api *API) standardTraceBlockToFile(ctx context.Context, block *types.Block canon = false } } + txs, stateSyncPresent := api.getAllBlockTransactions(ctx, block) for i, tx := range txs { // Prepare the trasaction for un-traced execution @@ -828,9 +831,11 @@ func (api *API) standardTraceBlockToFile(ctx context.Context, block *types.Block // Execute the transaction and flush any traces to disk vmenv := vm.NewEVM(vmctx, txContext, statedb, chainConfig, vmConf) statedb.Prepare(tx.Hash(), i) + if stateSyncPresent && i == len(txs)-1 { callmsg := prepareCallMessage(msg) _, err = statefull.ApplyBorMessage(*vmenv, callmsg) + if writer != nil { writer.Flush() } @@ -910,6 +915,7 @@ func (api *API) TraceTransaction(ctx context.Context, hash common.Hash, config * TxIndex: int(index), TxHash: hash, } + return api.traceTx(ctx, msg, txctx, vmctx, statedb, config, borTx) } @@ -964,6 +970,7 @@ func (api *API) TraceCall(ctx context.Context, args ethapi.TransactionArgs, bloc Reexec: config.Reexec, } } + return api.traceTx(ctx, msg, new(Context), vmctx, statedb, traceConfig, borTx) } @@ -1011,12 +1018,12 @@ func (api *API) traceTx(ctx context.Context, message core.Message, txctx *Contex statedb.Prepare(txctx.TxHash, txctx.TxIndex) var result *core.ExecutionResult + if borTx { callmsg := prepareCallMessage(message) if result, err = statefull.ApplyBorMessage(*vmenv, callmsg); err != nil { return nil, fmt.Errorf("tracing failed: %w", err) } - } else { result, err = core.ApplyMessage(vmenv, message, new(core.GasPool).AddGas(message.Gas())) if err != nil { From 554712e95447b4d78d0adab8f4c5d9cc110a278f Mon Sep 17 00:00:00 2001 From: Shivam Sharma Date: Mon, 17 Oct 2022 15:44:11 +0530 Subject: [PATCH 05/20] chg : use https://github.com/maticnetwork/crand instead of JekaMas/crand --- consensus/bor/snapshot_test.go | 2 +- core/forkchoice.go | 2 +- go.mod | 2 +- go.sum | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/consensus/bor/snapshot_test.go b/consensus/bor/snapshot_test.go index 503bd18b24..68a9ba042f 100644 --- a/consensus/bor/snapshot_test.go +++ b/consensus/bor/snapshot_test.go @@ -5,7 +5,7 @@ import ( "sort" "testing" - "github.com/JekaMas/crand" + "github.com/maticnetwork/crand" "github.com/stretchr/testify/require" "pgregory.net/rapid" diff --git a/core/forkchoice.go b/core/forkchoice.go index 9b7b60243d..018afdfac9 100644 --- a/core/forkchoice.go +++ b/core/forkchoice.go @@ -20,7 +20,7 @@ import ( "errors" "math/big" - "github.com/JekaMas/crand" + "github.com/maticnetwork/crand" "github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum/common" diff --git a/go.mod b/go.mod index 18a2ca0ae7..36595ca307 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,6 @@ go 1.19 require ( github.com/Azure/azure-sdk-for-go/sdk/storage/azblob v0.3.0 github.com/BurntSushi/toml v1.1.0 - github.com/JekaMas/crand v1.0.1 github.com/JekaMas/go-grpc-net-conn v0.0.0-20220708155319-6aff21f2d13d github.com/VictoriaMetrics/fastcache v1.6.0 github.com/aws/aws-sdk-go-v2 v1.2.0 @@ -47,6 +46,7 @@ require ( github.com/jedisct1/go-minisign v0.0.0-20190909160543-45766022959e github.com/julienschmidt/httprouter v1.3.0 github.com/karalabe/usb v0.0.2 + github.com/maticnetwork/crand v1.0.2 github.com/maticnetwork/polyproto v0.0.2 github.com/mattn/go-colorable v0.1.8 github.com/mattn/go-isatty v0.0.12 diff --git a/go.sum b/go.sum index dc419821c6..96fa9d3f04 100644 --- a/go.sum +++ b/go.sum @@ -29,8 +29,6 @@ github.com/BurntSushi/toml v1.1.0 h1:ksErzDEI1khOiGPgpwuI7x2ebx/uXQNw7xJpn9Eq1+I github.com/BurntSushi/toml v1.1.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/DATA-DOG/go-sqlmock v1.3.3/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM= -github.com/JekaMas/crand v1.0.1 h1:FMPxkUQqH/hExl0aUXsr0UCGYZ4lJH9IJ5H/KbM6Y9A= -github.com/JekaMas/crand v1.0.1/go.mod h1:GGzGpMCht/tbaNQ5A4kSiKSqEoNAhhyTfSDQyIENBQU= github.com/JekaMas/go-grpc-net-conn v0.0.0-20220708155319-6aff21f2d13d h1:RO27lgfZF8s9lZ3pWyzc0gCE0RZC+6/PXbRjAa0CNp8= github.com/JekaMas/go-grpc-net-conn v0.0.0-20220708155319-6aff21f2d13d/go.mod h1:romz7UPgSYhfJkKOalzEEyV6sWtt/eAEm0nX2aOrod0= github.com/Masterminds/goutils v1.1.0 h1:zukEsf/1JZwCMgHiK3GZftabmxiCw4apj3a28RPBiVg= @@ -345,6 +343,8 @@ github.com/leanovate/gopter v0.2.9/go.mod h1:U2L/78B+KVFIx2VmW6onHJQzXtFb+p5y3y2 github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= +github.com/maticnetwork/crand v1.0.2 h1:Af0tAivC8zrxXDpGWNWVT/0s1fOz8w0eRbahZgURS8I= +github.com/maticnetwork/crand v1.0.2/go.mod h1:/NRNL3bj2eYdqpWmoIP5puxndTpi0XRxpj5ZKxfHjyg= github.com/maticnetwork/polyproto v0.0.2 h1:cPxuxbIDItdwGnucc3lZB58U8Zfe1mH73PWTGd15554= github.com/maticnetwork/polyproto v0.0.2/go.mod h1:e1mU2EXSwEpn5jM7GfNwu3AupsV6WAGoPFFfswXOF0o= github.com/matryer/moq v0.0.0-20190312154309-6cfb0558e1bd/go.mod h1:9ELz6aaclSIGnZBoaSLZ3NAl1VTufbOrXBPvtcy6WiQ= From 225a826e0e8e3067fa5839e9f30e1306aa018327 Mon Sep 17 00:00:00 2001 From: Shivam Sharma Date: Mon, 17 Oct 2022 18:00:30 +0530 Subject: [PATCH 06/20] add : TriesInMemory flag --- builder/files/config.toml | 1 + core/blockchain.go | 13 ++++--- core/blockchain_test.go | 72 +++++++++++++++++------------------ internal/cli/server/config.go | 4 ++ internal/cli/server/flags.go | 7 ++++ les/handler_test.go | 4 +- les/peer.go | 2 +- les/server_requests.go | 4 +- les/test_helper.go | 2 +- 9 files changed, 61 insertions(+), 48 deletions(-) diff --git a/builder/files/config.toml b/builder/files/config.toml index 0d01f85d71..13bf82bf93 100644 --- a/builder/files/config.toml +++ b/builder/files/config.toml @@ -122,6 +122,7 @@ syncmode = "full" # noprefetch = false # preimages = false # txlookuplimit = 2350000 + # triesinmemory = 128 [accounts] # allow-insecure-unlock = true diff --git a/core/blockchain.go b/core/blockchain.go index 48d5e966db..d49bd5f937 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -92,7 +92,6 @@ const ( txLookupCacheLimit = 1024 maxFutureBlocks = 256 maxTimeFutureBlocks = 30 - TriesInMemory = 128 // BlockChainVersion ensures that an incompatible database forces a resync from scratch. // @@ -132,6 +131,7 @@ type CacheConfig struct { TrieTimeLimit time.Duration // Time limit after which to flush the current in-memory trie to disk SnapshotLimit int // Memory allowance (MB) to use for caching snapshot entries in memory Preimages bool // Whether to store preimage of trie key to the disk + TriesInMemory uint64 // Number of recent tries to keep in memory SnapshotWait bool // Wait for snapshot construction on startup. TODO(karalabe): This is a dirty hack for testing, nuke it } @@ -144,6 +144,7 @@ var DefaultCacheConfig = &CacheConfig{ TrieTimeLimit: 5 * time.Minute, SnapshotLimit: 256, SnapshotWait: true, + TriesInMemory: 128, } // BlockChain represents the canonical chain given a database with a genesis @@ -829,7 +830,7 @@ func (bc *BlockChain) Stop() { if !bc.cacheConfig.TrieDirtyDisabled { triedb := bc.stateCache.TrieDB() - for _, offset := range []uint64{0, 1, TriesInMemory - 1} { + for _, offset := range []uint64{0, 1, DefaultCacheConfig.TriesInMemory - 1} { if number := bc.CurrentBlock().NumberU64(); number > offset { recent := bc.GetBlockByNumber(number - offset) @@ -1297,7 +1298,7 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types. triedb.Reference(root, common.Hash{}) // metadata reference to keep trie alive bc.triegc.Push(root, -int64(block.NumberU64())) - if current := block.NumberU64(); current > TriesInMemory { + if current := block.NumberU64(); current > DefaultCacheConfig.TriesInMemory { // If we exceeded our memory allowance, flush matured singleton nodes to disk var ( nodes, imgs = triedb.Size() @@ -1307,7 +1308,7 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types. triedb.Cap(limit - ethdb.IdealBatchSize) } // Find the next state trie we need to commit - chosen := current - TriesInMemory + chosen := current - DefaultCacheConfig.TriesInMemory // If we exceeded out time allowance, flush an entire trie to disk if bc.gcproc > bc.cacheConfig.TrieTimeLimit { @@ -1319,8 +1320,8 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types. } else { // If we're exceeding limits but haven't reached a large enough memory gap, // warn the user that the system is becoming unstable. - if chosen < lastWrite+TriesInMemory && bc.gcproc >= 2*bc.cacheConfig.TrieTimeLimit { - log.Info("State in memory for too long, committing", "time", bc.gcproc, "allowance", bc.cacheConfig.TrieTimeLimit, "optimum", float64(chosen-lastWrite)/TriesInMemory) + if chosen < lastWrite+DefaultCacheConfig.TriesInMemory && bc.gcproc >= 2*bc.cacheConfig.TrieTimeLimit { + log.Info("State in memory for too long, committing", "time", bc.gcproc, "allowance", DefaultCacheConfig.TriesInMemory, "optimum", float64(chosen-lastWrite)/float64((DefaultCacheConfig.TriesInMemory))) } // Flush an entire trie and restart the counters triedb.Commit(header.Root, true, nil) diff --git a/core/blockchain_test.go b/core/blockchain_test.go index b2c14f81ba..4d05d11198 100644 --- a/core/blockchain_test.go +++ b/core/blockchain_test.go @@ -1652,7 +1652,7 @@ func TestTrieForkGC(t *testing.T) { db := rawdb.NewMemoryDatabase() genesis := (&Genesis{BaseFee: big.NewInt(params.InitialBaseFee)}).MustCommit(db) - blocks, _ := GenerateChain(params.TestChainConfig, genesis, engine, db, 2*TriesInMemory, func(i int, b *BlockGen) { b.SetCoinbase(common.Address{1}) }) + blocks, _ := GenerateChain(params.TestChainConfig, genesis, engine, db, 2*int(DefaultCacheConfig.TriesInMemory), func(i int, b *BlockGen) { b.SetCoinbase(common.Address{1}) }) // Generate a bunch of fork blocks, each side forking from the canonical chain forks := make([]*types.Block, len(blocks)) @@ -1681,7 +1681,7 @@ func TestTrieForkGC(t *testing.T) { } } // Dereference all the recent tries and ensure no past trie is left in - for i := 0; i < TriesInMemory; i++ { + for i := 0; i < int(DefaultCacheConfig.TriesInMemory); i++ { chain.stateCache.TrieDB().Dereference(blocks[len(blocks)-1-i].Root()) chain.stateCache.TrieDB().Dereference(forks[len(blocks)-1-i].Root()) } @@ -1700,8 +1700,8 @@ func TestLargeReorgTrieGC(t *testing.T) { genesis := (&Genesis{BaseFee: big.NewInt(params.InitialBaseFee)}).MustCommit(db) shared, _ := GenerateChain(params.TestChainConfig, genesis, engine, db, 64, func(i int, b *BlockGen) { b.SetCoinbase(common.Address{1}) }) - original, _ := GenerateChain(params.TestChainConfig, shared[len(shared)-1], engine, db, 2*TriesInMemory, func(i int, b *BlockGen) { b.SetCoinbase(common.Address{2}) }) - competitor, _ := GenerateChain(params.TestChainConfig, shared[len(shared)-1], engine, db, 2*TriesInMemory+1, func(i int, b *BlockGen) { b.SetCoinbase(common.Address{3}) }) + original, _ := GenerateChain(params.TestChainConfig, shared[len(shared)-1], engine, db, 2*int(DefaultCacheConfig.TriesInMemory), func(i int, b *BlockGen) { b.SetCoinbase(common.Address{2}) }) + competitor, _ := GenerateChain(params.TestChainConfig, shared[len(shared)-1], engine, db, 2*int(DefaultCacheConfig.TriesInMemory)+1, func(i int, b *BlockGen) { b.SetCoinbase(common.Address{3}) }) // Import the shared chain and the original canonical one diskdb := rawdb.NewMemoryDatabase() @@ -1736,7 +1736,7 @@ func TestLargeReorgTrieGC(t *testing.T) { if _, err := chain.InsertChain(competitor[len(competitor)-2:]); err != nil { t.Fatalf("failed to finalize competitor chain: %v", err) } - for i, block := range competitor[:len(competitor)-TriesInMemory] { + for i, block := range competitor[:len(competitor)-int(DefaultCacheConfig.TriesInMemory)] { if node, _ := chain.stateCache.TrieDB().Node(block.Root()); node != nil { t.Fatalf("competitor %d: competing chain state missing", i) } @@ -1882,8 +1882,8 @@ func TestInsertReceiptChainRollback(t *testing.T) { // overtake the 'canon' chain until after it's passed canon by about 200 blocks. // // Details at: -// - https://github.com/ethereum/go-ethereum/issues/18977 -// - https://github.com/ethereum/go-ethereum/pull/18988 +// - https://github.com/ethereum/go-ethereum/issues/18977 +// - https://github.com/ethereum/go-ethereum/pull/18988 func TestLowDiffLongChain(t *testing.T) { // Generate a canonical chain to act as the main dataset engine := ethash.NewFaker() @@ -1892,7 +1892,7 @@ func TestLowDiffLongChain(t *testing.T) { // We must use a pretty long chain to ensure that the fork doesn't overtake us // until after at least 128 blocks post tip - blocks, _ := GenerateChain(params.TestChainConfig, genesis, engine, db, 6*TriesInMemory, func(i int, b *BlockGen) { + blocks, _ := GenerateChain(params.TestChainConfig, genesis, engine, db, 6*int(DefaultCacheConfig.TriesInMemory), func(i int, b *BlockGen) { b.SetCoinbase(common.Address{1}) b.OffsetTime(-9) }) @@ -1910,7 +1910,7 @@ func TestLowDiffLongChain(t *testing.T) { } // Generate fork chain, starting from an early block parent := blocks[10] - fork, _ := GenerateChain(params.TestChainConfig, parent, engine, db, 8*TriesInMemory, func(i int, b *BlockGen) { + fork, _ := GenerateChain(params.TestChainConfig, parent, engine, db, 8*int(DefaultCacheConfig.TriesInMemory), func(i int, b *BlockGen) { b.SetCoinbase(common.Address{2}) }) @@ -1979,7 +1979,7 @@ func testSideImport(t *testing.T, numCanonBlocksInSidechain, blocksBetweenCommon // Set the terminal total difficulty in the config gspec.Config.TerminalTotalDifficulty = big.NewInt(0) } - blocks, _ := GenerateChain(&chainConfig, genesis, genEngine, db, 2*TriesInMemory, func(i int, gen *BlockGen) { + blocks, _ := GenerateChain(&chainConfig, genesis, genEngine, db, 2*int(DefaultCacheConfig.TriesInMemory), func(i int, gen *BlockGen) { tx, err := types.SignTx(types.NewTransaction(nonce, common.HexToAddress("deadbeef"), big.NewInt(100), 21000, big.NewInt(int64(i+1)*params.GWei), nil), signer, key) if err != nil { t.Fatalf("failed to create tx: %v", err) @@ -1991,9 +1991,9 @@ func testSideImport(t *testing.T, numCanonBlocksInSidechain, blocksBetweenCommon t.Fatalf("block %d: failed to insert into chain: %v", n, err) } - lastPrunedIndex := len(blocks) - TriesInMemory - 1 + lastPrunedIndex := len(blocks) - int(DefaultCacheConfig.TriesInMemory) - 1 lastPrunedBlock := blocks[lastPrunedIndex] - firstNonPrunedBlock := blocks[len(blocks)-TriesInMemory] + firstNonPrunedBlock := blocks[len(blocks)-int(DefaultCacheConfig.TriesInMemory)] // Verify pruning of lastPrunedBlock if chain.HasBlockAndState(lastPrunedBlock.Hash(), lastPrunedBlock.NumberU64()) { @@ -2019,7 +2019,7 @@ func testSideImport(t *testing.T, numCanonBlocksInSidechain, blocksBetweenCommon // Generate fork chain, make it longer than canon parentIndex := lastPrunedIndex + blocksBetweenCommonAncestorAndPruneblock parent := blocks[parentIndex] - fork, _ := GenerateChain(&chainConfig, parent, genEngine, db, 2*TriesInMemory, func(i int, b *BlockGen) { + fork, _ := GenerateChain(&chainConfig, parent, genEngine, db, 2*int(DefaultCacheConfig.TriesInMemory), func(i int, b *BlockGen) { b.SetCoinbase(common.Address{2}) }) // Prepend the parent(s) @@ -2046,7 +2046,8 @@ func testSideImport(t *testing.T, numCanonBlocksInSidechain, blocksBetweenCommon // That is: the sidechain for import contains some blocks already present in canon chain. // So the blocks are // [ Cn, Cn+1, Cc, Sn+3 ... Sm] -// ^ ^ ^ pruned +// +// ^ ^ ^ pruned func TestPrunedImportSide(t *testing.T) { //glogger := log.NewGlogHandler(log.StreamHandler(os.Stdout, log.TerminalFormat(false))) //glogger.Verbosity(3) @@ -2841,9 +2842,9 @@ func BenchmarkBlockChain_1x1000Executions(b *testing.B) { // This internally leads to a sidechain import, since the blocks trigger an // ErrPrunedAncestor error. // This may e.g. happen if -// 1. Downloader rollbacks a batch of inserted blocks and exits -// 2. Downloader starts to sync again -// 3. The blocks fetched are all known and canonical blocks +// 1. Downloader rollbacks a batch of inserted blocks and exits +// 2. Downloader starts to sync again +// 3. The blocks fetched are all known and canonical blocks func TestSideImportPrunedBlocks(t *testing.T) { // Generate a canonical chain to act as the main dataset engine := ethash.NewFaker() @@ -2851,7 +2852,7 @@ func TestSideImportPrunedBlocks(t *testing.T) { genesis := (&Genesis{BaseFee: big.NewInt(params.InitialBaseFee)}).MustCommit(db) // Generate and import the canonical chain - blocks, _ := GenerateChain(params.TestChainConfig, genesis, engine, db, 2*TriesInMemory, nil) + blocks, _ := GenerateChain(params.TestChainConfig, genesis, engine, db, 2*int(DefaultCacheConfig.TriesInMemory), nil) diskdb := rawdb.NewMemoryDatabase() (&Genesis{BaseFee: big.NewInt(params.InitialBaseFee)}).MustCommit(diskdb) @@ -2863,14 +2864,14 @@ func TestSideImportPrunedBlocks(t *testing.T) { t.Fatalf("block %d: failed to insert into chain: %v", n, err) } - lastPrunedIndex := len(blocks) - TriesInMemory - 1 + lastPrunedIndex := len(blocks) - int(DefaultCacheConfig.TriesInMemory) - 1 lastPrunedBlock := blocks[lastPrunedIndex] // Verify pruning of lastPrunedBlock if chain.HasBlockAndState(lastPrunedBlock.Hash(), lastPrunedBlock.NumberU64()) { t.Errorf("Block %d not pruned", lastPrunedBlock.NumberU64()) } - firstNonPrunedBlock := blocks[len(blocks)-TriesInMemory] + firstNonPrunedBlock := blocks[len(blocks)-int(DefaultCacheConfig.TriesInMemory)] // Verify firstNonPrunedBlock is not pruned if !chain.HasBlockAndState(firstNonPrunedBlock.Hash(), firstNonPrunedBlock.NumberU64()) { t.Errorf("Block %d pruned", firstNonPrunedBlock.NumberU64()) @@ -3356,20 +3357,19 @@ func TestDeleteRecreateSlotsAcrossManyBlocks(t *testing.T) { // TestInitThenFailCreateContract tests a pretty notorious case that happened // on mainnet over blocks 7338108, 7338110 and 7338115. -// - Block 7338108: address e771789f5cccac282f23bb7add5690e1f6ca467c is initiated -// with 0.001 ether (thus created but no code) -// - Block 7338110: a CREATE2 is attempted. The CREATE2 would deploy code on -// the same address e771789f5cccac282f23bb7add5690e1f6ca467c. However, the -// deployment fails due to OOG during initcode execution -// - Block 7338115: another tx checks the balance of -// e771789f5cccac282f23bb7add5690e1f6ca467c, and the snapshotter returned it as -// zero. +// - Block 7338108: address e771789f5cccac282f23bb7add5690e1f6ca467c is initiated +// with 0.001 ether (thus created but no code) +// - Block 7338110: a CREATE2 is attempted. The CREATE2 would deploy code on +// the same address e771789f5cccac282f23bb7add5690e1f6ca467c. However, the +// deployment fails due to OOG during initcode execution +// - Block 7338115: another tx checks the balance of +// e771789f5cccac282f23bb7add5690e1f6ca467c, and the snapshotter returned it as +// zero. // // The problem being that the snapshotter maintains a destructset, and adds items // to the destructset in case something is created "onto" an existing item. // We need to either roll back the snapDestructs, or not place it into snapDestructs // in the first place. -// func TestInitThenFailCreateContract(t *testing.T) { var ( // Generate a canonical chain to act as the main dataset @@ -3558,13 +3558,13 @@ func TestEIP2718Transition(t *testing.T) { // TestEIP1559Transition tests the following: // -// 1. A transaction whose gasFeeCap is greater than the baseFee is valid. -// 2. Gas accounting for access lists on EIP-1559 transactions is correct. -// 3. Only the transaction's tip will be received by the coinbase. -// 4. The transaction sender pays for both the tip and baseFee. -// 5. The coinbase receives only the partially realized tip when -// gasFeeCap - gasTipCap < baseFee. -// 6. Legacy transaction behave as expected (e.g. gasPrice = gasFeeCap = gasTipCap). +// 1. A transaction whose gasFeeCap is greater than the baseFee is valid. +// 2. Gas accounting for access lists on EIP-1559 transactions is correct. +// 3. Only the transaction's tip will be received by the coinbase. +// 4. The transaction sender pays for both the tip and baseFee. +// 5. The coinbase receives only the partially realized tip when +// gasFeeCap - gasTipCap < baseFee. +// 6. Legacy transaction behave as expected (e.g. gasPrice = gasFeeCap = gasTipCap). func TestEIP1559Transition(t *testing.T) { var ( aa = common.HexToAddress("0x000000000000000000000000000000000000aaaa") diff --git a/internal/cli/server/config.go b/internal/cli/server/config.go index e5e3a8b03e..8bc4c3e753 100644 --- a/internal/cli/server/config.go +++ b/internal/cli/server/config.go @@ -364,6 +364,9 @@ type CacheConfig struct { // TxLookupLimit sets the maximum number of blocks from head whose tx indices are reserved. TxLookupLimit uint64 `hcl:"txlookuplimit,optional" toml:"txlookuplimit,optional"` + + // Number of block states to keep in memory (default = 128) + TriesInMemory uint64 `hcl:"triesinmemory,optional" toml:"triesinmemory,optional"` } type AccountsConfig struct { @@ -509,6 +512,7 @@ func DefaultConfig() *Config { NoPrefetch: false, Preimages: false, TxLookupLimit: 2350000, + TriesInMemory: 128, }, Accounts: &AccountsConfig{ Unlock: []string{}, diff --git a/internal/cli/server/flags.go b/internal/cli/server/flags.go index 6e93a94de0..025ce8c80c 100644 --- a/internal/cli/server/flags.go +++ b/internal/cli/server/flags.go @@ -304,6 +304,13 @@ func (c *Command) Flags() *flagset.Flagset { Default: c.cliConfig.Cache.Preimages, Group: "Cache", }) + f.Uint64Flag(&flagset.Uint64Flag{ + Name: "cache.triesinmemory", + Usage: "Number of block states (tries) to keep in memory (default = 128)", + Value: &c.cliConfig.Cache.TriesInMemory, + Default: c.cliConfig.Cache.TriesInMemory, + Group: "Cache", + }) f.Uint64Flag(&flagset.Uint64Flag{ Name: "txlookuplimit", Usage: "Number of recent blocks to maintain transactions index for (default = about 56 days, 0 = entire chain)", diff --git a/les/handler_test.go b/les/handler_test.go index aba45764b3..3ceabdf8ec 100644 --- a/les/handler_test.go +++ b/les/handler_test.go @@ -316,7 +316,7 @@ func TestGetStaleCodeLes4(t *testing.T) { testGetStaleCode(t, 4) } func testGetStaleCode(t *testing.T, protocol int) { netconfig := testnetConfig{ - blocks: core.TriesInMemory + 4, + blocks: int(core.DefaultCacheConfig.TriesInMemory) + 4, protocol: protocol, nopruning: true, } @@ -430,7 +430,7 @@ func TestGetStaleProofLes4(t *testing.T) { testGetStaleProof(t, 4) } func testGetStaleProof(t *testing.T, protocol int) { netconfig := testnetConfig{ - blocks: core.TriesInMemory + 4, + blocks: int(core.DefaultCacheConfig.TriesInMemory) + 4, protocol: protocol, nopruning: true, } diff --git a/les/peer.go b/les/peer.go index 499429739d..a525336f0a 100644 --- a/les/peer.go +++ b/les/peer.go @@ -1058,7 +1058,7 @@ func (p *clientPeer) Handshake(td *big.Int, head common.Hash, headNum uint64, ge // If local ethereum node is running in archive mode, advertise ourselves we have // all version state data. Otherwise only recent state is available. - stateRecent := uint64(core.TriesInMemory - blockSafetyMargin) + stateRecent := uint64(core.DefaultCacheConfig.TriesInMemory - blockSafetyMargin) if server.archiveMode { stateRecent = 0 } diff --git a/les/server_requests.go b/les/server_requests.go index bab5f733d5..3595a6ab38 100644 --- a/les/server_requests.go +++ b/les/server_requests.go @@ -297,7 +297,7 @@ func handleGetCode(msg Decoder) (serveRequestFn, uint64, uint64, error) { // Refuse to search stale state data in the database since looking for // a non-exist key is kind of expensive. local := bc.CurrentHeader().Number.Uint64() - if !backend.ArchiveMode() && header.Number.Uint64()+core.TriesInMemory <= local { + if !backend.ArchiveMode() && header.Number.Uint64()+core.DefaultCacheConfig.TriesInMemory <= local { p.Log().Debug("Reject stale code request", "number", header.Number.Uint64(), "head", local) p.bumpInvalid() continue @@ -396,7 +396,7 @@ func handleGetProofs(msg Decoder) (serveRequestFn, uint64, uint64, error) { // Refuse to search stale state data in the database since looking for // a non-exist key is kind of expensive. local := bc.CurrentHeader().Number.Uint64() - if !backend.ArchiveMode() && header.Number.Uint64()+core.TriesInMemory <= local { + if !backend.ArchiveMode() && header.Number.Uint64()+core.DefaultCacheConfig.TriesInMemory <= local { p.Log().Debug("Reject stale trie request", "number", header.Number.Uint64(), "head", local) p.bumpInvalid() continue diff --git a/les/test_helper.go b/les/test_helper.go index a099458353..d68370d508 100644 --- a/les/test_helper.go +++ b/les/test_helper.go @@ -377,7 +377,7 @@ func (p *testPeer) handshakeWithClient(t *testing.T, td *big.Int, head common.Ha sendList = sendList.add("serveHeaders", nil) sendList = sendList.add("serveChainSince", uint64(0)) sendList = sendList.add("serveStateSince", uint64(0)) - sendList = sendList.add("serveRecentState", uint64(core.TriesInMemory-4)) + sendList = sendList.add("serveRecentState", uint64(core.DefaultCacheConfig.TriesInMemory-4)) sendList = sendList.add("txRelay", nil) sendList = sendList.add("flowControl/BL", testBufLimit) sendList = sendList.add("flowControl/MRR", testBufRecharge) From 3db339b6fe728fa47adfeef89fb6dc64ff52d495 Mon Sep 17 00:00:00 2001 From: Shivam Sharma Date: Mon, 17 Oct 2022 18:07:32 +0530 Subject: [PATCH 07/20] add : lint --- core/blockchain_test.go | 3 +++ les/peer.go | 2 +- les/test_helper.go | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/core/blockchain_test.go b/core/blockchain_test.go index 4d05d11198..d44d563b20 100644 --- a/core/blockchain_test.go +++ b/core/blockchain_test.go @@ -1736,6 +1736,7 @@ func TestLargeReorgTrieGC(t *testing.T) { if _, err := chain.InsertChain(competitor[len(competitor)-2:]); err != nil { t.Fatalf("failed to finalize competitor chain: %v", err) } + for i, block := range competitor[:len(competitor)-int(DefaultCacheConfig.TriesInMemory)] { if node, _ := chain.stateCache.TrieDB().Node(block.Root()); node != nil { t.Fatalf("competitor %d: competing chain state missing", i) @@ -1979,6 +1980,7 @@ func testSideImport(t *testing.T, numCanonBlocksInSidechain, blocksBetweenCommon // Set the terminal total difficulty in the config gspec.Config.TerminalTotalDifficulty = big.NewInt(0) } + blocks, _ := GenerateChain(&chainConfig, genesis, genEngine, db, 2*int(DefaultCacheConfig.TriesInMemory), func(i int, gen *BlockGen) { tx, err := types.SignTx(types.NewTransaction(nonce, common.HexToAddress("deadbeef"), big.NewInt(100), 21000, big.NewInt(int64(i+1)*params.GWei), nil), signer, key) if err != nil { @@ -2871,6 +2873,7 @@ func TestSideImportPrunedBlocks(t *testing.T) { if chain.HasBlockAndState(lastPrunedBlock.Hash(), lastPrunedBlock.NumberU64()) { t.Errorf("Block %d not pruned", lastPrunedBlock.NumberU64()) } + firstNonPrunedBlock := blocks[len(blocks)-int(DefaultCacheConfig.TriesInMemory)] // Verify firstNonPrunedBlock is not pruned if !chain.HasBlockAndState(firstNonPrunedBlock.Hash(), firstNonPrunedBlock.NumberU64()) { diff --git a/les/peer.go b/les/peer.go index a525336f0a..46a88cfff7 100644 --- a/les/peer.go +++ b/les/peer.go @@ -1058,7 +1058,7 @@ func (p *clientPeer) Handshake(td *big.Int, head common.Hash, headNum uint64, ge // If local ethereum node is running in archive mode, advertise ourselves we have // all version state data. Otherwise only recent state is available. - stateRecent := uint64(core.DefaultCacheConfig.TriesInMemory - blockSafetyMargin) + stateRecent := core.DefaultCacheConfig.TriesInMemory - blockSafetyMargin if server.archiveMode { stateRecent = 0 } diff --git a/les/test_helper.go b/les/test_helper.go index d68370d508..1e6fb6653f 100644 --- a/les/test_helper.go +++ b/les/test_helper.go @@ -377,7 +377,7 @@ func (p *testPeer) handshakeWithClient(t *testing.T, td *big.Int, head common.Ha sendList = sendList.add("serveHeaders", nil) sendList = sendList.add("serveChainSince", uint64(0)) sendList = sendList.add("serveStateSince", uint64(0)) - sendList = sendList.add("serveRecentState", uint64(core.DefaultCacheConfig.TriesInMemory-4)) + sendList = sendList.add("serveRecentState", core.DefaultCacheConfig.TriesInMemory-4) sendList = sendList.add("txRelay", nil) sendList = sendList.add("flowControl/BL", testBufLimit) sendList = sendList.add("flowControl/MRR", testBufRecharge) From ec57aabf9e64d6c3f6754aa9b3ed3d483e165aac Mon Sep 17 00:00:00 2001 From: Shivam Sharma Date: Tue, 18 Oct 2022 03:56:55 +0530 Subject: [PATCH 08/20] chg : minor fix --- core/blockchain.go | 13 ++++++++----- core/blockchain_test.go | 12 ++++++------ core/tests/blockchain_sethead_test.go | 2 ++ eth/backend.go | 1 + eth/ethconfig/config.go | 1 + 5 files changed, 18 insertions(+), 11 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index d49bd5f937..5349ea8866 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -230,6 +230,9 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par if cacheConfig == nil { cacheConfig = DefaultCacheConfig } + if cacheConfig.TriesInMemory <= 0 { + cacheConfig.TriesInMemory = 128 + } bodyCache, _ := lru.New(bodyCacheLimit) bodyRLPCache, _ := lru.New(bodyCacheLimit) receiptsCache, _ := lru.New(receiptsCacheLimit) @@ -830,7 +833,7 @@ func (bc *BlockChain) Stop() { if !bc.cacheConfig.TrieDirtyDisabled { triedb := bc.stateCache.TrieDB() - for _, offset := range []uint64{0, 1, DefaultCacheConfig.TriesInMemory - 1} { + for _, offset := range []uint64{0, 1, bc.cacheConfig.TriesInMemory - 1} { if number := bc.CurrentBlock().NumberU64(); number > offset { recent := bc.GetBlockByNumber(number - offset) @@ -1298,7 +1301,7 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types. triedb.Reference(root, common.Hash{}) // metadata reference to keep trie alive bc.triegc.Push(root, -int64(block.NumberU64())) - if current := block.NumberU64(); current > DefaultCacheConfig.TriesInMemory { + if current := block.NumberU64(); current > bc.cacheConfig.TriesInMemory { // If we exceeded our memory allowance, flush matured singleton nodes to disk var ( nodes, imgs = triedb.Size() @@ -1308,7 +1311,7 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types. triedb.Cap(limit - ethdb.IdealBatchSize) } // Find the next state trie we need to commit - chosen := current - DefaultCacheConfig.TriesInMemory + chosen := current - bc.cacheConfig.TriesInMemory // If we exceeded out time allowance, flush an entire trie to disk if bc.gcproc > bc.cacheConfig.TrieTimeLimit { @@ -1320,8 +1323,8 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types. } else { // If we're exceeding limits but haven't reached a large enough memory gap, // warn the user that the system is becoming unstable. - if chosen < lastWrite+DefaultCacheConfig.TriesInMemory && bc.gcproc >= 2*bc.cacheConfig.TrieTimeLimit { - log.Info("State in memory for too long, committing", "time", bc.gcproc, "allowance", DefaultCacheConfig.TriesInMemory, "optimum", float64(chosen-lastWrite)/float64((DefaultCacheConfig.TriesInMemory))) + if chosen < lastWrite+bc.cacheConfig.TriesInMemory && bc.gcproc >= 2*bc.cacheConfig.TrieTimeLimit { + log.Info("State in memory for too long, committing", "time", bc.gcproc, "allowance", bc.cacheConfig.TriesInMemory, "optimum", float64(chosen-lastWrite)/float64((bc.cacheConfig.TriesInMemory))) } // Flush an entire trie and restart the counters triedb.Commit(header.Root, true, nil) diff --git a/core/blockchain_test.go b/core/blockchain_test.go index d44d563b20..fa6b61225e 100644 --- a/core/blockchain_test.go +++ b/core/blockchain_test.go @@ -1681,7 +1681,7 @@ func TestTrieForkGC(t *testing.T) { } } // Dereference all the recent tries and ensure no past trie is left in - for i := 0; i < int(DefaultCacheConfig.TriesInMemory); i++ { + for i := 0; i < int(chain.cacheConfig.TriesInMemory); i++ { chain.stateCache.TrieDB().Dereference(blocks[len(blocks)-1-i].Root()) chain.stateCache.TrieDB().Dereference(forks[len(blocks)-1-i].Root()) } @@ -1737,7 +1737,7 @@ func TestLargeReorgTrieGC(t *testing.T) { t.Fatalf("failed to finalize competitor chain: %v", err) } - for i, block := range competitor[:len(competitor)-int(DefaultCacheConfig.TriesInMemory)] { + for i, block := range competitor[:len(competitor)-int(chain.cacheConfig.TriesInMemory)] { if node, _ := chain.stateCache.TrieDB().Node(block.Root()); node != nil { t.Fatalf("competitor %d: competing chain state missing", i) } @@ -1993,9 +1993,9 @@ func testSideImport(t *testing.T, numCanonBlocksInSidechain, blocksBetweenCommon t.Fatalf("block %d: failed to insert into chain: %v", n, err) } - lastPrunedIndex := len(blocks) - int(DefaultCacheConfig.TriesInMemory) - 1 + lastPrunedIndex := len(blocks) - int(chain.cacheConfig.TriesInMemory) - 1 lastPrunedBlock := blocks[lastPrunedIndex] - firstNonPrunedBlock := blocks[len(blocks)-int(DefaultCacheConfig.TriesInMemory)] + firstNonPrunedBlock := blocks[len(blocks)-int(chain.cacheConfig.TriesInMemory)] // Verify pruning of lastPrunedBlock if chain.HasBlockAndState(lastPrunedBlock.Hash(), lastPrunedBlock.NumberU64()) { @@ -2866,7 +2866,7 @@ func TestSideImportPrunedBlocks(t *testing.T) { t.Fatalf("block %d: failed to insert into chain: %v", n, err) } - lastPrunedIndex := len(blocks) - int(DefaultCacheConfig.TriesInMemory) - 1 + lastPrunedIndex := len(blocks) - int(chain.cacheConfig.TriesInMemory) - 1 lastPrunedBlock := blocks[lastPrunedIndex] // Verify pruning of lastPrunedBlock @@ -2874,7 +2874,7 @@ func TestSideImportPrunedBlocks(t *testing.T) { t.Errorf("Block %d not pruned", lastPrunedBlock.NumberU64()) } - firstNonPrunedBlock := blocks[len(blocks)-int(DefaultCacheConfig.TriesInMemory)] + firstNonPrunedBlock := blocks[len(blocks)-int(chain.cacheConfig.TriesInMemory)] // Verify firstNonPrunedBlock is not pruned if !chain.HasBlockAndState(firstNonPrunedBlock.Hash(), firstNonPrunedBlock.NumberU64()) { t.Errorf("Block %d pruned", firstNonPrunedBlock.NumberU64()) diff --git a/core/tests/blockchain_sethead_test.go b/core/tests/blockchain_sethead_test.go index ad6e78697d..6160dfb48f 100644 --- a/core/tests/blockchain_sethead_test.go +++ b/core/tests/blockchain_sethead_test.go @@ -2082,6 +2082,7 @@ func testSetHead(t *testing.T, tt *rewindTest, snapshots bool) { // verifyNoGaps checks that there are no gaps after the initial set of blocks in // the database and errors if found. +// //nolint:gocognit func verifyNoGaps(t *testing.T, chain *core.BlockChain, canonical bool, inserted types.Blocks) { t.Helper() @@ -2135,6 +2136,7 @@ func verifyNoGaps(t *testing.T, chain *core.BlockChain, canonical bool, inserted // verifyCutoff checks that there are no chain data available in the chain after // the specified limit, but that it is available before. +// //nolint:gocognit func verifyCutoff(t *testing.T, chain *core.BlockChain, canonical bool, inserted types.Blocks, head int) { t.Helper() diff --git a/eth/backend.go b/eth/backend.go index cc65d9837f..824fec8914 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -218,6 +218,7 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) { TrieTimeLimit: config.TrieTimeout, SnapshotLimit: config.SnapshotCache, Preimages: config.Preimages, + TriesInMemory: config.TriesInMemory, } ) diff --git a/eth/ethconfig/config.go b/eth/ethconfig/config.go index adb36ffadd..c9272758ab 100644 --- a/eth/ethconfig/config.go +++ b/eth/ethconfig/config.go @@ -176,6 +176,7 @@ type Config struct { TrieTimeout time.Duration SnapshotCache int Preimages bool + TriesInMemory uint64 // Mining options Miner miner.Config From b2c125f81e633ece84895f750b40598392852bc8 Mon Sep 17 00:00:00 2001 From: Shivam Sharma Date: Tue, 18 Oct 2022 03:58:31 +0530 Subject: [PATCH 09/20] add : lint --- core/blockchain.go | 1 + 1 file changed, 1 insertion(+) diff --git a/core/blockchain.go b/core/blockchain.go index 5349ea8866..e1fc269759 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -230,6 +230,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par if cacheConfig == nil { cacheConfig = DefaultCacheConfig } + if cacheConfig.TriesInMemory <= 0 { cacheConfig.TriesInMemory = 128 } From c954606250c0e05a703e41db214ef4fb804d0a34 Mon Sep 17 00:00:00 2001 From: marcello33 Date: Thu, 20 Oct 2022 10:20:45 +0200 Subject: [PATCH 10/20] dev: fix: ci (#559) * dev: fix: ci * dev: fix: add default block configs * dev: fix: add sprintSize * dev: fix: revert * dev: fix: go-versions --- .github/matic-cli-config.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/matic-cli-config.yml b/.github/matic-cli-config.yml index 2b83b684c6..8bdfe82b3b 100644 --- a/.github/matic-cli-config.yml +++ b/.github/matic-cli-config.yml @@ -3,9 +3,13 @@ defaultFee: 2000 borChainId: "15001" heimdallChainId: heimdall-15001 contractsBranch: jc/v0.3.1-backport +sprintSize: 64 +blockNumber: '0' +blockTime: '2' numOfValidators: 3 numOfNonValidators: 0 ethURL: http://ganache:9545 +ethHostUser: ubuntu devnetType: docker borDockerBuildContext: "../../bor" heimdallDockerBuildContext: "https://github.com/maticnetwork/heimdall.git#develop" From aab72143d9c51283fb74358dee69bcd7fabb21e3 Mon Sep 17 00:00:00 2001 From: Jerry Date: Wed, 19 Oct 2022 14:55:31 -0700 Subject: [PATCH 11/20] Force load default tracers --- internal/cli/server/server.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/internal/cli/server/server.go b/internal/cli/server/server.go index 905de36e09..863a800e48 100644 --- a/internal/cli/server/server.go +++ b/internal/cli/server/server.go @@ -36,6 +36,10 @@ import ( "github.com/ethereum/go-ethereum/metrics/influxdb" "github.com/ethereum/go-ethereum/metrics/prometheus" "github.com/ethereum/go-ethereum/node" + + // Force-load the tracer engines to trigger registration + _ "github.com/ethereum/go-ethereum/eth/tracers/js" + _ "github.com/ethereum/go-ethereum/eth/tracers/native" ) type Server struct { From 8aab37492630a1c4d82cf3ae30a7e9e801c35b64 Mon Sep 17 00:00:00 2001 From: Manav Darji Date: Fri, 21 Oct 2022 02:15:42 +0530 Subject: [PATCH 12/20] metrics: handle values from config file (#565) * metrics: handle metrics flag from config in metrics.init() * internal/cli/server: log for metrics enabling and misconfiguration --- internal/cli/server/server.go | 10 +++++++ metrics/metrics.go | 53 ++++++++++++++++++++++++++++++++--- 2 files changed, 59 insertions(+), 4 deletions(-) diff --git a/internal/cli/server/server.go b/internal/cli/server/server.go index 863a800e48..8d68fd69f0 100644 --- a/internal/cli/server/server.go +++ b/internal/cli/server/server.go @@ -257,6 +257,12 @@ func (s *Server) Stop() { } func (s *Server) setupMetrics(config *TelemetryConfig, serviceName string) error { + // Check the global metrics if they're matching with the provided config + if metrics.Enabled != config.Enabled || metrics.EnabledExpensive != config.Expensive { + log.Warn("Metric misconfiguration, some of them might not be visible") + } + + // Update the values anyways (for services which don't need immediate attention) metrics.Enabled = config.Enabled metrics.EnabledExpensive = config.Expensive @@ -267,6 +273,10 @@ func (s *Server) setupMetrics(config *TelemetryConfig, serviceName string) error log.Info("Enabling metrics collection") + if metrics.EnabledExpensive { + log.Info("Enabling expensive metrics collection") + } + // influxdb if v1Enabled, v2Enabled := config.InfluxDB.V1Enabled, config.InfluxDB.V2Enabled; v1Enabled || v2Enabled { if v1Enabled && v2Enabled { diff --git a/metrics/metrics.go b/metrics/metrics.go index 747d6471a7..1d0133e850 100644 --- a/metrics/metrics.go +++ b/metrics/metrics.go @@ -11,7 +11,7 @@ import ( "strings" "time" - "github.com/ethereum/go-ethereum/log" + "github.com/BurntSushi/toml" ) // Enabled is checked by the constructor functions for all of the @@ -32,26 +32,71 @@ var enablerFlags = []string{"metrics"} // expensiveEnablerFlags is the CLI flag names to use to enable metrics collections. var expensiveEnablerFlags = []string{"metrics.expensive"} +// configFlag is the CLI flag name to use to start node by providing a toml based config +var configFlag = "config" + // Init enables or disables the metrics system. Since we need this to run before // any other code gets to create meters and timers, we'll actually do an ugly hack // and peek into the command line args for the metrics flag. func init() { - for _, arg := range os.Args { + var configFile string + + for i := 0; i < len(os.Args); i++ { + arg := os.Args[i] + flag := strings.TrimLeft(arg, "-") + // check for existence of `config` flag + if flag == configFlag && i < len(os.Args)-1 { + configFile = strings.TrimLeft(os.Args[i+1], "-") // find the value of flag + } + for _, enabler := range enablerFlags { if !Enabled && flag == enabler { - log.Info("Enabling metrics collection") Enabled = true } } + for _, enabler := range expensiveEnablerFlags { if !EnabledExpensive && flag == enabler { - log.Info("Enabling expensive metrics collection") EnabledExpensive = true } } } + + // Update the global metrics value, if they're provided in the config file + updateMetricsFromConfig(configFile) +} + +func updateMetricsFromConfig(path string) { + // Don't act upon any errors here. They're already taken into + // consideration when the toml config file will be parsed in the cli. + data, err := os.ReadFile(path) + tomlData := string(data) + + if err != nil { + return + } + + // Create a minimal config to decode + type TelemetryConfig struct { + Enabled bool `hcl:"metrics,optional" toml:"metrics,optional"` + Expensive bool `hcl:"expensive,optional" toml:"expensive,optional"` + } + + type CliConfig struct { + Telemetry *TelemetryConfig `hcl:"telemetry,block" toml:"telemetry,block"` + } + + conf := &CliConfig{} + + if _, err := toml.Decode(tomlData, &conf); err != nil || conf == nil { + return + } + + // We have the values now, update them + Enabled = conf.Telemetry.Enabled + EnabledExpensive = conf.Telemetry.Expensive } // CollectProcessMetrics periodically collects various metrics about the running From 238d5a4f2c39bcbae44d981403648acd63f7014a Mon Sep 17 00:00:00 2001 From: Shivam Sharma Date: Fri, 21 Oct 2022 04:41:49 +0530 Subject: [PATCH 13/20] chg : major fix --- eth/tracers/api.go | 123 +++++++++++++++++++++++++++++----------- eth/tracers/api_test.go | 6 +- 2 files changed, 94 insertions(+), 35 deletions(-) diff --git a/eth/tracers/api.go b/eth/tracers/api.go index baf9417b82..135f1156cd 100644 --- a/eth/tracers/api.go +++ b/eth/tracers/api.go @@ -191,9 +191,11 @@ func (api *API) getAllBlockTransactions(ctx context.Context, block *types.Block) // TraceConfig holds extra parameters to trace functions. type TraceConfig struct { *logger.Config - Tracer *string - Timeout *string - Reexec *uint64 + Tracer *string + Timeout *string + Reexec *uint64 + BorTraceEnabled *bool + BorTx *bool } // TraceCallConfig is the config for traceCall API. It holds one more @@ -209,8 +211,9 @@ type TraceCallConfig struct { // StdTraceConfig holds extra parameters to standard-json trace functions. type StdTraceConfig struct { logger.Config - Reexec *uint64 - TxHash common.Hash + Reexec *uint64 + TxHash common.Hash + BorTrace *bool } // txTraceResult is the result of a single transaction trace. @@ -264,6 +267,11 @@ func (api *API) TraceChain(ctx context.Context, start, end rpc.BlockNumber, conf // executes all the transactions contained within. The return value will be one item // per transaction, dependent on the requested tracer. func (api *API) traceChain(ctx context.Context, start, end *types.Block, config *TraceConfig) (*rpc.Subscription, error) { + if config == nil { + config = &TraceConfig{ + BorTraceEnabled: newBoolPtr(false), + } + } // Tracing a chain is a **long** operation, only do with subscriptions notifier, supported := rpc.NotifierFromContext(ctx) if !supported { @@ -312,9 +320,14 @@ func (api *API) traceChain(ctx context.Context, start, end *types.Block, config var err error if stateSyncPresent && i == len(txs)-1 { - res, err = api.traceTx(localctx, msg, txctx, blockCtx, task.statedb, config, true) + if *config.BorTraceEnabled { + config.BorTx = newBoolPtr(true) + res, err = api.traceTx(localctx, msg, txctx, blockCtx, task.statedb, config) + } else { + break + } } else { - res, err = api.traceTx(localctx, msg, txctx, blockCtx, task.statedb, config, false) + res, err = api.traceTx(localctx, msg, txctx, blockCtx, task.statedb, config) } if err != nil { @@ -466,6 +479,11 @@ func (api *API) traceChain(ctx context.Context, start, end *types.Block, config return sub, nil } +func newBoolPtr(bb bool) *bool { + b := bb + return &b +} + // TraceBlockByNumber returns the structured logs created during the execution of // EVM and returns them as a JSON object. func (api *API) TraceBlockByNumber(ctx context.Context, number rpc.BlockNumber, config *TraceConfig) ([]*txTraceResult, error) { @@ -546,6 +564,12 @@ func prepareCallMessage(msg core.Message) statefull.Callmsg { // IntermediateRoots executes a block (bad- or canon- or side-), and returns a list // of intermediate roots: the stateroot after each transaction. func (api *API) IntermediateRoots(ctx context.Context, hash common.Hash, config *TraceConfig) ([]common.Hash, error) { + if config == nil { + config = &TraceConfig{ + BorTraceEnabled: newBoolPtr(false), + } + } + block, _ := api.blockByHash(ctx, hash) if block == nil { // Check in the bad blocks @@ -587,18 +611,23 @@ func (api *API) IntermediateRoots(ctx context.Context, hash common.Hash, config statedb.Prepare(tx.Hash(), i) if stateSyncPresent && i == len(txs)-1 { - callmsg := prepareCallMessage(msg) + if *config.BorTraceEnabled { + callmsg := prepareCallMessage(msg) - if _, err := statefull.ApplyMessage(ctx, callmsg, statedb, block.Header(), api.backend.ChainConfig(), api.chainContext(ctx)); err != nil { - log.Warn("Tracing intermediate roots did not complete", "txindex", i, "txhash", tx.Hash(), "err", err) - // We intentionally don't return the error here: if we do, then the RPC server will not - // return the roots. Most likely, the caller already knows that a certain transaction fails to - // be included, but still want the intermediate roots that led to that point. - // It may happen the tx_N causes an erroneous state, which in turn causes tx_N+M to not be - // executable. - // N.B: This should never happen while tracing canon blocks, only when tracing bad blocks. - return roots, nil + if _, err := statefull.ApplyMessage(ctx, callmsg, statedb, block.Header(), api.backend.ChainConfig(), api.chainContext(ctx)); err != nil { + log.Warn("Tracing intermediate roots did not complete", "txindex", i, "txhash", tx.Hash(), "err", err) + // We intentionally don't return the error here: if we do, then the RPC server will not + // return the roots. Most likely, the caller already knows that a certain transaction fails to + // be included, but still want the intermediate roots that led to that point. + // It may happen the tx_N causes an erroneous state, which in turn causes tx_N+M to not be + // executable. + // N.B: This should never happen while tracing canon blocks, only when tracing bad blocks. + return roots, nil + } + } else { + break } + } else { if _, err := core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(msg.Gas())); err != nil { log.Warn("Tracing intermediate roots did not complete", "txindex", i, "txhash", tx.Hash(), "err", err) @@ -635,6 +664,13 @@ func (api *API) StandardTraceBadBlockToFile(ctx context.Context, hash common.Has // executes all the transactions contained within. The return value will be one item // per transaction, dependent on the requestd tracer. func (api *API) traceBlock(ctx context.Context, block *types.Block, config *TraceConfig) ([]*txTraceResult, error) { + + if config == nil { + config = &TraceConfig{ + BorTraceEnabled: newBoolPtr(false), + } + } + if block.NumberU64() == 0 { return nil, errors.New("genesis is not traceable") } @@ -682,9 +718,14 @@ func (api *API) traceBlock(ctx context.Context, block *types.Block, config *Trac var err error if stateSyncPresent && task.index == len(txs)-1 { - res, err = api.traceTx(ctx, msg, txctx, blockCtx, task.statedb, config, true) + if *config.BorTraceEnabled { + config.BorTx = newBoolPtr(true) + res, err = api.traceTx(ctx, msg, txctx, blockCtx, task.statedb, config) + } else { + break + } } else { - res, err = api.traceTx(ctx, msg, txctx, blockCtx, task.statedb, config, false) + res, err = api.traceTx(ctx, msg, txctx, blockCtx, task.statedb, config) } if err != nil { results[task.index] = &txTraceResult{Error: err.Error()} @@ -708,9 +749,13 @@ func (api *API) traceBlock(ctx context.Context, block *types.Block, config *Trac vmenv := vm.NewEVM(blockCtx, core.NewEVMTxContext(msg), statedb, api.backend.ChainConfig(), vm.Config{}) if stateSyncPresent && i == len(txs)-1 { - callmsg := prepareCallMessage(msg) - if _, err := statefull.ApplyBorMessage(*vmenv, callmsg); err != nil { - failed = err + if *config.BorTraceEnabled { + callmsg := prepareCallMessage(msg) + if _, err := statefull.ApplyBorMessage(*vmenv, callmsg); err != nil { + failed = err + break + } + } else { break } } else { @@ -738,6 +783,11 @@ func (api *API) traceBlock(ctx context.Context, block *types.Block, config *Trac // and traces either a full block or an individual transaction. The return value will // be one filename per transaction traced. func (api *API) standardTraceBlockToFile(ctx context.Context, block *types.Block, config *StdTraceConfig) ([]string, error) { + if config == nil { + config = &StdTraceConfig{ + BorTrace: newBoolPtr(false), + } + } // If we're tracing a single transaction, make sure it's present if config != nil && config.TxHash != (common.Hash{}) { if !api.containsTx(ctx, block, config.TxHash) { @@ -833,11 +883,15 @@ func (api *API) standardTraceBlockToFile(ctx context.Context, block *types.Block statedb.Prepare(tx.Hash(), i) if stateSyncPresent && i == len(txs)-1 { - callmsg := prepareCallMessage(msg) - _, err = statefull.ApplyBorMessage(*vmenv, callmsg) + if *config.BorTrace { + callmsg := prepareCallMessage(msg) + _, err = statefull.ApplyBorMessage(*vmenv, callmsg) - if writer != nil { - writer.Flush() + if writer != nil { + writer.Flush() + } + } else { + break } } else { _, err = core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(msg.Gas())) @@ -879,7 +933,12 @@ func (api *API) containsTx(ctx context.Context, block *types.Block, hash common. // TraceTransaction returns the structured logs created during the execution of EVM // and returns them as a JSON object. -func (api *API) TraceTransaction(ctx context.Context, hash common.Hash, config *TraceConfig, borTx bool) (interface{}, error) { +func (api *API) TraceTransaction(ctx context.Context, hash common.Hash, config *TraceConfig) (interface{}, error) { + if config == nil { + config = &TraceConfig{ + BorTraceEnabled: newBoolPtr(false), + } + } tx, blockHash, blockNumber, index, err := api.backend.GetTransaction(ctx, hash) if tx == nil { // For BorTransaction, there will be no trace available @@ -916,14 +975,14 @@ func (api *API) TraceTransaction(ctx context.Context, hash common.Hash, config * TxHash: hash, } - return api.traceTx(ctx, msg, txctx, vmctx, statedb, config, borTx) + return api.traceTx(ctx, msg, txctx, vmctx, statedb, config) } // TraceCall lets you trace a given eth_call. It collects the structured logs // created during the execution of EVM if the given transaction was added on // top of the provided block and returns them as a JSON object. // You can provide -2 as a block number to trace on top of the pending block. -func (api *API) TraceCall(ctx context.Context, args ethapi.TransactionArgs, blockNrOrHash rpc.BlockNumberOrHash, config *TraceCallConfig, borTx bool) (interface{}, error) { +func (api *API) TraceCall(ctx context.Context, args ethapi.TransactionArgs, blockNrOrHash rpc.BlockNumberOrHash, config *TraceCallConfig) (interface{}, error) { // Try to retrieve the specified block var ( err error @@ -971,13 +1030,13 @@ func (api *API) TraceCall(ctx context.Context, args ethapi.TransactionArgs, bloc } } - return api.traceTx(ctx, msg, new(Context), vmctx, statedb, traceConfig, borTx) + return api.traceTx(ctx, msg, new(Context), vmctx, statedb, traceConfig) } // traceTx configures a new tracer according to the provided configuration, and // executes the given message in the provided environment. The return value will // be tracer dependent. -func (api *API) traceTx(ctx context.Context, message core.Message, txctx *Context, vmctx vm.BlockContext, statedb *state.StateDB, config *TraceConfig, borTx bool) (interface{}, error) { +func (api *API) traceTx(ctx context.Context, message core.Message, txctx *Context, vmctx vm.BlockContext, statedb *state.StateDB, config *TraceConfig) (interface{}, error) { // Assemble the structured logger or the JavaScript tracer var ( tracer vm.EVMLogger @@ -1019,7 +1078,7 @@ func (api *API) traceTx(ctx context.Context, message core.Message, txctx *Contex var result *core.ExecutionResult - if borTx { + if *config.BorTx { callmsg := prepareCallMessage(message) if result, err = statefull.ApplyBorMessage(*vmenv, callmsg); err != nil { return nil, fmt.Errorf("tracing failed: %w", err) diff --git a/eth/tracers/api_test.go b/eth/tracers/api_test.go index 68335239e8..6dd94e4870 100644 --- a/eth/tracers/api_test.go +++ b/eth/tracers/api_test.go @@ -290,7 +290,7 @@ func TestTraceCall(t *testing.T) { }, } for _, testspec := range testSuite { - result, err := api.TraceCall(context.Background(), testspec.call, rpc.BlockNumberOrHash{BlockNumber: &testspec.blockNumber}, testspec.config, false) + result, err := api.TraceCall(context.Background(), testspec.call, rpc.BlockNumberOrHash{BlockNumber: &testspec.blockNumber}, testspec.config) if testspec.expectErr != nil { if err == nil { t.Errorf("Expect error %v, get nothing", testspec.expectErr) @@ -330,7 +330,7 @@ func TestTraceTransaction(t *testing.T) { b.AddTx(tx) target = tx.Hash() })) - result, err := api.TraceTransaction(context.Background(), target, nil, false) + result, err := api.TraceTransaction(context.Background(), target, nil) if err != nil { t.Errorf("Failed to trace transaction %v", err) } @@ -513,7 +513,7 @@ func TestTracingWithOverrides(t *testing.T) { }, } for i, tc := range testSuite { - result, err := api.TraceCall(context.Background(), tc.call, rpc.BlockNumberOrHash{BlockNumber: &tc.blockNumber}, tc.config, false) + result, err := api.TraceCall(context.Background(), tc.call, rpc.BlockNumberOrHash{BlockNumber: &tc.blockNumber}, tc.config) if tc.expectErr != nil { if err == nil { t.Errorf("test %d: want error %v, have nothing", i, tc.expectErr) From 7554a16c4ffb3a2d2a9cb29ab67856b251d009ae Mon Sep 17 00:00:00 2001 From: Shivam Sharma Date: Fri, 21 Oct 2022 04:56:21 +0530 Subject: [PATCH 14/20] chg : minor fix --- eth/tracers/api.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/eth/tracers/api.go b/eth/tracers/api.go index 135f1156cd..c5ba6b9779 100644 --- a/eth/tracers/api.go +++ b/eth/tracers/api.go @@ -270,6 +270,7 @@ func (api *API) traceChain(ctx context.Context, start, end *types.Block, config if config == nil { config = &TraceConfig{ BorTraceEnabled: newBoolPtr(false), + BorTx: newBoolPtr(false), } } // Tracing a chain is a **long** operation, only do with subscriptions @@ -567,6 +568,7 @@ func (api *API) IntermediateRoots(ctx context.Context, hash common.Hash, config if config == nil { config = &TraceConfig{ BorTraceEnabled: newBoolPtr(false), + BorTx: newBoolPtr(false), } } @@ -668,6 +670,7 @@ func (api *API) traceBlock(ctx context.Context, block *types.Block, config *Trac if config == nil { config = &TraceConfig{ BorTraceEnabled: newBoolPtr(false), + BorTx: newBoolPtr(false), } } @@ -937,6 +940,7 @@ func (api *API) TraceTransaction(ctx context.Context, hash common.Hash, config * if config == nil { config = &TraceConfig{ BorTraceEnabled: newBoolPtr(false), + BorTx: newBoolPtr(false), } } tx, blockHash, blockNumber, index, err := api.backend.GetTransaction(ctx, hash) @@ -1078,6 +1082,10 @@ func (api *API) traceTx(ctx context.Context, message core.Message, txctx *Contex var result *core.ExecutionResult + if config.BorTx == nil { + config.BorTx = newBoolPtr(false) + } + if *config.BorTx { callmsg := prepareCallMessage(message) if result, err = statefull.ApplyBorMessage(*vmenv, callmsg); err != nil { From 2f648fb91c7225727aa4801e3fcda3f5f7d22d84 Mon Sep 17 00:00:00 2001 From: Shivam Sharma Date: Fri, 21 Oct 2022 05:38:45 +0530 Subject: [PATCH 15/20] chg : fix derference error on config without borTraceEnabled --- eth/tracers/api.go | 47 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/eth/tracers/api.go b/eth/tracers/api.go index c5ba6b9779..4534f4675f 100644 --- a/eth/tracers/api.go +++ b/eth/tracers/api.go @@ -65,6 +65,8 @@ const ( defaultTracechainMemLimit = common.StorageSize(500 * 1024 * 1024) ) +var defaultBorTraceEnabled = newBoolPtr(false) + // Backend interface provides the common API services (that are provided by // both full and light clients) with access to necessary functions. type Backend interface { @@ -269,10 +271,13 @@ func (api *API) TraceChain(ctx context.Context, start, end rpc.BlockNumber, conf func (api *API) traceChain(ctx context.Context, start, end *types.Block, config *TraceConfig) (*rpc.Subscription, error) { if config == nil { config = &TraceConfig{ - BorTraceEnabled: newBoolPtr(false), + BorTraceEnabled: defaultBorTraceEnabled, BorTx: newBoolPtr(false), } } + if config.BorTraceEnabled == nil { + config.BorTraceEnabled = defaultBorTraceEnabled + } // Tracing a chain is a **long** operation, only do with subscriptions notifier, supported := rpc.NotifierFromContext(ctx) if !supported { @@ -308,6 +313,10 @@ func (api *API) traceChain(ctx context.Context, start, end *types.Block, config blockCtx := core.NewEVMBlockContext(task.block.Header(), api.chainContext(localctx), nil) // Trace all the transactions contained within txs, stateSyncPresent := api.getAllBlockTransactions(ctx, task.block) + if !*config.BorTraceEnabled && stateSyncPresent { + txs = txs[:len(txs)-1] + stateSyncPresent = false + } for i, tx := range txs { msg, _ := tx.AsMessage(signer, task.block.BaseFee()) txctx := &Context{ @@ -324,8 +333,6 @@ func (api *API) traceChain(ctx context.Context, start, end *types.Block, config if *config.BorTraceEnabled { config.BorTx = newBoolPtr(true) res, err = api.traceTx(localctx, msg, txctx, blockCtx, task.statedb, config) - } else { - break } } else { res, err = api.traceTx(localctx, msg, txctx, blockCtx, task.statedb, config) @@ -567,10 +574,13 @@ func prepareCallMessage(msg core.Message) statefull.Callmsg { func (api *API) IntermediateRoots(ctx context.Context, hash common.Hash, config *TraceConfig) ([]common.Hash, error) { if config == nil { config = &TraceConfig{ - BorTraceEnabled: newBoolPtr(false), + BorTraceEnabled: defaultBorTraceEnabled, BorTx: newBoolPtr(false), } } + if config.BorTraceEnabled == nil { + config.BorTraceEnabled = defaultBorTraceEnabled + } block, _ := api.blockByHash(ctx, hash) if block == nil { @@ -669,10 +679,13 @@ func (api *API) traceBlock(ctx context.Context, block *types.Block, config *Trac if config == nil { config = &TraceConfig{ - BorTraceEnabled: newBoolPtr(false), + BorTraceEnabled: defaultBorTraceEnabled, BorTx: newBoolPtr(false), } } + if config.BorTraceEnabled == nil { + config.BorTraceEnabled = defaultBorTraceEnabled + } if block.NumberU64() == 0 { return nil, errors.New("genesis is not traceable") @@ -779,7 +792,12 @@ func (api *API) traceBlock(ctx context.Context, block *types.Block, config *Trac if failed != nil { return nil, failed } - return results, nil + + if !*config.BorTraceEnabled && stateSyncPresent { + return results[:len(results)-1], nil + } else { + return results, nil + } } // standardTraceBlockToFile configures a new tracer which uses standard JSON output, @@ -939,10 +957,14 @@ func (api *API) containsTx(ctx context.Context, block *types.Block, hash common. func (api *API) TraceTransaction(ctx context.Context, hash common.Hash, config *TraceConfig) (interface{}, error) { if config == nil { config = &TraceConfig{ - BorTraceEnabled: newBoolPtr(false), + BorTraceEnabled: defaultBorTraceEnabled, BorTx: newBoolPtr(false), } } + if config.BorTraceEnabled == nil { + config.BorTraceEnabled = defaultBorTraceEnabled + } + tx, blockHash, blockNumber, index, err := api.backend.GetTransaction(ctx, hash) if tx == nil { // For BorTransaction, there will be no trace available @@ -1041,6 +1063,17 @@ func (api *API) TraceCall(ctx context.Context, args ethapi.TransactionArgs, bloc // executes the given message in the provided environment. The return value will // be tracer dependent. func (api *API) traceTx(ctx context.Context, message core.Message, txctx *Context, vmctx vm.BlockContext, statedb *state.StateDB, config *TraceConfig) (interface{}, error) { + + if config == nil { + config = &TraceConfig{ + BorTraceEnabled: defaultBorTraceEnabled, + BorTx: newBoolPtr(false), + } + } + if config.BorTraceEnabled == nil { + config.BorTraceEnabled = defaultBorTraceEnabled + } + // Assemble the structured logger or the JavaScript tracer var ( tracer vm.EVMLogger From 654f56fbcba8895190ec21781b9ea958d23e1d5a Mon Sep 17 00:00:00 2001 From: Shivam Sharma Date: Fri, 21 Oct 2022 06:26:43 +0530 Subject: [PATCH 16/20] chg : standardTraceBlockToFile fix --- eth/tracers/api.go | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/eth/tracers/api.go b/eth/tracers/api.go index 4534f4675f..ad52373038 100644 --- a/eth/tracers/api.go +++ b/eth/tracers/api.go @@ -213,9 +213,9 @@ type TraceCallConfig struct { // StdTraceConfig holds extra parameters to standard-json trace functions. type StdTraceConfig struct { logger.Config - Reexec *uint64 - TxHash common.Hash - BorTrace *bool + Reexec *uint64 + TxHash common.Hash + BorTraceEnabled *bool } // txTraceResult is the result of a single transaction trace. @@ -806,9 +806,12 @@ func (api *API) traceBlock(ctx context.Context, block *types.Block, config *Trac func (api *API) standardTraceBlockToFile(ctx context.Context, block *types.Block, config *StdTraceConfig) ([]string, error) { if config == nil { config = &StdTraceConfig{ - BorTrace: newBoolPtr(false), + BorTraceEnabled: defaultBorTraceEnabled, } } + if config.BorTraceEnabled == nil { + config.BorTraceEnabled = defaultBorTraceEnabled + } // If we're tracing a single transaction, make sure it's present if config != nil && config.TxHash != (common.Hash{}) { if !api.containsTx(ctx, block, config.TxHash) { @@ -868,6 +871,11 @@ func (api *API) standardTraceBlockToFile(ctx context.Context, block *types.Block } txs, stateSyncPresent := api.getAllBlockTransactions(ctx, block) + if !*config.BorTraceEnabled && stateSyncPresent { + txs = txs[:len(txs)-1] + stateSyncPresent = false + } + for i, tx := range txs { // Prepare the trasaction for un-traced execution var ( @@ -904,15 +912,13 @@ func (api *API) standardTraceBlockToFile(ctx context.Context, block *types.Block statedb.Prepare(tx.Hash(), i) if stateSyncPresent && i == len(txs)-1 { - if *config.BorTrace { + if *config.BorTraceEnabled { callmsg := prepareCallMessage(msg) _, err = statefull.ApplyBorMessage(*vmenv, callmsg) if writer != nil { writer.Flush() } - } else { - break } } else { _, err = core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(msg.Gas())) From 2542403048dd9d45e598bbd25a599f7153413e31 Mon Sep 17 00:00:00 2001 From: Shivam Sharma Date: Fri, 21 Oct 2022 06:49:01 +0530 Subject: [PATCH 17/20] add : lint --- eth/tracers/api.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/eth/tracers/api.go b/eth/tracers/api.go index ad52373038..3fce91ac9c 100644 --- a/eth/tracers/api.go +++ b/eth/tracers/api.go @@ -275,6 +275,7 @@ func (api *API) traceChain(ctx context.Context, start, end *types.Block, config BorTx: newBoolPtr(false), } } + if config.BorTraceEnabled == nil { config.BorTraceEnabled = defaultBorTraceEnabled } @@ -317,6 +318,7 @@ func (api *API) traceChain(ctx context.Context, start, end *types.Block, config txs = txs[:len(txs)-1] stateSyncPresent = false } + for i, tx := range txs { msg, _ := tx.AsMessage(signer, task.block.BaseFee()) txctx := &Context{ @@ -578,6 +580,7 @@ func (api *API) IntermediateRoots(ctx context.Context, hash common.Hash, config BorTx: newBoolPtr(false), } } + if config.BorTraceEnabled == nil { config.BorTraceEnabled = defaultBorTraceEnabled } @@ -621,7 +624,7 @@ func (api *API) IntermediateRoots(ctx context.Context, hash common.Hash, config vmenv = vm.NewEVM(vmctx, txContext, statedb, chainConfig, vm.Config{}) ) statedb.Prepare(tx.Hash(), i) - + //nolint: nestif if stateSyncPresent && i == len(txs)-1 { if *config.BorTraceEnabled { callmsg := prepareCallMessage(msg) @@ -639,7 +642,6 @@ func (api *API) IntermediateRoots(ctx context.Context, hash common.Hash, config } else { break } - } else { if _, err := core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(msg.Gas())); err != nil { log.Warn("Tracing intermediate roots did not complete", "txindex", i, "txhash", tx.Hash(), "err", err) @@ -683,6 +685,7 @@ func (api *API) traceBlock(ctx context.Context, block *types.Block, config *Trac BorTx: newBoolPtr(false), } } + if config.BorTraceEnabled == nil { config.BorTraceEnabled = defaultBorTraceEnabled } @@ -733,6 +736,7 @@ func (api *API) traceBlock(ctx context.Context, block *types.Block, config *Trac var res interface{} var err error + if stateSyncPresent && task.index == len(txs)-1 { if *config.BorTraceEnabled { config.BorTx = newBoolPtr(true) @@ -763,7 +767,7 @@ func (api *API) traceBlock(ctx context.Context, block *types.Block, config *Trac statedb.Prepare(tx.Hash(), i) vmenv := vm.NewEVM(blockCtx, core.NewEVMTxContext(msg), statedb, api.backend.ChainConfig(), vm.Config{}) - + //nolint: nestif if stateSyncPresent && i == len(txs)-1 { if *config.BorTraceEnabled { callmsg := prepareCallMessage(msg) @@ -809,6 +813,7 @@ func (api *API) standardTraceBlockToFile(ctx context.Context, block *types.Block BorTraceEnabled: defaultBorTraceEnabled, } } + if config.BorTraceEnabled == nil { config.BorTraceEnabled = defaultBorTraceEnabled } @@ -910,7 +915,7 @@ func (api *API) standardTraceBlockToFile(ctx context.Context, block *types.Block // Execute the transaction and flush any traces to disk vmenv := vm.NewEVM(vmctx, txContext, statedb, chainConfig, vmConf) statedb.Prepare(tx.Hash(), i) - + //nolint: nestif if stateSyncPresent && i == len(txs)-1 { if *config.BorTraceEnabled { callmsg := prepareCallMessage(msg) @@ -967,6 +972,7 @@ func (api *API) TraceTransaction(ctx context.Context, hash common.Hash, config * BorTx: newBoolPtr(false), } } + if config.BorTraceEnabled == nil { config.BorTraceEnabled = defaultBorTraceEnabled } @@ -1076,6 +1082,7 @@ func (api *API) traceTx(ctx context.Context, message core.Message, txctx *Contex BorTx: newBoolPtr(false), } } + if config.BorTraceEnabled == nil { config.BorTraceEnabled = defaultBorTraceEnabled } From e17ee36ebe276c2c15adf52cec68b3e143eb6bd8 Mon Sep 17 00:00:00 2001 From: Pratik Patil Date: Fri, 21 Oct 2022 08:54:38 +0530 Subject: [PATCH 18/20] Changed default value of maxpeers from 200 to 50, update docs (#555) * changed default of maxpeers from 200 to 50 * docs: update additional notes for new-cli * docs: update additional notes for new-cli * small bug fix in the script to fix shopt issue Co-authored-by: Manav Darji --- cmd/geth/config.go | 4 ++-- docs/README.md | 18 ++++++++---------- docs/config.md | 2 +- internal/cli/server/config.go | 2 +- scripts/getconfig.sh | 2 +- 5 files changed, 13 insertions(+), 15 deletions(-) diff --git a/cmd/geth/config.go b/cmd/geth/config.go index e6cb36b121..80bf95b1ac 100644 --- a/cmd/geth/config.go +++ b/cmd/geth/config.go @@ -327,7 +327,7 @@ func setDefaultMumbaiGethConfig(ctx *cli.Context, config *gethConfig) { config.Eth.TxPool.AccountQueue = 64 config.Eth.TxPool.GlobalQueue = 131072 config.Eth.TxPool.Lifetime = 90 * time.Minute - config.Node.P2P.MaxPeers = 200 + config.Node.P2P.MaxPeers = 50 config.Metrics.Enabled = true // --pprof is enabled in 'internal/debug/flags.go' } @@ -350,7 +350,7 @@ func setDefaultBorMainnetGethConfig(ctx *cli.Context, config *gethConfig) { config.Eth.TxPool.AccountQueue = 64 config.Eth.TxPool.GlobalQueue = 131072 config.Eth.TxPool.Lifetime = 90 * time.Minute - config.Node.P2P.MaxPeers = 200 + config.Node.P2P.MaxPeers = 50 config.Metrics.Enabled = true // --pprof is enabled in 'internal/debug/flags.go' } diff --git a/docs/README.md b/docs/README.md index 95ba38b0da..5ebdbd7e26 100644 --- a/docs/README.md +++ b/docs/README.md @@ -5,18 +5,16 @@ - [Configuration file](./config.md) -## Deprecation notes +## Additional notes - The new entrypoint to run the Bor client is ```server```. -``` -$ bor server -``` + ``` + $ bor server + ``` -- Toml files to configure nodes are being deprecated. Currently, we only allow for static and trusted nodes to be configured using toml files. +- Toml files used earlier just to configure static/trusted nodes are being deprecated. Instead, a toml file now can be used instead of flags and can contain all configuration for the node to run. The link to a sample config file is given above. To simply run bor with a configuration file, the following command can be used. -``` -$ bor server --config ./legacy.toml -``` - -- ```Admin```, ```Personal``` and account related endpoints in ```Eth``` are being removed from the JsonRPC interface. Some of this functionality will be moved to the new GRPC server for operational tasks. + ``` + $ bor server --config + ``` diff --git a/docs/config.md b/docs/config.md index aebd9e12b9..57f4c25fef 100644 --- a/docs/config.md +++ b/docs/config.md @@ -22,7 +22,7 @@ ethstats = "" ["eth.requiredblocks"] [p2p] -maxpeers = 30 +maxpeers = 50 maxpendpeers = 50 bind = "0.0.0.0" port = 30303 diff --git a/internal/cli/server/config.go b/internal/cli/server/config.go index e5e3a8b03e..bf99f33c96 100644 --- a/internal/cli/server/config.go +++ b/internal/cli/server/config.go @@ -399,7 +399,7 @@ func DefaultConfig() *Config { LogLevel: "INFO", DataDir: DefaultDataDir(), P2P: &P2PConfig{ - MaxPeers: 30, + MaxPeers: 50, MaxPendPeers: 50, Bind: "0.0.0.0", Port: 30303, diff --git a/scripts/getconfig.sh b/scripts/getconfig.sh index 943d540a88..a2971c4f12 100755 --- a/scripts/getconfig.sh +++ b/scripts/getconfig.sh @@ -1,4 +1,4 @@ -#!/usr/bin/env sh +#!/bin/bash set -e # Instructions: From 723ff9a958180f08d7a3a44849b2e57d644a524d Mon Sep 17 00:00:00 2001 From: Shivam Sharma Date: Fri, 21 Oct 2022 15:11:28 +0530 Subject: [PATCH 19/20] chg : minor change --- core/blockchain.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/blockchain.go b/core/blockchain.go index e1fc269759..8103e4a05e 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -232,7 +232,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par } if cacheConfig.TriesInMemory <= 0 { - cacheConfig.TriesInMemory = 128 + cacheConfig.TriesInMemory = DefaultCacheConfig.TriesInMemory } bodyCache, _ := lru.New(bodyCacheLimit) bodyRLPCache, _ := lru.New(bodyCacheLimit) From 1ab225c0e47dd44278648bd620c0952d135a5e30 Mon Sep 17 00:00:00 2001 From: Pratik Patil Date: Fri, 4 Nov 2022 01:24:57 +0530 Subject: [PATCH 20/20] Removed vhosts for ws and replaced cors with origins (#574) * removed vhosts for ws and added replaced cors with origins * updated script --- builder/files/config.toml | 3 +-- docs/cli/server.md | 6 +++--- internal/cli/server/config.go | 8 +++++--- internal/cli/server/flags.go | 15 ++++----------- scripts/getconfig.go | 10 ++++------ 5 files changed, 17 insertions(+), 25 deletions(-) diff --git a/builder/files/config.toml b/builder/files/config.toml index 13bf82bf93..870c164a8d 100644 --- a/builder/files/config.toml +++ b/builder/files/config.toml @@ -78,8 +78,7 @@ syncmode = "full" # prefix = "" # host = "localhost" # api = ["web3", "net"] - # vhosts = ["*"] - # corsdomain = ["*"] + # origins = ["*"] # [jsonrpc.graphql] # enabled = false # port = 0 diff --git a/docs/cli/server.md b/docs/cli/server.md index 5fe440b5fd..d52b135fa3 100644 --- a/docs/cli/server.md +++ b/docs/cli/server.md @@ -80,6 +80,8 @@ The ```bor server``` command runs the Bor client. - ```cache.preimages```: Enable recording the SHA3/keccak preimages of trie keys +- ```cache.triesinmemory```: Number of block states (tries) to keep in memory (default = 128) + - ```txlookuplimit```: Number of recent blocks to maintain transactions index for (default = about 56 days, 0 = entire chain) ### JsonRPC Options @@ -96,9 +98,7 @@ The ```bor server``` command runs the Bor client. - ```http.vhosts```: Comma separated list of virtual hostnames from which to accept requests (server enforced). Accepts '*' wildcard. -- ```ws.corsdomain```: Comma separated list of domains from which to accept cross origin requests (browser enforced) - -- ```ws.vhosts```: Comma separated list of virtual hostnames from which to accept requests (server enforced). Accepts '*' wildcard. +- ```ws.origins```: Origins from which to accept websockets requests - ```graphql.corsdomain```: Comma separated list of domains from which to accept cross origin requests (browser enforced) diff --git a/internal/cli/server/config.go b/internal/cli/server/config.go index e71b45218d..b8310aea6f 100644 --- a/internal/cli/server/config.go +++ b/internal/cli/server/config.go @@ -266,6 +266,9 @@ type APIConfig struct { // Cors is the list of Cors endpoints Cors []string `hcl:"corsdomain,optional" toml:"corsdomain,optional"` + + // Origins is the list of endpoints to accept requests from (only consumed for websockets) + Origins []string `hcl:"origins,optional" toml:"origins,optional"` } type GpoConfig struct { @@ -473,8 +476,7 @@ func DefaultConfig() *Config { Prefix: "", Host: "localhost", API: []string{"net", "web3"}, - Cors: []string{"localhost"}, - VHost: []string{"localhost"}, + Origins: []string{"localhost"}, }, Graphql: &APIConfig{ Enabled: false, @@ -930,7 +932,7 @@ func (c *Config) buildNode() (*node.Config, error) { HTTPVirtualHosts: c.JsonRPC.Http.VHost, HTTPPathPrefix: c.JsonRPC.Http.Prefix, WSModules: c.JsonRPC.Ws.API, - WSOrigins: c.JsonRPC.Ws.Cors, + WSOrigins: c.JsonRPC.Ws.Origins, WSPathPrefix: c.JsonRPC.Ws.Prefix, GraphQLCors: c.JsonRPC.Graphql.Cors, GraphQLVirtualHosts: c.JsonRPC.Graphql.VHost, diff --git a/internal/cli/server/flags.go b/internal/cli/server/flags.go index 025ce8c80c..ba9be13376 100644 --- a/internal/cli/server/flags.go +++ b/internal/cli/server/flags.go @@ -363,17 +363,10 @@ func (c *Command) Flags() *flagset.Flagset { Group: "JsonRPC", }) f.SliceStringFlag(&flagset.SliceStringFlag{ - Name: "ws.corsdomain", - Usage: "Comma separated list of domains from which to accept cross origin requests (browser enforced)", - Value: &c.cliConfig.JsonRPC.Ws.Cors, - Default: c.cliConfig.JsonRPC.Ws.Cors, - Group: "JsonRPC", - }) - f.SliceStringFlag(&flagset.SliceStringFlag{ - Name: "ws.vhosts", - Usage: "Comma separated list of virtual hostnames from which to accept requests (server enforced). Accepts '*' wildcard.", - Value: &c.cliConfig.JsonRPC.Ws.VHost, - Default: c.cliConfig.JsonRPC.Ws.VHost, + Name: "ws.origins", + Usage: "Origins from which to accept websockets requests", + Value: &c.cliConfig.JsonRPC.Ws.Origins, + Default: c.cliConfig.JsonRPC.Ws.Origins, Group: "JsonRPC", }) f.SliceStringFlag(&flagset.SliceStringFlag{ diff --git a/scripts/getconfig.go b/scripts/getconfig.go index 136b69ecab..caf3f45a8e 100644 --- a/scripts/getconfig.go +++ b/scripts/getconfig.go @@ -95,7 +95,7 @@ var flagMap = map[string][]string{ "override.arrowglacier": {"notABoolFlag", "No"}, "override.terminaltotaldifficulty": {"notABoolFlag", "No"}, "verbosity": {"notABoolFlag", "YesFV"}, - "ws.origins": {"notABoolFlag", "YesF"}, + "ws.origins": {"notABoolFlag", "No"}, } // map from cli flags to corresponding toml tags @@ -150,8 +150,7 @@ var nameTagMap = map[string]string{ "ipcpath": "ipcpath", "1-corsdomain": "http.corsdomain", "1-vhosts": "http.vhosts", - "2-corsdomain": "ws.corsdomain", - "2-vhosts": "ws.vhosts", + "origins": "ws.origins", "3-corsdomain": "graphql.corsdomain", "3-vhosts": "graphql.vhosts", "1-enabled": "http", @@ -226,9 +225,8 @@ var replacedFlagsMapFlagAndValue = map[string]map[string]map[string]string{ }, } -var replacedFlagsMapFlag = map[string]string{ - "ws.origins": "ws.corsdomain", -} +// Do not remove +var replacedFlagsMapFlag = map[string]string{} var currentBoolFlags = []string{ "snapshot",