aboutsummaryrefslogtreecommitdiffstats
path: root/tests/block_test_util.go
blob: 06792cac177e73a5b4dc9409917fac8008e274ce (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
// Copyright 2015 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 tests

import (
    "bytes"
    "encoding/hex"
    "fmt"
    "io"
    "math/big"
    "runtime"
    "strconv"
    "strings"

    "github.com/ethereum/ethash"
    "github.com/ethereum/go-ethereum/common"
    "github.com/ethereum/go-ethereum/core"
    "github.com/ethereum/go-ethereum/core/state"
    "github.com/ethereum/go-ethereum/core/types"
    "github.com/ethereum/go-ethereum/core/vm"
    "github.com/ethereum/go-ethereum/ethdb"
    "github.com/ethereum/go-ethereum/event"
    "github.com/ethereum/go-ethereum/log"
    "github.com/ethereum/go-ethereum/params"
    "github.com/ethereum/go-ethereum/rlp"
)

// Block Test JSON Format
type BlockTest struct {
    Genesis *types.Block

    Json          *btJSON
    preAccounts   map[string]btAccount
    postAccounts  map[string]btAccount
    lastblockhash string
}

type btJSON struct {
    Blocks             []btBlock
    GenesisBlockHeader btHeader
    Pre                map[string]btAccount
    PostState          map[string]btAccount
    Lastblockhash      string
}

type btBlock struct {
    BlockHeader  *btHeader
    Rlp          string
    Transactions []btTransaction
    UncleHeaders []*btHeader
}

type btAccount struct {
    Balance    string
    Code       string
    Nonce      string
    Storage    map[string]string
    PrivateKey string
}

type btHeader struct {
    Bloom            string
    Coinbase         string
    MixHash          string
    Nonce            string
    Number           string
    Hash             string
    ParentHash       string
    ReceiptTrie      string
    SeedHash         string
    StateRoot        string
    TransactionsTrie string
    UncleHash        string

    ExtraData  string
    Difficulty string
    GasLimit   string
    GasUsed    string
    Timestamp  string
}

type btTransaction struct {
    Data     string
    GasLimit string
    GasPrice string
    Nonce    string
    R        string
    S        string
    To       string
    V        string
    Value    string
}

func RunBlockTestWithReader(homesteadBlock, daoForkBlock, gasPriceFork *big.Int, r io.Reader, skipTests []string) error {
    btjs := make(map[string]*btJSON)
    if err := readJson(r, &btjs); err != nil {
        return err
    }

    bt, err := convertBlockTests(btjs)
    if err != nil {
        return err
    }

    if err := runBlockTests(homesteadBlock, daoForkBlock, gasPriceFork, bt, skipTests); err != nil {
        return err
    }
    return nil
}

func RunBlockTest(homesteadBlock, daoForkBlock, gasPriceFork *big.Int, file string, skipTests []string) error {
    btjs := make(map[string]*btJSON)
    if err := readJsonFile(file, &btjs); err != nil {
        return err
    }

    bt, err := convertBlockTests(btjs)
    if err != nil {
        return err
    }
    if err := runBlockTests(homesteadBlock, daoForkBlock, gasPriceFork, bt, skipTests); err != nil {
        return err
    }
    return nil
}

func runBlockTests(homesteadBlock, daoForkBlock, gasPriceFork *big.Int, bt map[string]*BlockTest, skipTests []string) error {
    skipTest := make(map[string]bool, len(skipTests))
    for _, name := range skipTests {
        skipTest[name] = true
    }

    for name, test := range bt {
        if skipTest[name] /*|| name != "CallingCanonicalContractFromFork_CALLCODE"*/ {
            log.Info(fmt.Sprint("Skipping block test", name))
            continue
        }
        // test the block
        if err := runBlockTest(homesteadBlock, daoForkBlock, gasPriceFork, test); err != nil {
            return fmt.Errorf("%s: %v", name, err)
        }
        log.Info(fmt.Sprint("Block test passed: ", name))

    }
    return nil
}

func runBlockTest(homesteadBlock, daoForkBlock, gasPriceFork *big.Int, test *BlockTest) error {
    // import pre accounts & construct test genesis block & state root
    db, _ := ethdb.NewMemDatabase()
    if _, err := test.InsertPreState(db); err != nil {
        return fmt.Errorf("InsertPreState: %v", err)
    }

    core.WriteTd(db, test.Genesis.Hash(), 0, test.Genesis.Difficulty())
    core.WriteBlock(db, test.Genesis)
    core.WriteCanonicalHash(db, test.Genesis.Hash(), test.Genesis.NumberU64())
    core.WriteHeadBlockHash(db, test.Genesis.Hash())
    evmux := new(event.TypeMux)
    config := &params.ChainConfig{HomesteadBlock: homesteadBlock, DAOForkBlock: daoForkBlock, DAOForkSupport: true, EIP150Block: gasPriceFork}
    chain, err := core.NewBlockChain(db, config, ethash.NewShared(), evmux, vm.Config{})
    if err != nil {
        return err
    }
    defer chain.Stop()

    //vm.Debug = true
    validBlocks, err := test.TryBlocksInsert(chain)
    if err != nil {
        return err
    }

    lastblockhash := common.HexToHash(test.lastblockhash)
    cmlast := chain.LastBlockHash()
    if lastblockhash != cmlast {
        return fmt.Errorf("lastblockhash validation mismatch: want: %x, have: %x", lastblockhash, cmlast)
    }

    newDB, err := chain.State()
    if err != nil {
        return err
    }
    if err = test.ValidatePostState(newDB); err != nil {
        return fmt.Errorf("post state validation failed: %v", err)
    }

    return test.ValidateImportedHeaders(chain, validBlocks)
}

// InsertPreState populates the given database with the genesis
// accounts defined by the test.
func (t *BlockTest) InsertPreState(db ethdb.Database) (*state.StateDB, error) {
    statedb, err := state.New(common.Hash{}, db)
    if err != nil {
        return nil, err
    }
    for addrString, acct := range t.preAccounts {
        code, err := hex.DecodeString(strings.TrimPrefix(acct.Code, "0x"))
        if err != nil {
            return nil, err
        }
        balance, ok := new(big.Int).SetString(acct.Balance, 0)
        if !ok {
            return nil, err
        }
        nonce, err := strconv.ParseUint(prepInt(16, acct.Nonce), 16, 64)
        if err != nil {
            return nil, err
        }

        addr := common.HexToAddress(addrString)
        statedb.CreateAccount(addr)
        statedb.SetCode(addr, code)
        statedb.SetBalance(addr, balance)
        statedb.SetNonce(addr, nonce)
        for k, v := range acct.Storage {
            statedb.SetState(common.HexToAddress(addrString), common.HexToHash(k), common.HexToHash(v))
        }
    }

    root, err := statedb.Commit(false)
    if err != nil {
        return nil, fmt.Errorf("error writing state: %v", err)
    }
    if t.Genesis.Root() != root {
        return nil, fmt.Errorf("computed state root does not match genesis block: genesis=%x computed=%x", t.Genesis.Root().Bytes()[:4], root.Bytes()[:4])
    }
    return statedb, nil
}

/* See https://github.com/ethereum/tests/wiki/Blockchain-Tests-II

   Whether a block is valid or not is a bit subtle, it's defined by presence of
   blockHeader, transactions and uncleHeaders fields. If they are missing, the block is
   invalid and we must verify that we do not accept it.

   Since some tests mix valid and invalid blocks we need to check this for every block.

   If a block is invalid it does not necessarily fail the test, if it's invalidness is
   expected we are expected to ignore it and continue processing and then validate the
   post state.
*/
func (t *BlockTest) TryBlocksInsert(blockchain *core.BlockChain) ([]btBlock, error) {
    validBlocks := make([]btBlock, 0)
    // insert the test blocks, which will execute all transactions
    for _, b := range t.Json.Blocks {
        cb, err := mustConvertBlock(b)
        if err != nil {
            if b.BlockHeader == nil {
                continue // OK - block is supposed to be invalid, continue with next block
            } else {
                return nil, fmt.Errorf("Block RLP decoding failed when expected to succeed: %v", err)
            }
        }
        // RLP decoding worked, try to insert into chain:
        blocks := types.Blocks{cb}
        i, err := blockchain.InsertChain(blocks)
        if err != nil {
            if b.BlockHeader == nil {
                continue // OK - block is supposed to be invalid, continue with next block
            } else {
                return nil, fmt.Errorf("Block #%v insertion into chain failed: %v", blocks[i].Number(), err)
            }
        }
        if b.BlockHeader == nil {
            return nil, fmt.Errorf("Block insertion should have failed")
        }

        // validate RLP decoding by checking all values against test file JSON
        if err = validateHeader(b.BlockHeader, cb.Header()); err != nil {
            return nil, fmt.Errorf("Deserialised block header validation failed: %v", err)
        }
        validBlocks = append(validBlocks, b)
    }
    return validBlocks, nil
}

func validateHeader(h *btHeader, h2 *types.Header) error {
    expectedBloom := mustConvertBytes(h.Bloom)
    if !bytes.Equal(expectedBloom, h2.Bloom.Bytes()) {
        return fmt.Errorf("Bloom: want: %x have: %x", expectedBloom, h2.Bloom.Bytes())
    }

    expectedCoinbase := mustConvertBytes(h.Coinbase)
    if !bytes.Equal(expectedCoinbase, h2.Coinbase.Bytes()) {
        return fmt.Errorf("Coinbase: want: %x have: %x", expectedCoinbase, h2.Coinbase.Bytes())
    }

    expectedMixHashBytes := mustConvertBytes(h.MixHash)
    if !bytes.Equal(expectedMixHashBytes, h2.MixDigest.Bytes()) {
        return fmt.Errorf("MixHash: want: %x have: %x", expectedMixHashBytes, h2.MixDigest.Bytes())
    }

    expectedNonce := mustConvertBytes(h.Nonce)
    if !bytes.Equal(expectedNonce, h2.Nonce[:]) {
        return fmt.Errorf("Nonce: want: %x have: %x", expectedNonce, h2.Nonce)
    }

    expectedNumber := mustConvertBigInt(h.Number, 16)
    if expectedNumber.Cmp(h2.Number) != 0 {
        return fmt.Errorf("Number: want: %v have: %v", expectedNumber, h2.Number)
    }

    expectedParentHash := mustConvertBytes(h.ParentHash)
    if !bytes.Equal(expectedParentHash, h2.ParentHash.Bytes()) {
        return fmt.Errorf("Parent hash: want: %x have: %x", expectedParentHash, h2.ParentHash.Bytes())
    }

    expectedReceiptHash := mustConvertBytes(h.ReceiptTrie)
    if !bytes.Equal(expectedReceiptHash, h2.ReceiptHash.Bytes()) {
        return fmt.Errorf("Receipt hash: want: %x have: %x", expectedReceiptHash, h2.ReceiptHash.Bytes())
    }

    expectedTxHash := mustConvertBytes(h.TransactionsTrie)
    if !bytes.Equal(expectedTxHash, h2.TxHash.Bytes()) {
        return fmt.Errorf("Tx hash: want: %x have: %x", expectedTxHash, h2.TxHash.Bytes())
    }

    expectedStateHash := mustConvertBytes(h.StateRoot)
    if !bytes.Equal(expectedStateHash, h2.Root.Bytes()) {
        return fmt.Errorf("State hash: want: %x have: %x", expectedStateHash, h2.Root.Bytes())
    }

    expectedUncleHash := mustConvertBytes(h.UncleHash)
    if !bytes.Equal(expectedUncleHash, h2.UncleHash.Bytes()) {
        return fmt.Errorf("Uncle hash: want: %x have: %x", expectedUncleHash, h2.UncleHash.Bytes())
    }

    expectedExtraData := mustConvertBytes(h.ExtraData)
    if !bytes.Equal(expectedExtraData, h2.Extra) {
        return fmt.Errorf("Extra data: want: %x have: %x", expectedExtraData, h2.Extra)
    }

    expectedDifficulty := mustConvertBigInt(h.Difficulty, 16)
    if expectedDifficulty.Cmp(h2.Difficulty) != 0 {
        return fmt.Errorf("Difficulty: want: %v have: %v", expectedDifficulty, h2.Difficulty)
    }

    expectedGasLimit := mustConvertBigInt(h.GasLimit, 16)
    if expectedGasLimit.Cmp(h2.GasLimit) != 0 {
        return fmt.Errorf("GasLimit: want: %v have: %v", expectedGasLimit, h2.GasLimit)
    }
    expectedGasUsed := mustConvertBigInt(h.GasUsed, 16)
    if expectedGasUsed.Cmp(h2.GasUsed) != 0 {
        return fmt.Errorf("GasUsed: want: %v have: %v", expectedGasUsed, h2.GasUsed)
    }

    expectedTimestamp := mustConvertBigInt(h.Timestamp, 16)
    if expectedTimestamp.Cmp(h2.Time) != 0 {
        return fmt.Errorf("Timestamp: want: %v have: %v", expectedTimestamp, h2.Time)
    }

    return nil
}

func (t *BlockTest) ValidatePostState(statedb *state.StateDB) error {
    // validate post state accounts in test file against what we have in state db
    for addrString, acct := range t.postAccounts {
        // XXX: is is worth it checking for errors here?
        addr, err := hex.DecodeString(addrString)
        if err != nil {
            return err
        }
        code, err := hex.DecodeString(strings.TrimPrefix(acct.Code, "0x"))
        if err != nil {
            return err
        }
        balance, ok := new(big.Int).SetString(acct.Balance, 0)
        if !ok {
            return err
        }
        nonce, err := strconv.ParseUint(prepInt(16, acct.Nonce), 16, 64)
        if err != nil {
            return err
        }

        // address is indirectly verified by the other fields, as it's the db key
        code2 := statedb.GetCode(common.BytesToAddress(addr))
        balance2 := statedb.GetBalance(common.BytesToAddress(addr))
        nonce2 := statedb.GetNonce(common.BytesToAddress(addr))
        if !bytes.Equal(code2, code) {
            return fmt.Errorf("account code mismatch for addr: %s want: %s have: %s", addrString, hex.EncodeToString(code), hex.EncodeToString(code2))
        }
        if balance2.Cmp(balance) != 0 {
            return fmt.Errorf("account balance mismatch for addr: %s, want: %d, have: %d", addrString, balance, balance2)
        }
        if nonce2 != nonce {
            return fmt.Errorf("account nonce mismatch for addr: %s want: %d have: %d", addrString, nonce, nonce2)
        }
    }
    return nil
}

func (test *BlockTest) ValidateImportedHeaders(cm *core.BlockChain, validBlocks []btBlock) error {
    // to get constant lookup when verifying block headers by hash (some tests have many blocks)
    bmap := make(map[string]btBlock, len(test.Json.Blocks))
    for _, b := range validBlocks {
        bmap[b.BlockHeader.Hash] = b
    }

    // iterate over blocks backwards from HEAD and validate imported
    // headers vs test file. some tests have reorgs, and we import
    // block-by-block, so we can only validate imported headers after
    // all blocks have been processed by ChainManager, as they may not
    // be part of the longest chain until last block is imported.
    for b := cm.CurrentBlock(); b != nil && b.NumberU64() != 0; b = cm.GetBlockByHash(b.Header().ParentHash) {
        bHash := common.Bytes2Hex(b.Hash().Bytes()) // hex without 0x prefix
        if err := validateHeader(bmap[bHash].BlockHeader, b.Header()); err != nil {
            return fmt.Errorf("Imported block header validation failed: %v", err)
        }
    }
    return nil
}

func convertBlockTests(in map[string]*btJSON) (map[string]*BlockTest, error) {
    out := make(map[string]*BlockTest)
    for name, test := range in {
        var err error
        if out[name], err = convertBlockTest(test); err != nil {
            return out, fmt.Errorf("bad test %q: %v", name, err)
        }
    }
    return out, nil
}

func convertBlockTest(in *btJSON) (out *BlockTest, err error) {
    // the conversion handles errors by catching panics.
    // you might consider this ugly, but the alternative (passing errors)
    // would be much harder to read.
    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)
        }
    }()
    out = &BlockTest{preAccounts: in.Pre, postAccounts: in.PostState, Json: in, lastblockhash: in.Lastblockhash}
    out.Genesis = mustConvertGenesis(in.GenesisBlockHeader)
    return out, err
}

