test receiving and verifying incoming ibc packets
This commit is contained in:
parent
06492fa212
commit
555e0d8ec8
|
@ -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() {
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue