Skip to content

Commit

Permalink
feat: update prover storage data
Browse files Browse the repository at this point in the history
  • Loading branch information
atanmarko committed Jun 29, 2023
1 parent 74441b2 commit b4d20ca
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 23 deletions.
43 changes: 27 additions & 16 deletions jsonrpc/eth_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,13 @@ func (e *Eth) GetProverData(block BlockNumberOrHash) (interface{}, error) {
return nil, err
}

// 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
}

fullBlock, valid := e.store.GetBlockByHash(header.Hash, true)
if !valid {
return nil, fmt.Errorf("failed to get block by hash %s", header.Hash)
Expand All @@ -743,7 +750,7 @@ func (e *Eth) GetProverData(block BlockNumberOrHash) (interface{}, error) {
return nil, err
}

// Trace the block
// Configure the tracer
tracer, _, err := NewTracer(&TraceConfig{
EnableMemory: true,
EnableReturnData: true,
Expand Down Expand Up @@ -774,8 +781,9 @@ func (e *Eth) GetProverData(block BlockNumberOrHash) (interface{}, error) {
for _, accountStr := range accountsStr {
accountAddress := types.StringToAddress(accountStr)

// Get the full account nonce, balance, state root and code hash
acc, err := e.store.GetAccount(header.StateRoot, accountAddress)
// Get the full account nonce, balance, state root and code hash of the state before this
// block is executed
acc, err := e.store.GetAccount(previousHeader.StateRoot, accountAddress)
if err != nil {
return nil, err
}
Expand All @@ -788,21 +796,23 @@ func (e *Eth) GetProverData(block BlockNumberOrHash) (interface{}, error) {
}
}

// Get storage data for all accounts from this block
// Lear of the storage changes in this block
storages := make([]prover.Storage, 0)

storageChanges, err := prover.ParseTraceForStorageChanges(tracesJSON)
if err != nil {
return nil, err
}

// Get the starting state storage merkle proof for each account and each slot
// that will be changed in this block execution
for account, accountData := range accounts {
if accountData.CodeHash != EmptyCodeHash {
storageUpdates := make([]prover.StorageUpdate, 0)

// Account has code
// Account is smart contract (has code)
for _, storageChange := range storageChanges[account] {
storageMerkleProof, err := e.store.GetStorageProof(header.StateRoot,
storageMerkleProof, err := e.store.GetStorageProof(previousHeader.StateRoot,
types.StringToAddress(account), storageChange.Slot)
if err != nil {
return nil, err
Expand All @@ -828,7 +838,7 @@ func (e *Eth) GetProverData(block BlockNumberOrHash) (interface{}, error) {
}
}

// All transactions in the block
// All transactions in this block
transactions := fullBlock.Transactions

// Receipts from this block
Expand All @@ -842,7 +852,7 @@ func (e *Eth) GetProverData(block BlockNumberOrHash) (interface{}, error) {

for account, accountData := range accounts {
if accountData.CodeHash != EmptyCodeHash {
contractCode, err := e.store.GetCode(header.StateRoot, types.StringToAddress(account))
contractCode, err := e.store.GetCode(previousHeader.StateRoot, types.StringToAddress(account))
if err != nil {
return nil, err
}
Expand All @@ -856,7 +866,7 @@ func (e *Eth) GetProverData(block BlockNumberOrHash) (interface{}, error) {

// Get state Merkle proofs for all accounts
for account := range accounts {
accountProof, err := e.store.GetAccountProof(header.StateRoot, types.StringToAddress(account))
accountProof, err := e.store.GetAccountProof(previousHeader.StateRoot, types.StringToAddress(account))
if err != nil {
return nil, err
}
Expand All @@ -873,12 +883,13 @@ func (e *Eth) GetProverData(block BlockNumberOrHash) (interface{}, error) {
}

return &prover.ProverData{
BlockHeader: *header,
Accounts: accounts,
Storage: storages,
Transactions: transactions,
Receipts: receipts,
ContractCodes: contractCodes,
State: state,
BlockHeader: *header,
PreviosBlockHeader: *previousHeader,

Check failure on line 887 in jsonrpc/eth_endpoint.go

View workflow job for this annotation

GitHub Actions / build

unknown field 'PreviosBlockHeader' in struct literal of type prover.ProverData

Check failure on line 887 in jsonrpc/eth_endpoint.go

View workflow job for this annotation

GitHub Actions / build

unknown field 'PreviosBlockHeader' in struct literal of type prover.ProverData

Check failure on line 887 in jsonrpc/eth_endpoint.go

View workflow job for this annotation

GitHub Actions / Build / Verify Build Reproducibility

unknown field 'PreviosBlockHeader' in struct literal of type prover.ProverData

Check failure on line 887 in jsonrpc/eth_endpoint.go

View workflow job for this annotation

GitHub Actions / Build / Polygon Edge

unknown field 'PreviosBlockHeader' in struct literal of type prover.ProverData
Accounts: accounts,
PreviousStorage: storages,
Transactions: transactions,
Receipts: receipts,
ContractCodes: contractCodes,
PreviousState: state,
}, nil
}
15 changes: 8 additions & 7 deletions prover/prover.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ type ProverAccountProof struct {
}

type ProverData struct {
BlockHeader types.Header
Accounts interface{}
Storage interface{}
Transactions interface{}
Receipts interface{}
ContractCodes interface{}
State interface{}
BlockHeader types.Header
PreviousBlockHeader types.Header
Accounts interface{}
PreviousStorage interface{}
Transactions interface{}
Receipts interface{}
ContractCodes interface{}
PreviousState interface{}
}

func ParseBlockAccounts(block *types.Block) ([]string, error) {
Expand Down

0 comments on commit b4d20ca

Please sign in to comment.