Skip to content

Commit

Permalink
fix: storage access
Browse files Browse the repository at this point in the history
  • Loading branch information
atanmarko committed Jul 3, 2023
1 parent a129273 commit 4ac0a4b
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 77 deletions.
10 changes: 6 additions & 4 deletions jsonrpc/eth_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,7 @@ func (e *Eth) GetProverData(block BlockNumberOrHash) (interface{}, error) {

// Get the previous block header
var previousBlockNumber = BlockNumber(header.Number) - 1

previousHeader, err := GetHeaderFromBlockNumberOrHash(BlockNumberOrHash{BlockNumber: &previousBlockNumber}, e.store)
if err != nil {
return nil, err
Expand Down Expand Up @@ -799,7 +800,7 @@ func (e *Eth) GetProverData(block BlockNumberOrHash) (interface{}, error) {
// Lear of the storage changes in this block
storages := make([]prover.Storage, 0)

storageChanges, err := prover.ParseTraceForStorageChanges(tracesJSON)
storageChanges, err := prover.ParseTraceForStorageAccess(tracesJSON)
if err != nil {
return nil, err
}
Expand All @@ -808,7 +809,7 @@ func (e *Eth) GetProverData(block BlockNumberOrHash) (interface{}, error) {
// that will be changed in this block execution
for account, accountData := range accounts {
if accountData.CodeHash != EmptyCodeHash {
storageUpdates := make([]prover.StorageUpdate, 0)
storageAccesses := make([]prover.StorageAccess, 0)

// Account is smart contract (has code)
for _, storageChange := range storageChanges[account] {
Expand All @@ -823,7 +824,7 @@ func (e *Eth) GetProverData(block BlockNumberOrHash) (interface{}, error) {
ss[i] = hex.EncodeToHex(s)
}

storageUpdates = append(storageUpdates, prover.StorageUpdate{
storageAccesses = append(storageAccesses, prover.StorageAccess{
Slot: storageChange.Slot.String(),
MerkleProof: ss,
})
Expand All @@ -832,7 +833,7 @@ func (e *Eth) GetProverData(block BlockNumberOrHash) (interface{}, error) {
storages = append(storages, prover.Storage{
Account: account,
StorageRoot: accountData.Root,
Storage: storageUpdates,
Storage: storageAccesses,
})
}
}
Expand Down Expand Up @@ -869,6 +870,7 @@ func (e *Eth) GetProverData(block BlockNumberOrHash) (interface{}, error) {

// Manually add zero account (beneficiary account)
zeroAccount := "0x0000000000000000000000000000000000000000"

zeroAccountData, err := e.store.GetAccount(previousHeader.StateRoot, types.StringToAddress(zeroAccount))
if err != nil {
accounts[zeroAccount] = &prover.ProverAccount{
Expand Down
12 changes: 7 additions & 5 deletions prover/prover.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import (
"github.com/0xPolygon/polygon-edge/types"
)

type StorageUpdate struct {
type StorageAccess struct {
Slot string
MerkleProof []string
}

type Storage struct {
Account string
StorageRoot string
Storage []StorageUpdate
Storage []StorageAccess
}

type ProverAccount struct {
Expand Down Expand Up @@ -78,8 +78,8 @@ func ParseContractCodeForAccounts(tracesJSON []interface{}) ([]string, error) {
return result, nil
}

func ParseTraceForStorageChanges(tracesJSON []interface{}) (map[string][]structtracer.StorageUpdate, error) {
var storageChanges = make(map[string][]structtracer.StorageUpdate)
func ParseTraceForStorageAccess(tracesJSON []interface{}) (map[string][]structtracer.StorageAccess, error) {
var storageChanges = make(map[string][]structtracer.StorageAccess)

for _, traceJSON := range tracesJSON {
trace, ok := traceJSON.(*structtracer.StructTraceResult)
Expand All @@ -88,7 +88,9 @@ func ParseTraceForStorageChanges(tracesJSON []interface{}) (map[string][]structt
}

for account, storage := range trace.StorageUpdates {
storageChanges[account.String()] = append(storageChanges[account.String()], storage...)
for storageAccess := range storage {
storageChanges[account.String()] = append(storageChanges[account.String()], storageAccess)
}
}
}

Expand Down
51 changes: 25 additions & 26 deletions state/runtime/tracer/structtracer/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,8 @@ func (l *StructLog) ErrorString() string {
return ""
}

type StorageUpdate struct {
Slot types.Hash `json:"slot"`
Value types.Hash `json:"value"`
type StorageAccess struct {
Slot types.Hash `json:"slot"`
}

type StructTracer struct {
Expand All @@ -65,22 +64,24 @@ type StructTracer struct {
storage []map[types.Address]map[types.Hash]types.Hash
currentMemory []([]byte)
currentStack []([]*big.Int)
storageUpdates [][]StorageUpdate
accountStorageUpdates map[types.Address][]StorageUpdate
storageAccess []map[StorageAccess]bool
accountStorageUpdates map[types.Address]map[StorageAccess]bool
}

func NewStructTracer(config Config) *StructTracer {
storage := make([](map[types.Address]map[types.Hash]types.Hash), 1)
storage[0] = make(map[types.Address]map[types.Hash]types.Hash)
storageAccess := make([]map[StorageAccess]bool, 1)
storageAccess[0] = make(map[StorageAccess]bool)

return &StructTracer{
Config: config,
cancelLock: sync.RWMutex{},
storage: storage,
currentMemory: make([]([]byte), 1),
currentStack: make([]([]*big.Int), 1),
storageUpdates: make([][]StorageUpdate, 1),
accountStorageUpdates: make(map[types.Address][]StorageUpdate),
storageAccess: storageAccess,
accountStorageUpdates: make(map[types.Address]map[StorageAccess]bool),
}
}

Expand Down Expand Up @@ -224,7 +225,7 @@ func (t *StructTracer) captureStorage(
) {
if opCode == evm.CALL || opCode == evm.STATICCALL {
t.storage = append(t.storage, make(map[types.Address]map[types.Hash]types.Hash))
t.storageUpdates = append(t.storageUpdates, make([]StorageUpdate, 0))
t.storageAccess = append(t.storageAccess, make(map[StorageAccess]bool))
}

if !t.Config.EnableStorage || (opCode != evm.SLOAD && opCode != evm.SSTORE) {
Expand All @@ -247,6 +248,10 @@ func (t *StructTracer) captureStorage(
slot := types.BytesToHash(stack[sp-1].Bytes())
value := host.GetStorage(contractAddress, slot)

t.storageAccess[len(t.storageAccess)-1][StorageAccess{
Slot: slot,
}] = true

(*storage)[contractAddress][slot] = value

case evm.SSTORE:
Expand All @@ -263,10 +268,9 @@ func (t *StructTracer) captureStorage(

(*storage)[contractAddress][slot] = value

t.storageUpdates[len(t.storageUpdates)-1] = append(t.storageUpdates[len(t.storageUpdates)-1], StorageUpdate{
Slot: slot,
Value: value,
})
t.storageAccess[len(t.storageAccess)-1][StorageAccess{
Slot: slot,
}] = true
}
}

Expand Down Expand Up @@ -320,15 +324,10 @@ func (t *StructTracer) ExecuteState(
if opCode == evm.OpCode(evm.CALL).String() || opCode == evm.OpCode(evm.STATICCALL).String() {
contract := types.BytesToAddress(stack[len(stack)-2].Bytes())

if t.accountStorageUpdates[contract] != nil {
t.accountStorageUpdates[contract] = append(t.accountStorageUpdates[contract],
t.storageUpdates[len(t.storageUpdates)-1]...)
} else {
t.accountStorageUpdates[contract] = t.storageUpdates[len(t.storageUpdates)-1]
}
t.accountStorageUpdates[contract] = t.storageAccess[len(t.storageAccess)-1]

t.storage = t.storage[:len(t.storage)-1]
t.storageUpdates = t.storageUpdates[:len(t.storageUpdates)-1]
t.storageAccess = t.storageAccess[:len(t.storageAccess)-1]
}

contractStorage, ok := t.storage[len(t.storage)-1][contractAddress]
Expand Down Expand Up @@ -364,12 +363,12 @@ func (t *StructTracer) ExecuteState(
}

type StructTraceResult struct {
Account string `json:"account"`
Failed bool `json:"failed"`
StorageUpdates map[types.Address][]StorageUpdate `json:"storageUpdates"`
Gas uint64 `json:"gas"`
ReturnValue string `json:"returnValue"`
StructLogs []StructLogRes `json:"structLogs"`
Account string `json:"account"`
Failed bool `json:"failed"`
StorageUpdates map[types.Address]map[StorageAccess]bool `json:"storageUpdates"`
Gas uint64 `json:"gas"`
ReturnValue string `json:"returnValue"`
StructLogs []StructLogRes `json:"structLogs"`
}

type StructLogRes struct {
Expand Down Expand Up @@ -398,7 +397,7 @@ func (t *StructTracer) GetResult() (interface{}, error) {
returnValue = fmt.Sprintf("%x", t.output)
}

t.accountStorageUpdates[t.contractAddress] = t.storageUpdates[len(t.storageUpdates)-1]
t.accountStorageUpdates[t.contractAddress] = t.storageAccess[len(t.storageAccess)-1]
storageUpdates := t.accountStorageUpdates

return &StructTraceResult{
Expand Down
Loading

0 comments on commit 4ac0a4b

Please sign in to comment.