-
Notifications
You must be signed in to change notification settings - Fork 3
/
extraction.go
137 lines (123 loc) · 4.19 KB
/
extraction.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
package dbtypes
import (
"bytes"
"encoding/hex"
"fmt"
"github.com/dcrdata/dcrdata/txhelpers"
"github.com/decred/dcrd/blockchain/stake"
"github.com/decred/dcrd/chaincfg"
"github.com/decred/dcrd/txscript"
"github.com/decred/dcrd/wire"
)
// ExtractBlockTransactions extracts transaction information from a
// wire.MsgBlock and returns the processed information in slices of the dbtypes
// Tx, Vout, and VinTxPropertyARRAY.
func ExtractBlockTransactions(msgBlock *wire.MsgBlock, txTree int8,
chainParams *chaincfg.Params) ([]*Tx, [][]*Vout, []VinTxPropertyARRAY) {
dbTxs, dbTxVouts, dbTxVins := processTransactions(msgBlock, txTree,
chainParams)
if txTree != wire.TxTreeRegular && txTree != wire.TxTreeStake {
fmt.Printf("Invalid transaction tree: %v", txTree)
}
return dbTxs, dbTxVouts, dbTxVins
}
func processTransactions(msgBlock *wire.MsgBlock, tree int8,
chainParams *chaincfg.Params) ([]*Tx, [][]*Vout, []VinTxPropertyARRAY) {
var txs []*wire.MsgTx
switch tree {
case wire.TxTreeRegular:
txs = msgBlock.Transactions
case wire.TxTreeStake:
txs = msgBlock.STransactions
default:
return nil, nil, nil
}
blockHeight := msgBlock.Header.Height
blockHash := msgBlock.BlockHash()
blockTime := msgBlock.Header.Timestamp.Unix()
dbTransactions := make([]*Tx, 0, len(txs))
dbTxVouts := make([][]*Vout, len(txs))
dbTxVins := make([]VinTxPropertyARRAY, len(txs))
for txIndex, tx := range txs {
if txhelpers.IsStakeTx(tx) && tree != wire.TxTreeStake {
// You are doing it wrong
return nil, nil, nil
}
var spent, sent int64
for _, txin := range tx.TxIn {
spent += txin.ValueIn
}
for _, txout := range tx.TxOut {
sent += txout.Value
}
fees := spent - sent
dbTx := &Tx{
BlockHash: blockHash.String(),
BlockHeight: int64(blockHeight),
BlockTime: blockTime,
Time: blockTime, // TODO, receive time?
TxType: int16(stake.DetermineTxType(tx)),
Version: tx.Version,
Tree: tree,
TxID: tx.TxHash().String(),
BlockIndex: uint32(txIndex),
Locktime: tx.LockTime,
Expiry: tx.Expiry,
Size: uint32(tx.SerializeSize()),
Spent: spent,
Sent: sent,
Fees: fees,
NumVin: uint32(len(tx.TxIn)),
NumVout: uint32(len(tx.TxOut)),
}
//dbTx.Vins = make([]VinTxProperty, 0, dbTx.NumVin)
dbTxVins[txIndex] = make(VinTxPropertyARRAY, 0, len(tx.TxIn))
for idx, txin := range tx.TxIn {
dbTxVins[txIndex] = append(dbTxVins[txIndex], VinTxProperty{
PrevOut: txin.PreviousOutPoint.String(),
PrevTxHash: txin.PreviousOutPoint.Hash.String(),
PrevTxIndex: txin.PreviousOutPoint.Index,
PrevTxTree: uint16(txin.PreviousOutPoint.Tree),
Sequence: txin.Sequence,
ValueIn: uint64(txin.ValueIn),
TxID: dbTx.TxID,
TxIndex: uint32(idx),
TxTree: uint16(dbTx.Tree),
BlockHeight: txin.BlockHeight,
BlockIndex: txin.BlockIndex,
ScriptHex: txin.SignatureScript,
})
}
//dbTx.VinDbIds = make([]uint64, int(dbTx.NumVin))
// Vouts and their db IDs
dbTxVouts[txIndex] = make([]*Vout, 0, len(tx.TxOut))
//dbTx.Vouts = make([]*Vout, 0, len(tx.TxOut))
for io, txout := range tx.TxOut {
vout := Vout{
TxHash: dbTx.TxID,
TxIndex: uint32(io),
TxTree: tree,
Value: uint64(txout.Value),
Version: txout.Version,
ScriptPubKey: txout.PkScript,
}
scriptClass, scriptAddrs, reqSigs, err := txscript.ExtractPkScriptAddrs(
vout.Version, vout.ScriptPubKey, chainParams)
if err != nil && !bytes.Equal(vout.ScriptPubKey, chainParams.OrganizationPkScript) {
fmt.Println(len(vout.ScriptPubKey), err, hex.EncodeToString(vout.ScriptPubKey))
}
addys := make([]string, 0, len(scriptAddrs))
for ia := range scriptAddrs {
addys = append(addys, scriptAddrs[ia].String())
}
vout.ScriptPubKeyData.ReqSigs = uint32(reqSigs)
vout.ScriptPubKeyData.Type = scriptClass.String()
vout.ScriptPubKeyData.Addresses = addys
dbTxVouts[txIndex] = append(dbTxVouts[txIndex], &vout)
//dbTx.Vouts = append(dbTx.Vouts, &vout)
}
//dbTx.VoutDbIds = make([]uint64, len(dbTxVouts[txIndex]))
dbTransactions = append(dbTransactions, dbTx)
}
return dbTransactions, dbTxVouts, dbTxVins
}