Skip to content

Commit

Permalink
migrate sentio tracers
Browse files Browse the repository at this point in the history
fix tracemany error format

migrate override and gas changes (0xPolygonHermez#53)

fix race condition in p2p discover tab

add more information to root trace

correct handle call with value

set transfer value to zero if can't transfer

migrate memory compression (0xPolygonHermez#7)

correctly handle tracecallmany didn't find block

correctly handle if transaction index out of bound

fix(rpc): eth_blockNumber returns unsynced block number

adjust after api change

Add Mapping keys to post account (0xPolygonHermez#9)

fix when tracer failed before start and zero gas (0xPolygonHermez#11)

more fix on tracecall many format error

fix txn index, block hash in context

distinguish code address by storage slot (0xPolygonHermez#12)

code address for mapping keys (0xPolygonHermez#13)

return gas price of execution

ignore init code size limit (0xPolygonHermez#15)

chore: revert format changes, and add mev config json (0xPolygonHermez#6)

add mev-infra service

api change

code address field (0xPolygonHermez#16)

emit output field for revert (0xPolygonHermez#18)

fix(mev): fix parent block nil panic (0xPolygonHermez#17)

chore(mev): improve histoical state response (0xPolygonHermez#19)

patch after merge

no block para
  • Loading branch information
zfy0701 committed Nov 3, 2024
1 parent 5187e6f commit 3786286
Show file tree
Hide file tree
Showing 25 changed files with 9,873 additions and 31 deletions.
1 change: 1 addition & 0 deletions cmd/rpcdaemon/cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ import (
// Force-load native and js packages, to trigger registration
_ "github.com/ledgerwatch/erigon/eth/tracers/js"
_ "github.com/ledgerwatch/erigon/eth/tracers/native"
_ "github.com/ledgerwatch/erigon/eth/tracers/sentio"
)

var rootCmd = &cobra.Command{
Expand Down
11 changes: 11 additions & 0 deletions core/state/intra_block_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,10 @@ func (sdb *IntraBlockState) TxIndex() int {
return sdb.txIndex
}

func (sdb *IntraBlockState) BlockHash() libcommon.Hash {
return sdb.bhash
}

// DESCRIBED: docs/programmers_guide/guide.md#address---identifier-of-an-account
func (sdb *IntraBlockState) GetCode(addr libcommon.Address) []byte {
stateObject := sdb.getStateObject(addr)
Expand Down Expand Up @@ -885,3 +889,10 @@ func (sdb *IntraBlockState) AddressInAccessList(addr libcommon.Address) bool {
func (sdb *IntraBlockState) SlotInAccessList(addr libcommon.Address, slot libcommon.Hash) (addressPresent bool, slotPresent bool) {
return sdb.accessList.Contains(addr, slot)
}

func (sdb *IntraBlockState) SetCodeWithHashKnown(addr libcommon.Address, codeHash libcommon.Hash, code []byte) {
stateObject := sdb.GetOrNewStateObject(addr)
if stateObject != nil {
stateObject.SetCode(codeHash, code)
}
}
10 changes: 6 additions & 4 deletions core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,10 +404,12 @@ func (st *StateTransition) TransitionDb(refunds bool, gasBailout bool) (*Executi
if err != nil {
return nil, err
}
if st.gasRemaining < intrinsicGas {
return nil, fmt.Errorf("%w: have %d, want %d", ErrIntrinsicGas, st.gasRemaining, intrinsicGas)
if !st.evm.Config().IgnoreGas {
if st.gasRemaining < intrinsicGas {
return nil, fmt.Errorf("%w: have %d, want %d", ErrIntrinsicGas, st.gasRemaining, intrinsicGas)
}
st.gasRemaining -= intrinsicGas
}
st.gasRemaining -= intrinsicGas

var bailout bool
// Gas bailout (for trace_call) should only be applied if there is not sufficient balance to perform value transfer
Expand All @@ -418,7 +420,7 @@ func (st *StateTransition) TransitionDb(refunds bool, gasBailout bool) (*Executi
}

// Check whether the init code size has been exceeded.
if isEIP3860 && contractCreation && len(st.data) > params.MaxInitCodeSize {
if !st.evm.Config().IgnoreCodeSizeLimit && isEIP3860 && contractCreation && len(st.data) > params.MaxInitCodeSize {
return nil, fmt.Errorf("%w: code size %v limit %v", ErrMaxInitCodeSizeExceeded, len(st.data), params.MaxInitCodeSize)
}

Expand Down
26 changes: 25 additions & 1 deletion core/vm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,17 @@ func (evm *EVM) create(caller ContractRef, codeAndHash *codeAndHash, gasRemainin
var gasConsumption uint64
depth := evm.interpreter.Depth()

if evm.config.CreateAddressOverride != nil {
address = *evm.config.CreateAddressOverride
}
if evm.config.CreationCodeOverrides != nil {
if code, ok := evm.config.CreationCodeOverrides[address]; ok {
codeAndHash.code = code
codeAndHash.hash = libcommon.Hash{}
_ = codeAndHash.Hash()
}
}

if evm.config.Debug {
if depth == 0 {
evm.config.Tracer.CaptureStart(evm, caller.Address(), address, false /* precompile */, true /* create */, codeAndHash.code, gasRemaining+intrinsicGas, value, nil)
Expand Down Expand Up @@ -421,6 +432,15 @@ func (evm *EVM) create(caller ContractRef, codeAndHash *codeAndHash, gasRemainin
evm.intraBlockState.AddAddressToAccessList(address)
}

if evm.config.CreateAddressOverride == nil {
// Ensure there's no existing contract already at the designated address
contractHash := evm.intraBlockState.GetCodeHash(address)
if evm.intraBlockState.GetNonce(address) != 0 || (contractHash != (libcommon.Hash{}) && contractHash != emptyCodeHash) {
err = ErrContractAddressCollision
return nil, libcommon.Address{}, 0, err
}
}

// Create a new account on the state
snapshot := evm.intraBlockState.Snapshot()
evm.intraBlockState.CreateAccount(address, true)
Expand All @@ -441,7 +461,8 @@ func (evm *EVM) create(caller ContractRef, codeAndHash *codeAndHash, gasRemainin
ret, err = run(evm, contract, nil, false)

// EIP-170: Contract code size limit
if err == nil && evm.chainRules.IsSpuriousDragon && len(ret) > params.MaxCodeSize {

if err == nil && !evm.config.IgnoreCodeSizeLimit && evm.chainRules.IsSpuriousDragon && len(ret) > params.MaxCodeSize {
// Gnosis Chain prior to Shanghai didn't have EIP-170 enabled,
// but EIP-3860 (part of Shanghai) requires EIP-170.
if !evm.chainRules.IsAura || evm.config.HasEip3860(evm.chainRules) {
Expand All @@ -459,6 +480,9 @@ func (evm *EVM) create(caller ContractRef, codeAndHash *codeAndHash, gasRemainin
// by the error checking condition below.
if err == nil {
createDataGas := uint64(len(ret)) * params.CreateDataGas
if evm.config.IgnoreGas {
createDataGas = 0
}
if contract.UseGas(createDataGas) {
evm.intraBlockState.SetCode(address, ret)
} else if evm.chainRules.IsHomestead {
Expand Down
6 changes: 4 additions & 2 deletions core/vm/instructions.go
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ func opCreate(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]b
input = scope.Memory.GetCopy(int64(offset.Uint64()), int64(size.Uint64()))
gas = scope.Contract.Gas
)
if interpreter.evm.ChainRules().IsTangerineWhistle {
if !interpreter.cfg.IgnoreGas && interpreter.evm.ChainRules().IsTangerineWhistle {
gas -= gas / 64
}
// reuse size int for stackvalue
Expand Down Expand Up @@ -690,7 +690,9 @@ func opCreate2(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]
)

// Apply EIP150
gas -= gas / 64
if !interpreter.cfg.IgnoreGas {
gas -= gas / 64
}
scope.Contract.UseGas(gas)
// reuse size int for stackvalue
stackValue := size
Expand Down
23 changes: 23 additions & 0 deletions core/vm/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/ledgerwatch/erigon-lib/chain"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/common/hexutility"
"github.com/ledgerwatch/erigon-lib/common/math"
"github.com/ledgerwatch/log/v3"

Expand All @@ -42,6 +43,11 @@ type Config struct {
RestoreState bool // Revert all changes made to the state (useful for constant system calls)

ExtraEips []int // Additional EIPS that are to be enabled

CreationCodeOverrides map[libcommon.Address]hexutility.Bytes
CreateAddressOverride *libcommon.Address
IgnoreGas bool
IgnoreCodeSizeLimit bool
}

func NewTraceVmConfig() Config {
Expand Down Expand Up @@ -179,6 +185,23 @@ func NewEVMInterpreter(evm *EVM, cfg Config) *EVMInterpreter {
}
}

if cfg.IgnoreGas {
jt = copyJumpTable(jt)
for i, op := range jt {
opCode := OpCode(i)
// retain call costs to prevent call stack from going too deep
// some contracts use a loop to burn gas
// if all codes in the loop have zero cost, it will run forever
if opCode == CALL || opCode == STATICCALL || opCode == CALLCODE || opCode == DELEGATECALL || opCode == GAS {
continue
}
op.constantGas = 0
op.dynamicGas = func(*EVM, *Contract, *stack.Stack, *Memory, uint64) (uint64, error) {
return 0, nil
}
}
}

return &EVMInterpreter{
VM: &VM{
evm: evm,
Expand Down
7 changes: 7 additions & 0 deletions eth/tracers/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package tracers
import (
"encoding/json"

libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/common/hexutil"
"github.com/ledgerwatch/erigon-lib/common/hexutility"
"github.com/ledgerwatch/erigon/eth/tracers/logger"
"github.com/ledgerwatch/erigon/turbo/adapter/ethapi"
)
Expand All @@ -18,6 +20,11 @@ type TraceConfig struct {
NoRefunds *bool // Turns off gas refunds when tracing
StateOverrides *ethapi.StateOverrides

IgnoreGas *bool
IgnoreCodeSizeLimit *bool
CreationCodeOverrides map[libcommon.Address]hexutility.Bytes
CreateAddressOverride *libcommon.Address

BorTraceEnabled *bool
TxIndex *hexutil.Uint
}
51 changes: 43 additions & 8 deletions eth/tracers/logger/json_stream.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package logger

import (
"bytes"
"context"
"encoding/hex"
"sort"
Expand Down Expand Up @@ -31,6 +32,10 @@ type JsonStreamLogger struct {
output []byte //nolint
err error //nolint
env *vm.EVM

prevMem [][]byte
prevMemWindow int
prevMemIdx int
}

// NewStructLogger returns a new logger
Expand All @@ -43,6 +48,9 @@ func NewJsonStreamLogger(cfg *LogConfig, ctx context.Context, stream *jsoniter.S
}
if cfg != nil {
logger.cfg = *cfg
logger.prevMemWindow = cfg.MemoryCompressionWindow
logger.prevMemIdx = 0
logger.prevMem = make([][]byte, cfg.MemoryCompressionWindow)
}
return logger
}
Expand Down Expand Up @@ -145,16 +153,43 @@ func (l *JsonStreamLogger) CaptureState(pc uint64, op vm.OpCode, gas, cost uint6
}
if !l.cfg.DisableMemory {
memData := memory.Data()
l.stream.WriteMore()
l.stream.WriteObjectField("memory")
l.stream.WriteArrayStart()
for i := 0; i+32 <= len(memData); i += 32 {
if i > 0 {
l.stream.WriteMore()
foundEq := false
if l.prevMemWindow > 0 {
i := l.prevMemIdx
for dist := 1; dist <= l.prevMemWindow; dist++ {
if i--; i < 0 {
i = l.prevMemWindow - 1
}
if len(l.prevMem[i]) == len(memData) && bytes.Equal(l.prevMem[i], memData) {
foundEq = true
l.stream.WriteMore()
l.stream.WriteObjectField("meq")
l.stream.WriteInt(dist)
break
}
}
if l.prevMemIdx++; l.prevMemIdx == l.prevMemWindow {
l.prevMemIdx = 0
}
if foundEq {
l.prevMem[l.prevMemIdx] = l.prevMem[i]
} else {
l.prevMem[l.prevMemIdx] = make([]byte, len(memData))
copy(l.prevMem[l.prevMemIdx], memData)
}
l.stream.WriteString(string(l.hexEncodeBuf[0:hex.Encode(l.hexEncodeBuf[:], memData[i:i+32])]))
}
l.stream.WriteArrayEnd()
if !foundEq {
l.stream.WriteMore()
l.stream.WriteObjectField("memory")
l.stream.WriteArrayStart()
for i := 0; i+32 <= len(memData); i += 32 {
if i > 0 {
l.stream.WriteMore()
}
l.stream.WriteString(string(l.hexEncodeBuf[0:hex.Encode(l.hexEncodeBuf[:], memData[i:i+32])]))
}
l.stream.WriteArrayEnd()
}
}
if outputStorage {
l.stream.WriteMore()
Expand Down
13 changes: 7 additions & 6 deletions eth/tracers/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@ func (s Storage) Copy() Storage {

// LogConfig are the configuration options for structured logger the EVM
type LogConfig struct {
DisableMemory bool // disable memory capture
DisableStack bool // disable stack capture
DisableStorage bool // disable storage capture
DisableReturnData bool // disable return data capture
Debug bool // print output during capture end
Limit int // maximum length of output, but zero means unlimited
DisableMemory bool // disable memory capture
DisableStack bool // disable stack capture
DisableStorage bool // disable storage capture
DisableReturnData bool // disable return data capture
Debug bool // print output during capture end
Limit int // maximum length of output, but zero means unlimited
MemoryCompressionWindow int
// Chain overrides, can be used to execute a trace using future fork rules
Overrides *chain.Config `json:"overrides,omitempty"`
}
Expand Down
75 changes: 75 additions & 0 deletions eth/tracers/sentio/gen_account_json.go

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

Loading

0 comments on commit 3786286

Please sign in to comment.