Skip to content

Commit 9c13828

Browse files
committed
BlockScanner interface add GetBlockchainSyncStatus()
1 parent aa69c56 commit 9c13828

File tree

5 files changed

+98
-47
lines changed

5 files changed

+98
-47
lines changed

crypto/crypto_utils_test.go

+35-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func TestBase64(t *testing.T) {
106106
fmt.Printf("hex: %s \n", hex.EncodeToString(prv))
107107
}
108108

109-
func TestSECP256R1(t *testing.T) {
109+
func TestSECP256R1_sign(t *testing.T) {
110110
key, _ := hex.DecodeString("")
111111
pubkey, _ := owcrypt.GenPubkey(key, owcrypt.ECC_CURVE_SECP256R1)
112112
pubkey = owcrypt.PointCompress(pubkey, owcrypt.ECC_CURVE_SECP256R1)
@@ -116,3 +116,37 @@ func TestSECP256R1(t *testing.T) {
116116
fmt.Printf("ret: %d \n", ret)
117117
fmt.Printf("sig: %s \n", hex.EncodeToString(sig))
118118
}
119+
120+
func TestSECP256R1_verify(t *testing.T) {
121+
prvkey, _ := hex.DecodeString("")
122+
pubkey, _ := owcrypt.GenPubkey(prvkey, owcrypt.ECC_CURVE_SECP256R1)
123+
//pubkey, _ := hex.DecodeString("0267b7dca2a5620195bc84fbc9ffe331dfe3c4ee01545cd814789c4b636e5f5eb0")
124+
compressedPubkey := owcrypt.PointCompress(pubkey, owcrypt.ECC_CURVE_SECP256R1)
125+
fmt.Printf("pubkey: %s \n", hex.EncodeToString(compressedPubkey))
126+
//03fd78eac9fd3216191f884e47378e940fba0919adaa92820c8f82a099d6c40474
127+
//msg, _ := hex.DecodeString("6cec7463040014002c012c011400000400")
128+
//msg_hash := owcrypt.Hash(msg, 0, owcrypt.HASH_ALG_SHA256)
129+
msg_hash, _ := hex.DecodeString("1a15624e8521c8b4575cd6f2c33a6e71c78de695bed596ac01584c31dbf18c32")
130+
fmt.Printf("hash: %s \n", hex.EncodeToString(msg_hash))
131+
//signature, _, _ := owcrypt.Signature(prvkey, nil, msg_hash, owcrypt.ECC_CURVE_SECP256R1)
132+
//13a4030ffc51f7b3d9dafa60f4a567a34911972fb48103fabd56766e9401373b492e5522c63c431468e8d7bc5d655edc9c264dd768bfdf79232616d1bb97d3ff
133+
signature, _ := hex.DecodeString("e51f7701d9e1c0c10d7512b7d3d2781e661c15cd1d74ca7ac0de9d9f5d71e03c18b4118310bcfdac346bf0b5aa66744223eb557b1dec68a24c88b2ce1f887e78")
134+
fmt.Printf("signature: %s \n", hex.EncodeToString(signature))
135+
136+
ret := owcrypt.Verify(pubkey, nil, msg_hash, signature, owcrypt.ECC_CURVE_SECP256R1)
137+
fmt.Printf("ret: %d \n", ret)
138+
}
139+
140+
func TestSECP256K1(t *testing.T) {
141+
key, _ := hex.DecodeString("")
142+
pubkey, _ := owcrypt.GenPubkey(key, owcrypt.ECC_CURVE_SECP256K1)
143+
//pubkey = owcrypt.PointCompress(pubkey, owcrypt.ECC_CURVE_SECP256K1)
144+
fmt.Printf("pubkey: %s \n", hex.EncodeToString(pubkey))
145+
msg, _ := hex.DecodeString("b68a5c3e0fdffecc81459c522bfcdeeb787da05cfbcd59574337d7d2e376967e")
146+
//sig, _, _ := owcrypt.Signature(key, nil, msg, owcrypt.ECC_CURVE_SECP256K1)
147+
sig, _ := hex.DecodeString("6a6f5c658ef5c01a75d1474c91c4ef7bb8970dd156906fa7837e84be6b3bf5a61b6bdbaa9bcd2365cd29e919c50dafe68b3cedcc6e38feeddbac6bd4053de7ad")
148+
//sig = append(sig, v)
149+
ret := owcrypt.Verify(pubkey, nil, msg, sig, owcrypt.ECC_CURVE_SECP256K1)
150+
fmt.Printf("ret: %d \n", ret)
151+
//fmt.Printf("sig: %s \n", hex.EncodeToString(sig))
152+
}

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ require (
88
github.com/asdine/storm v2.1.2+incompatible
99
github.com/astaxie/beego v1.12.0
1010
github.com/blocktree/go-owcdrivers v1.2.0
11-
github.com/blocktree/go-owcrypt v1.1.1
11+
github.com/blocktree/go-owcrypt v1.1.9
1212
github.com/bndr/gotabulate v1.1.2
1313
github.com/bradfitz/gomemcache v0.0.0-20190913173617-a41fca850d0b
1414
github.com/btcsuite/btcd v0.23.1 // indirect

openwallet/blockscanner.go

+58-42
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ import (
2525
"github.com/blocktree/openwallet/v2/timer"
2626
)
2727

28-
//deprecated
28+
// deprecated
2929
// BlockScanAddressFunc 扫描地址是否存在算法
3030
// @return 地址所属源标识,是否存在
3131
type BlockScanAddressFunc func(address string) (string, bool)
3232

33-
//deprecated
33+
// deprecated
3434
// BlockScanTargetFunc 扫描对象是否存在算法
3535
// @return 对象所属源标识,是否存在
3636
type BlockScanTargetFunc func(target ScanTarget) (string, bool)
@@ -79,6 +79,12 @@ type ScanTargetResult struct {
7979
TargetInfo interface{}
8080
}
8181

82+
type BlockchainSyncStatus struct {
83+
NetworkBlockHeight uint64
84+
CurrentBlockHeight uint64
85+
Syncing bool
86+
}
87+
8288
// BlockScanner 区块扫描器
8389
// 负责扫描新区块,给观察者推送订阅地址的新交易单。
8490
type BlockScanner interface {
@@ -179,9 +185,13 @@ type BlockScanner interface {
179185
//ExtractTransactionAndReceiptData 提取交易单及交易回执数据
180186
//@required
181187
ExtractTransactionAndReceiptData(txid string, scanTargetFunc BlockScanTargetFuncV2) (map[string][]*TxExtractData, map[string]*SmartContractReceipt, error)
188+
189+
//GetBlockchainSyncStatus 获取当前区块链同步状态
190+
//@optional
191+
GetBlockchainSyncStatus() (*BlockchainSyncStatus, error)
182192
}
183193

184-
//BlockScanNotificationObject 扫描被通知对象
194+
// BlockScanNotificationObject 扫描被通知对象
185195
type BlockScanNotificationObject interface {
186196

187197
//BlockScanNotify 新区块扫描完成通知
@@ -199,7 +209,7 @@ type BlockScanNotificationObject interface {
199209
BlockExtractSmartContractDataNotify(sourceKey string, data *SmartContractReceipt) error
200210
}
201211

202-
//TxExtractData 区块扫描后的交易单提取结果,每笔交易单
212+
// TxExtractData 区块扫描后的交易单提取结果,每笔交易单
203213
type TxExtractData struct {
204214

205215
//消费记录,交易单输入部分
@@ -224,7 +234,7 @@ const (
224234
periodOfTask = 5 * time.Second //定时任务执行隔间
225235
)
226236

227-
//BlockScannerBase 区块链扫描器基本结构实现
237+
// BlockScannerBase 区块链扫描器基本结构实现
228238
type BlockScannerBase struct {
229239
AddressInScanning map[string]string //加入扫描的地址
230240
scanTask *timer.TaskTimer //扫描定时器
@@ -242,7 +252,7 @@ type BlockScannerBase struct {
242252
BlockchainDAI BlockchainDAI
243253
}
244254

245-
//NewBTCBlockScanner 创建区块链扫描器
255+
// NewBTCBlockScanner 创建区块链扫描器
246256
func NewBlockScannerBase() *BlockScannerBase {
247257
bs := BlockScannerBase{}
248258
bs.AddressInScanning = make(map[string]string)
@@ -253,7 +263,7 @@ func NewBlockScannerBase() *BlockScannerBase {
253263
return &bs
254264
}
255265

256-
//InitBlockScanner
266+
// InitBlockScanner
257267
func (bs *BlockScannerBase) InitBlockScanner() error {
258268

259269
bs.blockProducer = make(chan interface{})
@@ -267,16 +277,16 @@ func (bs *BlockScannerBase) InitBlockScanner() error {
267277
return nil
268278
}
269279

270-
//deprecated
271-
//SetBlockScanAddressFunc 设置区块扫描过程,查找地址过程方法
280+
// deprecated
281+
// SetBlockScanAddressFunc 设置区块扫描过程,查找地址过程方法
272282
func (bs *BlockScannerBase) SetBlockScanAddressFunc(scanAddressFunc BlockScanAddressFunc) error {
273283
bs.ScanAddressFunc = scanAddressFunc
274284
return nil
275285
}
276286

277-
//deprecated
278-
//SetBlockScanTargetFunc 设置区块扫描过程,查找扫描对象过程方法
279-
//@required
287+
// deprecated
288+
// SetBlockScanTargetFunc 设置区块扫描过程,查找扫描对象过程方法
289+
// @required
280290
func (bs *BlockScannerBase) SetBlockScanTargetFunc(scanTargetFunc BlockScanTargetFunc) error {
281291
bs.ScanTargetFunc = scanTargetFunc
282292

@@ -292,8 +302,8 @@ func (bs *BlockScannerBase) SetBlockScanTargetFunc(scanTargetFunc BlockScanTarge
292302
return nil
293303
}
294304

295-
//SetBlockScanTargetFuncV2 设置区块扫描过程,查找扫描对象过程方法
296-
//@required
305+
// SetBlockScanTargetFuncV2 设置区块扫描过程,查找扫描对象过程方法
306+
// @required
297307
func (bs *BlockScannerBase) SetBlockScanTargetFuncV2(scanTargetFuncV2 BlockScanTargetFuncV2) error {
298308
bs.ScanTargetFuncV2 = scanTargetFuncV2
299309

@@ -314,7 +324,7 @@ func (bs *BlockScannerBase) SetBlockScanTargetFuncV2(scanTargetFuncV2 BlockScanT
314324
return nil
315325
}
316326

317-
//AddObserver 添加观测者
327+
// AddObserver 添加观测者
318328
func (bs *BlockScannerBase) AddObserver(obj BlockScanNotificationObject) error {
319329
bs.Mu.Lock()
320330

@@ -333,7 +343,7 @@ func (bs *BlockScannerBase) AddObserver(obj BlockScanNotificationObject) error {
333343
return nil
334344
}
335345

336-
//RemoveObserver 移除观测者
346+
// RemoveObserver 移除观测者
337347
func (bs *BlockScannerBase) RemoveObserver(obj BlockScanNotificationObject) error {
338348
bs.Mu.Lock()
339349
defer bs.Mu.Unlock()
@@ -343,12 +353,12 @@ func (bs *BlockScannerBase) RemoveObserver(obj BlockScanNotificationObject) erro
343353
return nil
344354
}
345355

346-
//SetRescanBlockHeight 重置区块链扫描高度
356+
// SetRescanBlockHeight 重置区块链扫描高度
347357
func (bs *BlockScannerBase) SetRescanBlockHeight(height uint64) error {
348358
return nil
349359
}
350360

351-
//SetTask
361+
// SetTask
352362
func (bs *BlockScannerBase) SetTask(task func()) {
353363

354364
//if bs.scanTask == nil {
@@ -366,7 +376,7 @@ func (bs *BlockScannerBase) SetTask(task func()) {
366376
bs.scanTask = taskTimer
367377
}
368378

369-
//Run 运行
379+
// Run 运行
370380
func (bs *BlockScannerBase) Run() error {
371381

372382
if bs.IsClose() {
@@ -390,7 +400,7 @@ func (bs *BlockScannerBase) Run() error {
390400
return nil
391401
}
392402

393-
//Stop 停止扫描
403+
// Stop 停止扫描
394404
func (bs *BlockScannerBase) Stop() error {
395405

396406
if bs.IsClose() {
@@ -402,7 +412,7 @@ func (bs *BlockScannerBase) Stop() error {
402412
return nil
403413
}
404414

405-
//Pause 暂停扫描
415+
// Pause 暂停扫描
406416
func (bs *BlockScannerBase) Pause() error {
407417

408418
if bs.IsClose() {
@@ -414,7 +424,7 @@ func (bs *BlockScannerBase) Pause() error {
414424
return nil
415425
}
416426

417-
//Restart 继续扫描
427+
// Restart 继续扫描
418428
func (bs *BlockScannerBase) Restart() error {
419429

420430
if bs.IsClose() {
@@ -426,29 +436,29 @@ func (bs *BlockScannerBase) Restart() error {
426436
return nil
427437
}
428438

429-
//IsClose 是否已经关闭
439+
// IsClose 是否已经关闭
430440
func (bs *BlockScannerBase) IsClose() bool {
431441
return bs.isClose
432442
}
433443

434-
//ScanBlock 扫描指定高度区块
444+
// ScanBlock 扫描指定高度区块
435445
func (bs *BlockScannerBase) ScanBlock(height uint64) error {
436446
//扫描指定高度区块
437447
return fmt.Errorf("ScanBlock is not implemented")
438448
}
439449

440-
//GetCurrentBlockHeight 获取当前区块高度
450+
// GetCurrentBlockHeight 获取当前区块高度
441451
func (bs *BlockScannerBase) GetCurrentBlockHeader() (*BlockHeader, error) {
442452
return nil, fmt.Errorf("GetCurrentBlockHeader is not implemented")
443453
}
444454

445-
//GetGlobalMaxBlockHeight 获取区块链全网最大高度
446-
//@required
455+
// GetGlobalMaxBlockHeight 获取区块链全网最大高度
456+
// @required
447457
func (bs *BlockScannerBase) GetGlobalMaxBlockHeight() uint64 {
448458
return 0
449459
}
450460

451-
//GetScannedBlockHeight 获取已扫区块高度
461+
// GetScannedBlockHeight 获取已扫区块高度
452462
func (bs *BlockScannerBase) GetScannedBlockHeight() uint64 {
453463
return 0
454464
}
@@ -457,13 +467,13 @@ func (bs *BlockScannerBase) ExtractTransactionData(txid string, scanTargetFunc B
457467
return nil, fmt.Errorf("ExtractTransactionData is not implemented")
458468
}
459469

460-
//ExtractTransactionAndReceiptData 提取交易单及交易回执数据
461-
//@required
470+
// ExtractTransactionAndReceiptData 提取交易单及交易回执数据
471+
// @required
462472
func (bs *BlockScannerBase) ExtractTransactionAndReceiptData(txid string, scanTargetFunc BlockScanTargetFuncV2) (map[string][]*TxExtractData, map[string]*SmartContractReceipt, error) {
463473
return nil, nil, fmt.Errorf("ExtractTransactionAndReceiptData is not implemented")
464474
}
465475

466-
//GetBalanceByAddress 查询地址余额
476+
// GetBalanceByAddress 查询地址余额
467477
func (bs *BlockScannerBase) GetBalanceByAddress(address ...string) ([]*Balance, error) {
468478
return nil, fmt.Errorf("GetBalanceByAddress is not implemented")
469479
}
@@ -473,33 +483,33 @@ func (bs *BlockScannerBase) GetBalanceByAddress(address ...string) ([]*Balance,
473483
// return nil, nil
474484
//}
475485

476-
//GetTransactionsByAddress 查询基于账户的交易记录,通过账户关系的地址
477-
//返回的交易记录以资产账户为集合的结果,转账数量以基于账户来计算
486+
// GetTransactionsByAddress 查询基于账户的交易记录,通过账户关系的地址
487+
// 返回的交易记录以资产账户为集合的结果,转账数量以基于账户来计算
478488
func (bs *BlockScannerBase) GetTransactionsByAddress(offset, limit int, coin Coin, address ...string) ([]*TxExtractData, error) {
479489
return nil, fmt.Errorf("GetTransactionsByAddress is not implemented")
480490
}
481491

482-
//SetBlockScanWalletDAI 设置区块扫描过程,上层提供一个钱包数据接口
483-
//@optional
492+
// SetBlockScanWalletDAI 设置区块扫描过程,上层提供一个钱包数据接口
493+
// @optional
484494
func (bs *BlockScannerBase) SetBlockScanWalletDAI(dai WalletDAI) error {
485495
bs.WalletDAI = dai
486496
return nil
487497
}
488498

489-
//SupportBlockchainDAI 支持外部设置区块链数据访问接口
490-
//@optional
499+
// SupportBlockchainDAI 支持外部设置区块链数据访问接口
500+
// @optional
491501
func (bs *BlockScannerBase) SupportBlockchainDAI() bool {
492502
return false
493503
}
494504

495-
//SetBlockchainDAI 设置区块链数据访问接口,读取持久化的区块数据
496-
//@optional
505+
// SetBlockchainDAI 设置区块链数据访问接口,读取持久化的区块数据
506+
// @optional
497507
func (bs *BlockScannerBase) SetBlockchainDAI(dai BlockchainDAI) error {
498508
bs.BlockchainDAI = dai
499509
return nil
500510
}
501511

502-
//NewBlockNotify 获得新区块后,发送到通知通道
512+
// NewBlockNotify 获得新区块后,发送到通知通道
503513
func (bs *BlockScannerBase) NewBlockNotify(block *BlockHeader) error {
504514
bs.Mu.RLock()
505515
defer bs.Mu.RUnlock()
@@ -510,7 +520,7 @@ func (bs *BlockScannerBase) NewBlockNotify(block *BlockHeader) error {
510520
return nil
511521
}
512522

513-
//CloseBlockScanner 关闭扫描器
523+
// CloseBlockScanner 关闭扫描器
514524
func (bs *BlockScannerBase) CloseBlockScanner() error {
515525

516526
//保证只关闭一次
@@ -527,7 +537,7 @@ func (bs *BlockScannerBase) CloseBlockScanner() error {
527537
return nil
528538
}
529539

530-
//newBlockNotifyConsume
540+
// newBlockNotifyConsume
531541
func (bs *BlockScannerBase) newBlockNotifyConsume() {
532542

533543
for {
@@ -547,3 +557,9 @@ func (bs *BlockScannerBase) newBlockNotifyConsume() {
547557

548558
}
549559
}
560+
561+
// GetBlockchainSyncStatus 获取当前区块链同步状态
562+
// @optional
563+
func (bs *BlockScannerBase) GetBlockchainSyncStatus() (*BlockchainSyncStatus, error) {
564+
return nil, fmt.Errorf("GetBlockchainSyncStatus is not implemented")
565+
}

openwallet/contract_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ import (
2121
)
2222

2323
func TestGenContractID(t *testing.T) {
24-
address := "1234QWER"
24+
address := ""
2525
address = strings.ToLower(address)
2626
t.Logf("lower addr 1: %v", address)
27-
symbol := "qtum"
27+
symbol := "MATIC"
2828
ID := GenContractID(symbol, address)
2929
t.Logf("ID 1: %v", ID)
3030
ID = GenContractID(symbol, address)

owtp/http_client_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@ func TestHTTPClientCall(t *testing.T) {
143143
cert, _ := NewCertificate("E3cQTqKZfVVL6cQvyrSgbjmkVnnbkBuoqt7ed9wQLjgz", "aes")
144144
//httpClient := RandomOWTPNode()
145145
httpClient := NewNode(NodeConfig{
146-
Cert: cert,
146+
TimeoutSEC: 1000,
147+
Cert: cert,
147148
})
148149
httpClient.SetPeerstore(globalSessions)
149150
prv, pub := httpClient.Certificate().KeyPair()

0 commit comments

Comments
 (0)