Skip to content

Commit

Permalink
[dev] update SMT library
Browse files Browse the repository at this point in the history
  • Loading branch information
roysc committed Apr 20, 2022
1 parent e103a63 commit 1a37cc2
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 233 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,6 @@ replace github.com/gin-gonic/gin => github.com/gin-gonic/gin v1.7.0
replace github.com/cosmos/cosmos-sdk/db => ./db

retract v0.43.0

// DEV
replace github.com/lazyledger/smt => ../smt
2 changes: 1 addition & 1 deletion store/v2alpha1/multi/proof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func keyPath(prefix, key string) string {
func TestVerifySMTStoreProof(t *testing.T) {
// Create main tree for testing.
txn := memdb.NewDB().ReadWriter()
store := smt.NewStore(txn)
store := smt.NewStore(smt.StoreParams{TreeData: txn})
store.Set([]byte("MYKEY"), []byte("MYVALUE"))
root := store.Root()

Expand Down
47 changes: 24 additions & 23 deletions store/v2alpha1/multi/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,23 +503,27 @@ func (rs *Store) getSubstore(key string) (*substore, error) {
pfx := substorePrefix(key)
stateRW := prefixdb.NewPrefixReadWriter(rs.stateTxn, pfx)
stateCommitmentRW := prefixdb.NewPrefixReadWriter(rs.stateCommitmentTxn, pfx)
var stateCommitmentStore *smt.Store

var stateCommitmentStore *smt.Store
data := prefixdb.NewPrefixReadWriter(stateRW, dataPrefix)
rootHash, err := stateRW.Get(substoreMerkleRootKey)
if err != nil {
return nil, err
}
if rootHash != nil {
stateCommitmentStore = loadSMT(stateCommitmentRW, rootHash)
stateCommitmentStore = loadSMT(stateCommitmentRW, data, rootHash)
} else {
smtdb := prefixdb.NewPrefixReadWriter(stateCommitmentRW, smtPrefix)
stateCommitmentStore = smt.NewStore(smtdb)
params := smt.StoreParams{
TreeData: prefixdb.NewPrefixReadWriter(stateCommitmentRW, smtPrefix),
ValueData: data,
}
stateCommitmentStore = smt.NewStore(params)
}

return &substore{
root: rs,
name: key,
dataBucket: prefixdb.NewPrefixReadWriter(stateRW, dataPrefix),
dataBucket: data,
stateCommitmentStore: stateCommitmentStore,
}, nil
}
Expand All @@ -530,7 +534,7 @@ func (s *substore) refresh(rootHash []byte) {
stateRW := prefixdb.NewPrefixReadWriter(s.root.stateTxn, pfx)
stateCommitmentRW := prefixdb.NewPrefixReadWriter(s.root.stateCommitmentTxn, pfx)
s.dataBucket = prefixdb.NewPrefixReadWriter(stateRW, dataPrefix)
s.stateCommitmentStore = loadSMT(stateCommitmentRW, rootHash)
s.stateCommitmentStore = loadSMT(stateCommitmentRW, s.dataBucket, rootHash)
}

// Commit implements Committer.
Expand Down Expand Up @@ -582,26 +586,20 @@ func pruneVersions(current int64, opts types.PruningOptions, prune func(int64))
}
}

