Skip to content

Commit

Permalink
feat: Decode witness to SMT
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan-Ethernal committed Oct 25, 2024
1 parent 2723db7 commit 69ad2f0
Show file tree
Hide file tree
Showing 6 changed files with 581 additions and 13 deletions.
14 changes: 14 additions & 0 deletions smt/pkg/db/mdbx.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package db

import (
"context"
"encoding/hex"
"math/big"

"fmt"
Expand Down Expand Up @@ -304,6 +305,19 @@ func (m *EriRoDb) GetCode(codeHash []byte) ([]byte, error) {
return data, nil
}

func (m *EriDb) AddCode(code []byte) error {
codeHash := utils.HashContractBytecode(hex.EncodeToString(code))

codeHashBytes, err := hex.DecodeString(strings.TrimPrefix(codeHash, "0x"))
if err != nil {
return err
}

codeHashBytes = utils.ResizeHashTo32BytesByPrefixingWithZeroes(codeHashBytes)

return m.tx.Put(kv.Code, codeHashBytes, code)
}

func (m *EriRoDb) PrintDb() {
err := m.kvTxRo.ForEach(TableSmt, []byte{}, func(k, v []byte) error {
println(string(k), string(v))
Expand Down
45 changes: 35 additions & 10 deletions smt/pkg/smt/entity_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,55 @@ import (
"github.com/ledgerwatch/erigon/smt/pkg/utils"
)

// SetAccountState sets the balance and nonce of an account
func (s *SMT) SetAccountState(ethAddr string, balance, nonce *big.Int) (*big.Int, error) {
_, err := s.SetAccountBalance(ethAddr, balance)
if err != nil {
return nil, err
}

auxOut, err := s.SetAccountNonce(ethAddr, nonce)
if err != nil {
return nil, err
}

return auxOut, nil
}

// SetAccountBalance sets the balance of an account
func (s *SMT) SetAccountBalance(ethAddr string, balance *big.Int) (*big.Int, error) {
keyBalance := utils.KeyEthAddrBalance(ethAddr)
keyNonce := utils.KeyEthAddrNonce(ethAddr)

if _, err := s.InsertKA(keyBalance, balance); err != nil {
auxRes, err := s.InsertKA(keyBalance, balance)
if err != nil {
return nil, err
}

ks := utils.EncodeKeySource(utils.KEY_BALANCE, utils.ConvertHexToAddress(ethAddr), common.Hash{})
if err := s.Db.InsertKeySource(keyBalance, ks); err != nil {
err = s.Db.InsertKeySource(keyBalance, ks)
if err != nil {
return nil, err
}

return auxRes.NewRootScalar.ToBigInt(), err
}

// SetAccountNonce sets the nonce of an account
func (s *SMT) SetAccountNonce(ethAddr string, nonce *big.Int) (*big.Int, error) {
keyNonce := utils.KeyEthAddrNonce(ethAddr)

auxRes, err := s.InsertKA(keyNonce, nonce)
if err != nil {
return nil, err
}

ks = utils.EncodeKeySource(utils.KEY_NONCE, utils.ConvertHexToAddress(ethAddr), common.Hash{})
if err := s.Db.InsertKeySource(keyNonce, ks); err != nil {
ks := utils.EncodeKeySource(utils.KEY_NONCE, utils.ConvertHexToAddress(ethAddr), common.Hash{})
err = s.Db.InsertKeySource(keyNonce, ks)
if err != nil {
return nil, err
}

return auxRes.NewRootScalar.ToBigInt(), nil
return auxRes.NewRootScalar.ToBigInt(), err
}

func (s *SMT) SetAccountStorage(addr libcommon.Address, acc *accounts.Account) error {
Expand Down Expand Up @@ -304,7 +329,7 @@ func (s *SMT) SetStorage(ctx context.Context, logPrefix string, accChanges map[l

for k, v := range storage {
keyStoragePosition := utils.KeyContractStorage(ethAddrBigIngArray, k)
valueBigInt := convertStrintToBigInt(v)
valueBigInt := convertStringToBigInt(v)
keysBatchStorage = append(keysBatchStorage, &keyStoragePosition)
if valuesBatchStorage, isDelete, err = appendToValuesBatchStorageBigInt(valuesBatchStorage, valueBigInt); err != nil {
return nil, nil, err
Expand Down Expand Up @@ -341,7 +366,7 @@ func (s *SMT) DeleteKeySource(nodeKey *utils.NodeKey) error {
}

func calcHashVal(v string) (*utils.NodeValue8, [4]uint64, error) {
val := convertStrintToBigInt(v)
val := convertStringToBigInt(v)

x := utils.ScalarToArrayBig(val)
value, err := utils.NodeValue8FromBigIntArray(x)
Expand All @@ -354,10 +379,10 @@ func calcHashVal(v string) (*utils.NodeValue8, [4]uint64, error) {
return value, h, nil
}

func convertStrintToBigInt(v string) *big.Int {
func convertStringToBigInt(v string) *big.Int {
base := 10
if strings.HasPrefix(v, "0x") {
v = v[2:]
v = strings.TrimPrefix(v, "0x")
base = 16
}

Expand Down
94 changes: 92 additions & 2 deletions smt/pkg/smt/smt.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type DB interface {
InsertKeySource(key utils.NodeKey, value []byte) error
DeleteKeySource(key utils.NodeKey) error
InsertHashKey(key utils.NodeKey, value utils.NodeKey) error
AddCode(code []byte) error
DeleteHashKey(key utils.NodeKey) error
Delete(string) error
DeleteByNodeKey(key utils.NodeKey) error
Expand Down Expand Up @@ -297,7 +298,9 @@ func (s *SMT) insert(k utils.NodeKey, v utils.NodeValue8, newValH [4]uint64, old
if err != nil {
return nil, err
}
s.Db.InsertHashKey(newLeafHash, k)
if err := s.Db.InsertHashKey(newLeafHash, k); err != nil {
return nil, err
}
if level >= 0 {
for j := 0; j < 4; j++ {
siblings[level][keys[level]*4+j] = new(big.Int).SetUint64(newLeafHash[j])
Expand Down Expand Up @@ -649,7 +652,7 @@ func (s *SMT) updateDepth(newDepth int) {

newDepthAsByte := byte(newDepth & 0xFF)
if oldDepth < newDepthAsByte {
s.Db.SetDepth(newDepthAsByte)
_ = s.Db.SetDepth(newDepthAsByte)
}
}

Expand Down Expand Up @@ -728,3 +731,90 @@ func (s *RoSMT) traverseAndMark(ctx context.Context, node *big.Int, visited Visi
return true, nil
})
}

// InsertHashNode inserts a hash node into the SMT. The SMT should not contain any other leaf nodes with the same path prefix. Otherwise, the new root hash will be incorrect.
// TODO: Support insertion of hash nodes even if there are leaf nodes with the same path prefix in SMT.
func (s *SMT) InsertHashNode(path []int, hash *big.Int) (*big.Int, error) {
s.clearUpMutex.Lock()
defer s.clearUpMutex.Unlock()

or, err := s.getLastRoot()
if err != nil {
return nil, err
}

h := utils.ScalarToArray(hash)

var nodeHash [4]uint64
copy(nodeHash[:], h[:4])

lastRoot, err := s.insertHashNode(path, nodeHash, or)
if err != nil {
return nil, err
}

if err = s.setLastRoot(lastRoot); err != nil {
return nil, err
}

return lastRoot.ToBigInt(), nil
}

func (s *SMT) insertHashNode(path []int, hash [4]uint64, root utils.NodeKey) (utils.NodeKey, error) {
if len(path) == 0 {
newValHBig := utils.ArrayToScalar(hash[:])
v := utils.ScalarToNodeValue8(newValHBig)

err := s.hashSave(v.ToUintArray(), utils.LeafCapacity, hash)
if err != nil {
return utils.NodeKey{}, err
}

return hash, nil
}

rootVal := utils.NodeValue12{}

if !root.IsZero() {
v, err := s.Db.Get(root)
if err != nil {
return utils.NodeKey{}, err
}

rootVal = v
}

childIndex := path[0]

childOldRoot := rootVal[childIndex*4 : childIndex*4+4]

childNewRoot, err := s.insertHashNode(path[1:], hash, utils.NodeKeyFromBigIntArray(childOldRoot))

if err != nil {
return utils.NodeKey{}, err
}

var newIn [8]uint64

emptyRootVal := utils.NodeValue12{}

if childIndex == 0 {
var sibling [4]uint64
if rootVal == emptyRootVal {
sibling = [4]uint64{0, 0, 0, 0}
} else {
sibling = *rootVal.Get4to8()
}
newIn = utils.ConcatArrays4(childNewRoot, sibling)
} else {
var sibling [4]uint64
if rootVal == emptyRootVal {
sibling = [4]uint64{0, 0, 0, 0}
} else {
sibling = *rootVal.Get0to4()
}
newIn = utils.ConcatArrays4(sibling, childNewRoot)
}

return s.hashcalcAndSave(newIn, utils.BranchCapacity)
}
Loading

0 comments on commit 69ad2f0

Please sign in to comment.