-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathexplorer_helpers.go
More file actions
315 lines (265 loc) · 7.74 KB
/
Copy pathexplorer_helpers.go
File metadata and controls
315 lines (265 loc) · 7.74 KB
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
// Copyright 2025 HOLOGRAM Project. All rights reserved.
// Explorer Helpers - Extracted from explorer_service.go for maintainability
// Session 87: Code restructuring
package main
import (
"encoding/hex"
"fmt"
"time"
"github.com/deroproject/derohe/block"
"github.com/deroproject/derohe/cryptography/crypto"
"github.com/deroproject/derohe/rpc"
"github.com/deroproject/derohe/transaction"
)
// ParsedTransaction holds extracted transaction data
type ParsedTransaction struct {
TxType string
MinerAddress string
HeightBuilt uint64
BLID string
RootHash string
BurnValue uint64
SCArgs interface{}
Assets []map[string]interface{}
IsCoinbase bool
}
// extractRingMembers extracts ring members from transaction data
func extractRingMembers(txData map[string]interface{}) []map[string]interface{} {
rings := []map[string]interface{}{}
ringData, ok := txData["ring"].([]interface{})
if !ok {
return rings
}
for idx, payload := range ringData {
payloadRing := map[string]interface{}{
"index": idx,
"members": []string{},
"count": 0,
}
if addresses, ok := payload.([]interface{}); ok {
members := []string{}
for _, addr := range addresses {
if addrStr, ok := addr.(string); ok {
members = append(members, addrStr)
}
}
payloadRing["members"] = members
payloadRing["count"] = len(members)
}
rings = append(rings, payloadRing)
}
return rings
}
// extractValidInvalidBlocks extracts valid and invalid block references
func extractValidInvalidBlocks(txData map[string]interface{}) (string, []string) {
validBlock := ""
if vb, ok := txData["valid_block"].(string); ok {
validBlock = vb
}
invalidBlocks := []string{}
if ib, ok := txData["invalid_block"].([]interface{}); ok {
for _, b := range ib {
if blockStr, ok := b.(string); ok {
invalidBlocks = append(invalidBlocks, blockStr)
}
}
}
return validBlock, invalidBlocks
}
// parseTxBytes deserializes transaction hex and extracts structured data
func parseTxBytes(txHex string, rings []map[string]interface{}, isMainnet bool) ParsedTransaction {
result := ParsedTransaction{
TxType: "UNKNOWN",
Assets: []map[string]interface{}{},
}
if txHex == "" {
return result
}
txBytes, err := hex.DecodeString(txHex)
if err != nil {
return result
}
var tx transaction.Transaction
if err := tx.Deserialize(txBytes); err != nil {
return result
}
result.TxType = getTxTypeName(tx.TransactionType)
result.HeightBuilt = tx.Height
result.BLID = fmt.Sprintf("%x", tx.BLID)
result.IsCoinbase = tx.TransactionType == transaction.COINBASE
// Extract rootHash from first payload
if len(tx.Payloads) >= 1 {
result.RootHash = fmt.Sprintf("%x", tx.Payloads[0].Statement.Roothash[:])
}
// Get burn value
if tx.TransactionType == transaction.BURN_TX {
result.BurnValue = tx.Value
}
// Extract miner address for coinbase/registration/premine
if tx.TransactionType == transaction.COINBASE ||
tx.TransactionType == transaction.REGISTRATION ||
tx.TransactionType == transaction.PREMINE {
var acckey crypto.Point
if err := acckey.DecodeCompressed(tx.MinerAddress[:]); err == nil {
astring := rpc.NewAddressFromKeys(&acckey)
astring.Mainnet = isMainnet
result.MinerAddress = astring.String()
}
}
// Extract SC args if SC_TX
if tx.TransactionType == transaction.SC_TX {
result.SCArgs = tx.SCDATA
}
// Build assets breakdown (per payload)
for i, payload := range tx.Payloads {
asset := map[string]interface{}{
"index": i,
"scid": payload.SCID.String(),
"fees": payload.Statement.Fees,
"burn": payload.BurnValue,
"ring_size": int(payload.Statement.RingSize),
}
// Add ring members for this asset
if i < len(rings) {
asset["ring"] = rings[i]["members"]
}
result.Assets = append(result.Assets, asset)
}
return result
}
// ExtractSCCodeFromDeploymentTx extracts SC code from an SC_INSTALL deployment transaction.
// For deployment TX, SCID = TXID, so we can get the code from the tx payload when GetSC returns empty
// (e.g. simulator daemon may not populate GetSC code).
func ExtractSCCodeFromDeploymentTx(txHex string) string {
if txHex == "" {
return ""
}
txBytes, err := hex.DecodeString(txHex)
if err != nil {
return ""
}
var tx transaction.Transaction
if err := tx.Deserialize(txBytes); err != nil {
return ""
}
if tx.TransactionType != transaction.SC_TX {
return ""
}
if !tx.SCDATA.Has(rpc.SCACTION, rpc.DataUint64) {
return ""
}
action := rpc.SC_ACTION(tx.SCDATA.Value(rpc.SCACTION, rpc.DataUint64).(uint64))
if action != rpc.SC_INSTALL {
return ""
}
if c, ok := tx.SCDATA.Value(rpc.SCCODE, rpc.DataString).(string); ok {
return c
}
return ""
}
// calculateTxAge calculates the age of a transaction from block data
func (a *App) calculateTxAge(blockHeight float64) (age string, blockTime string) {
if blockHeight <= 0 {
return "", ""
}
blockResult := a.GetBlock(int64(blockHeight))
if !blockResult["success"].(bool) {
return "", ""
}
blockData, ok := blockResult["block"].(map[string]interface{})
if !ok {
return "", ""
}
bh, ok := blockData["block_header"].(map[string]interface{})
if !ok {
return "", ""
}
ts, ok := bh["timestamp"].(float64)
if !ok {
return "", ""
}
timestamp := uint64(ts)
durationMs := uint64(time.Now().UTC().UnixMilli()) - timestamp
age = formatAge(time.Duration(durationMs) * time.Millisecond)
blockTime = time.Unix(0, int64(timestamp*uint64(time.Millisecond))).Format("2006-01-02 15:04:05 UTC")
return age, blockTime
}
// getMaxRingSize returns the maximum ring size from a slice of rings
func getMaxRingSize(rings []map[string]interface{}) int {
maxRingSize := 0
for _, r := range rings {
if count, ok := r["count"].(int); ok && count > maxRingSize {
maxRingSize = count
}
}
return maxRingSize
}
// calculateTxSize calculates transaction size from hex string
func calculateTxSize(txHex string) int64 {
if txHex == "" {
return 0
}
return int64(len(txHex) / 2) // hex is 2 chars per byte
}
// ParsedBlock holds extracted block data
type ParsedBlock struct {
TxHashes []string
MinerAddress string
BlockSize int64
}
// parseBlockBlob deserializes block blob and extracts structured data
func parseBlockBlob(blob string, isMainnet bool) ParsedBlock {
result := ParsedBlock{
TxHashes: []string{},
}
if blob == "" {
return result
}
blockBin, err := hex.DecodeString(blob)
if err != nil {
return result
}
var bl block.Block
if err := bl.Deserialize(blockBin); err != nil {
return result
}
result.BlockSize = int64(len(blockBin))
// Get TX hashes
for _, txHash := range bl.Tx_hashes {
result.TxHashes = append(result.TxHashes, txHash.String())
}
// Extract miner address from coinbase TX
if bl.Miner_TX.TransactionType == transaction.COINBASE ||
bl.Miner_TX.TransactionType == transaction.PREMINE {
var acckey crypto.Point
if err := acckey.DecodeCompressed(bl.Miner_TX.MinerAddress[:]); err == nil {
astring := rpc.NewAddressFromKeys(&acckey)
astring.Mainnet = isMainnet
result.MinerAddress = astring.String()
}
}
return result
}
// extractStringArray extracts a string array from interface slice
func extractStringArray(data interface{}) []string {
result := []string{}
if arr, ok := data.([]interface{}); ok {
for _, item := range arr {
if str, ok := item.(string); ok {
result = append(result, str)
}
}
}
return result
}
// calculateBlockAge calculates age from block timestamp
func calculateBlockAge(timestamp float64) (age string, blockTime string) {
if timestamp <= 0 {
return "", ""
}
ts := uint64(timestamp)
durationMs := uint64(time.Now().UTC().UnixMilli()) - ts
age = formatAge(time.Duration(durationMs) * time.Millisecond)
blockTime = time.Unix(0, int64(ts*uint64(time.Millisecond))).Format("2006-01-02 15:04:05 UTC")
return age, blockTime
}