forked from tanitall/memo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blocks.go
117 lines (112 loc) · 2.88 KB
/
blocks.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
package main_node
import (
"fmt"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/jchavannes/bchutil"
"github.com/jchavannes/btcd/wire"
"github.com/jchavannes/jgo/jerr"
"github.com/memocash/memo/app/bitcoin/transaction"
"github.com/memocash/memo/app/db"
"github.com/memocash/memo/app/obj/user_stats"
)
const MinCheckHeight = 525000
func onBlock(n *Node, msg *wire.MsgBlock) {
block := bchutil.NewBlock(msg)
dbBlock, err := db.GetBlockByHash(*block.Hash())
if err != nil {
jerr.Getf(err, "error getting dbBlock (%s)", block.Hash().String()).Print()
return
}
var memosSaved int
var txnsSaved int
for _, txn := range block.Transactions() {
savedTxn, savedMemo, err := transaction.ConditionallySaveTransaction(txn.MsgTx(), dbBlock, n.UserNode)
if err != nil {
jerr.Getf(err, "error conditionally saving transaction: %s", txn.Hash().String()).Print()
continue
}
if savedTxn {
txnsSaved++
}
if savedMemo {
memosSaved++
}
}
_, errors := transaction.ProcessNotifications()
for _, err := range errors {
fmt.Println(err.Error())
}
_, errors = transaction.UpdateRootTxHashes()
for _, err := range errors {
fmt.Println(err.Error())
}
go func() {
if n.BlocksSyncComplete {
err = user_stats.Populate()
if err != nil {
jerr.Get("error populating user stats", err).Print()
}
}
}()
fmt.Printf("Block - height: %5d (%s), found: %4d, saved: %4d, memos: %4d\n",
dbBlock.Height,
dbBlock.Timestamp.String(),
len(block.Transactions()),
txnsSaved,
memosSaved,
)
if dbBlock.Height == n.NodeStatus.HeightChecked + 1 {
n.NodeStatus.HeightChecked = dbBlock.Height
err = n.NodeStatus.Save()
}
if err != nil {
jerr.Get("error saving node status", err).Print()
return
}
n.BlocksQueued--
if n.BlocksQueued == 0 {
queueBlocks(n)
}
}
func queueBlocks(n *Node) {
if n.BlocksQueued != 0 {
return
}
if n.NodeStatus.HeightChecked < MinCheckHeight {
n.NodeStatus.HeightChecked = MinCheckHeight
}
blocks, err := db.GetBlocksInHeightRange(n.NodeStatus.HeightChecked+1, n.NodeStatus.HeightChecked+2000)
if err != nil {
jerr.Get("error getting blocks in height range", err).Print()
return
}
if len(blocks) == 0 {
if ! n.BlocksSyncComplete {
n.BlocksSyncComplete = true
transaction.DisableBatchPostProcessing()
fmt.Println("Block sync complete")
queueMempool(n)
}
return
}
msgGetData := wire.NewMsgGetData()
for _, block := range blocks {
err := msgGetData.AddInvVect(&wire.InvVect{
Type: wire.InvTypeBlock,
Hash: *block.GetChainhash(),
})
if err != nil {
jerr.Get("error adding inventory vector: %s\n", err).Print()
return
}
}
n.Peer.QueueMessage(msgGetData, nil)
n.BlocksQueued += len(msgGetData.InvList)
if n.BlocksQueued > 1 {
fmt.Printf("Blocks queued: %d\n", n.BlocksQueued)
}
}
func getBlock(n *Node, hash chainhash.Hash) {
getBlocks := wire.NewMsgGetBlocks(&hash)
n.Peer.QueueMessage(getBlocks, nil)
}