func (s *Store) getMerkleRoots() (ret map[string][]byte, err error) {
ret = map[string][]byte{}
// Calculates root hashes and commits to DB. Does not verify target version or perform pruning.
func (s *Store) commit(target uint64) (id *types.CommitID, err error) {
storeHashes := make(map[string][]byte, len(s.schema))
for key := range s.schema {
sub, has := s.substoreCache[key.Name()]
if !has {
sub, err = s.getSubstore(key.Name())
if err != nil {
if sub, err = s.getSubstore(key.Name()); err != nil {
return
}
}
ret[key.Name()] = sub.stateCommitmentStore.Root()
}
return
}

// Calculates root hashes and commits to DB. Does not verify target version or perform pruning.
func (s *Store) commit(target uint64) (id *types.CommitID, err error) {
storeHashes, err := s.getMerkleRoots()
if err != nil {
return
if err = sub.stateCommitmentStore.Commit(); err != nil {
return
}
storeHashes[key.Name()] = sub.stateCommitmentStore.Root()
}
// Update substore Merkle roots
for key, storeHash := range storeHashes {
Expand Down Expand Up @@ -831,9 +829,12 @@ func (rs *Store) Query(req abci.RequestQuery) (res abci.ResponseQuery) {
return res
}

func loadSMT(stateCommitmentTxn dbm.DBReadWriter, root []byte) *smt.Store {
smtdb := prefixdb.NewPrefixReadWriter(stateCommitmentTxn, smtPrefix)
return smt.LoadStore(smtdb, root)
func loadSMT(sc, data dbm.DBReadWriter, root []byte) *smt.Store {
params := smt.StoreParams{
TreeData: prefixdb.NewPrefixReadWriter(sc, smtPrefix),
ValueData: data,
}
return smt.LoadStore(params, root)
}

// Returns closest index and whether it's a match
Expand Down
10 changes: 6 additions & 4 deletions store/v2alpha1/multi/view_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,13 @@ func (vs *viewStore) getSubstore(key string) (*viewSubstore, error) {
if err != nil {
return nil, err
}
data := prefixdb.NewPrefixReader(stateR, dataPrefix)
return &viewSubstore{
root: vs,
name: key,
dataBucket: prefixdb.NewPrefixReader(stateR, dataPrefix),
stateCommitmentStore: loadSMT(dbm.ReaderAsReadWriter(stateCommitmentR), rootHash),
root: vs,
name: key,
dataBucket: data,
stateCommitmentStore: loadSMT(
dbm.ReaderAsReadWriter(stateCommitmentR), dbm.ReaderAsReadWriter(data), rootHash),
}, nil
}

Expand Down
8 changes: 4 additions & 4 deletions store/v2alpha1/smt/ics23.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ import (
func createIcs23Proof(store *Store, key []byte) (*ics23.CommitmentProof, error) {
ret := &ics23.CommitmentProof{}
path := sha256.Sum256(key)
has, err := store.tree.Has(key)
valueHash, err := store.tree.Get(key)
if err != nil {
return nil, err
}
if has { // Membership proof
value, err := store.values.Get(path[:])
if valueHash != nil { // Membership proof
value, err := store.values.Get(key)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -52,7 +52,7 @@ func toNonExistenceProof(store *Store, path [32]byte) (*ics23.NonExistenceProof,
getNext := func(it dbm.Iterator) (*ics23.ExistenceProof, error) {
defer it.Close()
if it.Next() {
value, err := store.values.Get(it.Key())
value, err := store.values.Get(it.Value())
if err != nil {
return nil, err
}
Expand Down
5 changes: 3 additions & 2 deletions store/v2alpha1/smt/ics23_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

func TestProofICS23(t *testing.T) {
txn := memdb.NewDB().ReadWriter()
s := store.NewStore(txn)
s := store.NewStore(store.StoreParams{TreeData: txn})
// pick keys whose hashes begin with different bits
key00 := []byte("foo") // 00101100 = sha256(foo)[0]
key01 := []byte("bill") // 01100010
Expand Down Expand Up @@ -74,7 +74,8 @@ func TestProofICS23(t *testing.T) {

// Make sure proofs work with a loaded store
root := s.Root()
s = store.LoadStore(txn, root)
assert.NoError(t, s.Commit())
s = store.LoadStore(store.StoreParams{TreeData: txn}, root)
proof, err = s.GetProofICS23(key10)
assert.NoError(t, err)
nonexist = proof.GetNonexist()
Expand Down
93 changes: 0 additions & 93 deletions store/v2alpha1/smt/proof.go

This file was deleted.

69 changes: 0 additions & 69 deletions store/v2alpha1/smt/proof_test.go

This file was deleted.

Loading

0 comments on commit 1a37cc2

Please sign in to comment.