Skip to content

Commit

Permalink
test(vtransfer): pass IBC integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Mar 18, 2024
1 parent 96484b2 commit 7f3be7d
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 59 deletions.
13 changes: 7 additions & 6 deletions golang/cosmos/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ func NewAgoricApp(
ah := action.GetActionHeader()
ah.Type = "VTRANSFER_" + ah.Type

fmt.Println("@@@ vtransfer action", action)
// fmt.Println("@@@ vtransfer action", action)
return app.SwingSetKeeper.PushAction(ctx, action)
})
app.VtransferKeeper = vtransferkeeper.NewKeeper(appCodec, keys[vtransfer.StoreKey], vibcForVtransferKeeper)
Expand Down Expand Up @@ -572,7 +572,7 @@ func NewAgoricApp(
app.IBCKeeper.ChannelKeeper,
app.DistrKeeper,
app.BankKeeper,
app.VtransferKeeper.GetICS4Wrapper(),
app.IBCKeeper.ChannelKeeper,
)

app.TransferKeeper = ibctransferkeeper.NewKeeper(
Expand All @@ -587,30 +587,31 @@ func NewAgoricApp(
scopedTransferKeeper,
)

app.PacketForwardKeeper.SetTransferKeeper(app.TransferKeeper)

transferApp := transfer.NewAppModule(app.TransferKeeper)
var transferStack ibcporttypes.IBCModule
transferStack = transfer.NewIBCModule(app.TransferKeeper)
app.PacketForwardKeeper.SetTransferKeeper(app.TransferKeeper)
transferStack = vtransfer.NewIBCMiddleware(transferStack, app.VtransferKeeper)
transferStack = packetforward.NewIBCMiddleware(
transferStack,
app.PacketForwardKeeper,
0, // retries on timeout
packetforwardkeeper.DefaultForwardTransferPacketTimeoutTimestamp, // forward timeout
packetforwardkeeper.DefaultRefundTransferPacketTimeoutTimestamp, // refund timeout
)
transferStack = vtransfer.NewIBCMiddleware(transferStack, app.VtransferKeeper)

icaModule := ica.NewAppModule(nil, &app.ICAHostKeeper)
app.ICAHostKeeper = icahostkeeper.NewKeeper(
appCodec, keys[icahosttypes.StoreKey],
app.GetSubspace(icahosttypes.SubModuleName),
app.PacketForwardKeeper,
app.IBCKeeper.ChannelKeeper,
app.IBCKeeper.ChannelKeeper,
&app.IBCKeeper.PortKeeper,
app.AccountKeeper,
scopedICAHostKeeper,
app.MsgServiceRouter(),
)
icaModule := ica.NewAppModule(nil, &app.ICAHostKeeper)
icaHostIBCModule := icahost.NewIBCModule(app.ICAHostKeeper)

// create static IBC router, add transfer route, then set and seal it
Expand Down
11 changes: 2 additions & 9 deletions golang/cosmos/x/vtransfer/ibc_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,8 @@ func (im IBCMiddleware) OnRecvPacket(
packet channeltypes.Packet,
relayer sdk.AccAddress,
) exported.Acknowledgement {
var ack exported.Acknowledgement = im.app.OnRecvPacket(ctx, packet, relayer)
if ack != nil {
// Synchronous acknowledgement.
err := im.vtransferKeeper.ReceiveWriteAcknowledgement(ctx, packet, ack)
if err != nil {
return channeltypes.NewErrorAcknowledgement(err)
}
}
return nil
ack := im.app.OnRecvPacket(ctx, packet, relayer)
return im.vtransferKeeper.InterceptOnSendAck(ctx, packet, ack)
}

// OnTimeoutPacket implements the IBCModule interface.
Expand Down
127 changes: 123 additions & 4 deletions golang/cosmos/x/vtransfer/ibc_middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@ import (
"testing"

app "github.com/Agoric/agoric-sdk/golang/cosmos/app"
"github.com/Agoric/agoric-sdk/golang/cosmos/vm"
"github.com/cosmos/cosmos-sdk/store"
"github.com/stretchr/testify/suite"
"github.com/tendermint/tendermint/libs/log"
dbm "github.com/tendermint/tm-db"

swingsettypes "github.com/Agoric/agoric-sdk/golang/cosmos/x/swingset/types"
"github.com/Agoric/agoric-sdk/golang/cosmos/x/vbank"
vibckeeper "github.com/Agoric/agoric-sdk/golang/cosmos/x/vibc/keeper"

"github.com/cosmos/cosmos-sdk/baseapp"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
Expand Down Expand Up @@ -45,7 +50,7 @@ func SetupAgoricTestingApp() (ibctesting.TestingApp, map[string]json.RawMessage)
db := dbm.NewMemDB()
encCdc := app.MakeEncodingConfig()
controller := func(ctx context.Context, needReply bool, str string) (string, error) {
fmt.Printf("controller got: %s\n", str)
// fmt.Printf("controller got: %s\n", str)
// fmt.Fprintln(os.Stderr, "FIXME: Would upcall to controller with", str)
// FIXME: Unmarshal JSON and reply to the upcall.
jsonReply := `true`
Expand Down Expand Up @@ -126,6 +131,28 @@ func (s *IntegrationTestSuite) GetApp(chain *ibctesting.TestChain) *app.GaiaApp
return app
}

func (s *IntegrationTestSuite) PeekQueue(chain *ibctesting.TestChain, queuePath string) ([]string, error) {
app := s.GetApp(chain)
k := app.VstorageKeeper
ctx := chain.GetContext()
head, err := k.GetIntValue(ctx, queuePath+".head")
if err != nil {
return nil, err
}
tail, err := k.GetIntValue(ctx, queuePath+".tail")
if err != nil {
return nil, err
}
length := tail.Sub(head).Int64()
values := make([]string, length)
var i int64
for i = 0; i < length; i++ {
path := fmt.Sprintf("%s.%s", queuePath, head.Add(sdk.NewInt(i)).String())
values[i] = k.GetEntry(ctx, path).StringValue()
}
return values, nil
}

func (s *IntegrationTestSuite) NewTransferPath() *ibctesting.Path {
path := ibctesting.NewPath(s.chainA, s.chainB)
path.EndpointA.ChannelID = "channel-0"
Expand Down Expand Up @@ -158,18 +185,39 @@ func (s *IntegrationTestSuite) SetupContract() *ibctesting.Path {
return path
}

func (s *IntegrationTestSuite) checkQueue(qvalues []string, expected []swingsettypes.InboundQueueRecord) {
s.Equal(len(expected), len(qvalues))
for i, qv := range qvalues {
if i >= len(expected) {
break
}
var qr swingsettypes.InboundQueueRecord
err := json.Unmarshal([]byte(qv), &qr)
s.Require().NoError(err)
if expected[i].Context.TxHash == "" {
// Default the TxHash.
expected[i].Context.TxHash = qr.Context.TxHash
}

expi, err := json.Marshal(expected[i])
s.Require().NoError(err)
s.Equal(string(expi), qv)
}
}

func (s *IntegrationTestSuite) TestOnAcknowledgementPacket() {
path := s.NewTransferPath()
s.Require().Equal(path.EndpointA.ChannelID, "channel-0")

s.Run("OnReceiveTransferWithCallMemo", func() {
ibctransferAddress := authtypes.NewModuleAddress(ibctransfertypes.ModuleName).String()
s.Run("OnReceiveTransferToReceiverTarget", func() {
// create a transfer packet
transfer := ibctransfertypes.NewFungibleTokenPacketData(
"uosmo",
"1000000",
s.chainA.SenderAccount.GetAddress().String(),
s.chainB.SenderAccount.GetAddress().String(),
`{"invokeWriteAcknowledgement": "foo"}`,
`"This is a JSON memo"`,
)

// send a transfer packet
Expand All @@ -193,18 +241,89 @@ func (s *IntegrationTestSuite) TestOnAcknowledgementPacket() {
// commit the receive on chainB
s.coordinator.CommitBlock(s.chainA, s.chainB)

// Update Clients
// Update Clients
err = path.EndpointA.UpdateClient()
s.Require().NoError(err)
err = path.EndpointB.UpdateClient()
s.Require().NoError(err)
s.coordinator.CommitBlock(s.chainA, s.chainB)

{
qvalues, err := s.PeekQueue(s.chainA, "actionQueue")
s.Require().NoError(err)
expected := []swingsettypes.InboundQueueRecord{}

s.checkQueue(qvalues, expected)
}

{
qvalues, err := s.PeekQueue(s.chainB, "actionQueue")
s.Require().NoError(err)

ack := channeltypes.NewResultAcknowledgement([]byte(`{"result":"AQ=="}`))
expected := []swingsettypes.InboundQueueRecord{
{
Action: &vibckeeper.WriteAcknowledgementEvent{
ActionHeader: &vm.ActionHeader{
Type: "VTRANSFER_IBC_EVENT",
BlockHeight: 21,
BlockTime: 1577923395,
},
Event: "writeAcknowledgement",
Target: s.chainB.SenderAccount.GetAddress().String(),
Packet: channeltypes.Packet{
Sequence: 1,
SourcePort: "transfer",
SourceChannel: "channel-0",
DestinationPort: "transfer",
DestinationChannel: "channel-0",
Data: transfer.GetBytes(),
TimeoutHeight: timeoutHeight,
},
Acknowledgement: ack.GetResult(),
},
Context: swingsettypes.ActionContext{
BlockHeight: 21,
// TxHash is filled in below
MsgIdx: 0,
},
},
{
Action: &vbank.VbankBalanceUpdate{
ActionHeader: &vm.ActionHeader{
Type: "VBANK_BALANCE_UPDATE",
BlockHeight: 21,
BlockTime: 1577923395,
},
Nonce: 1,
Updated: []vbank.VbankSingleBalanceUpdate{
{
Address: ibctransferAddress,
Denom: "ibc/ED07A3391A112B175915CD8FAF43A2DA8E4790EDE12566649D0C2F97716B8518",
Amount: "0",
},
},
},
Context: swingsettypes.ActionContext{
BlockHeight: 21,
TxHash: "x/vbank",
MsgIdx: 0,
},
},
}

s.checkQueue(qvalues, expected)

// write out the acknowledgement verbatim from the transfer app.
s.GetApp(s.chainB).VtransferKeeper.ReceiveWriteAcknowledgement(s.chainB.GetContext(), packet, ack)

Check failure on line 318 in golang/cosmos/x/vtransfer/ibc_middleware_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint (no-failure)

Error return value of `.ReceiveWriteAcknowledgement` is not checked (errcheck)
}

// acknowledge the transfer packet
ack := s.chainB.GetAcknowledgement(packet)
s.Require().NotNil(ack)
err = path.EndpointB.AcknowledgePacket(packet, ack)
s.Require().NoError(err)

// commit the receive on chainA
// Update Clients
err = path.EndpointA.UpdateClient()
Expand Down
Loading

0 comments on commit 7f3be7d

Please sign in to comment.