-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Co-authored-by: Hieu Vu <72878483+hieuvubk@users.noreply.github.com> Co-authored-by: Julien Robert <julien@rbrt.fr>
- Loading branch information
1 parent
ec08ea7
commit 0eb9540
Showing
8 changed files
with
947 additions
and
4 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package mock | ||
|
||
import ( | ||
"context" | ||
|
||
"cosmossdk.io/core/transaction" | ||
"cosmossdk.io/server/v2/cometbft/mempool" | ||
) | ||
|
||
var _ mempool.Mempool[transaction.Tx] = (*MockMempool[transaction.Tx])(nil) | ||
|
||
// MockMempool implements Mempool | ||
// Used for testing instead of NoOpMempool | ||
type MockMempool[T transaction.Tx] struct{} | ||
|
||
func (MockMempool[T]) Insert(context.Context, T) error { return nil } | ||
func (MockMempool[T]) Select(context.Context, []T) mempool.Iterator[T] { return nil } | ||
func (MockMempool[T]) CountTx() int { return 0 } | ||
func (MockMempool[T]) Remove([]T) error { return nil } |
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,65 @@ | ||
package mock | ||
|
||
import ( | ||
corestore "cosmossdk.io/core/store" | ||
) | ||
|
||
// ReaderMap defines an adapter around a RootStore that only exposes read-only | ||
// operations. This is useful for exposing a read-only view of the RootStore at | ||
// a specific version in history, which could also be the latest state. | ||
type ReaderMap struct { | ||
store *MockStore | ||
version uint64 | ||
} | ||
|
||
func NewMockReaderMap(v uint64, rs *MockStore) *ReaderMap { | ||
return &ReaderMap{ | ||
store: rs, | ||
version: v, | ||
} | ||
} | ||
|
||
func (roa *ReaderMap) GetReader(actor []byte) (corestore.Reader, error) { | ||
return NewMockReader(roa.version, roa.store, actor), nil | ||
} | ||
|
||
// Reader represents a read-only adapter for accessing data from the root store. | ||
type MockReader struct { | ||
version uint64 // The version of the data. | ||
store *MockStore // The root store to read data from. | ||
actor []byte // The actor associated with the data. | ||
} | ||
|
||
func NewMockReader(v uint64, rs *MockStore, actor []byte) *MockReader { | ||
return &MockReader{ | ||
version: v, | ||
store: rs, | ||
actor: actor, | ||
} | ||
} | ||
|
||
func (roa *MockReader) Has(key []byte) (bool, error) { | ||
val, err := roa.store.GetStateStorage().Has(roa.actor, roa.version, key) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
return val, nil | ||
} | ||
|
||
func (roa *MockReader) Get(key []byte) ([]byte, error) { | ||
result, err := roa.store.GetStateStorage().Get(roa.actor, roa.version, key) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return result, nil | ||
} | ||
|
||
func (roa *MockReader) Iterator(start, end []byte) (corestore.Iterator, error) { | ||
return roa.store.GetStateStorage().Iterator(roa.actor, roa.version, start, end) | ||
} | ||
|
||
func (roa *MockReader) ReverseIterator(start, end []byte) (corestore.Iterator, error) { | ||
return roa.store.GetStateStorage().ReverseIterator(roa.actor, roa.version, start, end) | ||
} |
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,139 @@ | ||
package mock | ||
|
||
import ( | ||
"crypto/sha256" | ||
"fmt" | ||
|
||
"cosmossdk.io/core/log" | ||
corestore "cosmossdk.io/core/store" | ||
|
||
storev2 "cosmossdk.io/store/v2" | ||
"cosmossdk.io/store/v2/commitment" | ||
"cosmossdk.io/store/v2/commitment/iavl" | ||
dbm "cosmossdk.io/store/v2/db" | ||
"cosmossdk.io/store/v2/proof" | ||
"cosmossdk.io/store/v2/storage" | ||
"cosmossdk.io/store/v2/storage/sqlite" | ||
) | ||
|
||
type MockStore struct { | ||
Storage storev2.VersionedDatabase | ||
Commiter storev2.Committer | ||
} | ||
|
||
func NewMockStorage(logger log.Logger, dir string) storev2.VersionedDatabase { | ||
storageDB, _ := sqlite.New(dir) | ||
ss := storage.NewStorageStore(storageDB, logger) | ||
return ss | ||
} | ||
|
||
func NewMockCommiter(logger log.Logger, actors ...string) storev2.Committer { | ||
treeMap := make(map[string]commitment.Tree) | ||
for _, actor := range actors { | ||
tree := iavl.NewIavlTree(dbm.NewMemDB(), logger, iavl.DefaultConfig()) | ||
treeMap[actor] = tree | ||
} | ||
sc, _ := commitment.NewCommitStore(treeMap, treeMap, dbm.NewMemDB(), logger) | ||
return sc | ||
} | ||
|
||
func NewMockStore(ss storev2.VersionedDatabase, sc storev2.Committer) *MockStore { | ||
return &MockStore{Storage: ss, Commiter: sc} | ||
} | ||
|
||
func (s *MockStore) GetLatestVersion() (uint64, error) { | ||
lastCommitID, err := s.LastCommitID() | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
return lastCommitID.Version, nil | ||
} | ||
|
||
func (s *MockStore) StateLatest() (uint64, corestore.ReaderMap, error) { | ||
v, err := s.GetLatestVersion() | ||
if err != nil { | ||
return 0, nil, err | ||
} | ||
|
||
return v, NewMockReaderMap(v, s), nil | ||
} | ||
|
||
func (s *MockStore) Commit(changeset *corestore.Changeset) (corestore.Hash, error) { | ||
v, _, _ := s.StateLatest() | ||
err := s.Storage.ApplyChangeset(v, changeset) | ||
if err != nil { | ||
return []byte{}, err | ||
} | ||
|
||
err = s.Commiter.WriteChangeset(changeset) | ||
if err != nil { | ||
return []byte{}, err | ||
} | ||
|
||
commitInfo, err := s.Commiter.Commit(v + 1) | ||
fmt.Println("commitInfo", commitInfo, err) | ||
return []byte{}, err | ||
} | ||
|
||
func (s *MockStore) StateAt(version uint64) (corestore.ReaderMap, error) { | ||
info, err := s.Commiter.GetCommitInfo(version) | ||
if err != nil || info == nil { | ||
return nil, fmt.Errorf("failed to get commit info for version %d: %w", version, err) | ||
} | ||
return NewMockReaderMap(version, s), nil | ||
} | ||
|
||
func (s *MockStore) GetStateStorage() storev2.VersionedDatabase { | ||
return s.Storage | ||
} | ||
|
||
func (s *MockStore) GetStateCommitment() storev2.Committer { | ||
return s.Commiter | ||
} | ||
|
||
type Result struct { | ||
key []byte | ||
value []byte | ||
version uint64 | ||
proofOps []proof.CommitmentOp | ||
} | ||
|
||
func (s *MockStore) Query(storeKey []byte, version uint64, key []byte, prove bool) (storev2.QueryResult, error) { | ||
state, err := s.StateAt(version) | ||
reader, err := state.GetReader(storeKey) | ||
value, err := reader.Get(key) | ||
res := storev2.QueryResult{ | ||
Key: key, | ||
Value: value, | ||
Version: version, | ||
} | ||
return res, err | ||
} | ||
|
||
func (s *MockStore) LastCommitID() (proof.CommitID, error) { | ||
v, err := s.GetStateCommitment().GetLatestVersion() | ||
bz := sha256.Sum256([]byte{}) | ||
return proof.CommitID{ | ||
Version: v, | ||
Hash: bz[:], | ||
}, err | ||
} | ||
|
||
func (s *MockStore) SetInitialVersion(v uint64) error { | ||
return s.Commiter.SetInitialVersion(v) | ||
} | ||
|
||
func (s *MockStore) WorkingHash(changeset *corestore.Changeset) (corestore.Hash, error) { | ||
v, _, _ := s.StateLatest() | ||
err := s.Storage.ApplyChangeset(v, changeset) | ||
if err != nil { | ||
return []byte{}, err | ||
} | ||
|
||
err = s.Commiter.WriteChangeset(changeset) | ||
if err != nil { | ||
return []byte{}, err | ||
} | ||
return []byte{}, nil | ||
} |
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