Skip to content

Commit

Permalink
move txsize related keccak logging to inside logger
Browse files Browse the repository at this point in the history
  • Loading branch information
omerfirmak committed Aug 30, 2024
1 parent 7c0f014 commit 5030ce9
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 44 deletions.
1 change: 1 addition & 0 deletions accounts/abi/bind/backends/simulated.go
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,7 @@ func (m callMsg) Value() *big.Int { return m.CallMsg.Value }
func (m callMsg) Data() []byte { return m.CallMsg.Data }
func (m callMsg) AccessList() types.AccessList { return m.CallMsg.AccessList }
func (m callMsg) IsL1MessageTx() bool { return false }
func (m callMsg) TxSize() common.StorageSize { return 0 }

// filterBackend implements filters.Backend to support filtering for logs without
// taking bloom-bits acceleration structures into account.
Expand Down
1 change: 1 addition & 0 deletions core/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func NewEVMTxContext(msg Message) vm.TxContext {
To: msg.To(),
GasPrice: new(big.Int).Set(msg.GasPrice()),
IsL1MessageTx: msg.IsL1MessageTx(),
TxSize: msg.TxSize(),
}
}

Expand Down
1 change: 1 addition & 0 deletions core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ type Message interface {
Data() []byte
AccessList() types.AccessList
IsL1MessageTx() bool
TxSize() common.StorageSize
}

// ExecutionResult includes all output after executing given evm
Expand Down
27 changes: 15 additions & 12 deletions core/types/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,7 @@ type Message struct {
accessList AccessList
isFake bool
isL1MessageTx bool
txSize common.StorageSize
}

