-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnipopow.go
72 lines (54 loc) · 1.61 KB
/
nipopow.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
package main
import "go.uber.org/zap"
const genesisId = "5CBFsnFA67379N8Em59skSQUwDuK3VHoeCZ92DPWaPd7"
var logger *zap.SugaredLogger
func init() {
zapLogger, _ := zap.NewProduction()
defer zapLogger.Sync() // flushes buffer, if any
logger = zapLogger.Sugar().With(zap.String("logger", "ErgoNodeClient"))
}
type Chain struct {
blocks []Block
}
func IsValidInterlink(blocks []Block) bool {
if blocks[0].Header.Interlinks[0] != genesisId {
// Первый линк всегда генезис
return false
}
for i := len(blocks) - 1; i > 1; i-- {
blockInterlinks := blocks[i].Header.Interlinks
if blockInterlinks[len(blockInterlinks)-1] != blocks[i-1].Header.Id {
return false
}
logger.Info("Block %s has valid interlink", blocks[i].Header.Id)
}
return true
}
func IsValidChain(proofs []Block, lastBlocks []Block) bool {
if !IsValidInterlink(proofs) {
return false
}
// TODO: валидация интерлинков для последних блоков?
//if !IsValidInterlink(lastBlocks) {
// return false
//}
return true
}
// точка входа
func Verify(proofs []Block, lastBlocks []Block) (bool, []Block) {
logger.Info("Getting genesis block...")
ergoNodeClient := initializeClient()
genesisBlock, err := ergoNodeClient.GetBlock(genesisId)
if err != nil {
panic(err)
}
proofsChecked := make([]Block, 0)
proofsChecked = append(proofsChecked, *genesisBlock)
logger.Info("Start validating proofs and last blocks...")
if IsValidChain(proofs, lastBlocks) {
proofsChecked = proofs
logger.Info("Chain is valid")
return true, proofsChecked
}
return false, proofsChecked
}