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 30, 2023
1 parent 74441b2 commit a129273
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 26 deletions.
75 changes: 57 additions & 18 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 {

Check failure on line 738 in jsonrpc/eth_endpoint.go

View workflow job for this annotation

GitHub Actions / golangci_lint

only one cuddle assignment allowed before if statement (wsl)
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 @@ -814,7 +824,6 @@ func (e *Eth) GetProverData(block BlockNumberOrHash) (interface{}, error) {
}

storageUpdates = append(storageUpdates, prover.StorageUpdate{
Value: storageChange.Value.String(),
Slot: storageChange.Slot.String(),
MerkleProof: ss,
})
Expand All @@ -828,8 +837,12 @@ func (e *Eth) GetProverData(block BlockNumberOrHash) (interface{}, error) {
}
}

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

transactions := make([]string, 0)
for _, transaction := range fullBlock.Transactions {
transactions = append(transactions, hex.EncodeToHex(transaction.MarshalRLP()))
}

// Receipts from this block
receipts, err := e.store.GetReceiptsByHash(header.Hash)
Expand All @@ -842,7 +855,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 @@ -854,9 +867,28 @@ func (e *Eth) GetProverData(block BlockNumberOrHash) (interface{}, error) {

state := make([]prover.ProverAccountProof, 0)

// Manually add zero account (beneficiary account)
zeroAccount := "0x0000000000000000000000000000000000000000"
zeroAccountData, err := e.store.GetAccount(previousHeader.StateRoot, types.StringToAddress(zeroAccount))
if err != nil {

Check failure on line 873 in jsonrpc/eth_endpoint.go

View workflow job for this annotation

GitHub Actions / golangci_lint

only one cuddle assignment allowed before if statement (wsl)
accounts[zeroAccount] = &prover.ProverAccount{
Nonce: 0,
Balance: big.NewInt(0),
Root: types.Hash{}.String(),
CodeHash: types.Hash{}.String(),
}
} else {
accounts[zeroAccount] = &prover.ProverAccount{
Nonce: zeroAccountData.Nonce,
Balance: zeroAccountData.Balance,
Root: zeroAccountData.Root.String(),
CodeHash: hex.EncodeToHex(zeroAccountData.CodeHash),
}
}

// 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 @@ -872,13 +904,20 @@ func (e *Eth) GetProverData(block BlockNumberOrHash) (interface{}, error) {
})
}

chainId, err := e.ChainId()

Check failure on line 907 in jsonrpc/eth_endpoint.go

View workflow job for this annotation

GitHub Actions / golangci_lint

ST1003: var chainId should be chainID (stylecheck)
if err != nil {
return nil, err
}

return &prover.ProverData{
BlockHeader: *header,
Accounts: accounts,
Storage: storages,
Transactions: transactions,
Receipts: receipts,
ContractCodes: contractCodes,
State: state,
ChainId: chainId,
BlockHeader: *header,
PreviousBlockHeader: *previousHeader,
Accounts: accounts,
PreviousStorage: storages,
Transactions: transactions,
Receipts: receipts,
ContractCodes: contractCodes,
PreviousState: state,
}, nil
}
17 changes: 9 additions & 8 deletions prover/prover.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

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

Expand All @@ -34,13 +33,15 @@ type ProverAccountProof struct {
}

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

Check failure on line 36 in prover/prover.go

View workflow job for this annotation

GitHub Actions / golangci_lint

ST1003: struct field ChainId should be ChainID (stylecheck)
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 a129273

Please sign in to comment.