/
addrindexer.go
460 lines (411 loc) · 15.9 KB
/
addrindexer.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
package blockchain
import (
"crypto/sha256"
"encoding/binary"
"fmt"
"sync"
"github.com/fatzero/mass-core/blockchain/state"
"github.com/fatzero/mass-core/consensus/forks"
"github.com/fatzero/mass-core/database"
"github.com/fatzero/mass-core/logging"
"github.com/fatzero/mass-core/massutil"
"github.com/fatzero/mass-core/txscript"
"github.com/fatzero/mass-core/wire"
"golang.org/x/crypto/ripemd160"
)
const (
txIndexKeyLen = sha256.Size + 4 + 4
btxIndexKeyLen = ripemd160.Size + 4 + 4 + 4
btxSpentIndexKeyLen = ripemd160.Size + 4 + 4 + 8 + 4 + 4 + 4
)
type Server interface {
Stop() error
}
// AddrIndexer provides a concurrent service for indexing the transactions of
// target blocks based on the addresses involved in the transaction.
type AddrIndexer struct {
// server Server
blockLogger *BlockProgressLogger
db database.Db
sync.Mutex
stateBindingDb state.Database
}
type shTxLoc map[[txIndexKeyLen]byte]struct{}
type btxIndex map[[btxIndexKeyLen]byte]struct{}
type btxSpentIndex map[[btxSpentIndexKeyLen]byte]struct{}
func mustEncodeTxIndexKey(scriptHash []byte, txOffset, txLen int) []byte {
key := make([]byte, txIndexKeyLen)
copy(key[:sha256.Size], scriptHash)
binary.LittleEndian.PutUint32(key[sha256.Size:sha256.Size+4], uint32(txOffset))
binary.LittleEndian.PutUint32(key[sha256.Size+4:txIndexKeyLen], uint32(txLen))
return key
}
func mustDecodeTxIndexKey(key [txIndexKeyLen]byte) ([sha256.Size]byte, int, int) {
var scriptHash [sha256.Size]byte
copy(scriptHash[:], key[:sha256.Size])
txOffset := binary.LittleEndian.Uint32(key[sha256.Size : sha256.Size+4])
txLen := binary.LittleEndian.Uint32(key[sha256.Size+4 : txIndexKeyLen])
return scriptHash, int(txOffset), int(txLen)
}
func mustEncodeGtxIndexKey(scriptHash []byte, txOffset, txLen, index int) []byte {
key := make([]byte, btxIndexKeyLen)
copy(key[:ripemd160.Size], scriptHash)
binary.LittleEndian.PutUint32(key[ripemd160.Size:ripemd160.Size+4], uint32(txOffset))
binary.LittleEndian.PutUint32(key[ripemd160.Size+4:ripemd160.Size+4+4], uint32(txLen))
binary.LittleEndian.PutUint32(key[ripemd160.Size+4+4:btxIndexKeyLen], uint32(index))
return key
}
func mustDecodeGtxIndexKey(key [btxIndexKeyLen]byte) ([ripemd160.Size]byte, int, int, uint32) {
var scriptHash [ripemd160.Size]byte
copy(scriptHash[:], key[:ripemd160.Size])
txOffset := binary.LittleEndian.Uint32(key[ripemd160.Size : ripemd160.Size+4])
txLen := binary.LittleEndian.Uint32(key[ripemd160.Size+4 : ripemd160.Size+4+4])
index := binary.LittleEndian.Uint32(key[ripemd160.Size+4+4 : btxIndexKeyLen])
return scriptHash, int(txOffset), int(txLen), index
}
func mustEncodeGtxSpentIndexKey(scriptHash []byte, stxTxOffset, stxTxLen int, btxHeight uint64, btxTxOffset, btxTxLen int, gtxTxOutIndex uint32) []byte {
key := make([]byte, btxSpentIndexKeyLen)
copy(key[:ripemd160.Size], scriptHash)
binary.LittleEndian.PutUint32(key[ripemd160.Size:ripemd160.Size+4], uint32(stxTxOffset))
binary.LittleEndian.PutUint32(key[ripemd160.Size+4:ripemd160.Size+4+4], uint32(stxTxLen))
binary.LittleEndian.PutUint64(key[ripemd160.Size+4+4:ripemd160.Size+4+4+8], btxHeight)
binary.LittleEndian.PutUint32(key[ripemd160.Size+4+4+8:ripemd160.Size+4+4+8+4], uint32(btxTxOffset))
binary.LittleEndian.PutUint32(key[ripemd160.Size+4+4+8+4:ripemd160.Size+4+4+8+4+4], uint32(btxTxLen))
binary.LittleEndian.PutUint32(key[ripemd160.Size+4+4+8+4+4:btxSpentIndexKeyLen], gtxTxOutIndex)
return key
}
func mustDecodeGtxSpentIndexKey(key [btxSpentIndexKeyLen]byte) ([ripemd160.Size]byte, int, int, uint64, int, int, uint32) {
var scriptHash [ripemd160.Size]byte
copy(scriptHash[:], key[:ripemd160.Size])
stxTxOffset := binary.LittleEndian.Uint32(key[ripemd160.Size : ripemd160.Size+4])
stxTxLen := binary.LittleEndian.Uint32(key[ripemd160.Size+4 : ripemd160.Size+4+4])
btxHeight := binary.LittleEndian.Uint64(key[ripemd160.Size+4+4 : ripemd160.Size+4+4+8])
btxTxOffset := binary.LittleEndian.Uint32(key[ripemd160.Size+4+4+8 : ripemd160.Size+4+4+8+4])
btxTxLen := binary.LittleEndian.Uint32(key[ripemd160.Size+4+4+8+4 : ripemd160.Size+4+4+8+4+4])
btxTxOutIndex := binary.LittleEndian.Uint32(key[ripemd160.Size+4+4+8+4+4 : btxSpentIndexKeyLen])
return scriptHash, int(stxTxOffset), int(stxTxLen), btxHeight, int(btxTxOffset), int(btxTxLen), btxTxOutIndex
}
// newAddrIndexer creates a new block address indexer.
// Use Start to begin processing incoming index jobs.
func NewAddrIndexer(db database.Db, stateBindingDb state.Database) (*AddrIndexer, error) {
_, _, err := db.FetchAddrIndexTip()
if err != nil && err != database.ErrAddrIndexDoesNotExist {
return nil, err
}
ai := &AddrIndexer{
db: db,
stateBindingDb: stateBindingDb,
// server: server,
blockLogger: NewBlockProgressLogger("process"),
}
return ai, nil
}
// SyncAttachBlock synchronously queues a newly solved block to have its
// transactions indexed by address.
func (a *AddrIndexer) SyncAttachBlock(bindingState state.Trie, block *massutil.Block, txStore TxStore) error {
a.Lock()
defer a.Unlock()
currentIndexSha, currentIndexTip, err := a.db.FetchAddrIndexTip()
if err != nil {
return err
}
if block.Height() == currentIndexTip+1 && block.MsgBlock().Header.Previous.IsEqual(currentIndexSha) {
sha := block.Hash()
height := block.Height()
txAddrIndex, btxAddrIndex, btxSpentIndex, err := a.indexBlockAddrs(bindingState, block, txStore)
if err != nil {
logging.CPrint(logging.PANIC,
"Unable to index transactions of block",
logging.LogFormat{
"block hash": sha.String(),
"height": height,
"error": err})
// a.stopServer()
return err
}
addrIndexData := &database.AddrIndexData{
TxIndex: txAddrIndex,
BindingTxIndex: btxAddrIndex,
BindingTxSpentIndex: btxSpentIndex,
}
err = a.db.SubmitAddrIndex(sha, height, addrIndexData)
if err != nil {
logging.CPrint(logging.PANIC, "Unable to write index for block",
logging.LogFormat{
"block hash": sha.String(),
"height": height,
"error": err,
})
// a.stopServer()
return err
}
a.logBlockHeight(block)
return nil
} else {
logging.CPrint(logging.ERROR, errUnexpectedHeight.Error(), logging.LogFormat{
"excepted height(insert)": currentIndexTip + 1,
"block Height": block.Height(),
"excepted previous hash(insert)": currentIndexSha,
"block hash": block.MsgBlock().Header.Previous,
})
return errUnexpectedHeight
}
}
func (a *AddrIndexer) deleteTxAddressIndex(blkSha *wire.Hash, blkHeight uint64) error {
currentIndexSha, currentIndexTip, err := a.db.FetchAddrIndexTip()
if err != nil {
return err
}
if blkHeight == currentIndexTip && blkSha.IsEqual(currentIndexSha) {
err := a.db.DeleteAddrIndex(blkSha, blkHeight)
if err != nil {
logging.CPrint(logging.PANIC, "Unable to write index for block",
logging.LogFormat{
"block hash": blkSha.String(),
"height": blkHeight,
"error": err,
})
// a.stopServer()
return err
}
return nil
} else {
logging.CPrint(logging.ERROR, errUnexpectedHeight.Error(), logging.LogFormat{
"excepted height(delete)": currentIndexTip,
"block height": blkHeight,
})
return errUnexpectedHeight
}
}
func indexScriptPubKeyForTxIn(txAddrIndex shTxLoc, btxSpentIndex btxSpentIndex, scriptPubKey []byte, locInBlock *wire.TxLoc,
blkHeightBefore uint64, txLocBefore *wire.TxLoc, txBeforeIndex uint32) error {
scriptClass, pops := txscript.GetScriptInfo(scriptPubKey)
switch scriptClass {
case txscript.WitnessV0ScriptHashTy, txscript.StakingScriptHashTy, txscript.BindingScriptHashTy:
default:
logging.CPrint(logging.DEBUG, "nonstandard tx")
return nil
}
if scriptClass == txscript.BindingScriptHashTy {
holderScriptHash, bindingScriptHash, err := txscript.GetParsedBindingOpcode(pops)
if err != nil {
logging.CPrint(logging.ERROR, "failed to parse binding opcode", logging.LogFormat{"error": err})
return err
}
if len(bindingScriptHash) != txscript.OP_DATA_20 {
logging.CPrint(logging.ERROR, "unexpected script length", logging.LogFormat{"expect": 20, "actual": len(bindingScriptHash)})
return ErrInvalidBindingScript
}
var txKey [txIndexKeyLen]byte
copy(txKey[:], mustEncodeTxIndexKey(holderScriptHash, locInBlock.TxStart, locInBlock.TxLen))
if _, ok := txAddrIndex[txKey]; !ok {
txAddrIndex[txKey] = struct{}{}
}
var btxsKey [btxSpentIndexKeyLen]byte
copy(btxsKey[:], mustEncodeGtxSpentIndexKey(bindingScriptHash, locInBlock.TxStart, locInBlock.TxLen, blkHeightBefore, txLocBefore.TxStart, txLocBefore.TxLen, txBeforeIndex))
if _, ok := btxSpentIndex[btxsKey]; !ok {
btxSpentIndex[btxsKey] = struct{}{}
}
} else {
// WitnessV0ScriptHashTy or StakingScriptHashTy
// TODO: more rigorous inspection
_, rsh, err := txscript.GetParsedOpcode(pops, scriptClass)
if err != nil {
logging.CPrint(logging.ERROR, "failed to parse opcode")
return err
}
var key [txIndexKeyLen]byte
copy(key[:], mustEncodeTxIndexKey(rsh[:], locInBlock.TxStart, locInBlock.TxLen))
if _, ok := txAddrIndex[key]; !ok {
txAddrIndex[key] = struct{}{}
}
}
return nil
}
func indexScriptPubKeyForTxOut(txAddrIndex shTxLoc, btxAddrIndex btxIndex, scriptPubKey []byte, locInBlock *wire.TxLoc, index int) error {
scriptClass, pops := txscript.GetScriptInfo(scriptPubKey)
switch scriptClass {
case txscript.WitnessV0ScriptHashTy, txscript.StakingScriptHashTy, txscript.BindingScriptHashTy:
default:
logging.CPrint(logging.DEBUG, "nonstandard tx")
return nil
}
if scriptClass == txscript.BindingScriptHashTy {
holderScriptHash, bindingScriptHash, err := txscript.GetParsedBindingOpcode(pops)
if err != nil {
logging.CPrint(logging.ERROR, "failed to parse binding opcode", logging.LogFormat{"error": err})
return err
}
if len(bindingScriptHash) != txscript.OP_DATA_20 {
return ErrInvalidBindingScript
}
var txAddrIndexKey [txIndexKeyLen]byte
copy(txAddrIndexKey[:], mustEncodeTxIndexKey(holderScriptHash, locInBlock.TxStart, locInBlock.TxLen))
if _, ok := txAddrIndex[txAddrIndexKey]; !ok {
txAddrIndex[txAddrIndexKey] = struct{}{}
}
var btxAddrIndexKey [btxIndexKeyLen]byte
copy(btxAddrIndexKey[:], mustEncodeGtxIndexKey(bindingScriptHash, locInBlock.TxStart, locInBlock.TxLen, index))
if _, ok := btxAddrIndex[btxAddrIndexKey]; !ok {
btxAddrIndex[btxAddrIndexKey] = struct{}{}
}
} else {
_, rsh, err := txscript.GetParsedOpcode(pops, scriptClass)
if err != nil {
logging.CPrint(logging.ERROR, "failed to parse opcode")
return err
}
var key [txIndexKeyLen]byte
copy(key[:], mustEncodeTxIndexKey(rsh[:], locInBlock.TxStart, locInBlock.TxLen))
if _, ok := txAddrIndex[key]; !ok {
txAddrIndex[key] = struct{}{}
}
}
return nil
}
func (a *AddrIndexer) indexBlockAddrs(bindingState state.Trie, blk *massutil.Block, txStore TxStore) (database.TxAddrIndex, database.BindingTxAddrIndex, database.BindingTxSpentAddrIndex, error) {
addrIndex := make(database.TxAddrIndex)
bindingTxAddrIndex := make(database.BindingTxAddrIndex)
bindingTxSpentIndex := make(database.BindingTxSpentAddrIndex)
txLocs, err := blk.TxLoc()
if err != nil {
return nil, nil, nil, err
}
txAddrIndex := make(shTxLoc)
btxSpentIndex := make(btxSpentIndex)
btxAddrIndex := make(btxIndex)
networkBinding, err := GetNetworkBinding(bindingState)
if err != nil {
return nil, nil, nil, err
}
oldNetworkBinding := networkBinding
enforceMassIp2WarmUp := forks.EnforceMASSIP0002WarmUp(blk.Height())
txRecord := make(map[wire.Hash]int)
for txIdx, tx := range blk.Transactions() {
// Tx's offset and length in the block.
txSha := tx.Hash()
txRecord[*txSha] = txIdx
locInBlock := &txLocs[txIdx]
// Coinbases don't have any inputs.
if !IsCoinBase(tx) {
// Index the SPK's of each input's previous outpoint
// transaction.
for _, txIn := range tx.MsgTx().TxIn {
// Lookup and fetch the referenced output's tx.
prevOut := txIn.PreviousOutPoint
txD, ok := txStore[prevOut.Hash]
if !ok {
return nil, nil, nil, fmt.Errorf("transaction %v not found",
prevOut.Hash)
}
if txD.Err != nil {
return nil, nil, nil, txD.Err
}
blkHeightBefore, txOffsetBefore, txLenBefore, err := a.db.GetUnspentTxData(&prevOut.Hash)
if err != nil {
txIdx, ok := txRecord[prevOut.Hash]
if !ok {
return nil, nil, nil, fmt.Errorf("transaction %v not found in both db and this block",
prevOut.Hash)
}
txBeforeLoc := txLocs[txIdx]
blkHeightBefore = blk.Height()
txOffsetBefore = txBeforeLoc.TxStart
txLenBefore = txBeforeLoc.TxLen
}
txBeforeLoc := &wire.TxLoc{
TxStart: txOffsetBefore,
TxLen: txLenBefore,
}
if !forks.EnforceMASSIP0002WarmUp(blkHeightBefore) {
err = indexScriptPubKeyForTxIn(txAddrIndex, btxSpentIndex, txD.Tx.MsgTx().TxOut[prevOut.Index].PkScript, locInBlock, blkHeightBefore, txBeforeLoc, prevOut.Index)
if err != nil {
// TODO: Assess the risk of this error
return nil, nil, nil, err
}
} else {
isBinding, err := indexTxInPkScriptForMassip2(bindingState, txD.Tx.MsgTx().TxOut[prevOut.Index], txAddrIndex, locInBlock)
if err != nil {
return nil, nil, nil, err
}
if isBinding {
// subtract
if networkBinding, err = networkBinding.AddInt((-1) * txD.Tx.MsgTx().TxOut[prevOut.Index].Value); err != nil {
return nil, nil, nil, err
}
}
}
}
}
for index, txOut := range tx.MsgTx().TxOut {
if !enforceMassIp2WarmUp {
err = indexScriptPubKeyForTxOut(txAddrIndex, btxAddrIndex, txOut.PkScript, locInBlock, index)
if err != nil {
return nil, nil, nil, err
}
} else {
isBinding, err := indexTxOutPkScriptForMassip2(bindingState, txOut, txAddrIndex, locInBlock)
if err != nil {
return nil, nil, nil, err
}
if isBinding {
// add
if networkBinding, err = networkBinding.AddInt(txOut.Value); err != nil {
return nil, nil, nil, err
}
}
}
}
// payload
if !IsCoinBase(tx) && enforceMassIp2WarmUp {
if err := CheckNonceAndSetPoolPkCoinbase(bindingState, tx.MsgTx().Payload); err != nil {
logging.CPrint(logging.ERROR, "failed to put pool pk coinbase", logging.LogFormat{
"block": blk.Hash(),
"height": blk.Height(),
"tx": tx.Hash(),
"err": err,
})
return nil, nil, nil, err
}
}
}
if enforceMassIp2WarmUp && oldNetworkBinding.Cmp(networkBinding) != 0 {
if err := PutNetworkBinding(bindingState, networkBinding); err != nil {
return nil, nil, nil, err
}
}
for key := range txAddrIndex {
scriptHash, txOffset, txLen := mustDecodeTxIndexKey(key)
addrIndex[scriptHash] = append(addrIndex[scriptHash], &wire.TxLoc{TxStart: txOffset, TxLen: txLen})
}
for key := range btxAddrIndex {
scriptHash, txOffset, txLen, index := mustDecodeGtxIndexKey(key)
txLoc := &wire.TxLoc{TxStart: txOffset, TxLen: txLen}
bindingTxAddrIndex[scriptHash] = append(bindingTxAddrIndex[scriptHash], &database.AddrIndexOutPoint{TxLoc: txLoc, Index: index})
}
for key := range btxSpentIndex {
scriptHash, stxTxOffset, stxTxLen, btxHeight, btxTxOffset, btxTxLen, btxTxOutIndex := mustDecodeGtxSpentIndexKey(key)
bindingTxSpentIndex[scriptHash] = append(bindingTxSpentIndex[scriptHash], &database.BindingTxSpent{
SpentTxLoc: &wire.TxLoc{TxStart: stxTxOffset, TxLen: stxTxLen},
BTxBlkHeight: btxHeight,
BTxLoc: &wire.TxLoc{TxStart: btxTxOffset, TxLen: btxTxLen},
BTxIndex: btxTxOutIndex,
})
}
return addrIndex, bindingTxAddrIndex, bindingTxSpentIndex, nil
}
func (a *AddrIndexer) SyncDetachBlock(block *massutil.Block) error {
a.Lock()
defer a.Unlock()
return a.deleteTxAddressIndex(block.Hash(), block.Height())
}
// func (a *AddrIndexer) stopServer() {
// go func() {
// if err := a.server.Stop(); err != nil {
// logging.CPrint(logging.FATAL, "AddrIndexer stop Server duet to error", logging.LogFormat{"err": err})
// }
// }()
// }
func (a *AddrIndexer) logBlockHeight(blk *massutil.Block) {
a.blockLogger.LogBlockHeight(blk)
}