Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Elderberry blockhash adjustments #3424

Merged
merged 6 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions db/migrations/state/0017.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- +migrate Up
ALTER TABLE state.receipt
ADD COLUMN IF NOT EXISTS im_state_root BYTEA;

UPDATE state.receipt SET im_state_root = post_state WHERE block_num >= (SELECT MIN(block_num) FROM state.l2block WHERE batch_num >= (SELECT from_batch_num FROM state.fork_id WHERE fork_id = 7));

-- +migrate Down
ALTER TABLE state.receipt
DROP COLUMN IF EXISTS im_state_root;
2 changes: 1 addition & 1 deletion jsonrpc/endpoints_eth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3311,7 +3311,7 @@ func TestGetTransactionReceipt(t *testing.T) {
receipt.Bloom = ethTypes.CreateBloom(ethTypes.Receipts{receipt})

rpcReceipt := types.Receipt{
Root: stateRoot,
Root: &stateRoot,
CumulativeGasUsed: types.ArgUint64(receipt.CumulativeGasUsed),
LogsBloom: receipt.Bloom,
Logs: receipt.Logs,
Expand Down
2 changes: 1 addition & 1 deletion jsonrpc/endpoints_zkevm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2260,7 +2260,7 @@ func TestGetTransactionReceiptByL2Hash(t *testing.T) {
receipt.Bloom = ethTypes.CreateBloom(ethTypes.Receipts{receipt})

rpcReceipt := types.Receipt{
Root: stateRoot,
Root: &stateRoot,
CumulativeGasUsed: types.ArgUint64(receipt.CumulativeGasUsed),
LogsBloom: receipt.Bloom,
Logs: receipt.Logs,
Expand Down
8 changes: 6 additions & 2 deletions jsonrpc/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ func NewTransaction(

// Receipt structure
type Receipt struct {
Root common.Hash `json:"root"`
Root *common.Hash `json:"root,omitempty"`
CumulativeGasUsed ArgUint64 `json:"cumulativeGasUsed"`
LogsBloom types.Bloom `json:"logsBloom"`
Logs []*types.Log `json:"logs"`
Expand Down Expand Up @@ -643,7 +643,6 @@ func NewReceipt(tx types.Transaction, r *types.Receipt, l2Hash *common.Hash) (Re
return Receipt{}, err
}
receipt := Receipt{
Root: common.BytesToHash(r.PostState),
CumulativeGasUsed: ArgUint64(r.CumulativeGasUsed),
LogsBloom: r.Bloom,
Logs: logs,
Expand All @@ -659,6 +658,11 @@ func NewReceipt(tx types.Transaction, r *types.Receipt, l2Hash *common.Hash) (Re
Type: ArgUint64(r.Type),
TxL2Hash: l2Hash,
}
if common.BytesToHash(r.PostState).String() != state.ZeroHash.String() {
root := common.BytesToHash(r.PostState)
receipt.Root = &root
}

if r.EffectiveGasPrice != nil {
egp := ArgBig(*r.EffectiveGasPrice)
receipt.EffectiveGasPrice = &egp
Expand Down
2 changes: 2 additions & 0 deletions proto/src/proto/executor/v1/executor.proto
Original file line number Diff line number Diff line change
Expand Up @@ -872,4 +872,6 @@ enum ExecutorError {
EXECUTOR_ERROR_INVALID_DATA_STREAM = 115;
// EXECUTOR_ERROR_INVALID_UPDATE_MERKLE_TREE indicates that the provided update merkle tree is invalid, e.g. because the executor is configured not to write to database
EXECUTOR_ERROR_INVALID_UPDATE_MERKLE_TREE = 116;
// EXECUTOR_ERROR_SM_MAIN_INVALID_TX_STATUS_ERROR indicates that a TX has an invalid status-error combination
EXECUTOR_ERROR_SM_MAIN_INVALID_TX_STATUS_ERROR = 117;
}
1 change: 1 addition & 0 deletions state/convertersV2.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ func (s *State) convertToProcessTransactionResponseV2(responses []*executor.Proc
result.ReturnValue = response.ReturnValue
result.GasLeft = response.GasLeft
result.GasUsed = response.GasUsed
result.CumulativeGasUsed = response.CumulativeGasUsed
result.GasRefunded = response.GasRefunded
result.RomError = executor.RomErr(response.Error)
result.CreateAddress = common.HexToAddress(response.CreateAddress)
Expand Down
6 changes: 6 additions & 0 deletions state/datastream.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ func (b DSL2BlockStart) Decode(data []byte) DSL2BlockStart {
// DSL2Transaction represents a data stream L2 transaction
type DSL2Transaction struct {
L2BlockNumber uint64 // Not included in the encoded data
ImStateRoot common.Hash // Not included in the encoded data
EffectiveGasPricePercentage uint8 // 1 byte
IsValid uint8 // 1 byte
StateRoot common.Hash // 32 bytes
Expand Down Expand Up @@ -553,6 +554,9 @@ func GenerateDataStreamerFile(ctx context.Context, streamServer *datastreamer.St
}

for _, tx := range l2Block.Txs {
// < ETROG => IM State root is retrieved from the system SC (using cache is available)
// = ETROG => IM State root is retrieved from the receipt.post_state => Do nothing
// > ETROG => IM State root is retrieved from the receipt.im_state_root
if l2Block.ForkID < FORKID_ETROG {
// Populate intermediate state root with information from the system SC (or cache if available)
if imStateRoots == nil || (*imStateRoots)[blockStart.L2BlockNumber] == nil {
Expand All @@ -565,6 +569,8 @@ func GenerateDataStreamerFile(ctx context.Context, streamServer *datastreamer.St
} else {
tx.StateRoot = common.BytesToHash((*imStateRoots)[blockStart.L2BlockNumber])
}
} else if l2Block.ForkID > FORKID_ETROG {
tx.StateRoot = tx.ImStateRoot
}

_, err = streamServer.AddStreamEntry(EntryTypeL2Tx, tx.Encode())
Expand Down
2 changes: 1 addition & 1 deletion state/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func (s *State) SetGenesis(ctx context.Context, block Block, genesis Genesis, m
storeTxsEGPData := []StoreTxEGPData{}
txsL2Hash := []common.Hash{}

err = s.AddL2Block(ctx, batch.BatchNumber, l2Block, receipts, txsL2Hash, storeTxsEGPData, dbTx)
err = s.AddL2Block(ctx, batch.BatchNumber, l2Block, receipts, txsL2Hash, storeTxsEGPData, []common.Hash{}, dbTx)
if err != nil {
return common.Hash{}, err
}
Expand Down
36 changes: 22 additions & 14 deletions state/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,19 +279,23 @@ func DecodeTx(encodedTx string) (*types.Transaction, error) {
}

// GenerateReceipt generates a receipt from a processed transaction
func GenerateReceipt(blockNumber *big.Int, processedTx *ProcessTransactionResponse, txIndex uint) *types.Receipt {
func GenerateReceipt(blockNumber *big.Int, processedTx *ProcessTransactionResponse, txIndex uint, forkID uint64) *types.Receipt {
receipt := &types.Receipt{
Type: uint8(processedTx.Type),
PostState: processedTx.StateRoot.Bytes(),
CumulativeGasUsed: processedTx.GasUsed,
BlockNumber: blockNumber,
GasUsed: processedTx.GasUsed,
TxHash: processedTx.Tx.Hash(),
TransactionIndex: txIndex,
ContractAddress: processedTx.CreateAddress,
Logs: processedTx.Logs,
Type: uint8(processedTx.Type),
BlockNumber: blockNumber,
GasUsed: processedTx.GasUsed,
TxHash: processedTx.Tx.Hash(),
TransactionIndex: txIndex,
ContractAddress: processedTx.CreateAddress,
Logs: processedTx.Logs,
}
if forkID <= FORKID_ETROG {
receipt.PostState = processedTx.StateRoot.Bytes()
receipt.CumulativeGasUsed = processedTx.GasUsed
} else {
receipt.PostState = ZeroHash.Bytes()
receipt.CumulativeGasUsed = processedTx.CumulativeGasUsed
}

if processedTx.EffectiveGasPrice != "" {
effectiveGasPrice, ok := big.NewInt(0).SetString(processedTx.EffectiveGasPrice, 0)
if !ok {
Expand All @@ -309,10 +313,14 @@ func GenerateReceipt(blockNumber *big.Int, processedTx *ProcessTransactionRespon
for i := 0; i < len(receipt.Logs); i++ {
receipt.Logs[i].TxHash = processedTx.Tx.Hash()
}
if processedTx.RomError == nil {
receipt.Status = types.ReceiptStatusSuccessful
if forkID <= FORKID_ETROG {
if processedTx.RomError == nil {
receipt.Status = types.ReceiptStatusSuccessful
} else {
receipt.Status = types.ReceiptStatusFailed
}
} else {
receipt.Status = types.ReceiptStatusFailed
receipt.Status = uint64(processedTx.Status)
}

return receipt
Expand Down
4 changes: 2 additions & 2 deletions state/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ type storage interface {
GetL2BlockTransactionCountByHash(ctx context.Context, blockHash common.Hash, dbTx pgx.Tx) (uint64, error)
GetL2BlockTransactionCountByNumber(ctx context.Context, blockNumber uint64, dbTx pgx.Tx) (uint64, error)
GetTransactionEGPLogByHash(ctx context.Context, transactionHash common.Hash, dbTx pgx.Tx) (*EffectiveGasPriceLog, error)
AddL2Block(ctx context.Context, batchNumber uint64, l2Block *L2Block, receipts []*types.Receipt, txsL2Hash []common.Hash, txsEGPData []StoreTxEGPData, dbTx pgx.Tx) error
AddL2Block(ctx context.Context, batchNumber uint64, l2Block *L2Block, receipts []*types.Receipt, txsL2Hash []common.Hash, txsEGPData []StoreTxEGPData, imStateRoots []common.Hash, dbTx pgx.Tx) error
GetLastVirtualizedL2BlockNumber(ctx context.Context, dbTx pgx.Tx) (uint64, error)
GetLastConsolidatedL2BlockNumber(ctx context.Context, dbTx pgx.Tx) (uint64, error)
GetLastVerifiedL2BlockNumberUntilL1Block(ctx context.Context, l1FinalizedBlockNumber uint64, dbTx pgx.Tx) (uint64, error)
Expand All @@ -93,7 +93,7 @@ type storage interface {
IsL2BlockConsolidated(ctx context.Context, blockNumber uint64, dbTx pgx.Tx) (bool, error)
IsL2BlockVirtualized(ctx context.Context, blockNumber uint64, dbTx pgx.Tx) (bool, error)
GetLogs(ctx context.Context, fromBlock uint64, toBlock uint64, addresses []common.Address, topics [][]common.Hash, blockHash *common.Hash, since *time.Time, dbTx pgx.Tx) ([]*types.Log, error)
AddReceipt(ctx context.Context, receipt *types.Receipt, dbTx pgx.Tx) error
AddReceipt(ctx context.Context, receipt *types.Receipt, imStateRoot common.Hash, dbTx pgx.Tx) error
AddLog(ctx context.Context, l *types.Log, dbTx pgx.Tx) error
GetExitRootByGlobalExitRoot(ctx context.Context, ger common.Hash, dbTx pgx.Tx) (*GlobalExitRoot, error)
AddSequence(ctx context.Context, sequence Sequence, dbTx pgx.Tx) error
Expand Down
42 changes: 22 additions & 20 deletions state/mocks/mock_storage.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion state/pgstatestorage/datastream.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func scanL2Block(row pgx.Row) (*state.DSL2Block, error) {

// GetDSL2Transactions returns the L2 transactions
func (p *PostgresStorage) GetDSL2Transactions(ctx context.Context, firstL2Block, lastL2Block uint64, dbTx pgx.Tx) ([]*state.DSL2Transaction, error) {
const l2TxSQL = `SELECT l2_block_num, t.effective_percentage, t.encoded, r.post_state
const l2TxSQL = `SELECT l2_block_num, t.effective_percentage, t.encoded, r.post_state, r.im_state_root
FROM state.transaction t, state.receipt r
WHERE l2_block_num BETWEEN $1 AND $2 AND r.tx_hash = t.hash
ORDER BY t.l2_block_num ASC, r.tx_index ASC`
Expand Down Expand Up @@ -120,11 +120,13 @@ func scanDSL2Transaction(row pgx.Row) (*state.DSL2Transaction, error) {
l2Transaction := state.DSL2Transaction{}
encoded := []byte{}
postState := []byte{}
imStateRoot := []byte{}
if err := row.Scan(
&l2Transaction.L2BlockNumber,
&l2Transaction.EffectiveGasPricePercentage,
&encoded,
&postState,
&imStateRoot,
); err != nil {
return nil, err
}
Expand All @@ -142,6 +144,7 @@ func scanDSL2Transaction(row pgx.Row) (*state.DSL2Transaction, error) {
l2Transaction.EncodedLength = uint32(len(l2Transaction.Encoded))
l2Transaction.IsValid = 1
l2Transaction.StateRoot = common.BytesToHash(postState)
l2Transaction.ImStateRoot = common.BytesToHash(imStateRoot)
return &l2Transaction, nil
}

Expand Down
6 changes: 3 additions & 3 deletions state/pgstatestorage/l2block.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ func (p *PostgresStorage) GetL2BlockTransactionCountByNumber(ctx context.Context
}

// AddL2Block adds a new L2 block to the State Store
func (p *PostgresStorage) AddL2Block(ctx context.Context, batchNumber uint64, l2Block *state.L2Block, receipts []*types.Receipt, txsL2Hash []common.Hash, txsEGPData []state.StoreTxEGPData, dbTx pgx.Tx) error {
//TODO: Optmize this function using only one SQL (with several values) to insert all the txs, receips and logs
func (p *PostgresStorage) AddL2Block(ctx context.Context, batchNumber uint64, l2Block *state.L2Block, receipts []*types.Receipt, txsL2Hash []common.Hash, txsEGPData []state.StoreTxEGPData, imStateRoots []common.Hash, dbTx pgx.Tx) error {
// TODO: Optimize this function using only one SQL (with several values) to insert all the txs, receipts and logs
log.Debugf("[AddL2Block] adding L2 block %d", l2Block.NumberU64())
start := time.Now()

Expand Down Expand Up @@ -255,7 +255,7 @@ func (p *PostgresStorage) AddL2Block(ctx context.Context, batchNumber uint64, l2
}

if len(receipts) > 0 {
p.AddReceipts(ctx, receipts, dbTx)
p.AddReceipts(ctx, receipts, imStateRoots, dbTx)

var logs []*types.Log
for _, receipt := range receipts {
Expand Down
Loading
Loading