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

Integrate CaptureArbitrumStorageGet/Set into the prestate tracer #352

Open
wants to merge 3 commits into
base: merge-1.14.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions core/tracing/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ type (
LogHook = func(log *types.Log)

CaptureArbitrumTransferHook = func(from, to *common.Address, value *big.Int, before bool, purpose string)
CaptureArbitrumStorageGetHook = func(key common.Hash, depth int, before bool)
CaptureArbitrumStorageSetHook = func(key, value common.Hash, depth int, before bool)
CaptureArbitrumStorageGetHook = func(addr common.Address, key, mappedKey common.Hash, depth int, before bool)
CaptureArbitrumStorageSetHook = func(addr common.Address, key, mappedKey, value common.Hash, depth int, before bool)

CaptureStylusHostioHook = func(name string, args, outs []byte, startInk, endInk uint64)
)
Expand Down
6 changes: 4 additions & 2 deletions eth/tracers/js/tracer_arbitrum.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ func (jst *jsTracer) CaptureArbitrumTransfer(
}
}

func (*jsTracer) CaptureArbitrumStorageGet(key common.Hash, depth int, before bool) {}
func (*jsTracer) CaptureArbitrumStorageSet(key, value common.Hash, depth int, before bool) {}
func (*jsTracer) CaptureArbitrumStorageGet(addr common.Address, key, mappedKey common.Hash, depth int, before bool) {
}
func (*jsTracer) CaptureArbitrumStorageSet(addr common.Address, key, mappedKey, value common.Hash, depth int, before bool) {
}

