Skip to content

Commit

Permalink
Merge branch 'main' into ducnt/ibc
Browse files Browse the repository at this point in the history
  • Loading branch information
anhductn2001 committed Jul 21, 2023
2 parents 41839ef + 603ab5f commit e8470a8
Show file tree
Hide file tree
Showing 134 changed files with 14,484 additions and 883 deletions.
2 changes: 1 addition & 1 deletion app/ante/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
ante "github.com/cosmos/cosmos-sdk/x/auth/ante"
"github.com/cosmos/cosmos-sdk/x/auth/signing"
tfmwKeeper "github.com/notional-labs/centauri/v3/x/transfermiddleware/keeper"
tfmwKeeper "github.com/notional-labs/centauri/v4/x/transfermiddleware/keeper"
)

// Link to default ante handler used by cosmos sdk:
Expand Down
2 changes: 1 addition & 1 deletion app/ante/ibc_ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"github.com/cosmos/cosmos-sdk/x/authz"
clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
tfmwKeeper "github.com/notional-labs/centauri/v3/x/transfermiddleware/keeper"
tfmwKeeper "github.com/notional-labs/centauri/v4/x/transfermiddleware/keeper"
)

type IBCPermissionDecorator struct {
Expand Down
478 changes: 113 additions & 365 deletions app/app.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion app/helpers/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/cosmos/ibc-go/v7/testing/mock"
centauri "github.com/notional-labs/centauri/v3/app"
centauri "github.com/notional-labs/centauri/v4/app"
"github.com/stretchr/testify/require"
)

Expand Down
104 changes: 100 additions & 4 deletions app/ibctesting/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ package ibctesting

import (
"bytes"
"crypto/sha256"
"fmt"
"os"
"testing"
"time"

ratelimitmodulekeeper "github.com/notional-labs/centauri/v4/x/ratelimit/keeper"

"cosmossdk.io/errors"
abci "github.com/cometbft/cometbft/abci/types"
"github.com/cometbft/cometbft/crypto/tmhash"
Expand All @@ -19,12 +23,20 @@ import (
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
govkeeper "github.com/cosmos/cosmos-sdk/x/gov/keeper"
v1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
"github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"

"github.com/CosmWasm/wasmd/x/wasm"
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
capabilitykeeper "github.com/cosmos/cosmos-sdk/x/capability/keeper"
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
teststaking "github.com/cosmos/cosmos-sdk/x/staking/testutil"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
Expand All @@ -39,10 +51,11 @@ import (
ibctesting "github.com/cosmos/ibc-go/v7/testing"
"github.com/cosmos/ibc-go/v7/testing/mock"
ibctestingtypes "github.com/cosmos/ibc-go/v7/testing/types"
centauri "github.com/notional-labs/centauri/v3/app"
"github.com/notional-labs/centauri/v3/app/ibctesting/simapp"
routerKeeper "github.com/notional-labs/centauri/v3/x/transfermiddleware/keeper"
centauri "github.com/notional-labs/centauri/v4/app"
"github.com/notional-labs/centauri/v4/app/ibctesting/simapp"
routerKeeper "github.com/notional-labs/centauri/v4/x/transfermiddleware/keeper"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)

// TestChain is a testing struct that wraps a simapp with the last TM Header, the current ABCI
Expand Down Expand Up @@ -111,7 +124,7 @@ func NewTestChain(t *testing.T, coord *Coordinator, chainID string) *TestChain {
Coins: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, amount)),
}

app := NewTestingAppDecorator(t, centauri.SetupWithGenesisValSet(t, valSet, []authtypes.GenesisAccount{acc}, balance))
app := NewTestingAppDecorator(t, centauri.SetupWithGenesisValSet(t, coord.CurrentTime.UTC(), valSet, []authtypes.GenesisAccount{acc}, balance))

