aboutsummaryrefslogtreecommitdiffstats
path: root/core/types/block_test.go
blob: 8e1165a99ae3a256b651ee7b56c38a6a7eea5d4a (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright 2014 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package types

import (
    "database/sql/driver"
    "reflect"
    "testing"
)

/*

// from bcValidBlockTest.json, "SimpleTx"
func TestBlockEncoding(t *testing.T) {
    blockEnc := common.FromHex("f90260f901f9a083cafc574e1f51ba9dc0568fc617a08ea2429fb384059c972f13b19fa1c8dd55a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a0ef1552a40b7165c3cd773806b9e0c165b75356e0314bf0706f279c729f51e017a05fe50b260da6308036625b850b5d6ced6d0a9f814c0688bc91ffb7b7a3a54b67a0bc37d79753ad738a6dac4921e57392f145d8887476de3f783dfa7edae9283e52b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302000001832fefd8825208845506eb0780a0bd4472abb6659ebe3ee06ee4d7b72a00a9f4d001caca51342001075469aff49888a13a5a8c8f2bb1c4f861f85f800a82c35094095e7baea6a6c7c4c2dfeb977efac326af552d870a801ba09bea4c4daac7c7c52e093e6a4c35dbbcf8856f1af7b059ba20253e70848d094fa08a8fae537ce25ed8cb5af9adac3f141af69bd515bd2ba031522df09b97dd72b1c0")
    var block Block
    if err := rlp.DecodeBytes(blockEnc, &block); err != nil {
        t.Fatal("decode error: ", err)
    }

    check := func(f string, got, want interface{}) {
        if !reflect.DeepEqual(got, want) {
            t.Errorf("%s mismatch: got %v, want %v", f, got, want)
        }
    }
    check("Difficulty", block.Difficulty(), big.NewInt(131072))
    check("GasLimit", block.GasLimit(), uint64(3141592))
    check("GasUsed", block.GasUsed(), uint64(21000))
    check("Coinbase", block.Coinbase(), common.HexToAddress("8888f1f195afa192cfee860698584c030f4c9db1"))
    check("MixDigest", block.MixDigest(), common.HexToHash("bd4472abb6659ebe3ee06ee4d7b72a00a9f4d001caca51342001075469aff498"))
    check("Root", block.Root(), common.HexToHash("ef1552a40b7165c3cd773806b9e0c165b75356e0314bf0706f279c729f51e017"))
    check("Hash", block.Hash(), common.HexToHash("0a5843ac1cb04865017cb35a57b50b07084e5fcee39b5acadade33149f4fff9e"))
    check("Nonce", block.Nonce(), uint64(0xa13a5a8c8f2bb1c4))
    check("Time", block.Time(), uint64(1426516743))
    check("Size", block.Size(), common.StorageSize(len(blockEnc)))

    tx1 := NewTransaction(0, common.HexToAddress("095e7baea6a6c7c4c2dfeb977efac326af552d87"), big.NewInt(10), 50000, big.NewInt(10), nil)

    tx1, _ = tx1.WithSignature(HomesteadSigner{}, common.Hex2Bytes("9bea4c4daac7c7c52e093e6a4c35dbbcf8856f1af7b059ba20253e70848d094f8a8fae537ce25ed8cb5af9adac3f141af69bd515bd2ba031522df09b97dd72b100"))
    fmt.Println(block.Transactions()[0].Hash())
    fmt.Println(tx1.data)
    fmt.Println(tx1.Hash())
    check("len(Transactions)", len(block.Transactions()), 1)
    check("Transactions[0].Hash", block.Transactions()[0].Hash(), tx1.Hash())

    ourBlockEnc, err := rlp.EncodeToBytes(&block)
    if err != nil {
        t.Fatal("encode error: ", err)
    }
    if !bytes.Equal(ourBlockEnc, blockEnc) {
        t.Errorf("encoded block mismatch:\ngot:  %x\nwant: %x", ourBlockEnc, blockEnc)
    }
}
*/

func TestBlockNonce_Scan(t *testing.T) {
    type args struct {
        src interface{}
    }
    tests := []struct {
        name    string
        args    args
        wantErr bool
    }{
        {
            name: "working scan",
            args: args{src: []byte{
                0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc,
            }},
            wantErr: false,
        },
        {
            name:    "non working scan",
            args:    args{src: int64(1234567890)},
            wantErr: true,
        },
        {
            name: "invalid length scan",
            args: args{src: []byte{
                0xb2, 0x6f, 0x2b, 0x34, 0x2a,
            }},
            wantErr: true,
        },
    }
    for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            n := &BlockNonce{}
            if err := n.Scan(tt.args.src); (err != nil) != tt.wantErr {
                t.Errorf("BlockNonce.Scan() error = %v, wantErr %v", err, tt.wantErr)
            }

            if !tt.wantErr {
                for i := range n {
                    if n[i] != tt.args.src.([]byte)[i] {
                        t.Errorf(
                            "BlockNonce.Scan() didn't scan the %d src correctly (have %X, want %X)",
                            i, n[i], tt.args.src.([]byte)[i],
                        )
                    }
                }
            }
        })
    }
}

func TestBlockNonce_Value(t *testing.T) {
    b := []byte{
        0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc,
    }
    var nonce BlockNonce
    nonce.UnmarshalText([]byte("0xb26f2b342aab24bc"))
    tests := []struct {
        name    string
        n       BlockNonce
        want    driver.Value
        wantErr bool
    }{
        {
            name:    "Working value",
            n:       nonce,
            want:    b,
            wantErr: false,
        },
    }
    for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            got, err := tt.n.Value()
            if (err != nil) != tt.wantErr {
                t.Errorf("BlockNonce.Value() error = %v, wantErr %v", err, tt.wantErr)
                return
            }
            if !reflect.DeepEqual(got, tt.want) {
                t.Errorf("BlockNonce.Value() = %v, want %v", got, tt.want)
            }
        })
    }
}