Skip to content

Commit

Permalink
Add actual code in SMT witness (ledgerwatch#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
cffls committed Jan 24, 2024
1 parent 8916d17 commit 107c616
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
13 changes: 13 additions & 0 deletions smt/pkg/db/mdbx.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,19 @@ func (m *EriDb) GetHashKey(key utils.NodeKey) (utils.NodeKey, error) {
return utils.NodeKey{na[0], na[1], na[2], na[3]}, nil
}

func (m *EriDb) GetCode(codeHash []byte) ([]byte, error) {
data, err := m.tx.GetOne(kv.Code, codeHash)
if err != nil {
return nil, err
}

if data == nil {
return nil, fmt.Errorf("code hash %x not found", codeHash)
}

return data, nil
}

func (m *EriDb) Delete(key string) error {
return m.tx.Delete(TableSmt, []byte(key))
}
Expand Down
4 changes: 4 additions & 0 deletions smt/pkg/db/mem-db.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ func (m *MemDb) GetHashKey(key utils.NodeKey) (utils.NodeKey, error) {
return utils.NodeKey{na[0], na[1], na[2], na[3]}, nil
}

func (m *MemDb) GetCode(codeHash []byte) ([]byte, error) {
return nil, fmt.Errorf("not implemented")
}

func (m *MemDb) Delete(key string) error {
m.lock.Lock() // Lock for writing
defer m.lock.Unlock() // Make sure to unlock when done
Expand Down
1 change: 1 addition & 0 deletions smt/pkg/smt/smt.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type DB interface {
InsertHashKey(key utils.NodeKey, value utils.NodeKey) error
GetHashKey(key utils.NodeKey) (utils.NodeKey, error)
Delete(string) error
GetCode(codeHash []byte) ([]byte, error)

SetLastRoot(lr *big.Int) error
GetLastRoot() (*big.Int, error)
Expand Down
24 changes: 16 additions & 8 deletions smt/pkg/smt/witness.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ func BuildWitness(s *SMT, rd trie.RetainDecider, ctx context.Context) (*trie.Wit

action := func(prefix []byte, k utils.NodeKey, v utils.NodeValue12) (bool, error) {
if rd != nil && !rd.Retain(prefix) {
if !v.IsFinalNode() {
h := libcommon.BigToHash(k.ToBigInt())
hNode := trie.OperatorHash{Hash: h}
operands = append(operands, &hNode)
}
h := libcommon.BigToHash(k.ToBigInt())
hNode := trie.OperatorHash{Hash: h}
operands = append(operands, &hNode)
return false, nil
}

Expand All @@ -46,21 +44,31 @@ func BuildWitness(s *SMT, rd trie.RetainDecider, ctx context.Context) (*trie.Wit

valHash := v.Get4to8()

value, err := s.Db.Get(*valHash)
v, err := s.Db.Get(*valHash)

value8 := utils.BigIntArrayFromNodeValue8(value.GetNodeValue8())
vInBytes := utils.ArrayBigToScalar(utils.BigIntArrayFromNodeValue8(v.GetNodeValue8())).Bytes()

if err != nil {
return false, err
}

if t == utils.SC_CODE {
code, err := s.Db.GetCode(vInBytes)

if err != nil {
return false, err
} else {
operands = append(operands, &trie.OperatorCode{Code: code})
}
}

// fmt.Printf("Node hash: %s, Node type: %d, address %x, storage %x, value %x\n", utils.ConvertBigIntToHex(k.ToBigInt()), t, addr, storage, utils.ArrayBigToScalar(value8).Bytes())

operands = append(operands, &trie.OperatorSMTLeafValue{
NodeType: uint8(t),
Address: addr.Bytes(),
StorageKey: storage.Bytes(),
Value: utils.ArrayBigToScalar(value8).Bytes(),
Value: vInBytes,
})
return false, nil
}
Expand Down

0 comments on commit 107c616

Please sign in to comment.