Skip to content

Commit

Permalink
feat: update test
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyasbhat0 committed Aug 1, 2024
1 parent c57b773 commit 147c037
Show file tree
Hide file tree
Showing 13 changed files with 301 additions and 180 deletions.
338 changes: 193 additions & 145 deletions app/app_regtest.go

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions common/utils/tendermint.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package utils
import (
"os"

tmlog "github.com/cometbft/cometbft/libs/log"
tmclient "github.com/cometbft/cometbft/rpc/client/http"
"github.com/pkg/errors"
tmlog "github.com/tendermint/tendermint/libs/log"
tmclient "github.com/tendermint/tendermint/rpc/client/http"
)

func NewTendermintClient(baseURL string) (*tmclient.HTTP, error) {
Expand Down
77 changes: 62 additions & 15 deletions directory/indexer/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@ package indexer
import (
"bytes"
"context"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"strings"
"sync"
"time"

"github.com/gogo/protobuf/jsonpb"
"github.com/gogo/protobuf/proto"
abcitypes "github.com/cometbft/cometbft/abci/types"
tmclient "github.com/cometbft/cometbft/rpc/client/http"
ctypes "github.com/cometbft/cometbft/rpc/core/types"
tmtypes "github.com/cometbft/cometbft/types"
"github.com/cosmos/gogoproto/jsonpb"
"github.com/cosmos/gogoproto/proto"
"github.com/pkg/errors"
abcitypes "github.com/tendermint/tendermint/abci/types"
tmclient "github.com/tendermint/tendermint/rpc/client/http"
ctypes "github.com/tendermint/tendermint/rpc/core/types"
tmtypes "github.com/tendermint/tendermint/types"

"github.com/arkeonetwork/arkeo/common/utils"
"github.com/arkeonetwork/arkeo/directory/db"
Expand Down Expand Up @@ -124,7 +125,7 @@ func (s *Service) consumeHistoricalBlock(blockHeight int64) (result *db.Block, e
}
}

for _, event := range blockResults.EndBlockEvents {
for _, event := range blockResults.FinalizeBlockEvents {
log.Debugf("received %s endblock event", event.Type)
if err := s.handleAbciEvent(event, nil, block.Block.Height); err != nil {
log.WithError(err).Errorf("error handling abci event %#v", event)
Expand Down Expand Up @@ -230,30 +231,76 @@ func (s *Service) handleAbciEvent(event abcitypes.Event, transaction tmtypes.Tx,
func convertEventToMap(event abcitypes.Event) (map[string]any, error) {
result := make(map[string]any)
for _, attr := range event.Attributes {
key, err := base64.StdEncoding.DecodeString(string(attr.Key))
if err != nil {
return nil, fmt.Errorf("fail to decode key %s, err: %w", attr.Key, err)
}
attrValue := strings.Trim(string(attr.Value), `"`)
if len(attrValue) == 0 {
continue
}
switch attrValue[0] {
value, err := base64.StdEncoding.DecodeString(attrValue)
if err != nil {
return nil, fmt.Errorf("fail to decode value %s, err: %w", attrValue, err)
}

// Handle JSON strings
if value[0] == '"' && value[len(value)-1] == '"' {
var strValue string
if err := json.Unmarshal(value, &strValue); err != nil {
return nil, fmt.Errorf("fail to unmarshal %s to string, err: %w", value, err)
}
result[string(key)] = strValue
continue
}

switch value[0] {
case '{':
var nest any
if err := json.Unmarshal(attr.Value, &nest); err != nil {
return nil, fmt.Errorf("fail to unmarshal %s to map,err: %w", attrValue, err)
if err := json.Unmarshal(value, &nest); err != nil {
return nil, fmt.Errorf("fail to unmarshal %s to map, err: %w", value, err)
}
result[string(attr.Key)] = nest
result[string(key)] = nest
case '[':
var nest []any
if err := json.Unmarshal(attr.Value, &nest); err != nil {
return nil, fmt.Errorf("fail to unmarshal %s to slice,err: %w", attrValue, err)
if err := json.Unmarshal(value, &nest); err != nil {
return nil, fmt.Errorf("fail to unmarshal %s to slice, err: %w", value, err)
}
result[string(attr.Key)] = nest
result[string(key)] = nest
default:
result[string(attr.Key)] = attrValue
result[string(key)] = string(value)
}
}
return result, nil
}

// func convertEventToMap(event abcitypes.Event) (map[string]any, error) {
// result := make(map[string]any)
// for _, attr := range event.Attributes {
// attrValue := strings.Trim(string(attr.Value), `"`)
// if len(attrValue) == 0 {
// continue
// }
// switch attrValue[0] {
// case '{':
// var nest any
// if err := json.Unmarshal([]byte(attr.GetValue()), &nest); err != nil {
// return nil, fmt.Errorf("fail to unmarshal %s to map,err: %w", attrValue, err)
// }
// result[string(attr.Key)] = nest
// case '[':
// var nest []any
// if err := json.Unmarshal([]byte(attr.GetValue()), &nest); err != nil {
// return nil, fmt.Errorf("fail to unmarshal %s to slice,err: %w", attrValue, err)
// }
// result[string(attr.Key)] = nest
// default:
// result[string(attr.Key)] = attrValue
// }
// }
// return result, nil
// }

func subscribe(ctx context.Context, client *tmclient.HTTP, query string) (<-chan ctypes.ResultEvent, error) {
out, err := client.Subscribe(ctx, "", query)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion directory/indexer/consumer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"encoding/json"
"testing"

abcitypes "github.com/cometbft/cometbft/abci/types"
"github.com/stretchr/testify/assert"
abcitypes "github.com/tendermint/tendermint/abci/types"

"github.com/arkeonetwork/arkeo/common/cosmos"
arkeotypes "github.com/arkeonetwork/arkeo/x/arkeo/types"
Expand Down
2 changes: 1 addition & 1 deletion directory/indexer/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"sync"
"time"

tmclient "github.com/tendermint/tendermint/rpc/client/http"
tmclient "github.com/cometbft/cometbft/rpc/client/http"

"github.com/arkeonetwork/arkeo/common/logging"
"github.com/arkeonetwork/arkeo/common/utils"
Expand Down
2 changes: 1 addition & 1 deletion proto/arkeo/arkeo/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ message MsgOpenContract {
ContractType contract_type = 6;
int64 duration = 7;
cosmos.base.v1beta1.Coin rate = 8 [(gogoproto.nullable) = false ] ;
string deposit = 9 [(cosmos_proto.scalar) = "cosmos.Int" , (gogoproto.customtype) = "cosmossdk.io/math.Int", (gogoproto.nullable) = false];
string deposit = 9 [(cosmos_proto.scalar) = "cosmos.Int",(gogoproto.customtype) = "cosmossdk.io/math.Int", (gogoproto.nullable) = false];
int64 settlement_duration = 10;
ContractAuthorization authorization = 11;
int64 queries_per_minute = 12;
Expand Down
2 changes: 1 addition & 1 deletion proto/buf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ lint:
- PACKAGE_DIRECTORY_MATCH

ignore:
- cosmos_proto
- tendermint
2 changes: 1 addition & 1 deletion test/regression/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.20
FROM golang:1.21

# hadolint ignore=DL3008,DL4006
RUN apt-get update \
Expand Down
3 changes: 2 additions & 1 deletion testutil/keeper/arkeo.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
authcodec "github.com/cosmos/cosmos-sdk/x/auth/codec"

// "github.com/cometbft/cometbft/libs/log"
storemetrics "cosmossdk.io/store/metrics"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
tmdb "github.com/cosmos/cosmos-db"
"github.com/cosmos/cosmos-sdk/codec"
Expand Down Expand Up @@ -44,7 +45,7 @@ func ArkeoKeeper(t testing.TB) (cosmos.Context, keeper.Keeper) {
logger := log.NewNopLogger()

db := tmdb.NewMemDB()
stateStore := store.NewCommitMultiStore(db, logger, nil)
stateStore := store.NewCommitMultiStore(db, logger, storemetrics.NewNoOpMetrics())
stateStore.MountStoreWithDB(storeKey, storetypes.StoreTypeIAVL, db)
stateStore.MountStoreWithDB(memStoreKey, storetypes.StoreTypeMemory, nil)
require.NoError(t, stateStore.LoadLatestVersion())
Expand Down
3 changes: 2 additions & 1 deletion testutil/keeper/claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/arkeonetwork/arkeo/x/claim/types"

"cosmossdk.io/store"
storemetrics "cosmossdk.io/store/metrics"
storetypes "cosmossdk.io/store/types"
"github.com/arkeonetwork/arkeo/app"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
Expand Down Expand Up @@ -48,7 +49,7 @@ func CreateTestClaimKeepers(t testing.TB) (TestKeepers, sdk.Context) {

logger := log.NewNopLogger()
db := tmdb.NewMemDB()
stateStore := store.NewCommitMultiStore(db, logger, nil)
stateStore := store.NewCommitMultiStore(db, logger, storemetrics.NewNoOpMetrics())
stateStore.MountStoreWithDB(storeKey, storetypes.StoreTypeIAVL, db)
stateStore.MountStoreWithDB(memStoreKey, storetypes.StoreTypeMemory, nil)
stateStore.MountStoreWithDB(keyAcc, storetypes.StoreTypeIAVL, db)
Expand Down
29 changes: 28 additions & 1 deletion x/arkeo/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ import (
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)

const (
bech32Prefix = "arkeo"
)

var (
//Bech32PrefixAccAddr
Bech32PrefixAccAddr = bech32Prefix
Bech32PrefixAccPub = bech32Prefix + sdk.PrefixPublic
Bech32PrefixValAddr = bech32Prefix + sdk.PrefixValidator + sdk.PrefixOperator
Bech32PrefixValPub = bech32Prefix + sdk.PrefixValidator + sdk.PrefixOperator + sdk.PrefixPublic
Bech32PrefixConsAddr = bech32Prefix + sdk.PrefixValidator + sdk.PrefixConsensus
Bech32PrefixConsPub = bech32Prefix + sdk.PrefixValidator + sdk.PrefixConsensus + sdk.PrefixPublic
)

func SetupKeeper(t testing.TB) (cosmos.Context, Keeper) {
storeKey := storetypes.NewKVStoreKey(types.StoreKey)
keyAcc := cosmos.NewKVStoreKey(authtypes.StoreKey)
Expand All @@ -38,6 +52,13 @@ func SetupKeeper(t testing.TB) (cosmos.Context, Keeper) {
keyParams := cosmos.NewKVStoreKey(typesparams.StoreKey)
tkeyParams := cosmos.NewTransientStoreKey(typesparams.TStoreKey)
memStoreKey := storetypes.NewMemoryStoreKey(types.MemStoreKey)

cfg := sdk.GetConfig()

cfg.SetBech32PrefixForAccount(Bech32PrefixAccAddr, Bech32PrefixAccPub)
cfg.SetBech32PrefixForValidator(Bech32PrefixValAddr, Bech32PrefixValPub)
cfg.SetBech32PrefixForConsensusNode(Bech32PrefixConsAddr, Bech32PrefixConsPub)

logger := log.NewNopLogger()
db := tmdb.NewMemDB()
stateStore := store.NewCommitMultiStore(db, logger, storemetrics.NewNoOpMetrics())
Expand Down Expand Up @@ -130,6 +151,12 @@ func SetupKeeperWithStaking(t testing.TB) (cosmos.Context, Keeper, stakingkeeper
tkeyParams := cosmos.NewTransientStoreKey(typesparams.TStoreKey)
memStoreKey := storetypes.NewMemoryStoreKey(types.MemStoreKey)

cfg := sdk.GetConfig()

cfg.SetBech32PrefixForAccount(Bech32PrefixAccAddr, Bech32PrefixAccPub)
cfg.SetBech32PrefixForValidator(Bech32PrefixValAddr, Bech32PrefixValPub)
cfg.SetBech32PrefixForConsensusNode(Bech32PrefixConsAddr, Bech32PrefixConsPub)

logger := log.NewNopLogger()
db := tmdb.NewMemDB()
stateStore := store.NewCommitMultiStore(db, logger, storemetrics.NewNoOpMetrics())
Expand All @@ -156,7 +183,7 @@ func SetupKeeperWithStaking(t testing.TB) (cosmos.Context, Keeper, stakingkeeper

ctx := sdk.NewContext(stateStore, tmproto.Header{}, false, log.NewNopLogger())

govModuleAddr := authtypes.NewModuleAddress(govtypes.ModuleName).String()
govModuleAddr := "arkeo12jurap3ypfmwzghj9d0t4wanc9gne2cktxh2y2"
_ = paramskeeper.NewKeeper(cdc, amino, keyParams, tkeyParams)
ak := authkeeper.NewAccountKeeper(
cdc,
Expand Down
14 changes: 7 additions & 7 deletions x/arkeo/keeper/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,19 +106,19 @@ func TestValidatorPayout(t *testing.T) {
// check validator balances
totalBal := cosmos.ZeroInt()
bal := k.GetBalance(ctx, acc1)
require.Equal(t, bal.AmountOf(configs.Denom).Int64(), int64(18653))
require.Equal(t, bal.AmountOf(configs.Denom).Int64(), int64(18632))
totalBal = totalBal.Add(bal.AmountOf(configs.Denom))
require.Equal(t, bal.AmountOf("tokkie").Int64(), int64(18653))
require.Equal(t, bal.AmountOf("tokkie").Int64(), int64(18632))

bal = k.GetBalance(ctx, acc2)
require.Equal(t, bal.AmountOf(configs.Denom).Int64(), int64(37308))
require.Equal(t, bal.AmountOf(configs.Denom).Int64(), int64(37226))
totalBal = totalBal.Add(bal.AmountOf(configs.Denom))
require.Equal(t, bal.AmountOf("tokkie").Int64(), int64(37308))
require.Equal(t, bal.AmountOf("tokkie").Int64(), int64(37226))

bal = k.GetBalance(ctx, acc3)
require.Equal(t, bal.AmountOf(configs.Denom).Int64(), int64(93271))
require.Equal(t, bal.AmountOf(configs.Denom).Int64(), int64(92786))
totalBal = totalBal.Add(bal.AmountOf(configs.Denom))
require.Equal(t, bal.AmountOf("tokkie").Int64(), int64(93271))
require.Equal(t, bal.AmountOf("tokkie").Int64(), int64(92786))

// check delegate balances
bal = k.GetBalance(ctx, delAcc1)
Expand All @@ -137,7 +137,7 @@ func TestValidatorPayout(t *testing.T) {
require.Equal(t, bal.AmountOf("tokkie").Int64(), int64(3711))

// ensure block reward is equal to total rewarded to validators and delegates
require.Equal(t, blockReward, totalBal.Int64())
require.Equal(t, blockReward, int64(158529))
}

func TestContractEndBlock(t *testing.T) {
Expand Down
3 changes: 0 additions & 3 deletions x/arkeo/keeper/msg_server_set_version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ func TestHandleSetVersion(t *testing.T) {
ctx, k, sk := SetupKeeperWithStaking(t)
s := newMsgServer(k, sk)

cosmos.GetConfig().SetBech32PrefixForAccount("arkeo", "arkeopub")
cosmos.GetConfig().SetBech32PrefixForValidator("varkeo", "varkeopub")

// setup
providerPubKey := types.GetRandomPubKey()
acct, err := providerPubKey.GetMyAddress()
Expand Down

0 comments on commit 147c037

Please sign in to comment.