cosmos-sdk/tests/tmsp/tmsp_test.go

157 lines
4.3 KiB
Go
Raw Normal View History

2017-01-15 15:16:18 -08:00
package tmsp_test
2016-03-24 11:27:44 -07:00
import (
2017-02-24 14:25:48 -08:00
"encoding/json"
"testing"
2016-03-24 11:27:44 -07:00
2017-02-24 14:25:48 -08:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
2017-01-15 15:16:18 -08:00
"github.com/tendermint/basecoin/app"
2016-03-24 11:27:44 -07:00
"github.com/tendermint/basecoin/types"
2017-04-25 22:08:31 -07:00
cmn "github.com/tendermint/tmlibs/common"
2016-03-24 11:27:44 -07:00
"github.com/tendermint/go-wire"
eyescli "github.com/tendermint/merkleeyes/client"
)
func TestSendTx(t *testing.T) {
2017-01-28 09:29:32 -08:00
eyesCli := eyescli.NewLocalClient("", 0)
2016-04-18 08:09:19 -07:00
chainID := "test_chain_id"
2017-01-15 15:16:18 -08:00
bcApp := app.NewBasecoin(eyesCli)
2017-03-14 10:55:46 -07:00
bcApp.SetOption("base/chain_id", chainID)
2017-02-24 14:25:48 -08:00
// t.Log(bcApp.Info())
2016-03-24 11:27:44 -07:00
test1PrivAcc := types.PrivAccountFromSecret("test1")
test2PrivAcc := types.PrivAccountFromSecret("test2")
2016-03-24 11:27:44 -07:00
// Seed Basecoin with account
2016-04-18 08:09:19 -07:00
test1Acc := test1PrivAcc.Account
test1Acc.Balance = types.Coins{{"", 1000}}
2017-02-24 14:25:48 -08:00
accOpt, err := json.Marshal(test1Acc)
require.Nil(t, err)
bcApp.SetOption("base/account", string(accOpt))
2016-03-24 11:27:44 -07:00
// Construct a SendTx signature
tx := &types.SendTx{
2016-04-01 15:19:07 -07:00
Gas: 0,
Fee: types.Coin{"", 0},
2016-03-24 11:27:44 -07:00
Inputs: []types.TxInput{
types.NewTxInput(test1PrivAcc.Account.PubKey, types.Coins{{"", 1}}, 1),
2016-03-24 11:27:44 -07:00
},
Outputs: []types.TxOutput{
types.TxOutput{
2016-04-18 08:09:19 -07:00
Address: test2PrivAcc.Account.PubKey.Address(),
2016-04-01 15:19:07 -07:00
Coins: types.Coins{{"", 1}},
2016-03-24 11:27:44 -07:00
},
},
}
// Sign request
2016-04-18 08:09:19 -07:00
signBytes := tx.SignBytes(chainID)
2017-02-24 14:25:48 -08:00
// t.Log("Sign bytes: %X\n", signBytes)
sig := test1PrivAcc.Sign(signBytes)
tx.Inputs[0].Signature = sig
2017-02-24 14:25:48 -08:00
// t.Log("Signed TX bytes: %X\n", wire.BinaryBytes(types.TxS{tx}))
2016-03-24 11:27:44 -07:00
// Write request
txBytes := wire.BinaryBytes(types.TxS{tx})
2017-01-15 15:31:26 -08:00
res := bcApp.DeliverTx(txBytes)
2017-02-24 14:25:48 -08:00
// t.Log(res)
2017-03-28 13:32:55 -07:00
assert.True(t, res.IsOK(), "Failed: %v", res.Error())
}
func TestSequence(t *testing.T) {
2017-01-28 09:29:32 -08:00
eyesCli := eyescli.NewLocalClient("", 0)
2016-03-28 09:35:19 -07:00
chainID := "test_chain_id"
2017-01-15 15:16:18 -08:00
bcApp := app.NewBasecoin(eyesCli)
2017-03-14 10:55:46 -07:00
bcApp.SetOption("base/chain_id", chainID)
2017-02-24 14:25:48 -08:00
// t.Log(bcApp.Info())
2016-03-28 09:35:19 -07:00
2016-04-18 08:09:19 -07:00
// Get the test account
test1PrivAcc := types.PrivAccountFromSecret("test1")
2016-04-18 08:09:19 -07:00
test1Acc := test1PrivAcc.Account
test1Acc.Balance = types.Coins{{"", 1 << 53}}
2017-02-24 14:25:48 -08:00
accOpt, err := json.Marshal(test1Acc)
require.Nil(t, err)
bcApp.SetOption("base/account", string(accOpt))
2016-03-28 09:35:19 -07:00
sequence := int(1)
// Make a bunch of PrivAccounts
privAccounts := types.RandAccounts(1000, 1000000, 0)
2016-03-28 09:35:19 -07:00
privAccountSequences := make(map[string]int)
// Send coins to each account
2017-01-12 12:25:04 -08:00
2016-03-28 09:35:19 -07:00
for i := 0; i < len(privAccounts); i++ {
privAccount := privAccounts[i]
2017-01-12 12:25:04 -08:00
2016-03-28 09:35:19 -07:00
tx := &types.SendTx{
2017-01-12 14:46:18 -08:00
Gas: 2,
Fee: types.Coin{"", 2},
2017-01-12 14:46:18 -08:00
Inputs: []types.TxInput{
types.NewTxInput(test1Acc.PubKey, types.Coins{{"", 1000002}}, sequence),
2017-01-12 14:46:18 -08:00
},
2016-03-28 09:35:19 -07:00
Outputs: []types.TxOutput{
types.TxOutput{
Address: privAccount.Account.PubKey.Address(),
2016-04-01 15:19:07 -07:00
Coins: types.Coins{{"", 1000000}},
2016-03-28 09:35:19 -07:00
},
},
}
sequence += 1
// Sign request
signBytes := tx.SignBytes(chainID)
sig := test1PrivAcc.Sign(signBytes)
tx.Inputs[0].Signature = sig
// t.Log("ADDR: %X -> %X\n", tx.Inputs[0].Address, tx.Outputs[0].Address)
2016-03-28 09:35:19 -07:00
// Write request
2016-03-30 13:20:55 -07:00
txBytes := wire.BinaryBytes(struct{ types.Tx }{tx})
2017-01-14 20:42:47 -08:00
res := bcApp.DeliverTx(txBytes)
2017-03-28 13:32:55 -07:00
assert.True(t, res.IsOK(), "DeliverTx error: %v", res.Error())
2016-03-28 09:35:19 -07:00
}
2017-01-15 15:31:26 -08:00
res := bcApp.Commit()
2017-03-28 13:32:55 -07:00
assert.True(t, res.IsOK(), "Failed Commit: %v", res.Error())
2017-01-12 12:25:04 -08:00
2017-01-15 15:31:26 -08:00
t.Log("-------------------- RANDOM SENDS --------------------")
2016-03-28 09:35:19 -07:00
// Now send coins between these accounts
2016-04-17 12:41:26 -07:00
for i := 0; i < 10000; i++ {
randA := cmn.RandInt() % len(privAccounts)
randB := cmn.RandInt() % len(privAccounts)
2016-03-28 09:35:19 -07:00
if randA == randB {
continue
}
privAccountA := privAccounts[randA]
privAccountASequence := privAccountSequences[privAccountA.Account.PubKey.KeyString()]
privAccountSequences[privAccountA.Account.PubKey.KeyString()] = privAccountASequence + 1
privAccountB := privAccounts[randB]
tx := &types.SendTx{
2016-04-01 15:19:07 -07:00
Gas: 2,
Fee: types.Coin{"", 2},
2016-03-28 09:35:19 -07:00
Inputs: []types.TxInput{
types.NewTxInput(privAccountA.PubKey, types.Coins{{"", 3}}, privAccountASequence+1),
2016-03-28 09:35:19 -07:00
},
Outputs: []types.TxOutput{
types.TxOutput{
Address: privAccountB.PubKey.Address(),
2016-04-01 15:19:07 -07:00
Coins: types.Coins{{"", 1}},
2016-03-28 09:35:19 -07:00
},
},
}
// Sign request
signBytes := tx.SignBytes(chainID)
sig := privAccountA.Sign(signBytes)
tx.Inputs[0].Signature = sig
// t.Log("ADDR: %X -> %X\n", tx.Inputs[0].Address, tx.Outputs[0].Address)
2016-03-28 09:35:19 -07:00
// Write request
2016-03-30 13:20:55 -07:00
txBytes := wire.BinaryBytes(struct{ types.Tx }{tx})
2017-01-14 20:42:47 -08:00
res := bcApp.DeliverTx(txBytes)
2017-03-28 13:32:55 -07:00
assert.True(t, res.IsOK(), "DeliverTx error: %v", res.Error())
2016-03-28 09:35:19 -07:00
}
}