/
extraction.go
166 lines (151 loc) · 5.33 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
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
package dbtypes
import (
"bytes"
"encoding/hex"
"fmt"
"github.com/decred/dcrd/blockchain/stake"
"github.com/decred/dcrd/chaincfg"
"github.com/decred/dcrd/txscript"
"github.com/decred/dcrd/wire"
"github.com/decred/dcrdata/v3/txhelpers"
)
// DevSubsidyAddress returns the development subsidy address for the specified
// network.
func DevSubsidyAddress(params *chaincfg.Params) (string, error) {
var devSubsidyAddress string
var err error
switch params.Name {
case "testnet2":
// TestNet2 uses an invalid organization PkScript
devSubsidyAddress = "TccTkqj8wFqrUemmHMRSx8SYEueQYLmuuFk"
err = fmt.Errorf("testnet2 has invalid project fund script")
default:
_, devSubsidyAddresses, _, err0 := txscript.ExtractPkScriptAddrs(
params.OrganizationPkScriptVersion, params.OrganizationPkScript, params)
if err0 != nil || len(devSubsidyAddresses) != 1 {
err = fmt.Errorf("failed to decode dev subsidy address: %v", err0)
} else {
devSubsidyAddress = devSubsidyAddresses[0].String()
}
}
return devSubsidyAddress, err
}
// 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, isValid, isMainchain bool) ([]*Tx, [][]*Vout, []VinTxPropertyARRAY) {
dbTxs, dbTxVouts, dbTxVins := processTransactions(msgBlock, txTree,
chainParams, isValid, isMainchain)
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,
isValid, isMainchain bool) ([]*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)),
IsValidBlock: isValid,
IsMainchainBlock: isMainchain,
}
//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: txin.ValueIn,
TxID: dbTx.TxID,
TxIndex: uint32(idx),
TxType: dbTx.TxType,
TxTree: uint16(dbTx.Tree),
Time: blockTime,
BlockHeight: txin.BlockHeight,
BlockIndex: txin.BlockIndex,
ScriptHex: txin.SignatureScript,
IsValid: isValid,
IsMainchain: isMainchain,
})
}
//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,
TxType: dbTx.TxType,
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
}