Skip to content

Commit

Permalink
utilize ibs.RevertToSnapshot to not reexecute transactions and fix da… (
Browse files Browse the repository at this point in the history
#756)

* utilize ibs.RevertToSnapshot to not reexecute transactions and fix datastream last entry search

* fix merge

* fix merge
  • Loading branch information
V-Staykov authored Jul 15, 2024
1 parent 4e8c7dd commit b8ab17d
Show file tree
Hide file tree
Showing 11 changed files with 194 additions and 223 deletions.
2 changes: 1 addition & 1 deletion cmd/rpcdaemon/commands/tracing_zkevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ func (api *PrivateDebugAPIImpl) TraceCallMany(ctx context.Context, bundles []Bun
return err
}

if _, _, err := core.ApplyTransaction_zkevm(chainConfig, api.engine(), evm, gp, st, state.NewNoopWriter(), parent, txn, nil, effectiveGasPricePercentage); err != nil {
if _, _, err := core.ApplyTransaction_zkevm(chainConfig, api.engine(), evm, gp, st, state.NewNoopWriter(), parent, txn, nil, effectiveGasPricePercentage, true); err != nil {
stream.WriteNil()
return err
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/rpcdaemon/commands/zkevm_block_info_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ import (
"github.com/ledgerwatch/erigon/turbo/transactions"
"github.com/ledgerwatch/erigon/zk/hermez_db"

"errors"

"github.com/gateway-fm/cdk-erigon-lib/common"
"github.com/holiman/uint256"
"errors"
)

type TxInfo struct {
Expand Down Expand Up @@ -119,7 +120,7 @@ func (api *ZkEvmAPIImpl) GetL2BlockInfoTree(ctx context.Context, blockNum rpc.Bl
return nil, err
}

receipt, execResult, err := core.ApplyTransaction_zkevm(chainConfig, api.ethApi._engine, evm, gp, ibs, state.NewNoopWriter(), block.Header(), tx, usedGas, effectiveGasPricePercentage)
receipt, execResult, err := core.ApplyTransaction_zkevm(chainConfig, api.ethApi._engine, evm, gp, ibs, state.NewNoopWriter(), block.Header(), tx, usedGas, effectiveGasPricePercentage, true)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion core/blockchain_zkevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func ExecuteBlockEphemerallyZk(
return nil, err
}

receipt, execResult, err := ApplyTransaction_zkevm(chainConfig, engine, evm, gp, ibs, state.NewNoopWriter(), header, tx, usedGas, effectiveGasPricePercentage)
receipt, execResult, err := ApplyTransaction_zkevm(chainConfig, engine, evm, gp, ibs, state.NewNoopWriter(), header, tx, usedGas, effectiveGasPricePercentage, true)
if err != nil {
return nil, err
}
Expand Down
12 changes: 7 additions & 5 deletions core/state_processor_zkevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func GetTxContext(config *chain.Config, engine consensus.EngineReader, ibs *stat
// and uses the input parameters for its environment. It returns the receipt
// for the transaction, gas used and an error if the transaction failed,
// indicating the block was invalid.
func ApplyMessageWithTxContext(msg types.Message, txContext evmtypes.TxContext, gp *GasPool, ibs *state.IntraBlockState, stateWriter state.StateWriter, blockNumber *big.Int, tx types.Transaction, usedGas *uint64, evm vm.VMInterface) (*types.Receipt, *ExecutionResult, error) {
func ApplyMessageWithTxContext(msg types.Message, txContext evmtypes.TxContext, gp *GasPool, ibs *state.IntraBlockState, stateWriter state.StateWriter, blockNumber *big.Int, tx types.Transaction, usedGas *uint64, evm vm.VMInterface, shouldFinalizeIbs bool) (*types.Receipt, *ExecutionResult, error) {
rules := evm.ChainRules()

if evm.Config().TraceJumpDest {
Expand All @@ -81,10 +81,11 @@ func ApplyMessageWithTxContext(msg types.Message, txContext evmtypes.TxContext,
}

// Update the state with pending changes
if err = ibs.FinalizeTx(rules, stateWriter); err != nil {
return nil, nil, err
if shouldFinalizeIbs {
if err = ibs.FinalizeTx(rules, stateWriter); err != nil {
return nil, nil, err
}
}

if usedGas != nil {
*usedGas += result.UsedGas
}
Expand Down Expand Up @@ -167,11 +168,12 @@ func ApplyTransaction_zkevm(
tx types.Transaction,
usedGas *uint64,
effectiveGasPricePercentage uint8,
shouldFinalizeIbs bool,
) (*types.Receipt, *ExecutionResult, error) {
// Create a new context to be used in the EVM environment
msg, txContext, err := GetTxContext(config, engine, ibs, header, tx, evm, effectiveGasPricePercentage)
if err != nil {
return nil, nil, err
}
return ApplyMessageWithTxContext(msg, txContext, gp, ibs, stateWriter, header.Number, tx, usedGas, evm)
return ApplyMessageWithTxContext(msg, txContext, gp, ibs, stateWriter, header.Number, tx, usedGas, evm, shouldFinalizeIbs)
}
1 change: 1 addition & 0 deletions smt/pkg/blockinfo/block_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func BuildBlockInfoTree(
"previousStateRoot", previousStateRoot.String(),
"coinbase", coinbase.String(),
"blockGasLimit", blockGasLimit,
"blockGasUsed", blockGasUsed,
"blockTime", blockTime,
"ger", ger.String(),
"l1BlockHash", l1BlockHash.String(),
Expand Down
2 changes: 1 addition & 1 deletion turbo/transactions/tracing_zkevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func ComputeTxEnv_ZkEvm(ctx context.Context, engine consensus.EngineReader, bloc
return txEnv, nil
}

if _, _, err := core.ApplyMessageWithTxContext(msg, txContext, gp, statedb, reader.(*state.PlainState), header.Number, txn, nil, vmenv); err != nil {
if _, _, err := core.ApplyMessageWithTxContext(msg, txContext, gp, statedb, reader.(*state.PlainState), header.Number, txn, nil, vmenv, true); err != nil {
return TxEnv{}, err
}

Expand Down
23 changes: 16 additions & 7 deletions zk/datastream/server/data_stream_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,20 +326,29 @@ func (srv *DataStreamServer) GetHighestBlockNumber() (uint64, error) {
if err != nil {
return 0, err
}
if entry.Type == datastreamer.EntryType(2) {
if uint32(entry.Type) == uint32(types.EntryTypeL2Block) || uint32(entry.Type) == uint32(types.EntryTypeL2Tx) {
break
}
entryNum -= 1
}

l2Block, err := types.UnmarshalL2Block(entry.Data)
if err != nil {
return 0, err
}
if uint32(entry.Type) == uint32(types.EntryTypeL2Block) {
l2Block, err := types.UnmarshalL2Block(entry.Data)
if err != nil {
return 0, err
}

srv.highestBlockWritten = &l2Block.L2BlockNumber
return l2Block.L2BlockNumber, nil
} else if uint32(entry.Type) == uint32(types.EntryTypeL2Tx) {
tx, err := types.UnmarshalTx(entry.Data)
if err != nil {
return 0, err
}

return tx.L2BlockNumber, nil
}

return l2Block.L2BlockNumber, nil
return 0, nil
}

func (srv *DataStreamServer) GetHighestBatchNumber() (uint64, error) {
Expand Down
10 changes: 5 additions & 5 deletions zk/datastream/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ const dataStreamCardona = "datastream.cardona.zkevm-rpc.com:6900"
const dataStreamBali = "datastream.internal.zkevm-rpc.com:6900"
const datastreamMainnet = "stream.zkevm-rpc.com:6900"
const estest = "34.175.214.161:6900"
const local = "localhost:6910"
const localhost = "localhost:6910"

// This code downloads headers and blocks from a datastream server.
func main() {
// Create client
c := client.NewClient(context.Background(), local, 0, 0, 0)
c := client.NewClient(context.Background(), localhost, 0, 0, 0)

// Start client (connect to the server)
defer c.Stop()
Expand All @@ -31,10 +31,10 @@ func main() {
}

// create bookmark
bookmark := types.NewBookmarkProto(9756, datastream.BookmarkType_BOOKMARK_TYPE_BATCH)
bookmark := types.NewBookmarkProto(188312, datastream.BookmarkType_BOOKMARK_TYPE_L2_BLOCK)

// Read all entries from server
blocksRead, _, _, entriesReadAmount, _, err := c.ReadEntries(bookmark, 100)
blocksRead, _, _, entriesReadAmount, _, err := c.ReadEntries(bookmark, 1)
if err != nil {
panic(err)
}
Expand All @@ -43,7 +43,7 @@ func main() {

// forkId := uint16(0)
for _, dsBlock := range *blocksRead {
fmt.Println(dsBlock.BatchNumber)
fmt.Println(len(dsBlock.L2Txs))
}
}

Expand Down
Loading

0 comments on commit b8ab17d

Please sign in to comment.