-
Notifications
You must be signed in to change notification settings - Fork 107
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
base: master
Are you sure you want to change the base?
Changes from 2 commits
3cbde5f
264587f
2a2149b
27cb298
d8dbd10
32a724a
325b36f
22ec9af
02bbbd0
7470c19
bddfe08
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
ganeshvanahalli marked this conversation as resolved.
Show resolved
Hide resolved
|
||
arbStorageKeyMap map[common.Hash]common.Hash | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prestate is just about finding all the places in storage that chaged. |
||
} | ||
|
||
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 { | ||
|
@@ -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) | ||
|
@@ -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), | ||
} | ||
ganeshvanahalli marked this conversation as resolved.
Show resolved
Hide resolved
|
||
newBalance := t.env.StateDB.GetBalance(addr).ToBig() | ||
newNonce := t.env.StateDB.GetNonce(addr) | ||
newCode := t.env.StateDB.GetCode(addr) | ||
|
@@ -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 { | ||
|
@@ -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 | ||
|
@@ -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) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
addr should always be the arbosStorage address. We can add it here but not really necessary.
We shouldn't need mapped key because that's what "key" should mean. Storage is just key-value pairs and we don't care about the non-mapped key.