Skip to content

Commit

Permalink
test(server/v2/cometbft): Add abci unit tests (backport #21020) (#21605)
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
3 people authored Sep 9, 2024
1 parent ec08ea7 commit 0eb9540
Show file tree
Hide file tree
Showing 8 changed files with 947 additions and 4 deletions.
714 changes: 714 additions & 0 deletions server/v2/cometbft/abci_test.go

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion server/v2/cometbft/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ require (
cosmossdk.io/log v1.4.1
cosmossdk.io/server/v2 v2.0.0-20240829074658-81a225e6a29b // main
cosmossdk.io/server/v2/appmanager v0.0.0-20240827095516-355f748add9e // main
cosmossdk.io/server/v2/stf v0.0.0-20240909103429-ac53e337a9b5 // main
cosmossdk.io/store/v2 v2.0.0-20240906090851-36d9b25e8981 // main
cosmossdk.io/x/consensus v0.0.0-00010101000000-000000000000
github.com/cometbft/cometbft v1.0.0-rc1.0.20240908111210-ab0be101882f
Expand All @@ -35,6 +36,7 @@ require (
github.com/spf13/cobra v1.8.1
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.19.0
github.com/stretchr/testify v1.9.0
google.golang.org/genproto/googleapis/api v0.0.0-20240604185151-ef581f913117
google.golang.org/grpc v1.66.0
google.golang.org/protobuf v1.34.2
Expand Down Expand Up @@ -119,6 +121,7 @@ require (
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-sqlite3 v1.14.22 // indirect
github.com/minio/highwayhash v1.0.3 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
Expand All @@ -144,7 +147,6 @@ require (
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.7.0 // indirect
github.com/stretchr/testify v1.9.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/supranational/blst v0.3.13 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d // indirect
Expand All @@ -171,3 +173,5 @@ require (
gotest.tools/v3 v3.5.1 // indirect
pgregory.net/rapid v1.1.0 // indirect
)

require cosmossdk.io/core/testing v0.0.0-20240903211221-70488a89a87a // indirect
2 changes: 2 additions & 0 deletions server/v2/cometbft/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ cosmossdk.io/server/v2 v2.0.0-20240829074658-81a225e6a29b h1:FFixNVq2SbtRlYvr1fB
cosmossdk.io/server/v2 v2.0.0-20240829074658-81a225e6a29b/go.mod h1:MgjYKtPEW4FPEYxh1h0idVBjgblFgdrAWkeqn7UOrVA=
cosmossdk.io/server/v2/appmanager v0.0.0-20240827095516-355f748add9e h1:9eB2si1gT6c5ea8Jsh498Ei6E3Th1yMyuJgSLiT6D4Y=
cosmossdk.io/server/v2/appmanager v0.0.0-20240827095516-355f748add9e/go.mod h1:fJDDnWJCBRxLLIyu2byqtf3KTRYIVS4OxKwdZozJi20=
cosmossdk.io/server/v2/stf v0.0.0-20240909103429-ac53e337a9b5 h1:lrq7/YrEflKv3enDm5jMHHz1GsfmBzHM4FGgjSt1ZmQ=
cosmossdk.io/server/v2/stf v0.0.0-20240909103429-ac53e337a9b5/go.mod h1:Y0cRRjdM5JcXqFsGYgZgWkiG7fA4eN8oNBP1vSEE8Gc=
cosmossdk.io/store v1.0.0-rc.0.0.20240906090851-36d9b25e8981 h1:O510vQpVdyZgSlLm4NG2aQltDkKfoWcKrVCPPWQlNHs=
cosmossdk.io/store v1.0.0-rc.0.0.20240906090851-36d9b25e8981/go.mod h1:5lnCUSlWPWQYrUcNjpqBLTnlykyz1NeGQpMmZq1DCOg=
cosmossdk.io/store/v2 v2.0.0-20240906090851-36d9b25e8981 h1:Z6I2YbnF6b6mRD63hMQ5DA2efQ4Ip9Ujf1y/r31egWk=
Expand Down
19 changes: 19 additions & 0 deletions server/v2/cometbft/internal/mock/mock_mempool.go
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 }
65 changes: 65 additions & 0 deletions server/v2/cometbft/internal/mock/mock_reader.go
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)
}
139 changes: 139 additions & 0 deletions server/v2/cometbft/internal/mock/mock_store.go
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
}
2 changes: 1 addition & 1 deletion simapp/v2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ require (
cosmossdk.io/errors/v2 v2.0.0-20240731132947-df72853b3ca5 // indirect
cosmossdk.io/schema v0.2.0 // indirect
cosmossdk.io/server/v2/appmanager v0.0.0-20240827095516-355f748add9e // indirect; main
cosmossdk.io/server/v2/stf v0.0.0-20240827095516-355f748add9e // indirect; main
cosmossdk.io/server/v2/stf v0.0.0-20240909103429-ac53e337a9b5 // indirect; main
cosmossdk.io/store v1.1.1-0.20240815194237-858ec2fcb897 // indirect; main
cosmossdk.io/x/accounts/defaults/lockup v0.0.0-20240417181816-5e7aae0db1f5 // indirect
cosmossdk.io/x/accounts/defaults/multisig v0.0.0-00010101000000-000000000000 // indirect
Expand Down
4 changes: 2 additions & 2 deletions simapp/v2/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,8 @@ cosmossdk.io/server/v2 v2.0.0-20240829074658-81a225e6a29b h1:FFixNVq2SbtRlYvr1fB
cosmossdk.io/server/v2 v2.0.0-20240829074658-81a225e6a29b/go.mod h1:MgjYKtPEW4FPEYxh1h0idVBjgblFgdrAWkeqn7UOrVA=
cosmossdk.io/server/v2/appmanager v0.0.0-20240827095516-355f748add9e h1:9eB2si1gT6c5ea8Jsh498Ei6E3Th1yMyuJgSLiT6D4Y=
cosmossdk.io/server/v2/appmanager v0.0.0-20240827095516-355f748add9e/go.mod h1:fJDDnWJCBRxLLIyu2byqtf3KTRYIVS4OxKwdZozJi20=
cosmossdk.io/server/v2/stf v0.0.0-20240827095516-355f748add9e h1:A2JxCwJuoMRiBF8ZjpxRf+JO5cTSYw8Vc2iHImfHoKg=
cosmossdk.io/server/v2/stf v0.0.0-20240827095516-355f748add9e/go.mod h1:Y0cRRjdM5JcXqFsGYgZgWkiG7fA4eN8oNBP1vSEE8Gc=
cosmossdk.io/server/v2/stf v0.0.0-20240909103429-ac53e337a9b5 h1:lrq7/YrEflKv3enDm5jMHHz1GsfmBzHM4FGgjSt1ZmQ=
cosmossdk.io/server/v2/stf v0.0.0-20240909103429-ac53e337a9b5/go.mod h1:Y0cRRjdM5JcXqFsGYgZgWkiG7fA4eN8oNBP1vSEE8Gc=
cosmossdk.io/store v1.0.0-rc.0.0.20240906090851-36d9b25e8981 h1:O510vQpVdyZgSlLm4NG2aQltDkKfoWcKrVCPPWQlNHs=
cosmossdk.io/store v1.0.0-rc.0.0.20240906090851-36d9b25e8981/go.mod h1:5lnCUSlWPWQYrUcNjpqBLTnlykyz1NeGQpMmZq1DCOg=
cosmossdk.io/store/v2 v2.0.0-20240906090851-36d9b25e8981 h1:Z6I2YbnF6b6mRD63hMQ5DA2efQ4Ip9Ujf1y/r31egWk=
Expand Down

0 comments on commit 0eb9540

Please sign in to comment.