forkplan9controller.go raw
1 package blockchain
2
3 import (
4 "github.com/p9c/p9/pkg/chainhash"
5 "github.com/p9c/p9/pkg/fork"
6 "sort"
7 )
8
9 type Algo struct {
10 Name string
11 Params fork.AlgoParams
12 }
13
14 type AlgoList []Algo
15
16 func (al AlgoList) Len() int {
17 return len(al)
18 }
19
20 func (al AlgoList) Less(i, j int) bool {
21 return al[i].Params.Version < al[j].Params.Version
22 }
23
24 func (al AlgoList) Swap(i, j int) {
25 al[i], al[j] = al[j], al[i]
26 }
27
28 type Diffs map[int32]uint32
29
30 type Merkles map[int32]*chainhash.Hash
31
32 // CalcNextRequiredDifficultyPlan9Controller returns all of the algorithm difficulty targets for sending out with the
33 // other pieces required to construct a block, as these numbers are generated from block timestamps
34 func (b *BlockChain) CalcNextRequiredDifficultyPlan9Controller(lastNode *BlockNode) (
35 diffs Diffs, e error,
36 ) {
37 nH := lastNode.height + 1
38 currFork := fork.GetCurrent(nH)
39 nTB := make(Diffs)
40 switch currFork {
41 case 0:
42 for i := range fork.List[0].Algos {
43 v := fork.List[currFork].Algos[i].Version
44 nTB[v], e = b.CalcNextRequiredDifficultyHalcyon(lastNode, i, true)
45 }
46 return nTB, nil
47 case 1:
48 if b.DifficultyHeight.Load() != nH {
49 b.DifficultyHeight.Store(nH)
50 currFork := fork.GetCurrent(nH)
51 algos := make(AlgoList, len(fork.List[currFork].Algos))
52 var counter int
53 for i := range fork.List[1].Algos {
54 algos[counter] = Algo{
55 Name: i,
56 Params: fork.List[currFork].Algos[i],
57 }
58 counter++
59 }
60 sort.Sort(algos)
61 for _, v := range algos {
62 nTB[v.Params.Version], _, e = b.CalcNextRequiredDifficultyPlan9(lastNode, v.Name, true)
63 }
64 diffs = nTB
65 // Traces(diffs)
66 } else {
67 diffs = b.DifficultyBits.Load().(Diffs)
68 }
69 return
70 }
71 F.Ln("should not fall through here")
72 return
73 }
74