-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpartialValidation.go
86 lines (76 loc) · 2.11 KB
/
partialValidation.go
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
package main
import (
"bytes"
"encoding/hex"
"fmt"
"net"
"github.com/VIVelev/btcd/blockchain"
"github.com/VIVelev/btcd/crypto/hash"
"github.com/VIVelev/btcd/network"
"github.com/VIVelev/btcd/utils"
)
// partialValidation validates the block headers of the first 40,000 blocks
func partialValidation() {
// Start with the genesis block
// https://en.bitcoin.it/wiki/Genesis_block
previous := *new(blockchain.Block).Unmarshal(bytes.NewReader(blockchain.MainGenesisBlockBytes))
// Now let's crawl the blockchain block headers
conn, err := net.Dial("tcp", "mainnet.programmingbitcoin.com:8333")
if err != nil {
panic(err)
}
node := &network.Node{
Conn: conn,
Testnet: false,
Logging: true,
}
if err = node.Handshake(); err != nil {
panic(err)
}
blocks := []blockchain.Block{previous}
for i := 0; i < 20; i++ {
// request the next batch of 2,000 headers
b := previous.Marshal()
h := hash.Hash256(b[:])
copy(h[:], utils.Reversed(h[:]))
getheaders := network.GetHeadersMsg{
Version: 70015,
NumHashes: 1,
StartBlock: h,
EndBlock: [32]byte{},
}
node.Write(&getheaders)
msg, err := node.WaitFor("headers")
if err != nil {
panic(err)
}
headers := msg.(*network.HeadersMsg)
blocks = append(blocks, headers.Headers...)
l := len(blocks)
previous = blocks[l-1]
fmt.Printf("received another batch of blocks, now have %d\n", l)
}
node.Close()
// we now have 40,001 blocks total, 80 bytes each in raw, so total of ~3.2MB of data
// now (partially) validate the blockchain integrity
fmt.Println("Validating the blockchain... (partially)")
for i, block := range blocks {
// validate PoW
if !block.VerifyPoW() {
panic("PoW not valid")
}
// validate ptr to prev block
var expectedPrevBlock [32]byte
if i > 0 {
b, _ := hex.DecodeString(blocks[i-1].Id())
copy(expectedPrevBlock[:], b)
}
if !bytes.Equal(block.PrevBlock[:], expectedPrevBlock[:]) {
panic("Ptr to prev block not valid")
}
if i%1000 == 0 {
fmt.Printf("on block %d/%d\n", i+1, len(blocks))
}
}
fmt.Printf("Success! The blockchain is for sure valid up to block %d.\n", len(blocks))
}