-
Notifications
You must be signed in to change notification settings - Fork 226
/
main.go
521 lines (441 loc) · 13.7 KB
/
main.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
package main
import (
"fmt"
"github.com/piotrnar/gocoin"
"github.com/piotrnar/gocoin/client/common"
"github.com/piotrnar/gocoin/client/network"
"github.com/piotrnar/gocoin/client/rpcapi"
"github.com/piotrnar/gocoin/client/usif"
"github.com/piotrnar/gocoin/client/usif/textui"
"github.com/piotrnar/gocoin/client/usif/webui"
"github.com/piotrnar/gocoin/client/wallet"
"github.com/piotrnar/gocoin/lib/btc"
"github.com/piotrnar/gocoin/lib/chain"
"github.com/piotrnar/gocoin/lib/others/peersdb"
"github.com/piotrnar/gocoin/lib/others/qdb"
"github.com/piotrnar/gocoin/lib/others/sys"
"io/ioutil"
"os"
"os/signal"
"runtime"
"runtime/debug"
"time"
"unsafe"
)
var (
retryCachedBlocks bool
SaveBlockChain *time.Timer = time.NewTimer(24 * time.Hour)
)
const (
SaveBlockChainAfter = 2 * time.Second
)
func reset_save_timer() {
SaveBlockChain.Stop()
for len(SaveBlockChain.C) > 0 {
<-SaveBlockChain.C
}
SaveBlockChain.Reset(SaveBlockChainAfter)
}
func blockMined(bl *btc.Block) {
network.BlockMined(bl)
if int(bl.LastKnownHeight)-int(bl.Height) < 144 { // do not run it when syncing chain
usif.ProcessBlockFees(bl.Height, bl)
}
}
func LocalAcceptBlock(newbl *network.BlockRcvd) (e error) {
bl := newbl.Block
if common.FLAG.TrustAll || newbl.BlockTreeNode.Trusted {
bl.Trusted = true
}
common.BlockChain.Unspent.AbortWriting() // abort saving of UTXO.db
common.BlockChain.Blocks.BlockAdd(newbl.BlockTreeNode.Height, bl)
newbl.TmQueue = time.Now()
if newbl.DoInvs {
common.Busy()
network.NetRouteInv(network.MSG_BLOCK, bl.Hash, newbl.Conn)
}
network.MutexRcv.Lock()
bl.LastKnownHeight = network.LastCommitedHeader.Height
network.MutexRcv.Unlock()
e = common.BlockChain.CommitBlock(bl, newbl.BlockTreeNode)
if e == nil {
// new block accepted
newbl.TmAccepted = time.Now()
newbl.NonWitnessSize = bl.NoWitnessSize
common.RecalcAverageBlockSize()
common.Last.Mutex.Lock()
common.Last.Time = time.Now()
common.Last.Block = common.BlockChain.LastBlock()
common.Last.Mutex.Unlock()
reset_save_timer()
} else {
//fmt.Println("Warning: AcceptBlock failed. If the block was valid, you may need to rebuild the unspent DB (-r)")
new_end := common.BlockChain.LastBlock()
common.Last.Mutex.Lock()
common.Last.Block = new_end
common.Last.Mutex.Unlock()
// update network.LastCommitedHeader
network.MutexRcv.Lock()
if network.LastCommitedHeader != new_end {
network.LastCommitedHeader = new_end
//println("LastCommitedHeader moved to", network.LastCommitedHeader.Height)
}
network.DiscardedBlocks[newbl.Hash.BIdx()] = true
network.MutexRcv.Unlock()
}
return
}
func retry_cached_blocks() bool {
var idx int
common.CountSafe("RedoCachedBlks")
for idx < len(network.CachedBlocks) {
newbl := network.CachedBlocks[idx]
if CheckParentDiscarded(newbl.BlockTreeNode) {
common.CountSafe("DiscardCachedBlock")
if newbl.Block == nil {
os.Remove(common.TempBlocksDir() + newbl.BlockTreeNode.BlockHash.String())
}
network.CachedBlocks = append(network.CachedBlocks[:idx], network.CachedBlocks[idx+1:]...)
network.CachedBlocksLen.Store(len(network.CachedBlocks))
return len(network.CachedBlocks) > 0
}
if common.BlockChain.HasAllParents(newbl.BlockTreeNode) {
common.Busy()
if newbl.Block == nil {
tmpfn := common.TempBlocksDir() + newbl.BlockTreeNode.BlockHash.String()
dat, e := ioutil.ReadFile(tmpfn)
os.Remove(tmpfn)
if e != nil {
panic(e.Error())
}
if newbl.Block, e = btc.NewBlock(dat); e != nil {
panic(e.Error())
}
if e = newbl.Block.BuildTxList(); e != nil {
panic(e.Error())
}
newbl.Block.BlockExtraInfo = *newbl.BlockExtraInfo
}
e := LocalAcceptBlock(newbl)
if e != nil {
fmt.Println("AcceptBlock2", newbl.BlockTreeNode.BlockHash.String(), "-", e.Error())
newbl.Conn.Misbehave("LocalAcceptBl2", 250)
}
if usif.Exit_now.Get() {
return false
}
// remove it from cache
network.CachedBlocks = append(network.CachedBlocks[:idx], network.CachedBlocks[idx+1:]...)
network.CachedBlocksLen.Store(len(network.CachedBlocks))
return len(network.CachedBlocks) > 0
} else {
idx++
}
}
return false
}
// Return true iof the block's parent is on the DiscardedBlocks list
// Add it to DiscardedBlocks, if returning true
func CheckParentDiscarded(n *chain.BlockTreeNode) bool {
network.MutexRcv.Lock()
defer network.MutexRcv.Unlock()
if network.DiscardedBlocks[n.Parent.BlockHash.BIdx()] {
network.DiscardedBlocks[n.BlockHash.BIdx()] = true
return true
}
return false
}
// Called from the blockchain thread
func HandleNetBlock(newbl *network.BlockRcvd) {
defer func() {
common.CountSafe("MainNetBlock")
if common.GetUint32(&common.WalletOnIn) > 0 {
common.SetUint32(&common.WalletOnIn, 5) // snooze the timer to 5 seconds from now
}
}()
if CheckParentDiscarded(newbl.BlockTreeNode) {
common.CountSafe("DiscardFreshBlockA")
if newbl.Block == nil {
os.Remove(common.TempBlocksDir() + newbl.BlockTreeNode.BlockHash.String())
}
retryCachedBlocks = len(network.CachedBlocks) > 0
return
}
if !common.BlockChain.HasAllParents(newbl.BlockTreeNode) {
// it's not linking - keep it for later
network.CachedBlocks = append(network.CachedBlocks, newbl)
network.CachedBlocksLen.Store(len(network.CachedBlocks))
common.CountSafe("BlockPostone")
return
}
if newbl.Block == nil {
tmpfn := common.TempBlocksDir() + newbl.BlockTreeNode.BlockHash.String()
dat, e := ioutil.ReadFile(tmpfn)
os.Remove(tmpfn)
if e != nil {
panic(e.Error())
}
if newbl.Block, e = btc.NewBlock(dat); e != nil {
panic(e.Error())
}
if e = newbl.Block.BuildTxList(); e != nil {
panic(e.Error())
}
newbl.Block.BlockExtraInfo = *newbl.BlockExtraInfo
}
common.Busy()
if e := LocalAcceptBlock(newbl); e != nil {
common.CountSafe("DiscardFreshBlockB")
fmt.Println("AcceptBlock1", newbl.Block.Hash.String(), "-", e.Error())
newbl.Conn.Misbehave("LocalAcceptBl1", 250)
} else {
//println("block", newbl.Block.Height, "accepted")
retryCachedBlocks = retry_cached_blocks()
}
}
func HandleRpcBlock(msg *rpcapi.BlockSubmited) {
common.CountSafe("RPCNewBlock")
network.MutexRcv.Lock()
rb := network.ReceivedBlocks[msg.Block.Hash.BIdx()]
network.MutexRcv.Unlock()
if rb == nil {
panic("Block " + msg.Block.Hash.String() + " not in ReceivedBlocks map")
}
common.BlockChain.Unspent.AbortWriting()
rb.TmQueue = time.Now()
e, _, _ := common.BlockChain.CheckBlock(msg.Block)
if e == nil {
e = common.BlockChain.AcceptBlock(msg.Block)
rb.TmAccepted = time.Now()
}
if e != nil {
common.CountSafe("RPCBlockError")
msg.Error = e.Error()
msg.Done.Done()
return
}
network.NetRouteInv(network.MSG_BLOCK, msg.Block.Hash, nil)
common.RecalcAverageBlockSize()
common.CountSafe("RPCBlockOK")
println("New mined block", msg.Block.Height, "accepted OK in", rb.TmAccepted.Sub(rb.TmQueue).String())
common.Last.Mutex.Lock()
common.Last.Time = time.Now()
common.Last.Block = common.BlockChain.LastBlock()
common.Last.Mutex.Unlock()
msg.Done.Done()
}
func main() {
var ptr *byte
if unsafe.Sizeof(ptr) < 8 {
fmt.Println("WARNING: Gocoin client shall be build for 64-bit arch. It will likely crash now.")
}
fmt.Println("Gocoin client version", gocoin.Version)
runtime.GOMAXPROCS(runtime.NumCPU()) // It seems that Go does not do it by default
// Disable Ctrl+C
signal.Notify(common.KillChan, os.Interrupt, os.Kill)
defer func() {
if r := recover(); r != nil {
err, ok := r.(error)
if !ok {
err = fmt.Errorf("pkg: %v", r)
}
fmt.Println("main panic recovered:", err.Error())
fmt.Println(string(debug.Stack()))
network.NetCloseAll()
common.CloseBlockChain()
peersdb.ClosePeerDB()
sys.UnlockDatabaseDir()
os.Exit(1)
}
}()
common.InitConfig()
if common.FLAG.SaveConfig {
common.SaveConfig()
fmt.Println("Configuration file saved")
os.Exit(0)
}
if common.FLAG.VolatileUTXO {
fmt.Println("WARNING! Using UTXO database in a volatile mode. Make sure to close the client properly (do not kill it!)")
}
if common.FLAG.TrustAll {
fmt.Println("WARNING! Assuming all scripts inside new blocks to PASS. Verify the last block's hash when finished.")
}
host_init() // This will create the DB lock file and keep it open
os.RemoveAll(common.TempBlocksDir())
common.MkTempBlocksDir()
if common.FLAG.UndoBlocks > 0 {
usif.Exit_now.Set()
}
if common.FLAG.Rescan && common.FLAG.VolatileUTXO {
fmt.Println("UTXO database rebuilt complete in the volatile mode, so flush DB to disk and exit...")
} else if !usif.Exit_now.Get() {
common.RecalcAverageBlockSize()
peersTick := time.Tick(5 * time.Minute)
netTick := time.Tick(time.Second)
reset_save_timer() // we wil do one save try after loading, in case if ther was a rescan
peersdb.Testnet = common.Testnet
peersdb.ConnectOnly = common.CFG.ConnectOnly
peersdb.Services = common.Services
peersdb.InitPeers(common.GocoinHomeDir)
if common.FLAG.UnbanAllPeers {
var keys []qdb.KeyType
var vals [][]byte
peersdb.PeerDB.Browse(func(k qdb.KeyType, v []byte) uint32 {
peer := peersdb.NewPeer(v)
if peer.Banned != 0 {
fmt.Println("Unban", peer.NetAddr.String())
peer.Banned = 0
keys = append(keys, k)
vals = append(vals, peer.Bytes())
}
return 0
})
for i := range keys {
peersdb.PeerDB.Put(keys[i], vals[i])
}
fmt.Println(len(keys), "peers un-baned")
}
for k, v := range common.BlockChain.BlockIndex {
network.ReceivedBlocks[k] = &network.OneReceivedBlock{TmStart: time.Unix(int64(v.Timestamp()), 0)}
}
network.LastCommitedHeader = common.Last.Block
if common.CFG.TXPool.SaveOnDisk {
network.MempoolLoad2()
}
if common.CFG.TextUI_Enabled {
go textui.MainThread()
}
if common.CFG.WebUI.Interface != "" {
fmt.Println("Starting WebUI at", common.CFG.WebUI.Interface)
go webui.ServerThread(common.CFG.WebUI.Interface)
}
if common.CFG.RPC.Enabled {
go rpcapi.StartServer(common.RPCPort())
}
usif.LoadBlockFees()
wallet.FetchingBalanceTick = func() bool {
select {
case rec := <-usif.LocksChan:
common.CountSafe("DoMainLocks")
rec.In.Done()
rec.Out.Wait()
case newtx := <-network.NetTxs:
common.CountSafe("DoMainNetTx")
network.HandleNetTx(newtx, false)
case <-netTick:
common.CountSafe("DoMainNetTick")
network.NetworkTick()
case on := <-wallet.OnOff:
if !on {
return true
}
default:
}
return usif.Exit_now.Get()
}
startup_ticks := 5 // give 5 seconds for finding out missing blocks
if !common.FLAG.NoWallet {
// snooze the timer to 10 seconds after startup_ticks goes down
common.SetUint32(&common.WalletOnIn, 10)
}
for !usif.Exit_now.Get() {
common.Busy()
common.CountSafe("MainThreadLoops")
for retryCachedBlocks {
retryCachedBlocks = retry_cached_blocks()
// We have done one per loop - now do something else if pending...
if len(network.NetBlocks) > 0 || len(usif.UiChannel) > 0 {
break
}
}
// first check for priority messages; kill signal or a new block
select {
case <-common.KillChan:
common.Busy()
usif.Exit_now.Set()
continue
case newbl := <-network.NetBlocks:
common.Busy()
HandleNetBlock(newbl)
case rpcbl := <-rpcapi.RpcBlocks:
common.Busy()
HandleRpcBlock(rpcbl)
default: // timeout immediatelly if no priority message
}
common.Busy()
select {
case <-common.KillChan:
common.Busy()
usif.Exit_now.Set()
continue
case newbl := <-network.NetBlocks:
common.Busy()
HandleNetBlock(newbl)
case rpcbl := <-rpcapi.RpcBlocks:
common.Busy()
HandleRpcBlock(rpcbl)
case rec := <-usif.LocksChan:
common.Busy()
common.CountSafe("MainLocks")
rec.In.Done()
rec.Out.Wait()
case <-SaveBlockChain.C:
common.Busy()
common.CountSafe("SaveBlockChain")
if common.BlockChain.Idle() {
common.CountSafe("ChainIdleUsed")
}
case newtx := <-network.NetTxs:
common.Busy()
common.CountSafe("MainNetTx")
network.HandleNetTx(newtx, false)
case <-netTick:
common.Busy()
common.CountSafe("MainNetTick")
network.NetworkTick()
if startup_ticks > 0 {
startup_ticks--
break
}
if !common.BlockChainSynchronized && network.BlocksToGetCnt() == 0 &&
len(network.NetBlocks) == 0 && network.CachedBlocksLen.Get() == 0 {
// only when we have no pending blocks startup_ticks go down..
common.SetBool(&common.BlockChainSynchronized, true)
}
if common.WalletPendingTick() {
wallet.OnOff <- true
}
case cmd := <-usif.UiChannel:
common.Busy()
common.CountSafe("MainUICmd")
cmd.Handler(cmd.Param)
cmd.Done.Done()
case <-peersTick:
common.Busy()
peersdb.ExpirePeers()
usif.ExpireBlockFees()
case on := <-wallet.OnOff:
common.Busy()
if on {
wallet.LoadBalance()
} else {
wallet.Disable()
common.SetUint32(&common.WalletOnIn, 0)
}
}
}
common.BlockChain.Unspent.HurryUp()
wallet.UpdateMapSizes()
network.NetCloseAll()
}
sta := time.Now()
common.CloseBlockChain()
if common.FLAG.UndoBlocks == 0 {
network.MempoolSave(false)
}
fmt.Println("Blockchain closed in", time.Now().Sub(sta).String())
peersdb.ClosePeerDB()
usif.SaveBlockFees()
sys.UnlockDatabaseDir()
os.RemoveAll(common.TempBlocksDir())
}