github.com/eris-ltd/erisdb@v0.25.0/txs/amino_codec_test.go (about)

     1  package txs
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/hyperledger/burrow/acm"
     7  	"github.com/hyperledger/burrow/crypto"
     8  	"github.com/hyperledger/burrow/txs/payload"
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestAminoEncodeTxDecodeTx(t *testing.T) {
    14  	codec := NewAminoCodec()
    15  	inputAddress := crypto.Address{1, 2, 3, 4, 5}
    16  	outputAddress := crypto.Address{5, 4, 3, 2, 1}
    17  	amount := uint64(2)
    18  	sequence := uint64(3)
    19  	tx := &payload.SendTx{
    20  		Inputs: []*payload.TxInput{{
    21  			Address:  inputAddress,
    22  			Amount:   amount,
    23  			Sequence: sequence,
    24  		}},
    25  		Outputs: []*payload.TxOutput{{
    26  			Address: outputAddress,
    27  			Amount:  amount,
    28  		}},
    29  	}
    30  	txEnv := Enclose(chainID, tx)
    31  	txBytes, err := codec.EncodeTx(txEnv)
    32  	if err != nil {
    33  		t.Fatal(err)
    34  	}
    35  	txEnvOut, err := codec.DecodeTx(txBytes)
    36  	assert.NoError(t, err, "DecodeTx error")
    37  	assert.Equal(t, txEnv, txEnvOut)
    38  }
    39  
    40  func TestAminoEncodeTxDecodeTx_CallTx(t *testing.T) {
    41  	codec := NewAminoCodec()
    42  	inputAccount := acm.GeneratePrivateAccountFromSecret("fooo")
    43  	amount := uint64(2)
    44  	sequence := uint64(3)
    45  	tx := &payload.CallTx{
    46  		Input: &payload.TxInput{
    47  			Address:  inputAccount.GetAddress(),
    48  			Amount:   amount,
    49  			Sequence: sequence,
    50  		},
    51  		GasLimit: 233,
    52  		Fee:      2,
    53  		Address:  nil,
    54  		Data:     []byte("code"),
    55  	}
    56  	txEnv := Enclose(chainID, tx)
    57  	require.NoError(t, txEnv.Sign(inputAccount))
    58  	txBytes, err := codec.EncodeTx(txEnv)
    59  	if err != nil {
    60  		t.Fatal(err)
    61  	}
    62  	txEnvOut, err := codec.DecodeTx(txBytes)
    63  	assert.NoError(t, err, "DecodeTx error")
    64  	assert.Equal(t, txEnv, txEnvOut)
    65  }
    66  
    67  func TestAminoTxEnvelope(t *testing.T) {
    68  	codec := NewAminoCodec()
    69  	privAccFrom := acm.GeneratePrivateAccountFromSecret("foo")
    70  	privAccTo := acm.GeneratePrivateAccountFromSecret("bar")
    71  	toAddress := privAccTo.GetAddress()
    72  	txEnv := Enclose("testChain", payload.NewCallTxWithSequence(privAccFrom.GetPublicKey(), &toAddress,
    73  		[]byte{3, 4, 5, 5}, 343, 2323, 12, 3))
    74  	err := txEnv.Sign(privAccFrom)
    75  	require.NoError(t, err)
    76  
    77  	bs, err := codec.EncodeTx(txEnv)
    78  	require.NoError(t, err)
    79  	txEnvOut, err := codec.DecodeTx(bs)
    80  	require.NoError(t, err)
    81  	assert.Equal(t, txEnv, txEnvOut)
    82  }