func mustConvertGenesis(testGenesis btHeader) *types.Block {
    hdr := mustConvertHeader(testGenesis)
    hdr.Number = big.NewInt(0)

    return types.NewBlockWithHeader(hdr)
}

func mustConvertHeader(in btHeader) *types.Header {
    // hex decode these fields
    header := &types.Header{
        //SeedHash:    mustConvertBytes(in.SeedHash),
        MixDigest:   mustConvertHash(in.MixHash),
        Bloom:       mustConvertBloom(in.Bloom),
        ReceiptHash: mustConvertHash(in.ReceiptTrie),
        TxHash:      mustConvertHash(in.TransactionsTrie),
        Root:        mustConvertHash(in.StateRoot),
        Coinbase:    mustConvertAddress(in.Coinbase),
        UncleHash:   mustConvertHash(in.UncleHash),
        ParentHash:  mustConvertHash(in.ParentHash),
        Extra:       mustConvertBytes(in.ExtraData),
        GasUsed:     mustConvertBigInt(in.GasUsed, 16),
        GasLimit:    mustConvertBigInt(in.GasLimit, 16),
        Difficulty:  mustConvertBigInt(in.Difficulty, 16),
        Time:        mustConvertBigInt(in.Timestamp, 16),
        Nonce:       types.EncodeNonce(mustConvertUint(in.Nonce, 16)),
    }
    return header
}

