-
Notifications
You must be signed in to change notification settings - Fork 4
/
blocks.go
129 lines (114 loc) · 3.41 KB
/
blocks.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
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
package arsyncer
import (
"crypto/sha256"
"errors"
"fmt"
"github.com/everFinance/goar"
"github.com/everFinance/goar/types"
"github.com/everFinance/goar/utils"
"strings"
)
type BlockIdxs struct {
StartHeight int64
EndHeight int64
IndepHashMap map[string]struct{}
}
func GetBlockIdxs(startHeight int64, arCli *goar.Client) (*BlockIdxs, error) {
info, err := arCli.GetInfo()
if err != nil {
log.Error("arCli.GetInfo()", "err", err)
return nil, err
}
endHeight := info.Height
// get block hash_list from trust node
spiltList, err := arCli.GetBlockHashList(int(startHeight), int(endHeight))
if err != nil {
log.Error("GetBlockHashList(arCli,startHeight,endHeight)", "err", err)
// get block hash_list from no-trust node
spiltList, err = GetBlockHashListFromPeers(arCli, startHeight, endHeight, 5)
}
if err != nil {
return nil, err
}
hashMap := make(map[string]struct{})
for _, h := range spiltList {
hashMap[h] = struct{}{}
}
return &BlockIdxs{
StartHeight: startHeight,
EndHeight: endHeight,
IndepHashMap: hashMap,
}, nil
}
func (l *BlockIdxs) existBlock(b types.Block) bool {
if b.Height < l.StartHeight || b.Height > l.EndHeight {
log.Error("block height is outside", "height", b.Height, "startHeight", l.StartHeight, "endHeight", l.EndHeight)
return false
}
_, ok := l.IndepHashMap[b.IndepHash]
return ok
}
func (l *BlockIdxs) VerifyBlock(b types.Block) error {
/*
2.6 is out - https://github.com/ArweaveTeam/arweave/releases/tag/N.2.6.0.
The fork activates at height 1132210, approximately 2023-03-06 14:00 UTC.
You will need to make sure you have upgraded your miner before this time to connect to the network.
You can find more information in the release notes and the updated mining guide.
*/
if b.Height >= 1132210 { // not verify 2.6 block
return nil
}
if !l.existBlock(b) {
log.Warn("block indepHash not exist blockIdxs", "blockHeight", b.Height, "blockIndepHash", b.IndepHash)
return errors.New("block indepHash not exist blockIdxs")
}
indepHash := utils.GenerateIndepHash(b)
if indepHash != b.IndepHash {
return fmt.Errorf("generateIndepHash not equal; b.IndepHash: %s", b.IndepHash)
}
return nil
}
func GetBlockHashListFromPeers(c *goar.Client, startHeight, endHeight int64, checkNum int, peers ...string) ([]string, error) {
var err error
if len(peers) == 0 {
peers, err = c.GetPeers()
if err != nil {
return nil, err
}
}
checkSum := ""
successCount := 0
failedCount := 0
pNode := goar.NewTempConn()
for _, peer := range peers {
pNode.SetTempConnUrl("http://" + peer)
spiltList, err := pNode.GetBlockHashList(int(startHeight), int(endHeight))
if err != nil {
log.Error("pNode.GetBlockHashList()", "err", err)
continue
}
sum := strArrCheckSum(spiltList)
if checkSum == "" {
checkSum = sum
}
if checkSum == sum {
fmt.Printf("success get block hash_list; peer: %s\n", peer)
successCount++
} else {
fmt.Printf("failed get block hash_list, checksum failed; peer:%s\n", peer)
failedCount++
}
if successCount >= checkNum {
log.Debug("success get block hash_list from peers", "start", startHeight, "end", endHeight)
return spiltList, nil
}
if failedCount >= checkNum/2 {
return nil, errors.New("get hash_list from peers failed")
}
}
return nil, errors.New("get hash_list from peers failed")
}
func strArrCheckSum(ss []string) string {
hash := sha256.Sum256([]byte(strings.Join(ss, "")))
return string(hash[:])
}