-
Notifications
You must be signed in to change notification settings - Fork 0
/
timestamp_claim_repo.go
253 lines (243 loc) · 7.14 KB
/
timestamp_claim_repo.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package ethereum
import (
"errors"
"fmt"
"github.com/SmartPool/smartpool-client"
"github.com/SmartPool/smartpool-client/protocol"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"math/big"
"os"
"sync"
)
const ACTIVE_SHARE_FILE string = "active_shares"
// TimestampClaimRepo only select shares that don't have most recent timestamp
// in order to make sure coming shares' counters are greater than selected
// shares
type TimestampClaimRepo struct {
activeShares map[string]*Share
recentTimestamp *big.Int
noShares uint64
noRecentShares uint64
mu sync.Mutex
storage smartpool.PersistentStorage
diff *big.Int
miner string
}
func NewTimestampClaimRepo(diff *big.Int, miner, coinbase string, storage smartpool.PersistentStorage) *TimestampClaimRepo {
shares, err := loadActiveShares(storage)
if err != nil {
smartpool.Output.Printf("Couldn't load active shares from last session (%s). Initialize with empty share pool.\n", err)
}
noShares := 0
noRecentShares := 0
currentTimestamp := big.NewInt(0)
changedDiff := false
changedMiner := false
changedCoinbase := false
if len(shares) > 0 {
for _, s := range shares {
if currentTimestamp.Cmp(s.Timestamp()) < 0 {
currentTimestamp.Add(s.Timestamp(), common.Big0)
}
}
for _, s := range shares {
if s.Timestamp().Cmp(currentTimestamp) == 0 {
noRecentShares++
} else {
noShares++
}
if s.ShareDifficulty().Cmp(diff) != 0 {
changedDiff = true
}
if s.MinerAddress() != miner {
changedMiner = true
}
if s.BlockHeader().Coinbase.Hex() != coinbase {
changedCoinbase = true
}
}
}
var oneShare *Share
if changedCoinbase {
smartpool.Output.Printf("SmartPool contract address changed. Discarded %d shares from last session.\n", len(shares))
shares = map[string]*Share{}
noShares = 0
noRecentShares = 0
currentTimestamp = big.NewInt(0)
} else if changedMiner {
for _, s := range shares {
oneShare = s
break
}
fmt.Printf("You have %d shares from last session with miner %s that were not submitted to the contract.\n", len(shares), oneShare.BlockHeader().Coinbase.Hex())
fmt.Printf("However you are going to run SmartPool with different miner %s.\n", miner)
fmt.Printf("Please choose one of following options:\n")
fmt.Printf("1. Discard those shares and continue running SmartPool with new miner.\n")
fmt.Printf("2. Abort SmartPool and rerun it with --miner %s\n", oneShare.MinerAddress())
var choice string
for {
fmt.Printf("Enter 1 or 2: ")
fmt.Scanf("%s", &choice)
if choice == "1" {
shares = map[string]*Share{}
noShares = 0
noRecentShares = 0
currentTimestamp = big.NewInt(0)
smartpool.Output.Printf("You chose to discard the shares from last session.\n")
break
} else if choice == "2" {
os.Exit(1)
}
}
} else if changedDiff {
for _, s := range shares {
oneShare = s
break
}
fmt.Printf("You have %d shares from last session with difficulty %s that were not submitted to the contract.\n", len(shares), oneShare.ShareDifficulty().Text(10))
fmt.Printf("However you are going to run SmartPool with different share difficulty %s.\n", diff.Text(10))
fmt.Printf("Please choose one of following options:\n")
fmt.Printf("1. Discard those shares and continue running SmartPool with new difficulty.\n")
fmt.Printf("2. Abort SmartPool and rerun it with --diff %s\n", oneShare.ShareDifficulty().Text(10))
var choice string
for {
fmt.Printf("Enter 1 or 2: ")
fmt.Scanf("%s", &choice)
if choice == "1" {
shares = map[string]*Share{}
noShares = 0
noRecentShares = 0
currentTimestamp = big.NewInt(0)
smartpool.Output.Printf("You chose to discard the shares from last session.\n")
break
} else if choice == "2" {
os.Exit(1)
}
}
}
cr := TimestampClaimRepo{
shares,
currentTimestamp,
uint64(noShares),
uint64(noRecentShares),
sync.Mutex{},
storage,
diff,
miner,
}
smartpool.Output.Printf("Loaded %d valid shares\n", noShares)
smartpool.Output.Printf("Loaded timestamp: 0x%s\n", currentTimestamp.Text(16))
smartpool.Output.Printf("Loaded %d shares with current timestamp\n", noRecentShares)
return &cr
}
type gobShare struct {
BlockHeader *types.Header
Nonce types.BlockNonce
MixDigest common.Hash
ShareDifficulty *big.Int
MinerAddress string
SolutionState int
}
func loadActiveShares(storage smartpool.PersistentStorage) (map[string]*Share, error) {
shares := map[string]*Share{}
gobShares := map[string]gobShare{}
loadedGobShares, err := storage.Load(&gobShares, ACTIVE_SHARE_FILE)
gobShares = *loadedGobShares.(*map[string]gobShare)
if err != nil {
return shares, err
}
for k, gobShare := range gobShares {
shares[k] = &Share{
gobShare.BlockHeader,
gobShare.Nonce,
gobShare.MixDigest,
gobShare.ShareDifficulty,
gobShare.MinerAddress,
gobShare.SolutionState,
nil,
}
}
return shares, nil
}
func (cr *TimestampClaimRepo) NoActiveShares() uint64 {
return cr.noShares + cr.noRecentShares
}
func (cr *TimestampClaimRepo) Persist(storage smartpool.PersistentStorage) error {
gobShares := map[string]gobShare{}
var shareID string
for _, s := range cr.activeShares {
shareID = fmt.Sprintf(
"%s-%s",
s.BlockHeader().Hash().Hex(),
s.Nonce())
gobShares[shareID] = gobShare{
s.BlockHeader(),
s.nonce,
s.mixDigest,
s.shareDifficulty,
s.minerAddress,
s.SolutionState,
}
}
smartpool.Output.Printf("Saving active shares to disk...")
err := storage.Persist(&gobShares, ACTIVE_SHARE_FILE)
if err == nil {
smartpool.Output.Printf("Done.\n")
} else {
smartpool.Output.Printf("Failed. (%s)\n", err.Error())
}
return err
}
func (cr *TimestampClaimRepo) AddShare(s smartpool.Share) error {
cr.mu.Lock()
defer cr.mu.Unlock()
share := s.(*Share)
shareID := fmt.Sprintf(
"%s-%s",
share.BlockHeader().Hash().Hex(),
share.Nonce())
if cr.activeShares[shareID] != nil {
return errors.New("duplicated share")
} else {
cr.activeShares[shareID] = share
}
if share.Timestamp().Cmp(cr.recentTimestamp) == 0 {
cr.noRecentShares++
} else if share.Timestamp().Cmp(cr.recentTimestamp) < 0 {
cr.noShares++
} else if share.Timestamp().Cmp(cr.recentTimestamp) > 0 {
cr.noShares += cr.noRecentShares
cr.noRecentShares = 1
cr.recentTimestamp = big.NewInt(0)
cr.recentTimestamp.Add(share.Timestamp(), common.Big0)
}
return nil
}
func (cr *TimestampClaimRepo) GetCurrentClaim(threshold int) smartpool.Claim {
cr.mu.Lock()
defer cr.mu.Unlock()
smartpool.Output.Printf("Have %d valid shares\n", cr.noShares)
smartpool.Output.Printf("Current timestamp: 0x%s\n", cr.recentTimestamp.Text(16))
smartpool.Output.Printf("Shares with current timestamp: %d\n", cr.noRecentShares)
if cr.noShares < uint64(threshold) {
return nil
}
c := protocol.NewClaim()
newActiveShares := map[string]*Share{}
for _, s := range cr.activeShares {
if s.Timestamp().Cmp(cr.recentTimestamp) < 0 {
c.AddShare(s)
} else {
shareID := fmt.Sprintf(
"%s-%s",
s.BlockHeader().Hash().Hex(),
s.Nonce())
newActiveShares[shareID] = s
}
}
cr.activeShares = newActiveShares
cr.noShares = 0
cr.Persist(cr.storage)
return c
}