Skip to content

Commit

Permalink
feat: add receipts, accounts and code hash
Browse files Browse the repository at this point in the history
  • Loading branch information
atanmarko authored and Marko Atanasievski committed Jun 16, 2023
1 parent 034ea90 commit b214a86
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 13 deletions.
55 changes: 45 additions & 10 deletions jsonrpc/eth_endpoint.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package jsonrpc

import (
"bytes"
"errors"
"fmt"
"math/big"

"github.com/hashicorp/go-hclog"

"github.com/0xPolygon/polygon-edge/chain"
"github.com/0xPolygon/polygon-edge/crypto"
"github.com/0xPolygon/polygon-edge/helper/common"
"github.com/0xPolygon/polygon-edge/helper/hex"
"github.com/0xPolygon/polygon-edge/helper/progress"
"github.com/0xPolygon/polygon-edge/prover"
"github.com/0xPolygon/polygon-edge/state"
Expand Down Expand Up @@ -97,8 +98,8 @@ type Eth struct {

var (
ErrInsufficientFunds = errors.New("insufficient funds for execution")
EmptyCodeHash = []byte{197, 210, 70, 1, 134, 247, 35, 60, 146, 126, 125, 178, 220, 199, 3, 192, 229,
0, 182, 83, 202, 130, 39, 59, 123, 250, 216, 4, 93, 133, 164, 112}
EmptyCodeHash = hex.EncodeToHex([]byte{197, 210, 70, 1, 134, 247, 35, 60, 146, 126, 125,
178, 220, 199, 3, 192, 229, 0, 182, 83, 202, 130, 39, 59, 123, 250, 216, 4, 93, 133, 164, 112})
)

// ChainId returns the chain id of the client
Expand Down Expand Up @@ -725,6 +726,7 @@ func (e *Eth) Unsubscribe(id string) (bool, error) {
}

func (e *Eth) GetProverData(block BlockNumberOrHash) (interface{}, error) {
// Get block metadata
header, err := GetHeaderFromBlockNumberOrHash(block, e.store)
if err != nil {
return nil, err
Expand Down Expand Up @@ -766,7 +768,7 @@ func (e *Eth) GetProverData(block BlockNumberOrHash) (interface{}, error) {

accountsStr = append(accountsStr, contractAccounts...)

accounts := make(map[string]*Account)
accounts := make(map[string]*prover.ProverAccount)

// Get all the data for the accounts
for _, accountStr := range accountsStr {
Expand All @@ -778,9 +780,15 @@ func (e *Eth) GetProverData(block BlockNumberOrHash) (interface{}, error) {
return nil, err
}

accounts[accountAddress.String()] = acc
accounts[accountAddress.String()] = &prover.ProverAccount{
Nonce: acc.Nonce,
Balance: acc.Balance,
Root: acc.Root.String(),
CodeHash: hex.EncodeToHex(acc.CodeHash),
}
}

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

storageChanges, err := prover.ParseTraceForStorageChanges(tracesJSON)
Expand All @@ -789,9 +797,10 @@ func (e *Eth) GetProverData(block BlockNumberOrHash) (interface{}, error) {
}

for account, accountData := range accounts {
if !bytes.Equal(accountData.CodeHash, EmptyCodeHash) {
if accountData.CodeHash != EmptyCodeHash {
// Account has code
rlpRootData, storageHash, err := e.store.GetContractStorageData(header.StateRoot, types.StringToAddress(account))
rlpRootData, storageHash, err := e.store.GetContractStorageData(header.StateRoot,
types.StringToAddress(account))
if err != nil {
return nil, err
}
Expand All @@ -805,9 +814,35 @@ func (e *Eth) GetProverData(block BlockNumberOrHash) (interface{}, error) {
}
}

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

// Receipts from this block
receipts, err := e.store.GetReceiptsByHash(header.Hash)
if err != nil {
return nil, err
}

// Contract code map CodeHash -> ContractCode
contractCodes := make(map[string]string)
for account, accountData := range accounts {

Check failure on line 828 in jsonrpc/eth_endpoint.go

View workflow job for this annotation

GitHub Actions / golangci_lint

ranges should only be cuddled with assignments used in the iteration (wsl)
if accountData.CodeHash != EmptyCodeHash {
contractCode, err := e.store.GetCode(header.StateRoot, types.StringToAddress(account))
if err != nil {
return nil, err
}

codeHash := crypto.Keccak256(contractCode)
contractCodes[hex.EncodeToHex(codeHash)] = hex.EncodeToHex(contractCode)
}
}

return &prover.ProverData{
BlockHeader: *header,
Accounts: accounts,
Storage: storages,
BlockHeader: *header,
Accounts: accounts,
Storage: storages,
Transactions: transactions,
Receipts: receipts,
ContractCodes: contractCodes,
}, nil
}
17 changes: 14 additions & 3 deletions prover/prover.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package prover

import (
"fmt"
"math/big"

"github.com/0xPolygon/polygon-edge/state/runtime/tracer/structtracer"
"github.com/0xPolygon/polygon-edge/types"
Expand All @@ -15,10 +16,20 @@ type Storage struct {
Storage []structtracer.StorageUpdate
}

type ProverAccount struct {
Balance *big.Int
Nonce uint64
Root string
CodeHash string
}

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

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

0 comments on commit b214a86

Please sign in to comment.