aboutsummaryrefslogtreecommitdiffstats
path: root/core/types/transaction_test.go
blob: 6a015cb9a79375a474220ba31c415195ddc46630 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package types

import (
    "bytes"
    "crypto/ecdsa"
    "math/big"
    "testing"

    "github.com/ethereum/go-ethereum/common"
    "github.com/ethereum/go-ethereum/crypto"
    "github.com/ethereum/go-ethereum/rlp"
)

// The values in those tests are from the Transaction Tests
// at github.com/ethereum/tests.

var (
    emptyTx = NewTransactionMessage(
        common.HexToAddress("095e7baea6a6c7c4c2dfeb977efac326af552d87"),
        big.NewInt(0), big.NewInt(0), big.NewInt(0),
        nil,
    )

    rightvrsRecipient = common.HexToAddress("b94f5374fce5edbc8e2a8697c15331677e6ebf0b")
    rightvrsTx        = &Transaction{
        Recipient:    &rightvrsRecipient,
        AccountNonce: 3,
        Price:        big.NewInt(1),
        GasLimit:     big.NewInt(2000),
        Amount:       big.NewInt(10),
        Payload:      common.FromHex("5544"),
        V:            28,
        R:            common.FromHex("98ff921201554726367d2be8c804a7ff89ccf285ebc57dff8ae4c44b9c19ac4a"),
        S:            common.FromHex("8887321be575c8095f789dd4c743dfe42c1820f9231f98a962b210e3ac2452a3"),
    }
)

func TestTransactionHash(t *testing.T) {
    // "EmptyTransaction"
    if emptyTx.Hash() != common.HexToHash("c775b99e7ad12f50d819fcd602390467e28141316969f4b57f0626f74fe3b386") {
        t.Errorf("empty transaction hash mismatch, got %x", emptyTx.Hash())
    }

    // "RightVRSTest"
    if rightvrsTx.Hash() != common.HexToHash("fe7a79529ed5f7c3375d06b26b186a8644e0e16c373d7a12be41c62d6042b77a") {
        t.Errorf("RightVRS transaction hash mismatch, got %x", rightvrsTx.Hash())
    }
}

func TestTransactionEncode(t *testing.T) {
    // "RightVRSTest"
    txb, err := rlp.EncodeToBytes(rightvrsTx)
    if err != nil {
        t.Fatalf("encode error: %v", err)
    }
    should := common.FromHex("f86103018207d094b94f5374fce5edbc8e2a8697c15331677e6ebf0b0a8255441ca098ff921201554726367d2be8c804a7ff89ccf285ebc57dff8ae4c44b9c19ac4aa08887321be575c8095f789dd4c743dfe42c1820f9231f98a962b210e3ac2452a3")
    if !bytes.Equal(txb, should) {
        t.Errorf("encoded RLP mismatch, got %x", txb)
    }
}

func decodeTx(data []byte) (*Transaction, error) {
    var tx Transaction
    return &tx, rlp.Decode(bytes.NewReader(data), &tx)
}

func defaultTestKey() (*ecdsa.PrivateKey, []byte) {
    key := crypto.ToECDSA(common.Hex2Bytes("45a915e4d060149eb4365960e6a7a45f334393093061116b197e3240065ff2d8"))
    addr := crypto.PubkeyToAddress(key.PublicKey)
    return key, addr
}

func TestRecipientEmpty(t *testing.T) {
    _, addr := defaultTestKey()

    tx, err := decodeTx(common.Hex2Bytes("f8498080808080011ca09b16de9d5bdee2cf56c28d16275a4da68cd30273e2525f3959f5d62557489921a0372ebd8fb3345f7db7b5a86d42e24d36e983e259b0664ceb8c227ec9af572f3d"))
    if err != nil {
        t.Error(err)
        t.FailNow()
    }

    from, err := tx.From()
    if err != nil {
        t.Error(err)
        t.FailNow()
    }

    if !bytes.Equal(addr, from.Bytes()) {
        t.Error("derived address doesn't match")
    }
}

func TestRecipientNormal(t *testing.T) {
    _, addr := defaultTestKey()

    tx, err := decodeTx(common.Hex2Bytes("f85d80808094000000000000000000000000000000000000000080011ca0527c0d8f5c63f7b9f41324a7c8a563ee1190bcbf0dac8ab446291bdbf32f5c79a0552c4ef0a09a04395074dab9ed34d3fbfb843c2f2546cc30fe89ec143ca94ca6"))
    if err != nil {
        t.Error(err)
        t.FailNow()
    }

    from, err := tx.From()
    if err != nil {
        t.Error(err)
        t.FailNow()
    }

    if !bytes.Equal(addr, from.Bytes()) {
        t.Error("derived address doesn't match")
    }
}