-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
411 lines (381 loc) · 12.1 KB
/
Copy pathmain_test.go
File metadata and controls
411 lines (381 loc) · 12.1 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
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
package main
import (
"bytes"
"encoding/hex"
"encoding/json"
"fmt"
"log"
"os"
"testing"
"time"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/chaincfg"
//"github.com/btcsuite/btcd/wire"
"github.com/bitcoinsv/bsvd/wire"
"github.com/cockroachdb/pebble"
"github.com/metaid/utxo_indexer/blockchain"
"github.com/metaid/utxo_indexer/config"
"github.com/metaid/utxo_indexer/mempool"
"github.com/metaid/utxo_indexer/storage"
"github.com/metaid/utxo_indexer/syslogs"
)
func TestDefaultMerger(t *testing.T) {
opts := &pebble.Options{}
// Create a temporary database directory
dir := "data/test_merge"
db, err := pebble.Open(dir, opts)
if err != nil {
t.Fatalf("failed to open db: %v", err)
}
defer func() {
db.Close()
_ = os.RemoveAll(dir)
}()
key := []byte("key")
// First Merge
if err := db.Merge(key, []byte("hello"), pebble.Sync); err != nil {
t.Fatalf("merge 1 failed: %v", err)
}
// Second Merge
if err := db.Merge(key, []byte(",world"), pebble.Sync); err != nil {
t.Fatalf("merge 2 failed: %v", err)
}
// Get the final value
value, closer, err := db.Get(key)
if err != nil {
t.Fatalf("get failed: %v", err)
}
defer closer.Close()
expected := "hello,world"
if string(value) != expected {
t.Errorf("expected %q, got %q", expected, value)
} else {
fmt.Printf("Merge result: %s\n", value)
}
}
func TestMerger(t *testing.T) {
cfg, err := config.LoadConfig("config.yaml")
if err != nil {
log.Fatalf("Failed to load config: %v", err)
}
// Create auto configuration
params := config.AutoConfigure(config.SystemResources{
CPUCores: cfg.CPUCores, // 16-core CPU
MemoryGB: cfg.MemoryGB, // 64GB memory
HighPerf: cfg.HighPerf, // Prefer performance
ShardCount: cfg.ShardCount,
})
addressStore, err := storage.NewPebbleStore(params, cfg.DataDir, storage.StoreTypeIncome, cfg.ShardCount)
if err != nil {
log.Fatalf("Failed to initialize address store: %v", err)
}
defer addressStore.Close()
data1 := map[string][]string{
"key1": {"value1-1", "value1-2"},
"key2": {"value2-1"},
}
addressStore.BulkMergeMapConcurrent(&data1, 1)
data2 := map[string][]string{
"key1": {"value1-3", "value1-4"},
"key2": {"value2-1"},
}
addressStore.BulkMergeMapConcurrent(&data2, 1)
v, err := addressStore.Get([]byte("key1"))
fmt.Println(err, string(v))
addressStore.Delete([]byte("key1"))
addressStore.Delete([]byte("key2"))
}
func TestGetTx(t *testing.T) {
cfg, err := config.LoadConfig("config.yaml")
if err != nil {
log.Fatalf("Failed to load config: %v", err)
}
client, err := blockchain.NewClient(cfg)
if err != nil {
log.Fatalf("Failed to create blockchain client: %v", err)
}
tx, err := client.GetRawTransaction("4e2a33ebdd11d2c32f9d7b05ac5ca745cd1dae68700a5f3fc5b7d3ef139d30cb")
if err != nil {
log.Fatalf("Failed to get transaction: %v", err)
}
net, _ := cfg.GetChainParams()
fmt.Println("===============OUT======================")
for _, txOut := range tx.MsgTx().TxOut {
_, addrs, _, err := txscript.ExtractPkScriptAddrs(txOut.PkScript, net)
var address string
if err == nil && len(addrs) > 0 {
address = addrs[0].EncodeAddress()
} else {
// Fallback for non-standard scripts
address = "errAddress"
}
fmt.Println(address, txOut.Value)
}
}
func TestGetBestBlock(t *testing.T) {
cfg, err := config.LoadConfig("config_btc.yaml")
if err != nil {
log.Fatalf("Failed to load config: %v", err)
}
client, err := blockchain.NewClient(cfg)
if err != nil {
log.Fatalf("Failed to create blockchain client: %v", err)
}
cnt, err := client.GetBlockCount()
fmt.Println(err, cnt)
}
func TestTransaction(t *testing.T) {
cfg, err := config.LoadConfig("config.yaml")
if err != nil {
log.Fatalf("Failed to load config: %v", err)
}
client, err := blockchain.NewClient(cfg)
if err != nil {
log.Fatalf("Failed to create blockchain client: %v", err)
}
tx, err := client.GetRawTransaction("6deaac29be2cb13f69b1b5afb4b8e9f85ce4efb712fbcd1e2c5ea6cc35c1fde6")
if err != nil {
log.Fatalf("Failed to get transaction: %v", err)
}
net, _ := cfg.GetChainParams()
fmt.Println("===============IN======================")
for _, txIn := range tx.MsgTx().TxIn {
fmt.Println("==>txIn", txIn.PreviousOutPoint.Hash.String(), txIn.PreviousOutPoint.Index)
prevTx, err := client.GetRawTransaction(txIn.PreviousOutPoint.Hash.String())
if err != nil {
log.Fatalf("Failed to get previous transaction: %v", err)
}
if int(txIn.PreviousOutPoint.Index) >= len(prevTx.MsgTx().TxOut) {
log.Fatalf("Index out of range for previous transaction outputs")
}
prevTxOut := prevTx.MsgTx().TxOut[txIn.PreviousOutPoint.Index]
_, addrs, _, err := txscript.ExtractPkScriptAddrs(prevTxOut.PkScript, net)
var address string
if err == nil && len(addrs) > 0 {
address = addrs[0].EncodeAddress()
} else {
// Fallback for non-standard scripts
address = "errAddress"
}
fmt.Println(" ==from address:", address, "amount:", prevTxOut.Value)
}
fmt.Println("===============OUT======================")
for _, txOut := range tx.MsgTx().TxOut {
_, addrs, _, err := txscript.ExtractPkScriptAddrs(txOut.PkScript, net)
var address string
if err == nil && len(addrs) > 0 {
address = addrs[0].EncodeAddress()
} else {
// Fallback for non-standard scripts
address = "errAddress"
}
fmt.Println(address, txOut.Value)
}
}
func TestGetBlock(t *testing.T) {
cfg, err := config.LoadConfig("config.yaml")
if err != nil {
log.Fatalf("Failed to load config: %v", err)
}
client, err := blockchain.NewClient(cfg)
if err != nil {
log.Fatalf("Failed to create blockchain client: %v", err)
}
hash, err := client.GetBlockHash(200)
if err != nil {
log.Fatalf("Failed to get block hash: %v", err)
}
block1, err := client.GetBlockOnlyTxId(hash)
if err != nil {
log.Fatalf("Failed to get block only tx id: %v", err)
}
fmt.Println(block1.Height, ",txNUm:", len(block1.Tx))
// for _, tx := range block1.Tx {
// fmt.Println("==>tx", tx.Txid)
// for _, in := range tx.Vin {
// fmt.Println(" ==in", in.Txid, in.Vout)
// }
// }
// fmt.Println("=====================================")
block2, _ := client.GetBlock2(hash)
for _, tx := range block2.Transactions() {
for _, in := range tx.MsgTx().TxIn {
fmt.Println("==>processSpend2", in.PreviousOutPoint.Hash.String(), in.PreviousOutPoint.Index)
}
// for _,out := range tx.MsgTx().TxOut {
// }
}
// size1 := 0
// var txhash []string
// for i := 557; i < 558; i++ {
// hash, _ := client.GetBlockHash(int64(i))
// fmt.Printf("Checking block at height %d, hash: %s\n", i, hash)
// block1, _ := client.GetBlock(hash)
// fmt.Printf("GetBlock returned %d transactions\n", len(block1.Tx))
// for _, tx := range block1.Tx {
// hash := tx.Hash
// fmt.Printf("Transaction hash: %s,%s\n", hash, tx.Txid)
// txhash = append(txhash, hash)
// if hash == "eb6130a2d2a528c06f7285beff07616d2349d37d4c510bd561e81d453281397f" {
// fmt.Printf("find tx (converted hash) at height %d: %s\n", i, hash)
// fmt.Printf("Full tx data: %+v\n", tx)
// }
// }
// size1 += len(block1.Tx)
// }
// fmt.Println("size1:", size1)
// fmt.Println("txhash:", len(txhash))
// size2 := 0
// for i := 557; i < 558; i++ {
// hash, _ := client.GetBlockHash(int64(i))
// block2, _ := client.GetBlock2(hash)
// fmt.Printf("GetBlock2 returned %d transactions\n", len(block2.Transactions()))
// for _, tx := range block2.Transactions() {
// hashStr := tx.Hash().String()
// if hashStr == "eb6130a2d2a528c06f7285beff07616d2349d37d4c510bd561e81d453281397f" {
// fmt.Printf("find tx2 (direct compare) at height %d: %s\n", i, hashStr)
// fmt.Printf("Full tx data: %+v\n", tx)
// }
// }
// size2 += len(block2.Transactions())
// }
// fmt.Println("size2:", size2)
}
// reverseHexString reverses the byte order of a hexadecimal string
func reverseHexString(s string) string {
b, err := hex.DecodeString(s)
if err != nil {
return s
}
for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
b[i], b[j] = b[j], b[i]
}
return hex.EncodeToString(b)
}
func TestGetUtxoDb(t *testing.T) {
cfg, err := config.LoadConfig("config.yaml")
if err != nil {
log.Fatalf("Failed to load config: %v", err)
}
fmt.Println(cfg.RPC.Chain)
// Create auto configuration
params := config.AutoConfigure(config.SystemResources{
CPUCores: cfg.CPUCores, // 16-core CPU
MemoryGB: cfg.MemoryGB, // 64GB memory
HighPerf: cfg.HighPerf, // Prefer performance
ShardCount: cfg.ShardCount,
})
utxoStore, err := storage.NewPebbleStore(params, cfg.DataDir, storage.StoreTypeUTXO, cfg.ShardCount)
if err != nil {
log.Fatalf("Failed to initialize UTXO store: %v", err)
}
defer utxoStore.Close()
key := "135db46fc8e8efe42b77c13f5b7ed91b1ce18068bcd84cfd935715bdfef45275"
value, err := utxoStore.Get([]byte(key))
fmt.Println(string(value), err)
}
func TestGetNodeData(t *testing.T) {
cfg, err := config.LoadConfig("config.yaml")
if err != nil {
log.Fatalf("Failed to load config: %v", err)
}
client, err := blockchain.NewClient(cfg)
if err != nil {
log.Fatalf("Failed to create blockchain client: %v", err)
}
height := int64(121051)
hash, _ := client.GetBlockHash(height)
block1, _ := client.GetBlock(hash)
for _, tx := range block1.Tx {
fmt.Println("1==>txID", tx.Txid)
fmt.Println("1==>txHash", tx.Hash)
}
resp, err := client.Rpc.RawRequest("getblock", []json.RawMessage{
json.RawMessage(fmt.Sprintf("\"%s\"", hash.String())),
json.RawMessage("0"),
})
if err != nil {
log.Printf("Failed to get raw block data, height %d: %v, retrying in 3 seconds...", height, err)
}
var blockHex string
if err := json.Unmarshal(resp, &blockHex); err != nil {
log.Println("Failed to parse raw block data, height, retrying in 3 seconds...", height, err)
}
blockBytes, err := hex.DecodeString(blockHex)
if err != nil {
log.Println("Block hex decode failed, height", height, err)
}
msgBlock := &wire.MsgBlock{}
if err := msgBlock.Deserialize(bytes.NewReader(blockBytes)); err != nil {
log.Println("Block deserialization failed,", height, err)
}
for _, tx := range msgBlock.Transactions {
fmt.Println("2==>txID", tx.TxHash().String())
fmt.Println("2==>txHash", tx.TxHash().String())
for _, in := range tx.TxIn {
fmt.Println(" ==in", in.PreviousOutPoint.Hash.String(), in.PreviousOutPoint.Index)
}
}
}
func TestErrLog(t *testing.T) {
syslogs.InitIndexerLogDB("./higun.db")
errMsg := syslogs.ErrLog{
Height: 0,
BlockHash: "abc",
ErrType: "IndexBlock",
Timestamp: time.Now().Unix(),
ErrorMessage: "test",
}
err := syslogs.InsertErrLog(errMsg)
fmt.Println(err)
}
func TestNewMempoolManagerIfConfiguredSkipsWhenZMQDisabled(t *testing.T) {
oldConstructor := newMempoolManagerFn
t.Cleanup(func() {
newMempoolManagerFn = oldConstructor
})
constructorCalled := false
newMempoolManagerFn = func(_ string, _ *storage.PebbleStore, _ *chaincfg.Params, _ []string) *mempool.MempoolManager {
constructorCalled = true
return &mempool.MempoolManager{}
}
cfg := &config.Config{
DataDir: t.TempDir(),
ZMQAddress: []string{"", " "},
}
manager := newMempoolManagerIfConfigured(cfg, nil, nil)
if manager != nil {
t.Fatal("expected nil mempool manager when zmq_address is disabled")
}
if constructorCalled {
t.Fatal("expected mempool constructor to be skipped when zmq_address is disabled")
}
}
func TestNewMempoolManagerIfConfiguredBuildsWhenZMQConfigured(t *testing.T) {
oldConstructor := newMempoolManagerFn
t.Cleanup(func() {
newMempoolManagerFn = oldConstructor
})
called := false
newMempoolManagerFn = func(basePath string, store *storage.PebbleStore, chainCfg *chaincfg.Params, addrs []string) *mempool.MempoolManager {
called = true
if basePath == "" {
t.Fatal("expected basePath to be passed to mempool constructor")
}
if len(addrs) != 1 || addrs[0] != "tcp://127.0.0.1:28332" {
t.Fatalf("unexpected zmq addresses: %#v", addrs)
}
return &mempool.MempoolManager{}
}
cfg := &config.Config{
DataDir: t.TempDir(),
ZMQAddress: []string{"tcp://127.0.0.1:28332"},
}
manager := newMempoolManagerIfConfigured(cfg, nil, nil)
if manager == nil {
t.Fatal("expected mempool manager when zmq_address is configured")
}
if !called {
t.Fatal("expected mempool constructor to be called when zmq_address is configured")
}
}