Skip to content

Commit

Permalink
merge main
Browse files Browse the repository at this point in the history
  • Loading branch information
GNaD13 committed Jul 17, 2023
2 parents 382f999 + f422fb5 commit 4677af9
Show file tree
Hide file tree
Showing 19 changed files with 1,699 additions and 8 deletions.
15 changes: 12 additions & 3 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,18 @@ import (

ibctestingtypes "github.com/cosmos/ibc-go/v7/testing/types"

ibc_hooks "github.com/notional-labs/centauri/v3/x/ibc-hooks"
ibchookstypes "github.com/notional-labs/centauri/v3/x/ibc-hooks/types"

"github.com/CosmWasm/wasmd/x/wasm"
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
)

const (
Name = "centauri"
dirName = "banksy"
ForkHeight = 244008
Name = "centauri"
dirName = "banksy"
ForkHeight = 244008
)

var (
Expand Down Expand Up @@ -191,6 +194,7 @@ var (
wasm08.AppModuleBasic{},
wasm.AppModuleBasic{},
router.AppModuleBasic{},
ibc_hooks.AppModuleBasic{},
transfermiddleware.AppModuleBasic{},
consensus.AppModuleBasic{},
alliancemodule.AppModuleBasic{},
Expand Down Expand Up @@ -305,6 +309,7 @@ func NewCentauriApp(
routerModule := router.NewAppModule(app.RouterKeeper)
transfermiddlewareModule := transfermiddleware.NewAppModule(&app.TransferMiddlewareKeeper)
icqModule := icq.NewAppModule(app.ICQKeeper)
ibcHooksModule := ibc_hooks.NewAppModule()
/**** Module Options ****/

// NOTE: we may consider parsing `appOpts` inside module constructors. For the moment
Expand Down Expand Up @@ -337,6 +342,7 @@ func NewCentauriApp(
params.NewAppModule(app.ParamsKeeper),
transferModule,
icqModule,
ibcHooksModule,
consensus.NewAppModule(appCodec, app.ConsensusParamsKeeper),
wasm08.NewAppModule(app.Wasm08Keeper),
wasm.NewAppModule(appCodec, &app.WasmKeeper, app.StakingKeeper, app.AccountKeeper, app.BankKeeper, app.MsgServiceRouter(), app.GetSubspace(wasmtypes.ModuleName)),
Expand All @@ -363,6 +369,7 @@ func NewCentauriApp(
ibctransfertypes.ModuleName,
routertypes.ModuleName,
transfermiddlewaretypes.ModuleName,
ibchookstypes.ModuleName,
icqtypes.ModuleName,
authtypes.ModuleName,
banktypes.ModuleName,
Expand Down Expand Up @@ -399,6 +406,7 @@ func NewCentauriApp(
ibchost.ModuleName,
routertypes.ModuleName,
transfermiddlewaretypes.ModuleName,
ibchookstypes.ModuleName,
ibctransfertypes.ModuleName,
icqtypes.ModuleName,
consensusparamtypes.ModuleName,
Expand Down Expand Up @@ -432,6 +440,7 @@ func NewCentauriApp(
icqtypes.ModuleName,
routertypes.ModuleName,
transfermiddlewaretypes.ModuleName,
ibchookstypes.ModuleName,
feegrant.ModuleName,
group.ModuleName,
consensusparamtypes.ModuleName,
Expand Down
90 changes: 90 additions & 0 deletions app/ibctesting/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package ibctesting

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

Expand All @@ -19,12 +21,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 @@ -43,6 +53,7 @@ import (
"github.com/notional-labs/centauri/v3/app/ibctesting/simapp"
routerKeeper "github.com/notional-labs/centauri/v3/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 @@ -621,6 +632,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 +719,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 +747,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
30 changes: 28 additions & 2 deletions app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
porttypes "github.com/cosmos/ibc-go/v7/modules/core/05-port/types"

authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
Expand Down Expand Up @@ -87,6 +88,10 @@ import (
storetypes "github.com/cosmos/cosmos-sdk/store/types"
wasm08Keeper "github.com/cosmos/ibc-go/v7/modules/light-clients/08-wasm/keeper"
wasmtypes "github.com/cosmos/ibc-go/v7/modules/light-clients/08-wasm/types"

ibc_hooks "github.com/notional-labs/centauri/v3/x/ibc-hooks"
ibchookskeeper "github.com/notional-labs/centauri/v3/x/ibc-hooks/keeper"
ibchookstypes "github.com/notional-labs/centauri/v3/x/ibc-hooks/types"
)

const (
Expand Down Expand Up @@ -120,6 +125,9 @@ type AppKeepers struct {
GroupKeeper groupkeeper.Keeper
Wasm08Keeper wasm08Keeper.Keeper // TODO: use this name ?
WasmKeeper wasm.Keeper
IBCHooksKeeper *ibchookskeeper.Keeper
Ics20WasmHooks *ibc_hooks.WasmHooks
HooksICS4Wrapper ibc_hooks.ICS4Middleware
// make scoped keepers public for test purposes
ScopedIBCKeeper capabilitykeeper.ScopedKeeper
ScopedTransferKeeper capabilitykeeper.ScopedKeeper
Expand Down Expand Up @@ -214,12 +222,26 @@ func (appKeepers *AppKeepers) InitNormalKeepers(
)

appKeepers.Wasm08Keeper = wasm08Keeper.NewKeeper(appCodec, appKeepers.keys[wasmtypes.StoreKey], authorityAddress, homePath)

// Create Transfer Keepers
hooksKeeper := ibchookskeeper.NewKeeper(
appKeepers.keys[ibchookstypes.StoreKey],
)
appKeepers.IBCHooksKeeper = &hooksKeeper

centauriPrefix := sdk.GetConfig().GetBech32AccountAddrPrefix()
wasmHooks := ibc_hooks.NewWasmHooks(&hooksKeeper, nil, centauriPrefix) // The contract keeper needs to be set later
appKeepers.Ics20WasmHooks = &wasmHooks
appKeepers.HooksICS4Wrapper = ibc_hooks.NewICS4Middleware(
appKeepers.IBCKeeper.ChannelKeeper,
appKeepers.Ics20WasmHooks,
)

appKeepers.TransferMiddlewareKeeper = transfermiddlewarekeeper.NewKeeper(
appKeepers.keys[transfermiddlewaretypes.StoreKey],
appKeepers.GetSubspace(transfermiddlewaretypes.ModuleName),
appCodec,
appKeepers.IBCKeeper.ChannelKeeper,
&appKeepers.HooksICS4Wrapper,
&appKeepers.TransferKeeper,
appKeepers.BankKeeper,
authorityAddress,
Expand Down Expand Up @@ -272,6 +294,8 @@ func (appKeepers *AppKeepers) InitNormalKeepers(
routerkeeper.DefaultRefundTransferPacketTimeoutTimestamp,
)

hooksTransferMiddleware := ibc_hooks.NewIBCMiddleware(ibcMiddlewareStack, &appKeepers.HooksICS4Wrapper)

// Create evidence Keeper for to register the IBC light client misbehaviour evidence route
evidenceKeeper := evidencekeeper.NewKeeper(
appCodec, appKeepers.keys[evidencetypes.StoreKey], appKeepers.StakingKeeper, appKeepers.SlashingKeeper,
Expand Down Expand Up @@ -309,6 +333,8 @@ func (appKeepers *AppKeepers) InitNormalKeepers(
wasmOpts...,
)

appKeepers.Ics20WasmHooks.ContractKeeper = &appKeepers.WasmKeeper

// Register Gov (must be registered after stakeibc)
govRouter := govtypesv1beta1.NewRouter()
govRouter.AddRoute(govtypes.RouterKey, govtypesv1beta1.ProposalHandler).
Expand Down Expand Up @@ -337,7 +363,7 @@ func (appKeepers *AppKeepers) InitNormalKeepers(
)

ibcRouter := porttypes.NewRouter()
ibcRouter.AddRoute(ibctransfertypes.ModuleName, ibcMiddlewareStack)
ibcRouter.AddRoute(ibctransfertypes.ModuleName, hooksTransferMiddleware)
ibcRouter.AddRoute(icqtypes.ModuleName, icqIBCModule)
ibcRouter.AddRoute(wasm.ModuleName, wasm.NewIBCHandler(appKeepers.WasmKeeper, appKeepers.IBCKeeper.ChannelKeeper, appKeepers.IBCKeeper.ChannelKeeper))

Expand Down
Loading

0 comments on commit 4677af9

Please sign in to comment.