diff options
author | bas-vk <bas-vk@users.noreply.github.com> | 2017-01-27 04:16:24 +0800 |
---|---|---|
committer | Felix Lange <fjl@users.noreply.github.com> | 2017-01-27 04:16:24 +0800 |
commit | c46c41eae35e3083090e5806932fc5076e53e37c (patch) | |
tree | f8f9e6f48b32fad111c5a3378729487b091930c9 /core | |
parent | 82aa5b1de69331a43f5050717d593aa97eda1ef5 (diff) | |
download | go-tangerine-c46c41eae35e3083090e5806932fc5076e53e37c.tar.gz go-tangerine-c46c41eae35e3083090e5806932fc5076e53e37c.tar.zst go-tangerine-c46c41eae35e3083090e5806932fc5076e53e37c.zip |
core/types: add unittest for tx json serialization (#3609)
Diffstat (limited to 'core')
-rw-r--r-- | core/types/transaction_test.go | 44 |
1 files changed, 43 insertions, 1 deletions
diff --git a/core/types/transaction_test.go b/core/types/transaction_test.go index f52f80d34..6e4519c04 100644 --- a/core/types/transaction_test.go +++ b/core/types/transaction_test.go @@ -19,6 +19,7 @@ package types import ( "bytes" "crypto/ecdsa" + "encoding/json" "math/big" "testing" @@ -29,7 +30,6 @@ import ( // The values in those tests are from the Transaction Tests // at github.com/ethereum/tests. - var ( emptyTx = NewTransaction( 0, @@ -190,3 +190,45 @@ func TestTransactionPriceNonceSort(t *testing.T) { } } } + +// TestTransactionJSON tests serializing/de-serializing to/from JSON. +func TestTransactionJSON(t *testing.T) { + key, err := crypto.GenerateKey() + if err != nil { + t.Fatalf("could not generate key: %v", err) + } + signer := NewEIP155Signer(common.Big1) + + for i := uint64(0); i < 25; i++ { + var tx *Transaction + switch i % 2 { + case 0: + tx = NewTransaction(i, common.Address{1}, common.Big0, common.Big1, common.Big2, []byte("abcdef")) + case 1: + tx = NewContractCreation(i, common.Big0, common.Big1, common.Big2, []byte("abcdef")) + } + + tx, err := SignTx(tx, signer, key) + if err != nil { + t.Fatalf("could not sign transaction: %v", err) + } + + data, err := json.Marshal(tx) + if err != nil { + t.Errorf("json.Marshal failed: %v", err) + } + + var parsedTx *Transaction + if err := json.Unmarshal(data, &parsedTx); err != nil { + t.Errorf("json.Unmarshal failed: %v", err) + } + + // compare nonce, price, gaslimit, recipient, amount, payload, V, R, S + if tx.Hash() != parsedTx.Hash() { + t.Errorf("parsed tx differs from original tx, want %v, got %v", tx, parsedTx) + } + if tx.ChainId().Cmp(parsedTx.ChainId()) != 0 { + t.Errorf("invalid chain id, want %d, got %d", tx.ChainId(), parsedTx.ChainId()) + } + } +} |