From 555e0d8ec8ffdb9fd645748d74b99274d983939c Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Wed, 19 Jul 2017 20:25:36 +0200 Subject: [PATCH] test receiving and verifying incoming ibc packets --- modules/coin/store.go | 16 ++++++++++++++++ modules/ibc/ibc_test.go | 23 +++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/modules/coin/store.go b/modules/coin/store.go index 4d1d4c1090..f84ad2bef9 100644 --- a/modules/coin/store.go +++ b/modules/coin/store.go @@ -38,10 +38,26 @@ func ChangeCoins(store state.SimpleDB, addr basecoin.Actor, coins Coins) (Coins, return acct.Coins, err } +// ChainAddr collapses all addresses from another chain into one, so we can +// keep an over-all balance +// +// TODO: is there a better way to do this? +func ChainAddr(addr basecoin.Actor) basecoin.Actor { + if addr.ChainID == "" { + return addr + } + addr.App = "" + addr.Address = nil + return addr +} + // updateCoins will load the account, make all checks, and return the updated account. // // it doesn't save anything, that is up to you to decide (Check/Change Coins) func updateCoins(store state.SimpleDB, addr basecoin.Actor, coins Coins) (acct Account, err error) { + // if the actor is another chain, we use one address for the chain.... + addr = ChainAddr(addr) + acct, err = loadAccount(store, addr.Bytes()) // we can increase an empty account... if IsNoAccountErr(err) && coins.IsPositive() { diff --git a/modules/ibc/ibc_test.go b/modules/ibc/ibc_test.go index f23ad02ac1..c19bbf1add 100644 --- a/modules/ibc/ibc_test.go +++ b/modules/ibc/ibc_test.go @@ -355,6 +355,7 @@ func makePostPacket(tree *iavl.IAVLTree, packet Packet, fromID string, fromHeigh Packet: packet, } } + func updateChain(app basecoin.Handler, store state.KVStore, keys certifiers.ValKeys, chain string, h int, appHash []byte) error { seed := genEmptySeed(keys, chain, h, appHash, len(keys)) @@ -398,6 +399,14 @@ func TestIBCPostPacket(t *testing.T) { coin.Coins{{"eth", 100}, {"ltc", 300}}, ) + // set some cash on this chain (TODO: via set options...) + otherAddr := coin.ChainAddr(sender) + acct := coin.Account{ + Coins: coin.Coins{{"btc", 300}, {"eth", 2000}, {"ltc", 5000}}, + } + cstore := stack.PrefixedStore(coin.NameCoin, store) + cstore.Set(otherAddr.Bytes(), wire.BinaryBytes(acct)) + // make proofs for some packets.... tree := iavl.NewIAVLTree(0, nil) pbad := Packet{ @@ -419,6 +428,13 @@ func TestIBCPostPacket(t *testing.T) { Permissions: basecoin.Actors{sender}, Tx: coinTx, } + // this sends money we don't have registered + p2 := Packet{ + DestChain: ourID, + Sequence: 2, + Permissions: basecoin.Actors{sender}, + Tx: coin.NewSendOneTx(sender, recipient, coin.Coins{{"missing", 20}}), + } packet0 := makePostPacket(tree, p0, otherID, start+5) err = updateChain(app, store, keys, otherID, start+5, tree.Hash()) @@ -434,6 +450,10 @@ func TestIBCPostPacket(t *testing.T) { packet1badProof := packet1 packet1badProof.Key = []byte("random-data") + packet2 := makePostPacket(tree, p2, otherID, start+50) + err = updateChain(app, store, keys, otherID, start+50, tree.Hash()) + require.Nil(err, "%+v", err) + ibcPerm := basecoin.Actors{AllowIBC(coin.NameCoin)} cases := []struct { packet PostPacketTx @@ -463,6 +483,9 @@ func TestIBCPostPacket(t *testing.T) { // repeat -> error {packet0, ibcPerm, IsPacketAlreadyExistsErr}, + + // packet 2 attempts to spend money this chain doesn't have + {packet2, ibcPerm, coin.IsInsufficientFundsErr}, } for i, tc := range cases {