cosmos-sdk/examples/democoin/x/pow/handler_test.go

59 lines
1.5 KiB
Go
Raw Normal View History

package pow
import (
"testing"
"github.com/stretchr/testify/assert"
abci "github.com/tendermint/abci/types"
sdk "github.com/cosmos/cosmos-sdk/types"
2018-04-07 00:02:00 -07:00
wire "github.com/cosmos/cosmos-sdk/wire"
auth "github.com/cosmos/cosmos-sdk/x/auth"
bank "github.com/cosmos/cosmos-sdk/x/bank"
)
func TestPowHandler(t *testing.T) {
ms, capKey := setupMultiStore()
2018-04-07 00:02:00 -07:00
cdc := wire.NewCodec()
auth.RegisterBaseAccount(cdc)
2018-04-07 00:02:00 -07:00
am := auth.NewAccountMapper(cdc, capKey, &auth.BaseAccount{})
ctx := sdk.NewContext(ms, abci.Header{}, false, nil)
2018-04-18 21:49:24 -07:00
config := NewConfig("pow", int64(1))
ck := bank.NewKeeper(am)
keeper := NewKeeper(capKey, config, ck, DefaultCodespace)
2018-03-24 13:41:26 -07:00
handler := keeper.Handler
addr := sdk.Address([]byte("sender"))
count := uint64(1)
difficulty := uint64(2)
2018-04-05 06:16:54 -07:00
2018-04-18 21:49:24 -07:00
err := keeper.InitGenesis(ctx, Genesis{uint64(1), uint64(0)})
2018-04-05 06:16:54 -07:00
assert.Nil(t, err)
nonce, proof := mine(addr, count, difficulty)
2018-04-18 21:49:24 -07:00
msg := NewMsgMine(addr, difficulty, count, nonce, proof)
result := handler(ctx, msg)
assert.Equal(t, result, sdk.Result{})
2018-03-24 13:41:26 -07:00
newDiff, err := keeper.GetLastDifficulty(ctx)
assert.Nil(t, err)
assert.Equal(t, newDiff, uint64(2))
2018-03-24 13:41:26 -07:00
newCount, err := keeper.GetLastCount(ctx)
assert.Nil(t, err)
assert.Equal(t, newCount, uint64(1))
// todo assert correct coin change, awaiting https://github.com/cosmos/cosmos-sdk/pull/691
difficulty = uint64(4)
nonce, proof = mine(addr, count, difficulty)
2018-04-18 21:49:24 -07:00
msg = NewMsgMine(addr, difficulty, count, nonce, proof)
result = handler(ctx, msg)
assert.NotEqual(t, result, sdk.Result{})
}