func mustConvertBlock(testBlock btBlock) (*types.Block, error) {
    var b types.Block
    r := bytes.NewReader(mustConvertBytes(testBlock.Rlp))
    err := rlp.Decode(r, &b)
    return &b, err
}

func mustConvertBytes(in string) []byte {
    if in == "0x" {
        return []byte{}
    }
    h := unfuckFuckedHex(strings.TrimPrefix(in, "0x"))
    out, err := hex.DecodeString(h)
    if err != nil {
        panic(fmt.Errorf("invalid hex: %q", h))
    }
    return out
}

func mustConvertHash(in string) common.Hash {
    out, err := hex.DecodeString(strings.TrimPrefix(in, "0x"))
    if err != nil {
        panic(fmt.Errorf("invalid hex: %q", in))
    }
    return common.BytesToHash(out)
}

func mustConvertAddress(in string) common.Address {
    out, err := hex.DecodeString(strings.TrimPrefix(in, "0x"))
    if err != nil {
        panic(fmt.Errorf("invalid hex: %q", in))
    }
    return common.BytesToAddress(out)
}

func mustConvertBloom(in string) types.Bloom {
    out, err := hex.DecodeString(strings.TrimPrefix(in, "0x"))
    if err != nil {
        panic(fmt.Errorf("invalid hex: %q", in))
    }
    return types.BytesToBloom(out)
}

func mustConvertBigInt(in string, base int) *big.Int {
    in = prepInt(base, in)
    out, ok := new(big.Int).SetString(in, base)
    if !ok {
        panic(fmt.Errorf("invalid integer: %q", in))
    }
    return out
}

func mustConvertUint(in string, base int) uint64 {
    in = prepInt(base, in)
    out, err := strconv.ParseUint(in, base, 64)
    if err != nil {
        panic(fmt.Errorf("invalid integer: %q", in))
    }
    return out
}

func LoadBlockTests(file string) (map[string]*BlockTest, error) {
    btjs := make(map[string]*btJSON)
    if err := readJsonFile(file, &btjs); err != nil {
        return nil, err
    }

    return convertBlockTests(btjs)
}

// Nothing to see here, please move along...
func prepInt(base int, s string) string {
    if base == 16 {
        s = strings.TrimPrefix(s, "0x")
        if len(s) == 0 {
            s = "00"
        }
        s = nibbleFix(s)
    }
    return s
}

// don't ask
func unfuckFuckedHex(almostHex string) string {
    return nibbleFix(strings.Replace(almostHex, "v", "", -1))
}

func nibbleFix(s string) string {
    if len(s)%2 != 0 {
        s = "0" + s
    }
    return s
}