-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathblock_process_test.go
More file actions
56 lines (47 loc) · 1.41 KB
/
block_process_test.go
File metadata and controls
56 lines (47 loc) · 1.41 KB
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
package main
import (
"strings"
"testing"
)
func TestProcessBlock_EnforcesValidationInternally(t *testing.T) {
chain, storage, cleanup := mustCreateTestChain(t)
defer cleanup()
mustAddGenesisBlock(t, chain)
genesis := chain.GetBlockByHeight(0)
if genesis == nil {
t.Fatal("expected genesis block at height 0")
}
prevTipHash := chain.BestHash()
prevTipHeight := chain.Height()
invalid := &Block{
Header: BlockHeader{
Version: 1,
Height: genesis.Header.Height + 1,
PrevHash: genesis.Hash(),
Timestamp: genesis.Header.Timestamp + BlockIntervalSec,
Difficulty: MinDifficulty + 1, // should be MinDifficulty in early epoch
},
Transactions: nil,
}
accepted, isMainChain, err := chain.ProcessBlock(invalid)
if err == nil {
t.Fatal("expected ProcessBlock to reject invalid block")
}
if !strings.Contains(err.Error(), "invalid difficulty") {
t.Fatalf("expected invalid difficulty error, got: %v", err)
}
if accepted {
t.Fatal("invalid block should not be accepted")
}
if isMainChain {
t.Fatal("invalid block must not be marked as main chain")
}
assertTipUnchanged(t, chain, prevTipHash, prevTipHeight)
invalidHash := invalid.Hash()
if storage.HasBlock(invalidHash) {
t.Fatalf("invalid block was persisted in storage: %x", invalidHash[:8])
}
if got := chain.GetBlock(invalidHash); got != nil {
t.Fatalf("invalid block was cached in chain memory: %x", invalidHash[:8])
}
}