func (jst *jsTracer) CaptureStylusHostio(name string, args, outs []byte, startInk, endInk uint64) {
hostio, ok := goja.AssertFunction(jst.obj.Get("hostio"))
Expand Down
24 changes: 16 additions & 8 deletions eth/tracers/logger/logger_arbitrum.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,23 @@ func (*StructLogger) CaptureArbitrumTransfer(from, to *common.Address, value *bi
func (*mdLogger) CaptureArbitrumTransfer(from, to *common.Address, amount *big.Int, before bool, purpose string) {
}

func (*AccessListTracer) CaptureArbitrumStorageGet(key common.Hash, depth int, before bool) {}
func (*jsonLogger) CaptureArbitrumStorageGet(key common.Hash, depth int, before bool) {}
func (*StructLogger) CaptureArbitrumStorageGet(key common.Hash, depth int, before bool) {}
func (*mdLogger) CaptureArbitrumStorageGet(key common.Hash, depth int, before bool) {}
func (*AccessListTracer) CaptureArbitrumStorageGet(addr common.Address, key, mappedKey common.Hash, depth int, before bool) {
}
func (*jsonLogger) CaptureArbitrumStorageGet(addr common.Address, key, mappedKey common.Hash, depth int, before bool) {
}
func (*StructLogger) CaptureArbitrumStorageGet(addr common.Address, key, mappedKey common.Hash, depth int, before bool) {
}
func (*mdLogger) CaptureArbitrumStorageGet(addr common.Address, key, mappedKey common.Hash, depth int, before bool) {
}

func (*AccessListTracer) CaptureArbitrumStorageSet(key, value common.Hash, depth int, before bool) {}
func (*jsonLogger) CaptureArbitrumStorageSet(key, value common.Hash, depth int, before bool) {}
func (*StructLogger) CaptureArbitrumStorageSet(key, value common.Hash, depth int, before bool) {}
func (*mdLogger) CaptureArbitrumStorageSet(key, value common.Hash, depth int, before bool) {}
func (*AccessListTracer) CaptureArbitrumStorageSet(addr common.Address, key, mappedKey, value common.Hash, depth int, before bool) {
}
func (*jsonLogger) CaptureArbitrumStorageSet(addr common.Address, key, mappedKey, value common.Hash, depth int, before bool) {
}
func (*StructLogger) CaptureArbitrumStorageSet(addr common.Address, key, mappedKey, value common.Hash, depth int, before bool) {
}
func (*mdLogger) CaptureArbitrumStorageSet(addr common.Address, key, mappedKey, value common.Hash, depth int, before bool) {
}

func (*AccessListTracer) CaptureStylusHostio(name string, args, outs []byte, startInk, endInk uint64) {
}
Expand Down
22 changes: 14 additions & 8 deletions eth/tracers/native/gen_account_json.go

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

8 changes: 4 additions & 4 deletions eth/tracers/native/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,15 @@ func (t *muxTracer) OnLog(log *types.Log) {
}
}

func (t *muxTracer) CaptureArbitrumStorageGet(key common.Hash, depth int, before bool) {
func (t *muxTracer) CaptureArbitrumStorageGet(addr common.Address, key, mappedKey common.Hash, depth int, before bool) {
for _, t := range t.tracers {
t.CaptureArbitrumStorageGet(key, depth, before)
t.CaptureArbitrumStorageGet(addr, key, mappedKey, depth, before)
}
}

func (t *muxTracer) CaptureArbitrumStorageSet(key, value common.Hash, depth int, before bool) {
func (t *muxTracer) CaptureArbitrumStorageSet(addr common.Address, key, mappedKey, value common.Hash, depth int, before bool) {
for _, t := range t.tracers {
t.CaptureArbitrumStorageSet(key, value, depth, before)
t.CaptureArbitrumStorageSet(addr, key, mappedKey, value, depth, before)
}
}

Expand Down
65 changes: 52 additions & 13 deletions eth/tracers/native/prestate.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,18 @@ func init() {
type stateMap = map[common.Address]*account

type account struct {
Balance *big.Int `json:"balance,omitempty"`
Code []byte `json:"code,omitempty"`
Nonce uint64 `json:"nonce,omitempty"`
Storage map[common.Hash]common.Hash `json:"storage,omitempty"`
empty bool
Balance *big.Int `json:"balance,omitempty"`
Code []byte `json:"code,omitempty"`
Nonce uint64 `json:"nonce,omitempty"`
Storage map[common.Hash]common.Hash `json:"storage,omitempty"`
ganeshvanahalli marked this conversation as resolved.
Show resolved Hide resolved
ArbitrumStorage map[common.Hash]common.Hash `json:"arbitrumStorage,omitempty"`

empty bool
arbStorageKeyMap map[common.Hash]common.Hash
}

func (a *account) exists() bool {
return a.Nonce > 0 || len(a.Code) > 0 || len(a.Storage) > 0 || (a.Balance != nil && a.Balance.Sign() != 0)
return a.Nonce > 0 || len(a.Code) > 0 || len(a.Storage) > 0 || (a.Balance != nil && a.Balance.Sign() != 0) || len(a.ArbitrumStorage) > 0
}

type accountMarshaling struct {
Expand Down Expand Up @@ -119,7 +122,7 @@ func (t *prestateTracer) OnOpcode(pc uint64, opcode byte, gas, cost uint64, scop
switch {
case stackLen >= 1 && (op == vm.SLOAD || op == vm.SSTORE):
slot := common.Hash(stackData[stackLen-1].Bytes32())
t.lookupStorage(caller, slot)
t.lookupStorage(caller, slot, common.Hash{}, false)
case stackLen >= 1 && (op == vm.EXTCODECOPY || op == vm.EXTCODEHASH || op == vm.EXTCODESIZE || op == vm.BALANCE || op == vm.SELFDESTRUCT):
addr := common.Address(stackData[stackLen-1].Bytes20())
t.lookupAccount(addr)
Expand Down Expand Up @@ -212,7 +215,10 @@ func (t *prestateTracer) processDiffState() {
continue
}
modified := false
postAccount := &account{Storage: make(map[common.Hash]common.Hash)}
postAccount := &account{
Storage: make(map[common.Hash]common.Hash),
ArbitrumStorage: make(map[common.Hash]common.Hash),
}
newBalance := t.env.StateDB.GetBalance(addr).ToBig()
newNonce := t.env.StateDB.GetNonce(addr)
newCode := t.env.StateDB.GetCode(addr)
Expand Down Expand Up @@ -248,6 +254,24 @@ func (t *prestateTracer) processDiffState() {
}
}

for key, val := range state.ArbitrumStorage {
// don't include the empty slot
if val == (common.Hash{}) {
delete(t.pre[addr].ArbitrumStorage, key)
}

newVal := t.env.StateDB.GetState(types.ArbosStateAddress, state.arbStorageKeyMap[key])
if val == newVal {
// Omit unchanged slots
delete(t.pre[addr].ArbitrumStorage, key)
} else {
modified = true
if newVal != (common.Hash{}) {
postAccount.ArbitrumStorage[key] = newVal
}
}
}

if modified {
t.post[addr] = postAccount
} else {
Expand All @@ -265,10 +289,12 @@ func (t *prestateTracer) lookupAccount(addr common.Address) {
}

acc := &account{
Balance: t.env.StateDB.GetBalance(addr).ToBig(),
Nonce: t.env.StateDB.GetNonce(addr),
Code: t.env.StateDB.GetCode(addr),
Storage: make(map[common.Hash]common.Hash),
Balance: t.env.StateDB.GetBalance(addr).ToBig(),
Nonce: t.env.StateDB.GetNonce(addr),
Code: t.env.StateDB.GetCode(addr),
Storage: make(map[common.Hash]common.Hash),
ArbitrumStorage: make(map[common.Hash]common.Hash),
arbStorageKeyMap: make(map[common.Hash]common.Hash),
}
if !acc.exists() {
acc.empty = true
Expand All @@ -279,9 +305,22 @@ func (t *prestateTracer) lookupAccount(addr common.Address) {
// lookupStorage fetches the requested storage slot and adds
// it to the prestate of the given contract. It assumes `lookupAccount`
// has been performed on the contract before.
func (t *prestateTracer) lookupStorage(addr common.Address, key common.Hash) {
func (t *prestateTracer) lookupStorage(addr common.Address, key, mappedKey common.Hash, isArbitrumStorage bool) {
ganeshvanahalli marked this conversation as resolved.
Show resolved Hide resolved
if isArbitrumStorage {
if _, ok := t.pre[addr].ArbitrumStorage[key]; ok {
return
}
t.pre[addr].ArbitrumStorage[key] = t.env.StateDB.GetState(types.ArbosStateAddress, mappedKey)
t.pre[addr].arbStorageKeyMap[key] = mappedKey
return
}
if _, ok := t.pre[addr].Storage[key]; ok {
return
}
t.pre[addr].Storage[key] = t.env.StateDB.GetState(addr, key)
}

func (t *prestateTracer) captureArbitrumStorageOps(addr common.Address, key, mappedKey common.Hash) {
t.lookupAccount(addr)
t.lookupStorage(addr, key, mappedKey, true)
}
34 changes: 24 additions & 10 deletions eth/tracers/native/tracer_arbitrum.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,31 @@ func (t *flatCallTracer) CaptureArbitrumTransfer(from, to *common.Address, value
}
}

func (*callTracer) CaptureArbitrumStorageGet(key common.Hash, depth int, before bool) {}
func (*fourByteTracer) CaptureArbitrumStorageGet(key common.Hash, depth int, before bool) {}
func (*noopTracer) CaptureArbitrumStorageGet(key common.Hash, depth int, before bool) {}
func (*prestateTracer) CaptureArbitrumStorageGet(key common.Hash, depth int, before bool) {}
func (*flatCallTracer) CaptureArbitrumStorageGet(key common.Hash, depth int, before bool) {}
func (t *prestateTracer) CaptureArbitrumStorageGet(addr common.Address, key, mappedKey common.Hash, depth int, before bool) {
t.captureArbitrumStorageOps(addr, key, mappedKey)
}

func (t *prestateTracer) CaptureArbitrumStorageSet(addr common.Address, key, mappedKey, value common.Hash, depth int, before bool) {
t.captureArbitrumStorageOps(addr, key, mappedKey)
}

func (*callTracer) CaptureArbitrumStorageGet(addr common.Address, key, mappedKey common.Hash, depth int, before bool) {
}
func (*fourByteTracer) CaptureArbitrumStorageGet(addr common.Address, key, mappedKey common.Hash, depth int, before bool) {
}
func (*noopTracer) CaptureArbitrumStorageGet(addr common.Address, key, mappedKey common.Hash, depth int, before bool) {
}
func (*flatCallTracer) CaptureArbitrumStorageGet(addr common.Address, key, mappedKey common.Hash, depth int, before bool) {
}

func (*callTracer) CaptureArbitrumStorageSet(key, value common.Hash, depth int, before bool) {}
func (*fourByteTracer) CaptureArbitrumStorageSet(key, value common.Hash, depth int, before bool) {}
func (*noopTracer) CaptureArbitrumStorageSet(key, value common.Hash, depth int, before bool) {}
func (*prestateTracer) CaptureArbitrumStorageSet(key, value common.Hash, depth int, before bool) {}
func (*flatCallTracer) CaptureArbitrumStorageSet(key, value common.Hash, depth int, before bool) {}
func (*callTracer) CaptureArbitrumStorageSet(addr common.Address, key, mappedKey, value common.Hash, depth int, before bool) {
}
func (*fourByteTracer) CaptureArbitrumStorageSet(addr common.Address, key, mappedKey, value common.Hash, depth int, before bool) {
}
func (*noopTracer) CaptureArbitrumStorageSet(addr common.Address, key, mappedKey, value common.Hash, depth int, before bool) {
}
func (*flatCallTracer) CaptureArbitrumStorageSet(addr common.Address, key, mappedKey, value common.Hash, depth int, before bool) {
}

func (*callTracer) CaptureStylusHostio(name string, args, outs []byte, startInk, endInk uint64) {}
func (*fourByteTracer) CaptureStylusHostio(name string, args, outs []byte, startInk, endInk uint64) {}
Expand Down
Loading