cosmos-sdk/types/tx_test.go

127 lines
3.2 KiB
Go
Raw Normal View History

2016-03-20 03:00:43 -07:00
package types
import (
2017-03-23 17:51:50 -07:00
"fmt"
2016-03-20 03:00:43 -07:00
"testing"
2017-02-22 14:30:50 -08:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
2017-04-25 22:08:31 -07:00
data "github.com/tendermint/go-wire/data"
2016-03-20 03:00:43 -07:00
)
var chainID string = "test_chain"
func TestSendTxSignable(t *testing.T) {
sendTx := &SendTx{
2016-04-01 15:19:07 -07:00
Gas: 222,
Fee: Coin{"", 111},
2016-03-20 03:00:43 -07:00
Inputs: []TxInput{
TxInput{
Address: []byte("input1"),
2016-04-01 15:19:07 -07:00
Coins: Coins{{"", 12345}},
2016-03-20 03:00:43 -07:00
Sequence: 67890,
},
TxInput{
Address: []byte("input2"),
2016-04-01 15:19:07 -07:00
Coins: Coins{{"", 111}},
2016-03-20 03:00:43 -07:00
Sequence: 222,
},
},
Outputs: []TxOutput{
TxOutput{
Address: []byte("output1"),
2016-04-01 15:19:07 -07:00
Coins: Coins{{"", 333}},
2016-03-20 03:00:43 -07:00
},
TxOutput{
Address: []byte("output2"),
2016-04-01 15:19:07 -07:00
Coins: Coins{{"", 444}},
2016-03-20 03:00:43 -07:00
},
},
}
signBytes := sendTx.SignBytes(chainID)
2017-03-23 17:51:50 -07:00
signBytesHex := fmt.Sprintf("%X", signBytes)
expected := "010A746573745F636861696E0100000000000000DE00000000000000006F01020106696E7075743101010000000000000030390301093200000106696E70757432010100000000000000006F01DE0000010201076F757470757431010100000000000000014D01076F75747075743201010000000000000001BC"
2017-02-22 14:30:50 -08:00
2017-03-23 17:51:50 -07:00
assert.Equal(t, signBytesHex, expected,
2017-04-17 16:53:06 -07:00
"Got unexpected sign string for SendTx. Expected:\n%v\nGot:\n%v", expected, signBytesHex)
2016-03-20 03:00:43 -07:00
}
2016-03-27 12:47:50 -07:00
func TestAppTxSignable(t *testing.T) {
callTx := &AppTx{
2016-04-01 15:19:07 -07:00
Gas: 222,
Fee: Coin{"", 111},
2017-01-13 16:10:22 -08:00
Name: "X",
2016-03-20 03:00:43 -07:00
Input: TxInput{
Address: []byte("input1"),
2016-04-01 15:19:07 -07:00
Coins: Coins{{"", 12345}},
2016-03-20 03:00:43 -07:00
Sequence: 67890,
},
2016-03-27 12:47:50 -07:00
Data: []byte("data1"),
2016-03-20 03:00:43 -07:00
}
signBytes := callTx.SignBytes(chainID)
2017-03-23 17:51:50 -07:00
signBytesHex := fmt.Sprintf("%X", signBytes)
expected := "010A746573745F636861696E0100000000000000DE00000000000000006F0101580106696E70757431010100000000000000303903010932000001056461746131"
2017-02-22 14:30:50 -08:00
2017-03-23 17:51:50 -07:00
assert.Equal(t, signBytesHex, expected,
2017-04-17 16:53:06 -07:00
"Got unexpected sign string for SendTx. Expected:\n%v\nGot:\n%v", expected, signBytesHex)
}
func TestSendTxJSON(t *testing.T) {
2017-03-23 17:51:50 -07:00
assert, require := assert.New(t), require.New(t)
chainID := "test_chain_id"
test1PrivAcc := PrivAccountFromSecret("sendtx1")
test2PrivAcc := PrivAccountFromSecret("sendtx2")
// Construct a SendTx signature
tx := &SendTx{
Gas: 1,
Fee: Coin{"foo", 2},
Inputs: []TxInput{
NewTxInput(test1PrivAcc.PubKey, Coins{{"foo", 10}}, 1),
},
Outputs: []TxOutput{
TxOutput{
Address: test2PrivAcc.PubKey.Address(),
Coins: Coins{{"foo", 8}},
},
},
}
// serialize this as json and back
js, err := data.ToJSON(TxS{tx})
2017-03-23 17:51:50 -07:00
require.Nil(err)
// fmt.Println(string(js))
txs := TxS{}
err = data.FromJSON(js, &txs)
2017-03-23 17:51:50 -07:00
require.Nil(err)
tx2, ok := txs.Tx.(*SendTx)
2017-03-23 17:51:50 -07:00
require.True(ok)
// make sure they are the same!
signBytes := tx.SignBytes(chainID)
signBytes2 := tx2.SignBytes(chainID)
2017-03-23 17:51:50 -07:00
assert.Equal(signBytes, signBytes2)
assert.Equal(tx, tx2)
// sign this thing
sig := test1PrivAcc.Sign(signBytes)
// we handle both raw sig and wrapped sig the same
tx.SetSignature(test1PrivAcc.PubKey.Address(), sig)
tx2.SetSignature(test1PrivAcc.PubKey.Address(), sig)
2017-03-23 17:51:50 -07:00
assert.Equal(tx, tx2)
// let's marshal / unmarshal this with signature
js, err = data.ToJSON(TxS{tx})
2017-03-23 17:51:50 -07:00
require.Nil(err)
// fmt.Println(string(js))
err = data.FromJSON(js, &txs)
2017-03-23 17:51:50 -07:00
require.Nil(err)
tx2, ok = txs.Tx.(*SendTx)
2017-03-23 17:51:50 -07:00
require.True(ok)
// and make sure the sig is preserved
2017-03-23 17:51:50 -07:00
assert.Equal(tx, tx2)
assert.False(tx2.Inputs[0].Signature.Empty())
2016-03-20 03:00:43 -07:00
}