forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
listener.go
181 lines (150 loc) · 4.92 KB
/
listener.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
package bitcoin
import (
"strings"
"time"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
"github.com/stellar/go/services/bifrost/common"
"github.com/stellar/go/support/errors"
"github.com/stellar/go/support/log"
)
func (l *Listener) Start() error {
l.log = common.CreateLogger("BitcoinListener")
l.log.Info("BitcoinListener starting")
genesisBlockHash, err := l.Client.GetBlockHash(0)
if err != nil {
return errors.Wrap(err, "Error getting genesis block")
}
if l.Testnet {
l.chainParams = &chaincfg.TestNet3Params
} else {
l.chainParams = &chaincfg.MainNetParams
}
if !genesisBlockHash.IsEqual(l.chainParams.GenesisHash) {
return errors.New("Invalid genesis hash")
}
blockNumber, err := l.Storage.GetBitcoinBlockToProcess()
if err != nil {
err = errors.Wrap(err, "Error getting bitcoin block to process from DB")
l.log.Error(err)
return err
}
if blockNumber == 0 {
blockNumberTmp, err := l.Client.GetBlockCount()
if err != nil {
err = errors.Wrap(err, "Error getting the block count from bitcoin-core")
l.log.Error(err)
return err
}
blockNumber = uint64(blockNumberTmp)
}
go l.processBlocks(blockNumber)
return nil
}
func (l *Listener) processBlocks(blockNumber uint64) {
l.log.Infof("Starting from block %d", blockNumber)
// Time when last new block has been seen
lastBlockSeen := time.Now()
missingBlockWarningLogged := false
for {
block, err := l.getBlock(blockNumber)
if err != nil {
l.log.WithFields(log.F{"err": err, "blockNumber": blockNumber}).Error("Error getting block")
time.Sleep(time.Second)
continue
}
// Block doesn't exist yet
if block == nil {
if time.Since(lastBlockSeen) > 20*time.Minute && !missingBlockWarningLogged {
l.log.Warn("No new block in more than 20 minutes")
missingBlockWarningLogged = true
}
time.Sleep(time.Second)
continue
}
// Reset counter when new block appears
lastBlockSeen = time.Now()
missingBlockWarningLogged = false
err = l.processBlock(block)
if err != nil {
l.log.WithFields(log.F{"err": err, "blockHash": block.Header.BlockHash().String()}).Error("Error processing block")
time.Sleep(time.Second)
continue
}
// Persist block number
err = l.Storage.SaveLastProcessedBitcoinBlock(blockNumber)
if err != nil {
l.log.WithField("err", err).Error("Error saving last processed block")
time.Sleep(time.Second)
// We continue to the next block.
// The idea behind this is if there was a problem with this single query we want to
// continue processing because it's safe to reprocess blocks and we don't want a downtime.
}
blockNumber++
}
}
// getBlock returns (nil, nil) if block has not been found (not exists yet)
func (l *Listener) getBlock(blockNumber uint64) (*wire.MsgBlock, error) {
blockHeight := int64(blockNumber)
blockHash, err := l.Client.GetBlockHash(blockHeight)
if err != nil {
if strings.Contains(err.Error(), "Block height out of range") {
// Block does not exist yet
return nil, nil
}
err = errors.Wrap(err, "Error getting block hash from bitcoin-core")
l.log.WithField("blockHeight", blockHeight).Error(err)
return nil, err
}
block, err := l.Client.GetBlock(blockHash)
if err != nil {
err = errors.Wrap(err, "Error getting block from bitcoin-core")
l.log.WithField("blockHash", blockHash.String()).Error(err)
return nil, err
}
return block, nil
}
func (l *Listener) processBlock(block *wire.MsgBlock) error {
transactions := block.Transactions
localLog := l.log.WithFields(log.F{
"blockHash": block.Header.BlockHash().String(),
"blockTime": block.Header.Timestamp,
"transactions": len(transactions),
})
localLog.Info("Processing block")
for _, transaction := range transactions {
transactionLog := localLog.WithField("transactionHash", transaction.TxHash().String())
for index, output := range transaction.TxOut {
class, addresses, _, err := txscript.ExtractPkScriptAddrs(output.PkScript, l.chainParams)
if err != nil {
// txscript.ExtractPkScriptAddrs returns error on non-standard scripts
// so this can be Warn.
transactionLog.WithField("err", err).Warn("Error extracting addresses")
continue
}
// We only support P2PK and P2PKH addresses
if class != txscript.PubKeyTy && class != txscript.PubKeyHashTy {
transactionLog.WithField("class", class).Debug("Unsupported addresses class")
continue
}
// Paranoid. We access address[0] later.
if len(addresses) != 1 {
transactionLog.WithField("addresses", addresses).Error("Invalid addresses length")
continue
}
handlerTransaction := Transaction{
Hash: transaction.TxHash().String(),
TxOutIndex: index,
ValueSat: output.Value,
To: addresses[0].EncodeAddress(),
}
err = l.TransactionHandler(handlerTransaction)
if err != nil {
return errors.Wrap(err, "Error processing transaction")
}
}
}
localLog.Info("Processed block")
return nil
}