Skip to content

Commit

Permalink
feat: simplifications in SMT state reader
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan-Ethernal committed Oct 28, 2024
1 parent 15834d8 commit c2d0368
Showing 1 changed file with 36 additions and 22 deletions.
58 changes: 36 additions & 22 deletions smt/pkg/smt/smt_state_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,44 @@ package smt
import (
"bytes"
"context"
"errors"
"math/big"

"github.com/holiman/uint256"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon/core/state"
"github.com/ledgerwatch/erigon/core/types/accounts"
"github.com/ledgerwatch/erigon/smt/pkg/utils"
"github.com/ledgerwatch/erigon/zkevm/log"
)

var _ state.StateReader = (*SMT)(nil)

// ReadAccountData reads account data from the SMT
func (s *SMT) ReadAccountData(address libcommon.Address) (*accounts.Account, error) {
account := accounts.Account{}

balance, err := s.GetAccountBalance(address)
if err != nil {
return nil, err
}
account.Balance = *balance

nonce, err := s.GetAccountNonce(address)
if err != nil {
return nil, err
}
account.Nonce = nonce.Uint64()

codeHash, err := s.GetAccountCodeHash(address)
if err != nil {
return nil, err
}
account.CodeHash = codeHash

account.Root = libcommon.Hash{}
account := &accounts.Account{
Balance: *balance,
Nonce: nonce.Uint64(),
CodeHash: codeHash,
Root: libcommon.Hash{},
}

return &account, nil
return account, nil
}

// ReadAccountStorage reads account storage from the SMT (not implemented for SMT)
Expand All @@ -60,59 +64,69 @@ func (s *SMT) ReadAccountCode(address libcommon.Address, incarnation uint64, cod
}

// ReadAccountCodeSize reads account code size from the SMT
func (s *SMT) ReadAccountCodeSize(address libcommon.Address, incarnation uint64, codeHash libcommon.Hash) (int, error) {
func (s *SMT) ReadAccountCodeSize(address libcommon.Address, _ uint64, _ libcommon.Hash) (int, error) {
valueInBytes, err := s.getValue(utils.SC_LENGTH, address, nil)
if err != nil {
return 0, err
}

sizeBig := big.NewInt(0).SetBytes(valueInBytes)

return int(sizeBig.Int64()), nil
if !sizeBig.IsInt64() {
err = errors.New("code size value is too large to fit into an int")
return 0, err
}

sizeInt64 := sizeBig.Int64()
if sizeInt64 > int64(^uint(0)>>1) {
err = errors.New("code size value overflows int")
log.Error("failed to get account code size", "error", err)
return 0, err
}

return int(sizeInt64), nil
}

// ReadAccountIncarnation reads account incarnation from the SMT (not implemented for SMT)
func (s *SMT) ReadAccountIncarnation(address libcommon.Address) (uint64, error) {
return 0, nil
func (s *SMT) ReadAccountIncarnation(_ libcommon.Address) (uint64, error) {
return 0, errors.New("ReadAccountIncarnation not implemented for SMT")
}

// GetAccountBalance returns the balance of an account from the SMT
func (s *SMT) GetAccountBalance(address libcommon.Address) (*uint256.Int, error) {
balance := uint256.NewInt(0)

valueInBytes, err := s.getValue(utils.KEY_BALANCE, address, nil)
if err != nil {
log.Error("error getting balance", "error", err)
log.Error("failed to get balance", "error", err)
return nil, err
}
balance.SetBytes(valueInBytes)

balance := uint256.NewInt(0).SetBytes(valueInBytes)

return balance, nil
}

// GetAccountNonce returns the nonce of an account from the SMT
func (s *SMT) GetAccountNonce(address libcommon.Address) (*uint256.Int, error) {
nonce := uint256.NewInt(0)

valueInBytes, err := s.getValue(utils.KEY_NONCE, address, nil)
if err != nil {
log.Error("error getting nonce", "error", err)
log.Error("failed to get nonce", "error", err)
return nil, err
}
nonce.SetBytes(valueInBytes)

nonce := uint256.NewInt(0).SetBytes(valueInBytes)

return nonce, nil
}

// GetAccountCodeHash returns the code hash of an account from the SMT
func (s *SMT) GetAccountCodeHash(address libcommon.Address) (libcommon.Hash, error) {
codeHash := libcommon.Hash{}

valueInBytes, err := s.getValue(utils.SC_CODE, address, nil)
if err != nil {
log.Error("error getting codehash", "error", err)
log.Error("failed to get code hash", "error", err)
return libcommon.Hash{}, err
}

codeHash := libcommon.Hash{}
codeHash.SetBytes(valueInBytes)

return codeHash, nil
Expand Down

0 comments on commit c2d0368

Please sign in to comment.