-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathchain.go
158 lines (128 loc) · 3.01 KB
/
chain.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package ozcoin
import (
"errors"
"log"
)
func CoinbaseValue(seqnum uint64) uint64 {
return (50 * 100000000) >> (seqnum / 21000)
}
func (c *Client) MainChainTotalDifficulty() (uint64, error) {
total := uint64(0)
prevHash := c.LastHeader.Hash()
for {
header, err := c.GetHeader(prevHash)
if err != nil {
return 0, err
}
if header == nil {
return total, nil
}
total += header.Difficulty
prevHash = header.PrevHash
}
}
func (c *Client) SideChainTotalDifficulty(hash SHA256Sum) (uint64, error) {
total := uint64(0)
prevHash := hash
for {
header, err := c.GetHeader(prevHash)
if err != nil {
return 0, err
}
if header != nil {
total += header.Difficulty
prevHash = header.PrevHash
continue
}
header, err = c.GetSideHeader(prevHash)
if err != nil {
return 0, err
}
if header != nil {
total += header.Difficulty
prevHash = header.PrevHash
}
return total, nil
}
}
func (c *Client) ComputeDifficulty(b Block) uint64 {
if b.Header.SeqNum <= DIFFICULTY_SPACING {
return INITIAL_DIFFICULTY
}
last, err := c.NthAncestorHeader(b.Header.PrevHash, DIFFICULTY_SPACING)
if err != nil {
log.Println(err)
return uint64(1) << 63
}
currTime := b.Header.Time.Unix()
lastTime := last.Time.Unix()
actualTime := currTime - lastTime
oldTarget := last.Difficulty
newTarget := uint64(float64(oldTarget) * (float64(TWO_WEEKS_SEC) /
float64(actualTime)))
lowerBound := uint64(0)
if oldTarget > 0 {
lowerBound = oldTarget - 1
}
upperBound := oldTarget + 1
if newTarget < lowerBound {
newTarget = lowerBound
}
if newTarget > upperBound {
newTarget = upperBound
}
return newTarget
}
func (c *Client) ValidDifficulty(b Block) bool {
return uint64(b.Header.Difficulty) == c.ComputeDifficulty(b)
}
func (c *Client) NthAncestorHeader(hash SHA256Sum, n int) (*BlockHeader, error) {
prevHash := hash
prevHeader := &BlockHeader{}
depth := 0
for depth < n {
// Load previous header from main database
header, err := c.GetHeader(prevHash)
if err != nil {
// Load previous header from sidechain database
header, err = c.GetSideHeader(prevHash)
if err != nil {
log.Println("Could not find nth ancestor", err)
return nil, err
}
}
prevHash = header.PrevHash
*prevHeader = *header
depth += 1
}
return prevHeader, nil
}
func (c *Client) ForkTxnsAndPreimages(path []SHA256Sum) (map[SHA256Sum]Output, map[SHA256Sum]struct{}, error) {
txns := make(map[SHA256Sum]Output)
pimgs := make(map[SHA256Sum]struct{})
for _, hash := range path {
b, err := c.GetBlock(hash)
if err != nil {
return nil, nil, err
}
if b == nil {
b, err = c.GetSideBlock(hash)
if err != nil {
return nil, nil, err
}
}
if b == nil {
return nil, nil, errors.New("Path does not exist")
}
for _, txn := range b.Txns {
// Add preimage
pimgHash := Hash(txn.Sig.Preimage.Bytes())
pimgs[pimgHash] = SIGNAL
// Add txn outputs
for _, output := range txn.Body.Outputs {
txns[output.Hash()] = output
}
}
}
return txns, pimgs, nil
}