func NewMessage(from common.Address, to *common.Address, nonce uint64, amount *big.Int, gasLimit uint64, gasPrice, gasFeeCap, gasTipCap *big.Int, data []byte, accessList AccessList, isFake bool) Message {
Expand Down Expand Up @@ -785,6 +786,7 @@ func (tx *Transaction) AsMessage(s Signer, baseFee *big.Int) (Message, error) {
accessList: tx.AccessList(),
isFake: false,
isL1MessageTx: tx.IsL1MessageTx(),
txSize: tx.Size(),
}
// If baseFee provided, set gasPrice to effectiveGasPrice.
if baseFee != nil {
Expand All @@ -795,18 +797,19 @@ func (tx *Transaction) AsMessage(s Signer, baseFee *big.Int) (Message, error) {
return msg, err
}

func (m Message) From() common.Address { return m.from }
func (m Message) To() *common.Address { return m.to }
func (m Message) GasPrice() *big.Int { return m.gasPrice }
func (m Message) GasFeeCap() *big.Int { return m.gasFeeCap }
func (m Message) GasTipCap() *big.Int { return m.gasTipCap }
func (m Message) Value() *big.Int { return m.amount }
func (m Message) Gas() uint64 { return m.gasLimit }
func (m Message) Nonce() uint64 { return m.nonce }
func (m Message) Data() []byte { return m.data }
func (m Message) AccessList() AccessList { return m.accessList }
func (m Message) IsFake() bool { return m.isFake }
func (m Message) IsL1MessageTx() bool { return m.isL1MessageTx }
func (m Message) From() common.Address { return m.from }
func (m Message) To() *common.Address { return m.to }
func (m Message) GasPrice() *big.Int { return m.gasPrice }
func (m Message) GasFeeCap() *big.Int { return m.gasFeeCap }
func (m Message) GasTipCap() *big.Int { return m.gasTipCap }
func (m Message) Value() *big.Int { return m.amount }
func (m Message) Gas() uint64 { return m.gasLimit }
func (m Message) Nonce() uint64 { return m.nonce }
func (m Message) Data() []byte { return m.data }
func (m Message) AccessList() AccessList { return m.accessList }
func (m Message) IsFake() bool { return m.isFake }
func (m Message) IsL1MessageTx() bool { return m.isL1MessageTx }
func (m Message) TxSize() common.StorageSize { return m.txSize }

// copyAddressPtr copies an address.
func copyAddressPtr(a *common.Address) *common.Address {
Expand Down
9 changes: 5 additions & 4 deletions core/vm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,11 @@ type BlockContext struct {
// All fields can change between transactions.
type TxContext struct {
// Message information
Origin common.Address // Provides information for ORIGIN
To *common.Address // Provides information for trace
IsL1MessageTx bool // Provides information for trace
GasPrice *big.Int // Provides information for GASPRICE
Origin common.Address // Provides information for ORIGIN
To *common.Address // Provides information for trace
IsL1MessageTx bool // Provides information for trace
TxSize common.StorageSize // Provides information for trace
GasPrice *big.Int // Provides information for GASPRICE
}

// EVM is the Ethereum Virtual Machine base object and provides
Expand Down
8 changes: 0 additions & 8 deletions miner/scroll_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -699,19 +699,11 @@ func (w *worker) processTxn(tx *types.Transaction) (bool, error) {
w.current.txs = append(w.current.txs, tx)
w.current.receipts = append(w.current.receipts, receipt)

txRlp, _ := tx.MarshalBinary()
if !tx.IsL1MessageTx() {
// only consider block size limit for L2 transactions
w.current.blockSize += tx.Size()
// keccak: all l2 tx's rlp_signed_bytes combined together makes a single input to keccak
w.current.cccLogger.LogKeccakAllL2TxsLen(uint64(len(txRlp)))
// keccak: add block txs' rlp unsigned bytes,
// @note we should use unsigned length, but no interface yet use signed one. so the results will over-estimate a little.
w.current.cccLogger.LogKeccakChunks(uint64(len(txRlp)))
} else {
w.current.nextL1MsgIndex = tx.AsL1MessageTx().QueueIndex + 1
// keccak: each l1 tx's signed bytes is a standalone input to keccak
w.current.cccLogger.LogKeccakChunks(uint64(len(txRlp)))
}
return false, nil
}
Expand Down
33 changes: 13 additions & 20 deletions rollup/ccc/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,6 @@ func (l *Logger) logSha256(inputLen uint64) {
l.sha256Usage += numBlocks * blockRows
}

// LogKeccakChunks records the number of chunks used for keccak given the length of bytes.
func (l *Logger) LogKeccakChunks(length uint64) {
l.keccakChunkCount += computeKeccakChunks(length)
}

func (l *Logger) LogKeccakAllL2TxsLen(length uint64) {
l.keccakAllL2TxsLen += length
}

func (l *Logger) CaptureStart(env *vm.EVM, from common.Address, to common.Address, create bool, input []byte, gas uint64, value *big.Int) {
l.currentEnv = env
l.isCreate = create
Expand All @@ -176,9 +167,11 @@ func (l *Logger) CaptureStart(env *vm.EVM, from common.Address, to common.Addres

if !env.TxContext.IsL1MessageTx {
l.sigCount++
l.keccakAllL2TxsLen += uint64(env.TxContext.TxSize)
} else {
l.l1TxnCount++
}
l.keccakChunkCount += computeKeccakChunks(uint64(env.TxContext.TxSize))
}

func (l *Logger) CaptureState(pc uint64, op vm.OpCode, gas, cost uint64, scope *vm.ScopeContext, rData []byte, depth int, err error) {
Expand All @@ -204,12 +197,11 @@ func (l *Logger) CaptureState(pc uint64, op vm.OpCode, gas, cost uint64, scope *
l.logCopy(scope.Stack.Back(3).Uint64())
case vm.CALLDATACOPY, vm.RETURNDATACOPY, vm.CODECOPY, vm.MCOPY, vm.CREATE, vm.CREATE2:
l.logCopy(scope.Stack.Back(2).Uint64())
case vm.LOG0, vm.LOG1, vm.LOG2, vm.LOG3, vm.LOG4, vm.SHA3, vm.RETURN, vm.REVERT:
case vm.SHA3:
l.keccakChunkCount += computeKeccakChunks(scope.Stack.Back(1).Uint64())
fallthrough // log copy as well
case vm.LOG0, vm.LOG1, vm.LOG2, vm.LOG3, vm.LOG4, vm.RETURN, vm.REVERT:
l.logCopy(scope.Stack.Back(1).Uint64())
// keccak: add inputs from sha3 opcode in the block
if op == vm.SHA3 {
l.LogKeccakChunks(scope.Stack.Back(1).Uint64())
}
case vm.DELEGATECALL, vm.STATICCALL:
inputOffset := int64(scope.Stack.Back(2).Uint64())
inputLen := int64(scope.Stack.Back(3).Uint64())
Expand Down Expand Up @@ -302,12 +294,13 @@ func (l *Logger) RowConsumption() types.RowConsumption {
RowNumber: l.modExpUsage,
}, {
Name: "keccak",
// dummy tx: 37 rlp unsigned tx bytes
// ecrecover: (sigCount + 1) * computeKeccakChunks(64), another one from dummy tx
// data_bytes: 8 + 8 + 32 + 8 + 2 + 32 * numL1Txs bytes
// pi_bytes: 8 + 32 * 5 = 168 bytes
// all_l2_txs_bytes_mashed_together:
RowNumber: (l.keccakChunkCount+computeKeccakChunks(37)+(l.sigCount+1)*computeKeccakChunks(64)+computeKeccakChunks(58+32*l.l1TxnCount)+computeKeccakChunks(168)+computeKeccakChunks(l.keccakAllL2TxsLen))*keccakRowsPerChunk + keccakRowsPerRound,
RowNumber: (l.keccakChunkCount+
computeKeccakChunks(27)+ // dummy tx: 27 rlp unsigned tx bytes
(l.sigCount+1)*computeKeccakChunks(64)+ // ecrecover: (sigCount + 1) * computeKeccakChunks(64), another one from dummy tx
computeKeccakChunks(58+32*l.l1TxnCount)+ // data_bytes: 8 + 8 + 32 + 8 + 2 + 32 * numL1Txs bytes
computeKeccakChunks(168)+ // pi_bytes: 8 + 32 * 5 = 168 bytes
computeKeccakChunks(l.keccakAllL2TxsLen))*keccakRowsPerChunk + // all_l2_txs_bytes_mashed_together
keccakRowsPerRound,
},
}
}
Expand Down

0 comments on commit 5030ce9

Please sign in to comment.