-
Notifications
You must be signed in to change notification settings - Fork 42
/
script.go
168 lines (148 loc) · 4.97 KB
/
script.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
package upgrademanager
import (
"encoding/hex"
"github.com/lbryio/chainquery/daemon/processing"
"github.com/lbryio/chainquery/lbrycrd"
"github.com/lbryio/chainquery/model"
"github.com/lbryio/chainquery/util"
"github.com/sirupsen/logrus"
"github.com/volatiletech/sqlboiler/v4/boil"
"github.com/volatiletech/sqlboiler/v4/queries/qm"
)
func reProcessAllClaims() {
outputs := model.Outputs(qm.Where(model.OutputColumns.Type+" =?", lbrycrd.NonStandard),
qm.Select(model.OutputColumns.TransactionHash)).AllGP()
for i, output := range outputs {
processClaimOut(i, len(outputs), output.TransactionHash)
}
}
func processClaimOut(index int, total int, txHash string) {
tx, err := model.Transactions(qm.Where(model.TransactionColumns.Hash+"=?", txHash),
qm.Select(model.TransactionColumns.Hash, model.TransactionColumns.BlockHashID)).OneG()
if err != nil {
logrus.Panic(err)
}
txResult, err := lbrycrd.GetRawTransactionResponse(tx.Hash)
if err != nil {
logrus.Panic(err)
}
block, err := model.Blocks(qm.Where(model.BlockColumns.Hash+"=?", txResult.BlockHash)).OneG()
if err != nil {
logrus.Panic(err)
}
if index%50 == 0 {
logrus.Info("(", index, "/", total, ")", "Processing@Height ", block.Height)
}
err = processing.ProcessTx(txResult, block.BlockTime, block.Height)
if err != nil {
logrus.Error("Reprocess Claim Error: ", err)
}
}
func setClaimAddresses() {
type claimForClaimAddress struct {
ID uint64 `boil:"id"`
ScriptPubKeyHex string `boil:"script_pub_key_hex"`
ClaimAddress string `boil:"claim_address"`
}
rows, err := boil.GetDB().Query(`
SELECT claim.id,output.script_pub_key_hex FROM claim
LEFT JOIN output ON output.transaction_hash = claim.transaction_hash_id AND output.vout = claim.vout
WHERE claim_address = ''`)
if err != nil {
logrus.Panic("Error During Upgrade: ", err)
}
defer util.CloseRows(rows)
var slice []claimForClaimAddress
for rows.Next() {
var claimForCA claimForClaimAddress
err = rows.Scan(&claimForCA.ID, &claimForCA.ScriptPubKeyHex)
if err != nil {
logrus.Panic("Error During Upgrade: ", err)
}
slice = append(slice, claimForCA)
}
for i, claimAddress := range slice {
if i%1000 == 0 {
logrus.Info("Processing: ", "(", i, "/", len(slice), ")")
}
claim := model.Claim{ID: claimAddress.ID}
scriptBytes, err := hex.DecodeString(claimAddress.ScriptPubKeyHex)
if err != nil {
logrus.Panic("Error During Upgrade: ", err)
}
var pkscript []byte
if lbrycrd.IsClaimScript(scriptBytes) {
if lbrycrd.IsClaimNameScript(scriptBytes) {
_, _, pkscript, err = lbrycrd.ParseClaimNameScript(scriptBytes)
} else if lbrycrd.IsClaimUpdateScript(scriptBytes) {
_, _, _, pkscript, err = lbrycrd.ParseClaimUpdateScript(scriptBytes)
} else if lbrycrd.IsClaimSupportScript(scriptBytes) {
_, _, _, pkscript, err = lbrycrd.ParseClaimSupportScript(scriptBytes)
} else {
continue
}
if err != nil {
logrus.Error("Error Parsing claim script: ", err)
continue
}
pksAddress := lbrycrd.GetAddressFromPublicKeyScript(pkscript)
claim.ClaimAddress = pksAddress
if err := claim.UpdateG(boil.Whitelist(model.ClaimColumns.ClaimAddress)); err != nil {
logrus.Error("Saving Claim Address Error: ", err)
}
}
}
}
func setBlockHeightOnAllClaims() {
type claimInfo struct {
ID uint64 `boil:"id"`
height uint64 `boil:"height"`
}
rows, err := boil.GetDB().Query(`
SELECT c.id,b.height
FROM claim c
LEFT JOIN transaction t on c.transaction_hash_id = t.hash
LEFT JOIN block b on b.hash = t.block_hash_id
WHERE c.height = 0`)
if err != nil {
logrus.Panic("Error During Upgrade: ", err)
}
defer util.CloseRows(rows)
var slice []claimInfo
for rows.Next() {
var info claimInfo
err = rows.Scan(&info.ID, &info.height)
if err != nil {
logrus.Panic("Error During Upgrade: ", err)
}
slice = append(slice, info)
}
for i, info := range slice {
if i%1000 == 0 {
logrus.Info("Processing: ", "(", i, "/", len(slice), ")")
}
claim := model.Claim{ID: info.ID, Height: uint(info.height)}
if err := claim.UpdateG(boil.Whitelist(model.ClaimColumns.Height)); err != nil {
println(err)
}
}
}
func reProcessAllClaimsFromHeight(height uint) {
transaction := model.TableNames.Transaction
txHash := transaction + "." + model.TransactionColumns.Hash
outputTxHash := model.TableNames.Output + "." + model.OutputColumns.TransactionHash
block := model.TableNames.Block
blockHash := block + "." + model.BlockColumns.Hash
blockHeight := block + "." + model.BlockColumns.Height
txBlockHash := transaction + "." + model.TransactionColumns.BlockHashID
outputs := model.Outputs(
qm.Select(model.OutputColumns.TransactionHash, model.BlockColumns.Height),
qm.InnerJoin(transaction+" on "+txHash+" = "+outputTxHash),
qm.InnerJoin(block+" on "+txBlockHash+" = "+blockHash),
qm.Where(model.OutputColumns.Type+" =?", lbrycrd.NonStandard),
qm.And(blockHeight+" >= ?", height),
).AllGP()
for i, output := range outputs {
processClaimOut(i, len(outputs), output.TransactionHash)
}
}