Skip to content

Commit

Permalink
basecoin/tests: send real tx, check balances (#471)
Browse files Browse the repository at this point in the history
  • Loading branch information
zramsay committed Feb 19, 2018
1 parent c9dd62d commit 0e93f69
Showing 1 changed file with 88 additions and 3 deletions.
91 changes: 88 additions & 3 deletions examples/basecoin/app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package app

import (
"encoding/json"
"fmt"
"os"
"testing"

Expand All @@ -28,7 +29,7 @@ func newBasecoinApp() *BasecoinApp {
func TestSendMsg(t *testing.T) {
bapp := newBasecoinApp()

// Construct a SendMsg.
// Construct a SendMsg
var msg = bank.SendMsg{
Inputs: []bank.Input{
{
Expand Down Expand Up @@ -65,7 +66,7 @@ func TestSendMsg(t *testing.T) {
func TestGenesis(t *testing.T) {
bapp := newBasecoinApp()

// construct some genesis bytes to reflect basecoin/types/AppAccount
// Construct some genesis bytes to reflect basecoin/types/AppAccount
pk := crypto.GenPrivKeyEd25519().PubKey()
addr := pk.Address()
coins, err := sdk.ParseCoins("77foocoin,99barcoin")
Expand All @@ -86,9 +87,93 @@ func TestGenesis(t *testing.T) {
vals := []abci.Validator{}
bapp.InitChain(abci.RequestInitChain{vals, stateBytes})

// a checkTx context
// A checkTx context
ctx := bapp.BaseApp.NewContext(true, abci.Header{})

res1 := bapp.accountMapper.GetAccount(ctx, baseAcc.Address)
assert.Equal(t, acc, res1)
}

func TestSendMsgWithAccounts(t *testing.T) {
bapp := newBasecoinApp()

// Construct some genesis bytes to reflect basecoin/types/AppAccount
// First key goes in genesis, used for sending
priv1 := crypto.GenPrivKeyEd25519()
pk1 := priv1.PubKey()
addr1 := pk1.Address()

// Second key receies
pk2 := crypto.GenPrivKeyEd25519().PubKey()
addr2 := pk2.Address()

// Give 77 foocoin to the first key
coins, err := sdk.ParseCoins("77foocoin")
require.Nil(t, err)
baseAcc := auth.BaseAccount{
Address: addr1,
Coins: coins,
}
acc1 := &types.AppAccount{baseAcc, "foobart"}

// Construct genesis state
genesisState := types.GenesisState{
Accounts: []*types.GenesisAccount{
types.NewGenesisAccount(acc1),
},
}
stateBytes, err := json.MarshalIndent(genesisState, "", "\t")

// Initialize the chain
vals := []abci.Validator{}
bapp.InitChain(abci.RequestInitChain{vals, stateBytes})

// A checkTx context (true)
ctxCheck := bapp.BaseApp.NewContext(true, abci.Header{})

res1 := bapp.accountMapper.GetAccount(ctxCheck, addr1)
assert.Equal(t, acc1, res1)

// Construct a SendMsg
var msg = bank.SendMsg{
Inputs: []bank.Input{
{
Address: crypto.Address(addr1),
Coins: sdk.Coins{{"foocoin", 10}},
Sequence: 1,
},
},
Outputs: []bank.Output{
{
Address: crypto.Address(addr2),
Coins: sdk.Coins{{"foocoin", 10}},
},
},
}

// Sign the tx
sig := priv1.Sign(msg.GetSignBytes())
tx := sdk.NewStdTx(msg, []sdk.StdSignature{{
PubKey: priv1.PubKey(),
Signature: sig,
}})

// Run a Check
res := bapp.Check(tx)
assert.Equal(t, sdk.CodeOK, res.Code, res.Log)

// Simulate a Block
bapp.BeginBlock(abci.RequestBeginBlock{})
res = bapp.Deliver(tx)
assert.Equal(t, sdk.CodeOK, res.Code, res.Log)

// A deliverTx context
ctxDeliver := bapp.BaseApp.NewContext(false, abci.Header{})

// Check balances
res2 := bapp.accountMapper.GetAccount(ctxDeliver, addr1)
res3 := bapp.accountMapper.GetAccount(ctxDeliver, addr2)

assert.Equal(t, fmt.Sprintf("%v", res2.GetCoins()), "67foocoin")
assert.Equal(t, fmt.Sprintf("%v", res3.GetCoins()), "10foocoin")
}

0 comments on commit 0e93f69

Please sign in to comment.