-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5431a69
commit 81cd74d
Showing
18 changed files
with
814 additions
and
310 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package branch | ||
|
||
import ( | ||
"io" | ||
"slices" | ||
|
||
"golang.org/x/exp/maps" | ||
|
||
"cosmossdk.io/store/v2" | ||
) | ||
|
||
var ( | ||
_ store.KVStore = (*Store)(nil) | ||
_ store.BranchedKVStore = (*Store)(nil) | ||
) | ||
|
||
// Store implements both a KVStore and BranchedKVStore interfaces. It is used to | ||
// accumulate writes that can be later committed to backing SS and SC engines or | ||
// discarded altogether. If a read is not found through an uncommitted write, it | ||
// will be delegated to the SS backend. | ||
type Store struct { | ||
// storage reflects backing storage (SS) for reads that are not found in uncommitted volatile state | ||
storage store.VersionedDatabase | ||
|
||
// storeKey reflects the store key used for the store | ||
storeKey string | ||
|
||
// parent reflects a parent store if branched (it may be nil) | ||
// parent store.KVStore | ||
|
||
// changeSet reflects the uncommitted writes to the store | ||
// | ||
// Note, this field might be removed depending on how the branching fields | ||
// below are defined and used. | ||
changeSet map[string]store.KVPair | ||
|
||
// TODO: Fields for branching functionality. These fields should most likely | ||
// reflect what currently exists in cachekv.Store. | ||
} | ||
|
||
func New(storeKey string, ss store.VersionedDatabase) store.KVStore { | ||
return &Store{ | ||
storage: ss, | ||
storeKey: storeKey, | ||
changeSet: make(map[string]store.KVPair), | ||
} | ||
} | ||
|
||
func (s *Store) GetStoreType() store.StoreType { | ||
return store.StoreTypeBranch | ||
} | ||
|
||
// GetChangeSet returns the uncommitted writes to the store, ordered by key. | ||
func (s *Store) GetChangeSet() *store.ChangeSet { | ||
keys := maps.Keys(s.changeSet) | ||
slices.Sort(keys) | ||
|
||
pairs := make([]store.KVPair, len(keys)) | ||
for i, key := range keys { | ||
pairs[i] = s.changeSet[key] | ||
} | ||
|
||
return store.NewChangeSet(pairs...) | ||
} | ||
|
||
func (s *Store) Reset() { | ||
clear(s.changeSet) | ||
} | ||
|
||
func (s *Store) Branch() store.BranchedKVStore { | ||
panic("not implemented!") | ||
} | ||
|
||
func (s *Store) BranchWithTrace(w io.Writer, tc store.TraceContext) store.BranchedKVStore { | ||
panic("not implemented!") | ||
} | ||
|
||
func (s *Store) Iterator(start, end []byte) store.Iterator { | ||
panic("not implemented!") | ||
} | ||
|
||
func (s *Store) ReverseIterator(start, end []byte) store.Iterator { | ||
panic("not implemented!") | ||
} | ||
|
||
func (s *Store) Get(key []byte) []byte { | ||
panic("not implemented!") | ||
} | ||
|
||
func (s *Store) Has(key []byte) bool { | ||
panic("not implemented!") | ||
} | ||
|
||
func (s *Store) Set(key, value []byte) { | ||
panic("not implemented!") | ||
} | ||
|
||
func (s *Store) Delete(key []byte) { | ||
panic("not implemented!") | ||
} | ||
|
||
func (s *Store) Write() { | ||
panic("not implemented!") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package store | ||
|
||
// KVPair defines a key-value pair with additional metadata that is used to | ||
// track writes. Deletion can be denoted by a nil value or explicitly by the | ||
// Delete field. | ||
type KVPair struct { | ||
Key []byte | ||
Value []byte | ||
StoreKey string // optional | ||
} | ||
|
||
// ChangeSet defines a set of KVPair entries. | ||
type ChangeSet struct { | ||
Pairs []KVPair | ||
} | ||
|
||
func NewChangeSet(pairs ...KVPair) *ChangeSet { | ||
return &ChangeSet{ | ||
Pairs: pairs, | ||
} | ||
} | ||
|
||
// Size returns the number of key-value pairs in the batch. | ||
func (cs *ChangeSet) Size() int { | ||
return len(cs.Pairs) | ||
} | ||
|
||
// Add adds a key-value pair to the ChangeSet. | ||
func (cs *ChangeSet) Add(key, value []byte) { | ||
cs.Pairs = append(cs.Pairs, KVPair{ | ||
Key: key, | ||
Value: value, | ||
}) | ||
} | ||
|
||
// AddKVPair adds a KVPair to the ChangeSet. | ||
func (cs *ChangeSet) AddKVPair(pair KVPair) { | ||
cs.Pairs = append(cs.Pairs, pair) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package store | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"cosmossdk.io/store/v2/internal/maps" | ||
) | ||
|
||
type ( | ||
// CommitHeader defines the interface for a block header that can be provided | ||
// to a MultiStore upon Commit. This should be optional and used to facilitate | ||
// time-based queries only. | ||
CommitHeader interface { | ||
GetTime() time.Time | ||
GetHeight() uint64 | ||
} | ||
|
||
// CommitInfo defines commit information used by the multi-store when committing | ||
// a version/height. | ||
CommitInfo struct { | ||
Version uint64 | ||
StoreInfos []StoreInfo | ||
Timestamp time.Time | ||
} | ||
|
||
// StoreInfo defines store-specific commit information. It contains a reference | ||
// between a store name/key and the commit ID. | ||
StoreInfo struct { | ||
Name string | ||
CommitID CommitID | ||
} | ||
|
||
// CommitID defines the commitment information when a specific store is | ||
// committed. | ||
CommitID struct { | ||
Version uint64 | ||
Hash []byte | ||
} | ||
) | ||
|
||
func (si StoreInfo) GetHash() []byte { | ||
return si.CommitID.Hash | ||
} | ||
|
||
// Hash returns the root hash of all committed stores represented by CommitInfo, | ||
// sorted by store name/key. | ||
func (ci CommitInfo) Hash() []byte { | ||
if len(ci.StoreInfos) == 0 { | ||
return nil | ||
} | ||
|
||
rootHash, _, _ := maps.ProofsFromMap(ci.toMap()) | ||
return rootHash | ||
} | ||
|
||
func (ci CommitInfo) toMap() map[string][]byte { | ||
m := make(map[string][]byte, len(ci.StoreInfos)) | ||
for _, storeInfo := range ci.StoreInfos { | ||
m[storeInfo.Name] = storeInfo.GetHash() | ||
} | ||
|
||
return m | ||
} | ||
|
||
func (ci CommitInfo) CommitID() CommitID { | ||
return CommitID{ | ||
Version: ci.Version, | ||
Hash: ci.Hash(), | ||
} | ||
} | ||
|
||
func (m *CommitInfo) GetVersion() uint64 { | ||
if m != nil { | ||
return m.Version | ||
} | ||
return 0 | ||
} | ||
|
||
func (cid CommitID) String() string { | ||
return fmt.Sprintf("CommitID{%v:%X}", cid.Hash, cid.Version) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# State Commitment (SC) | ||
|
||
TODO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.