aboutsummaryrefslogtreecommitdiffstats
path: root/rpc/args_test.go
blob: 380ed176b32e3e1b313110e846259a0a7521ecc2 (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
package rpc

import (
    "bytes"
    "encoding/json"
    "math/big"
    "testing"
)

func TestSha3(t *testing.T) {
    input := `["0x68656c6c6f20776f726c64"]`
    expected := "0x68656c6c6f20776f726c64"

    args := new(Sha3Args)
    json.Unmarshal([]byte(input), &args)

    if args.Data != expected {
        t.Error("got %s expected %s", input, expected)
    }
}

func TestGetBalanceArgs(t *testing.T) {
    input := `["0x407d73d8a49eeb85d32cf465507dd71d507100c1", "0x1f"]`
    expected := new(GetBalanceArgs)
    expected.Address = "0x407d73d8a49eeb85d32cf465507dd71d507100c1"
    expected.BlockNumber = 31

    args := new(GetBalanceArgs)
    if err := json.Unmarshal([]byte(input), &args); err != nil {
        t.Error(err)
    }

    if args.Address != expected.Address {
        t.Errorf("Address should be %v but is %v", expected.Address, args.Address)
    }

    if args.BlockNumber != expected.BlockNumber {
        t.Errorf("BlockNumber should be %v but is %v", expected.BlockNumber, args.BlockNumber)
    }
}

func TestGetBlockByHashArgs(t *testing.T) {
    input := `["0xe670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d1527331", true]`
    expected := new(GetBlockByHashArgs)
    expected.BlockHash = "0xe670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d1527331"
    expected.Transactions = true

    args := new(GetBlockByHashArgs)
    if err := json.Unmarshal([]byte(input), &args); err != nil {
        t.Error(err)
    }

    if args.BlockHash != expected.BlockHash {
        t.Errorf("BlockHash should be %v but is %v", expected.BlockHash, args.BlockHash)
    }

    if args.Transactions != expected.Transactions {
        t.Errorf("Transactions should be %v but is %v", expected.Transactions, args.Transactions)
    }
}

func TestGetBlockByNumberArgs(t *testing.T) {
    input := `["0x1b4", false]`
    expected := new(GetBlockByNumberArgs)
    expected.BlockNumber = 436
    expected.Transactions = false

    args := new(GetBlockByNumberArgs)
    if err := json.Unmarshal([]byte(input), &args); err != nil {
        t.Error(err)
    }

    if args.BlockNumber != expected.BlockNumber {
        t.Errorf("BlockHash should be %v but is %v", expected.BlockNumber, args.BlockNumber)
    }

    if args.Transactions != expected.Transactions {
        t.Errorf("Transactions should be %v but is %v", expected.Transactions, args.Transactions)
    }
}

func TestNewTxArgs(t *testing.T) {
    input := `[{"from": "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
  "to": "0xd46e8dd67c5d32be8058bb8eb970870f072445675",
  "gas": "0x76c0",
  "gasPrice": "0x9184e72a000",
  "value": "0x9184e72a000",
  "data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"}]`
    expected := new(NewTxArgs)
    expected.From = "0xb60e8dd61c5d32be8058bb8eb970870f07233155"
    expected.To = "0xd46e8dd67c5d32be8058bb8eb970870f072445675"
    expected.Gas = big.NewInt(30400)
    expected.GasPrice = big.NewInt(10000000000000)
    expected.Value = big.NewInt(10000000000000)
    expected.Data = "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"

    args := new(NewTxArgs)
    if err := json.Unmarshal([]byte(input), &args); err != nil {
        t.Error(err)
    }

    if expected.From != args.From {
        t.Errorf("From shoud be %#v but is %#v", expected.From, args.From)
    }

    if expected.To != args.To {
        t.Errorf("To shoud be %#v but is %#v", expected.To, args.To)
    }

    if bytes.Compare(expected.Gas.Bytes(), args.Gas.Bytes()) != 0 {
        t.Errorf("Gas shoud be %#v but is %#v", expected.Gas.Bytes(), args.Gas.Bytes())
    }

    if bytes.Compare(expected.GasPrice.Bytes(), args.GasPrice.Bytes()) != 0 {
        t.Errorf("GasPrice shoud be %#v but is %#v", expected.GasPrice, args.GasPrice)
    }

    if bytes.Compare(expected.Value.Bytes(), args.Value.Bytes()) != 0 {
        t.Errorf("Value shoud be %#v but is %#v", expected.Value, args.Value)
    }

    if expected.Data != args.Data {
        t.Errorf("Data shoud be %#v but is %#v", expected.Data, args.Data)
    }
}