// create current header and call begin block
header := tmproto.Header{
Expand Down Expand Up @@ -605,6 +618,10 @@ func (chain *TestChain) TransferMiddleware() routerKeeper.Keeper {
return chain.GetTestSupport().TransferMiddleware()
}

func (chain *TestChain) RateLimit() ratelimitmodulekeeper.Keeper {
return chain.GetTestSupport().RateLimit()
}

func (chain *TestChain) Balance(acc sdk.AccAddress, denom string) sdk.Coin {
return chain.GetTestSupport().BankKeeper().GetBalance(chain.GetContext(), acc, denom)
}
Expand All @@ -621,6 +638,73 @@ func (chain TestChain) GetTestSupport() *centauri.TestSupport {
return chain.App.(*TestingAppDecorator).TestSupport()
}

func (chain *TestChain) QueryContract(suite *suite.Suite, contract sdk.AccAddress, key []byte) string {
wasmKeeper := chain.GetTestSupport().WasmdKeeper()
state, err := wasmKeeper.QuerySmart(chain.GetContext(), contract, key)
suite.Require().NoError(err)
return string(state)
}

func (chain *TestChain) StoreContractCode(suite *suite.Suite, path string) {
govModuleAddress := chain.GetTestSupport().AccountKeeper().GetModuleAddress(govtypes.ModuleName)
wasmCode, err := os.ReadFile(path)
suite.Require().NoError(err)

src := wasmtypes.StoreCodeProposalFixture(func(p *wasmtypes.StoreCodeProposal) {
p.RunAs = govModuleAddress.String()
p.WASMByteCode = wasmCode
checksum := sha256.Sum256(wasmCode)
p.CodeHash = checksum[:]
})

govKeeper := chain.GetTestSupport().GovKeeper()
// when
mustSubmitAndExecuteLegacyProposal(suite.T(), chain.GetContext(), src, chain.SenderAccount.GetAddress().String(), &govKeeper, govModuleAddress.String())
suite.Require().NoError(err)
}

func (chain *TestChain) InstantiateContract(suite *suite.Suite, msg string, codeID uint64) sdk.AccAddress {
wasmKeeper := chain.GetTestSupport().WasmdKeeper()
govModuleAddress := chain.GetTestSupport().AccountKeeper().GetModuleAddress(govtypes.ModuleName)

contractKeeper := wasmkeeper.NewDefaultPermissionKeeper(wasmKeeper)
addr, _, err := contractKeeper.Instantiate(chain.GetContext(), codeID, govModuleAddress, govModuleAddress, []byte(msg), "contract", nil)
suite.Require().NoError(err)
return addr
}

func mustSubmitAndExecuteLegacyProposal(t *testing.T, ctx sdk.Context, content v1beta1.Content, myActorAddress string, govKeeper *govkeeper.Keeper, authority string) {
t.Helper()
msgServer := govkeeper.NewMsgServerImpl(govKeeper)
// ignore all submit events
contentMsg, err := submitLegacyProposal(t, ctx.WithEventManager(sdk.NewEventManager()), content, myActorAddress, authority, msgServer)
require.NoError(t, err)

_, err = msgServer.ExecLegacyContent(sdk.WrapSDKContext(ctx), v1.NewMsgExecLegacyContent(contentMsg.Content, authority))
require.NoError(t, err)
}

// does not fail on submit proposal
func submitLegacyProposal(t *testing.T, ctx sdk.Context, content v1beta1.Content, myActorAddress string, govAuthority string, msgServer v1.MsgServer) (*v1.MsgExecLegacyContent, error) {
t.Helper()
contentMsg, err := v1.NewLegacyContent(content, govAuthority)
require.NoError(t, err)

proposal, err := v1.NewMsgSubmitProposal(
[]sdk.Msg{contentMsg},
sdk.Coins{},
myActorAddress,
"",
"my title",
"my description",
)
require.NoError(t, err)

// when stored
_, err = msgServer.SubmitProposal(sdk.WrapSDKContext(ctx), proposal)
return contentMsg, err
}

var _ ibctesting.TestingApp = TestingAppDecorator{}

type TestingAppDecorator struct {
Expand All @@ -641,6 +725,14 @@ func (a TestingAppDecorator) GetStakingKeeper() ibctestingtypes.StakingKeeper {
return a.TestSupport().StakingKeeper()
}

func (a TestingAppDecorator) GetAccountKeeper() authkeeper.AccountKeeper {
return a.TestSupport().AccountKeeper()
}

func (a TestingAppDecorator) GetGovKeeper() govkeeper.Keeper {
return a.TestSupport().GovKeeper()
}

func (a TestingAppDecorator) GetBankKeeper() bankkeeper.Keeper {
return a.TestSupport().BankKeeper()
}
Expand All @@ -661,6 +753,10 @@ func (a TestingAppDecorator) TestSupport() *centauri.TestSupport {
return centauri.NewTestSupport(a.t, a.CentauriApp)
}

func (a TestingAppDecorator) GetWasmdKeeper() wasm.Keeper {
return a.TestSupport().WasmdKeeper()
}

func (a TestingAppDecorator) GetWasmKeeper() wasm08.Keeper {
return a.TestSupport().Wasm08Keeper()
}
60 changes: 60 additions & 0 deletions app/ibctesting/event_utils.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package ibctesting

import (
"fmt"
"strconv"
"strings"

abci "github.com/cometbft/cometbft/abci/types"
sdk "github.com/cosmos/cosmos-sdk/types"
clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"
)
Expand Down Expand Up @@ -56,6 +58,64 @@ func parsePacketFromEvent(evt abci.Event) channeltypes.Packet {
}
}

// ParsePacketFromEvents parses events emitted from a MsgRecvPacket and returns the
// acknowledgement.
func ParsePacketFromEvents(events sdk.Events) (channeltypes.Packet, error) {
for _, ev := range events {
if ev.Type == channeltypes.EventTypeSendPacket {
packet := channeltypes.Packet{}
for _, attr := range ev.Attributes {
switch string(attr.Key) {
case channeltypes.AttributeKeyData:
packet.Data = []byte(attr.Value)

case channeltypes.AttributeKeySequence:
seq, err := strconv.ParseUint(string(attr.Value), 10, 64)
if err != nil {
return channeltypes.Packet{}, err
}

packet.Sequence = seq

case channeltypes.AttributeKeySrcPort:
packet.SourcePort = string(attr.Value)

case channeltypes.AttributeKeySrcChannel:
packet.SourceChannel = string(attr.Value)

case channeltypes.AttributeKeyDstPort:
packet.DestinationPort = string(attr.Value)

case channeltypes.AttributeKeyDstChannel:
packet.DestinationChannel = string(attr.Value)

case channeltypes.AttributeKeyTimeoutHeight:
height, err := clienttypes.ParseHeight(string(attr.Value))
if err != nil {
return channeltypes.Packet{}, err
}

packet.TimeoutHeight = height

case channeltypes.AttributeKeyTimeoutTimestamp:
timestamp, err := strconv.ParseUint(string(attr.Value), 10, 64)
if err != nil {
return channeltypes.Packet{}, err
}

packet.TimeoutTimestamp = timestamp

default:
continue
}
}

return packet, nil
}
}
return channeltypes.Packet{}, fmt.Errorf("acknowledgement event attribute not found")
}

// return the value for the attribute with the given name
func getField(evt abci.Event, key string) string {
for _, attr := range evt.Attributes {
Expand Down
21 changes: 11 additions & 10 deletions app/ibctesting/simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ import (
"github.com/gorilla/mux"

// TODO: mint module not complete yet,
"github.com/notional-labs/centauri/v3/x/mint"
mintkeeper "github.com/notional-labs/centauri/v3/x/mint/keeper"
minttypes "github.com/notional-labs/centauri/v3/x/mint/types"
"github.com/notional-labs/centauri/v4/x/mint"
mintkeeper "github.com/notional-labs/centauri/v4/x/mint/keeper"
minttypes "github.com/notional-labs/centauri/v4/x/mint/types"

"github.com/rakyll/statik/fs"
"github.com/spf13/cast"
Expand Down Expand Up @@ -123,13 +123,13 @@ import (
wasm08 "github.com/cosmos/ibc-go/v7/modules/light-clients/08-wasm/keeper"
ibcmock "github.com/cosmos/ibc-go/v7/testing/mock"
ibctestingtypes "github.com/cosmos/ibc-go/v7/testing/types"
simappparams "github.com/notional-labs/centauri/v3/app/ibctesting/simapp/params"
simappupgrades "github.com/notional-labs/centauri/v3/app/ibctesting/simapp/upgrades"
v6 "github.com/notional-labs/centauri/v3/app/ibctesting/simapp/upgrades/v6"
v7 "github.com/notional-labs/centauri/v3/app/ibctesting/simapp/upgrades/v7"
transfermiddleware "github.com/notional-labs/centauri/v3/x/transfermiddleware"
transfermiddlewarekeeper "github.com/notional-labs/centauri/v3/x/transfermiddleware/keeper"
transfermiddlewaretypes "github.com/notional-labs/centauri/v3/x/transfermiddleware/types"
simappparams "github.com/notional-labs/centauri/v4/app/ibctesting/simapp/params"
simappupgrades "github.com/notional-labs/centauri/v4/app/ibctesting/simapp/upgrades"
v6 "github.com/notional-labs/centauri/v4/app/ibctesting/simapp/upgrades/v6"
v7 "github.com/notional-labs/centauri/v4/app/ibctesting/simapp/upgrades/v7"
transfermiddleware "github.com/notional-labs/centauri/v4/x/transfermiddleware"
transfermiddlewarekeeper "github.com/notional-labs/centauri/v4/x/transfermiddleware/keeper"
transfermiddlewaretypes "github.com/notional-labs/centauri/v4/x/transfermiddleware/types"
)

const appName = "SimApp"
Expand Down Expand Up @@ -379,6 +379,7 @@ func NewSimApp(
app.UpgradeKeeper = upgradekeeper.NewKeeper(skipUpgradeHeights, keys[upgradetypes.StoreKey], appCodec, homePath, app.BaseApp, authtypes.NewModuleAddress(govtypes.ModuleName).String())
app.TransferMiddlewarekeeper = transfermiddlewarekeeper.NewKeeper(
keys[transfermiddlewaretypes.StoreKey],
app.GetSubspace(transfermiddlewaretypes.ModuleName),
appCodec,
app.IBCKeeper.ChannelKeeper,
app.TransferKeeper,
Expand Down
2 changes: 1 addition & 1 deletion app/ibctesting/simapp/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package simapp
import (
"github.com/cosmos/cosmos-sdk/std"

simappparams "github.com/notional-labs/centauri/v3/app/ibctesting/simapp/params"
simappparams "github.com/notional-labs/centauri/v4/app/ibctesting/simapp/params"
)

// MakeTestEncodingConfig creates an EncodingConfig for testing. This function
Expand Down
2 changes: 1 addition & 1 deletion app/ibctesting/simapp/sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
"github.com/cosmos/cosmos-sdk/x/simulation"
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
minttypes "github.com/notional-labs/centauri/v3/x/mint/types"
minttypes "github.com/notional-labs/centauri/v4/x/mint/types"
"github.com/stretchr/testify/require"

ibctransfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
Expand Down
Loading

0 comments on commit e8470a8

Please sign in to comment.