diff --git a/modules/coin/store.go b/modules/coin/store.go index f84ad2bef..9aa951340 100644 --- a/modules/coin/store.go +++ b/modules/coin/store.go @@ -12,6 +12,8 @@ import ( // GetAccount - Get account from store and address func GetAccount(store state.SimpleDB, addr basecoin.Actor) (Account, error) { + // if the actor is another chain, we use one address for the chain.... + addr = ChainAddr(addr) acct, err := loadAccount(store, addr.Bytes()) // for empty accounts, don't return an error, but rather an empty account @@ -23,12 +25,18 @@ func GetAccount(store state.SimpleDB, addr basecoin.Actor) (Account, error) { // CheckCoins makes sure there are funds, but doesn't change anything func CheckCoins(store state.SimpleDB, addr basecoin.Actor, coins Coins) (Coins, error) { + // if the actor is another chain, we use one address for the chain.... + addr = ChainAddr(addr) + acct, err := updateCoins(store, addr, coins) return acct.Coins, err } // ChangeCoins changes the money, returns error if it would be negative func ChangeCoins(store state.SimpleDB, addr basecoin.Actor, coins Coins) (Coins, error) { + // if the actor is another chain, we use one address for the chain.... + addr = ChainAddr(addr) + acct, err := updateCoins(store, addr, coins) if err != nil { return acct.Coins, err @@ -55,9 +63,6 @@ func ChainAddr(addr basecoin.Actor) basecoin.Actor { // // 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 c19bbf1ad..61e58e92b 100644 --- a/modules/ibc/ibc_test.go +++ b/modules/ibc/ibc_test.go @@ -391,6 +391,26 @@ func TestIBCPostPacket(t *testing.T) { _, err := app.DeliverTx(ctx, store, tx) require.Nil(err, "%+v", err) + // set up a rich guy on this chain + wealth := coin.Coins{{"btc", 300}, {"eth", 2000}, {"ltc", 5000}} + rich := coin.NewAccountWithKey(wealth) + _, err = app.SetOption(log.NewNopLogger(), store, + "coin", "account", rich.MakeOption()) + require.Nil(err, "%+v", err) + + // sends money to another guy on a different chain, now other chain has credit + buddy := basecoin.Actor{ChainID: otherID, App: auth.NameSigs, Address: []byte("dude")} + outTx := coin.NewSendOneTx(rich.Actor(), buddy, wealth) + _, err = app.DeliverTx(ctx.WithPermissions(rich.Actor()), store, outTx) + require.Nil(err, "%+v", err) + + // make sure the money moved to the other chain... + cstore := stack.PrefixedStore(coin.NameCoin, store) + acct, err := coin.GetAccount(cstore, coin.ChainAddr(buddy)) + require.Nil(err, "%+v", err) + require.Equal(wealth, acct.Coins) + + // these are the people for testing incoming ibc from the other chain recipient := basecoin.Actor{ChainID: ourID, App: auth.NameSigs, Address: []byte("bar")} sender := basecoin.Actor{ChainID: otherID, App: auth.NameSigs, Address: []byte("foo")} coinTx := coin.NewSendOneTx( @@ -399,14 +419,6 @@ 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{