aboutsummaryrefslogtreecommitdiffstats
path: root/tests/transaction_test_util.go
blob: 800891887addbb628cb2a8b4eec3522641ba3980 (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package tests

import (
    "bytes"
    "fmt"
    "math/big"
    "runtime"

    "github.com/ethereum/go-ethereum/common"
    "github.com/ethereum/go-ethereum/core/types"
    "github.com/ethereum/go-ethereum/rlp"
)

// Transaction Test JSON Format
type TtTransaction struct {
    Data     string
    GasLimit string
    GasPrice string
    Nonce    string
    R        string
    S        string
    To       string
    V        string
    Value    string
}

type TransactionTest struct {
    Rlp         string
    Sender      string
    Transaction TtTransaction
}

func RunTransactionTests(file string, notWorking  map[string]bool) error {
    bt := make(map[string]TransactionTest)
    if err := LoadJSON(file, &bt); err != nil {
        return err
    }
    for name, in := range bt {
        var err error
        // TODO: remove this, we currently ignore some tests which are broken
        if !notWorking[name] {
            if err = runTest(in); err != nil {
                return fmt.Errorf("bad test %s: %v", name, err)
            }
            fmt.Println("Test passed:", name)
        }
    }
    return nil
}

func runTest(txTest TransactionTest) (err error) {
    expectedSender, expectedTo, expectedData, rlpBytes, expectedGasLimit, expectedGasPrice, expectedValue, expectedR, expectedS, expectedNonce, expectedV, err := convertTestTypes(txTest)

    if err != nil {
        if txTest.Sender == "" { // tx is invalid and this is expected (test OK)
            return nil
        } else {
            return err // tx is invalid and this is NOT expected (test FAIL)
        }
    }
    tx := new(types.Transaction)
    rlp.DecodeBytes(rlpBytes, tx)

    sender, err := tx.From()
    if err != nil {
        return err
    }

    if expectedSender != sender {
        return fmt.Errorf("Sender mismatch: %v %v", expectedSender, sender)
    }
    if !bytes.Equal(expectedData, tx.Payload) {
        return fmt.Errorf("Tx input data mismatch: %#v %#v", expectedData, tx.Payload)
    }
    if expectedGasLimit.Cmp(tx.GasLimit) != 0 {
        return fmt.Errorf("GasLimit mismatch: %v %v", expectedGasLimit, tx.GasLimit)
    }
    if expectedGasPrice.Cmp(tx.Price) != 0 {
        return fmt.Errorf("GasPrice mismatch: %v %v", expectedGasPrice, tx.Price)
    }
    if expectedNonce != tx.AccountNonce {
        return fmt.Errorf("Nonce mismatch: %v %v", expectedNonce, tx.AccountNonce)
    }
    if expectedR.Cmp(tx.R) != 0 {
        return fmt.Errorf("R mismatch: %v %v", expectedR, tx.R)
    }
    if expectedS.Cmp(tx.S) != 0 {
        return fmt.Errorf("S mismatch: %v %v", expectedS, tx.S)
    }
    if expectedV != uint64(tx.V) {
        return fmt.Errorf("V mismatch: %v %v", expectedV, uint64(tx.V))
    }
    if expectedTo != *tx.Recipient {
        return fmt.Errorf("To mismatch: %v %v", expectedTo, *tx.Recipient)
    }
    if expectedValue.Cmp(tx.Amount) != 0 {
        return fmt.Errorf("Value mismatch: %v %v", expectedValue, tx.Amount)
    }

    return nil
}

func convertTestTypes(txTest TransactionTest) (sender, to common.Address,
    txInputData, rlpBytes []byte,
    gasLimit, gasPrice, value, r, s *big.Int,
    nonce, v uint64,
    err error) {

    defer func() {
        if recovered := recover(); recovered != nil {
            buf := make([]byte, 64<<10)
            buf = buf[:runtime.Stack(buf, false)]
            err = fmt.Errorf("%v\n%s", recovered, buf)
        }
    }()

    sender = mustConvertAddress(txTest.Sender)
    to = mustConvertAddress(txTest.Transaction.To)

    txInputData = mustConvertBytes(txTest.Transaction.Data)
    rlpBytes = mustConvertBytes(txTest.Rlp)

    gasLimit = mustConvertBigIntHex(txTest.Transaction.GasLimit)
    gasPrice = mustConvertBigIntHex(txTest.Transaction.GasPrice)
    value = mustConvertBigIntHex(txTest.Transaction.Value)

    r = common.Bytes2Big(mustConvertBytes(txTest.Transaction.R))
    s = common.Bytes2Big(mustConvertBytes(txTest.Transaction.S))

    nonce = mustConvertUintHex(txTest.Transaction.Nonce)
    v = mustConvertUintHex(txTest.Transaction.V)

    return sender, to, txInputData, rlpBytes, gasLimit, gasPrice, value, r, s, nonce, v, nil
}