forked from aergoio/aergo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chainanchor.go
66 lines (55 loc) · 1.3 KB
/
chainanchor.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
/**
* @file
* @copyright defined in aergo/LICENSE.txt
*/
package chain
import (
"github.com/aergoio/aergo/internal/enc"
"github.com/aergoio/aergo/types"
)
type ChainAnchor []([]byte)
// returns anchor blocks of chain
// use config
func (cs *ChainService) getAnchorsFromHash(blockHash []byte) ChainAnchor {
/* TODO: use config */
anchors := make(ChainAnchor, 0, 1000)
anchors = append(anchors, blockHash)
// collect 10 latest hashes
latestNo := cs.getBestBlockNo()
for i := 0; i < 10; i++ {
blockHash, err := cs.getHashByNo(latestNo)
if err != nil {
logger.Info().Msg("assertion - hash get failed")
// assertion!
return nil
}
logger.Debug().Uint64("no", latestNo).Str("hash", enc.ToString(blockHash)).Msg("anchor")
anchors = append(anchors, blockHash)
if latestNo == 0 {
return anchors
}
latestNo--
}
count := MaxAnchorCount
// collect exponential
var dec types.BlockNo = 1
for i := 0; i < count; i++ {
blockHash, err := cs.getHashByNo(latestNo)
if err != nil {
// assertion!
return nil
}
logger.Debug().Uint64("no", latestNo).Str("hash", enc.ToString(blockHash)).Msg("anchor")
anchors = append(anchors, blockHash)
if latestNo <= dec {
if latestNo == 0 {
break
}
latestNo = 0
} else {
latestNo -= dec
dec *= 2
}
}
return anchors
}