Skip to content

Commit

Permalink
Add debug info of gas cost of each opcode
Browse files Browse the repository at this point in the history
  • Loading branch information
andyzhang2023 committed Oct 12, 2024
1 parent cc24a25 commit 19fecc5
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
8 changes: 4 additions & 4 deletions core/block_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,11 @@ func (v *BlockValidator) ValidateBody(block *types.Block) error {
func debugGsSummary(gasSummaries map[int]*state.GasSummary, block *types.Block) {
for txIndex, s := range gasSummaries {
if s == nil {
log.Error("GasSummary debug, gs is nil", "blockNumber", block.NumberU64(), "txIndex", txIndex)
fmt.Printf("GasSummary, block:%d, txIndex:%d, is nil\n", block.NumberU64(), txIndex)
log.Error("[DEBUG invalid gas used], gs is nil", "blockNumber", block.NumberU64(), "txIndex", txIndex)
fmt.Printf("[DEBUG invalid gas used], block:%d, txIndex:%d, is nil\n", block.NumberU64(), txIndex)
} else {
log.Error("GasSummary debug, gs collected", "blockNumber", block.NumberU64(), "txIndex", txIndex, "gasSummary", s.Debug())
fmt.Printf("GasSummary, block:%d, txIndex:%d, gasSummary:%s\n", block.NumberU64(), txIndex, s.Debug())
log.Error("[DEBUG invalid gas used], gs collected", "blockNumber", block.NumberU64(), "txIndex", txIndex, "gasSummary", s.Debug())
fmt.Printf("[DEBUG invalid gas used], block:%d, txIndex:%d, gasSummary:%s\n", block.NumberU64(), txIndex, s.Debug())
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1949,11 +1949,13 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error)

// Process block using the parent state as reference point
pstart = time.Now()
fmt.Printf("[DEBUG invalid gas used] block %d, gas used %d\n", block.NumberU64(), block.GasUsed())
receipts, logs, usedGas, err = bc.processor.Process(block, statedb, bc.vmConfig)
if err == FallbackToSerialProcessorErr {
bc.UseSerialProcessor()
receipts, logs, usedGas, err = bc.processor.Process(block, statedb, bc.vmConfig)
}
fmt.Printf("[DEBUG invalid gas used] block %d, gas used %d\n", block.NumberU64(), block.GasUsed())
if err != nil {
bc.reportBlock(block, receipts, err)
followupInterrupt.Store(true)
Expand Down
3 changes: 3 additions & 0 deletions core/vm/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package vm

import (
"fmt"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/core/types"
Expand Down Expand Up @@ -199,6 +201,7 @@ func (in *EVMInterpreter) Run(contract *Contract, input []byte, readOnly bool) (
} else if sLen > operation.maxStack {
return nil, &ErrStackOverflow{stackLen: sLen, limit: operation.maxStack}
}
fmt.Printf("[DEBUG invalid gas used] from:%s, to:%s, op:%s, cost:%d\n", contract.caller.Address(), contract.self.Address(), op.String(), cost)
if !contract.UseGas(cost) {
return nil, ErrOutOfGas
}
Expand Down

0 comments on commit 19fecc5

Please sign in to comment.