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

backport state revert fix and add logging #1764

Merged
merged 2 commits into from
Jan 29, 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
51 changes: 40 additions & 11 deletions go/enclave/components/batch_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ func (executor *batchExecutor) payL1Fees(stateDB *state.StateDB, context *BatchE
stateDB.SubBalance(*sender, cost)
stateDB.AddBalance(context.Creator, cost)
// todo - add refund logic.
executor.logger.Info("Tx L1 cost", log.TxKey, tx.Hash(), "cost", cost)

transactions = append(transactions, tx)
}
Expand All @@ -127,6 +128,7 @@ func (executor *batchExecutor) refundL1Fees(stateDB *state.StateDB, context *Bat

stateDB.AddBalance(*sender, cost)
stateDB.SubBalance(context.Creator, cost)
executor.logger.Info("Tx L1 cost refund", log.TxKey, tx.Hash(), "cost", cost)
}
}

Expand Down Expand Up @@ -169,6 +171,8 @@ func (executor *batchExecutor) ComputeBatch(context *BatchExecutionContext, fail
return nil, fmt.Errorf("could not create stateDB. Cause: %w", err)
}

snap := stateDB.Snapshot()

var messages common.CrossChainMessages
var transfers common.ValueTransferEvents
if context.SequencerNo.Int64() > int64(common.L2GenesisSeqNo+1) {
Expand Down Expand Up @@ -198,6 +202,30 @@ func (executor *batchExecutor) ComputeBatch(context *BatchExecutionContext, fail
return nil, fmt.Errorf("batch computation failed due to cross chain messages. Cause: %w", err)
}

if failForEmptyBatch &&
len(txReceipts) == 0 &&
len(ccReceipts) == 0 &&
len(transactionsToProcess)-len(excludedTxs) == 0 &&
len(crossChainTransactions) == 0 &&
len(messages) == 0 &&
len(transfers) == 0 {
if snap > 0 {
//// revert any unexpected mutation to the statedb
stateDB.RevertToSnapshot(snap)
}
return nil, ErrNoTransactionsToProcess
}

for _, xChainTx := range crossChainTransactions {
executor.logger.Info("Xchain tx",
log.TxKey, xChainTx.Hash(),
"type", xChainTx.Type(),
"time", xChainTx.Time(),
"payload", gethcommon.Bytes2Hex(xChainTx.Data()),
"gas", xChainTx.Gas(),
)
}

// we need to copy the batch to reset the internal hash cache
copyBatch := *batch
copyBatch.Header.Root = stateDB.IntermediateRoot(false)
Expand All @@ -210,15 +238,6 @@ func (executor *batchExecutor) ComputeBatch(context *BatchExecutionContext, fail

allReceipts := append(txReceipts, ccReceipts...)
executor.populateHeader(&copyBatch, allReceipts)
if failForEmptyBatch &&
len(txReceipts) == 0 &&
len(ccReceipts) == 0 &&
len(transactionsToProcess)-len(excludedTxs) == 0 &&
len(crossChainTransactions) == 0 &&
len(messages) == 0 &&
len(transfers) == 0 {
return nil, ErrNoTransactionsToProcess
}

// the logs and receipts produced by the EVM have the wrong hash which must be adjusted
for _, receipt := range allReceipts {
Expand All @@ -229,8 +248,9 @@ func (executor *batchExecutor) ComputeBatch(context *BatchExecutionContext, fail
}

return &ComputedBatch{
Batch: &copyBatch,
Receipts: allReceipts,
Batch: &copyBatch,
Receipts: allReceipts,
XChainTxs: crossChainTransactions,
Commit: func(deleteEmptyObjects bool) (gethcommon.Hash, error) {
executor.stateDBMutex.Lock()
defer executor.stateDBMutex.Unlock()
Expand Down Expand Up @@ -270,6 +290,15 @@ func (executor *batchExecutor) ExecuteBatch(batch *core.Batch) (types.Receipts,
if cb.Batch.Hash() != batch.Hash() {
// todo @stefan - generate a validator challenge here and return it
executor.logger.Error(fmt.Sprintf("Error validating batch. Calculated: %+v Incoming: %+v\n", cb.Batch.Header, batch.Header))
for _, xChainTx := range cb.XChainTxs {
executor.logger.Error("Xchain tx",
log.TxKey, xChainTx.Hash(),
"type", xChainTx.Type(),
"time", xChainTx.Time(),
"payload", gethcommon.Bytes2Hex(xChainTx.Data()),
"gas", xChainTx.Gas(),
)
}
return nil, fmt.Errorf("batch is in invalid state. Incoming hash: %s Computed hash: %s", batch.Hash(), cb.Batch.Hash())
}

Expand Down
7 changes: 4 additions & 3 deletions go/enclave/components/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@ type BatchExecutionContext struct {
// the computation of the batch. One might not want to commit in case the
// resulting batch differs than what is being validated for example.
type ComputedBatch struct {
Batch *core.Batch
Receipts types.Receipts
Commit func(bool) (gethcommon.Hash, error)
Batch *core.Batch
Receipts types.Receipts
XChainTxs common.L2Transactions
Commit func(bool) (gethcommon.Hash, error)
}

type BatchExecutor interface {
Expand Down
Loading