Documentation
¶
Overview ¶
Package scanner 区块扫描器接口与基类,负责按高度扫描区块、提取交易/回执并返回结果。
Index ¶
- type BalanceQueryFunc
- type Base
- func (bs *Base) ExtractTransactionAndReceiptData(txid string, scanTargetFunc BlockScanTargetFunc) ([]*types.ExtractDataItem, []*types.ContractReceiptItem, error)
- func (bs *Base) GetBalanceByAddress(address ...string) ([]*types.Balance, error)
- func (bs *Base) GetCurrentBlockHeader() (*types.BlockHeader, error)
- func (bs *Base) GetGlobalMaxBlockHeight() uint64
- func (bs *Base) Pause() error
- func (bs *Base) QueryBalancesConcurrent(symbol string, addresses []string, query BalanceQueryFunc, concurrency int) ([]*types.Balance, error)
- func (bs *Base) ResetScanHeight(height uint64) error
- func (bs *Base) Restart() error
- func (bs *Base) Run() error
- func (bs *Base) RunScanLoop(params ScanLoopParams) error
- func (bs *Base) ScanBlockOnce(height uint64) (*types.BlockScanResult, error)
- func (bs *Base) ScanBlockPrioritize(heights []uint64) error
- func (bs *Base) ScanBlockWithResult(height uint64) (*types.BlockScanResult, error)
- func (bs *Base) SetBlockScanTargetFunc(f BlockScanTargetFunc) error
- func (bs *Base) SetTask(task func())
- func (bs *Base) SetTokenMetadataFunc(f TokenMetadataFunc) error
- func (bs *Base) Stop() error
- func (bs *Base) VerifyTransactionByTxID(txid string, scanTargetFunc BlockScanTargetFunc, minConfirmations uint64) (*types.TxVerifyResult, error)
- func (bs *Base) VerifyTransactionMatch(txid string, expected *types.TxVerifyExpected, ...) (*types.TxVerifyMatchResult, error)
- type BlockScanTargetFunc
- type BlockScanner
- type ScanLoopParams
- type TokenMetadataFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BalanceQueryFunc ¶
BalanceQueryFunc 查询单个地址余额的回调函数,由 QueryBalancesConcurrent 调用。
type Base ¶
type Base struct {
Mu sync.RWMutex
ScanTargetFunc BlockScanTargetFunc
TokenMetadataFunc TokenMetadataFunc
PeriodOfTask time.Duration
// contains filtered or unexported fields
}
Base 区块扫描器基类:提供 ScanTargetFunc/TokenMetadataFunc 注入、任务运行控制与默认未实现方法。 各链实现建议嵌入该结构体,并按需重写 BlockScanner 接口中的方法。
func (*Base) ExtractTransactionAndReceiptData ¶
func (bs *Base) ExtractTransactionAndReceiptData(txid string, scanTargetFunc BlockScanTargetFunc) ([]*types.ExtractDataItem, []*types.ContractReceiptItem, error)
func (*Base) GetBalanceByAddress ¶
GetBalanceByAddress 返回未实现错误,由具体链的扫描器实现。 各链实现应调用 QueryBalancesConcurrent 辅助函数进行并发查询。
func (*Base) GetCurrentBlockHeader ¶
func (bs *Base) GetCurrentBlockHeader() (*types.BlockHeader, error)
func (*Base) GetGlobalMaxBlockHeight ¶
func (*Base) QueryBalancesConcurrent ¶
func (bs *Base) QueryBalancesConcurrent(symbol string, addresses []string, query BalanceQueryFunc, concurrency int) ([]*types.Balance, error)
QueryBalancesConcurrent 并发查询多个地址余额。 各链扫描器在实现 GetBalanceByAddress 时调用此辅助方法。
参数:
- symbol: 链标识
- addresses: 地址列表
- query: 单地址查询回调函数,返回已确认余额、未确认余额、总余额
- concurrency: 并发限制,默认20
返回按传入地址顺序排列的 Balance 列表。
func (*Base) ResetScanHeight ¶
func (*Base) RunScanLoop ¶
func (bs *Base) RunScanLoop(params ScanLoopParams) error
func (*Base) ScanBlockOnce ¶
func (bs *Base) ScanBlockOnce(height uint64) (*types.BlockScanResult, error)
func (*Base) ScanBlockPrioritize ¶
ScanBlockPrioritize 默认返回未实现错误,各链扫描器应重写此方法。 实现参考:在 RunScanLoop 运行时,将插队高度加入优先队列,由 RunScanLoop 在主线间隙处理。
func (*Base) ScanBlockWithResult ¶
func (bs *Base) ScanBlockWithResult(height uint64) (*types.BlockScanResult, error)
func (*Base) SetBlockScanTargetFunc ¶
func (bs *Base) SetBlockScanTargetFunc(f BlockScanTargetFunc) error
func (*Base) SetTokenMetadataFunc ¶
func (bs *Base) SetTokenMetadataFunc(f TokenMetadataFunc) error
func (*Base) VerifyTransactionByTxID ¶
func (bs *Base) VerifyTransactionByTxID(txid string, scanTargetFunc BlockScanTargetFunc, minConfirmations uint64) (*types.TxVerifyResult, error)
func (*Base) VerifyTransactionMatch ¶
func (bs *Base) VerifyTransactionMatch(txid string, expected *types.TxVerifyExpected, scanTargetFunc BlockScanTargetFunc, minConfirmations uint64) (*types.TxVerifyMatchResult, error)
type BlockScanTargetFunc ¶
type BlockScanTargetFunc func(target types.ScanTargetParam) types.ScanTargetResult
BlockScanTargetFunc 根据扫描目标参数(地址/别名等)查询所属源与是否存在,供扫块时过滤交易。
type BlockScanner ¶
type BlockScanner interface {
SetBlockScanTargetFunc(scanTargetFunc BlockScanTargetFunc) error
SetTokenMetadataFunc(tokenMetadataFunc TokenMetadataFunc) error
// 运行控制:启动/停止内部扫描任务。
Run() error
Pause() error
// ScanBlockWithResult 按高度扫描区块并返回摘要结果,供外部系统推进游标与重试。
// 约定:error 用于表达“无法完成该高度扫描”(如 RPC 失败/块不存在等);result.Success 表达业务语义上的成功与否。
ScanBlockWithResult(height uint64) (*types.BlockScanResult, error)
// ScanBlockOnce 指定高度扫描一次(用于补扫/漏扫修复),不进入持续循环、不维护外部游标。
ScanBlockOnce(height uint64) (*types.BlockScanResult, error)
// ResetScanHeight 将“持续扫块循环”的起始高度重置到指定值(用于后台指令修正游标/回滚重扫)。
// 约定:该方法只影响正在运行的 RunScanLoop(若实现方支持),不应引起进程重启。
ResetScanHeight(height uint64) error
GetCurrentBlockHeader() (*types.BlockHeader, error)
GetGlobalMaxBlockHeight() uint64
ExtractTransactionAndReceiptData(txid string, scanTargetFunc BlockScanTargetFunc) ([]*types.ExtractDataItem, []*types.ContractReceiptItem, error)
// GetBalanceByAddress 查询指定地址的余额。
GetBalanceByAddress(address ...string) ([]*types.Balance, error)
// VerifyTransactionByTxID 入账前按 txid 二次复核链上结果并返回可入账结果集。
// 约定:error 用于表达“RPC/系统错误导致无法完成复核”;业务层面的不通过以 result.Verified=false + Reason 表达。
VerifyTransactionByTxID(txid string, scanTargetFunc BlockScanTargetFunc, minConfirmations uint64) (*types.TxVerifyResult, error)
// VerifyTransactionMatch 入账前对链上结果集做二次复核,并与外部期望对象 expected 严格比对。
// 约定:error 表达系统/RPC 异常;业务不通过以 result.Verified=false + Reason/Mismatches 表达。
VerifyTransactionMatch(txid string, expected *types.TxVerifyExpected, scanTargetFunc BlockScanTargetFunc, minConfirmations uint64) (*types.TxVerifyMatchResult, error)
// RunScanLoop 按高度持续扫描区块:
// - 从 params.StartHeight+1 开始,串行向上扫描;
// - 直接扫描至 latest(链上最新高度),不再减去 Confirmations;
// - Confirmations 仅用于计算 BlockHeader.Confirmations 字段供业务层参考;
// - 每个高度只扫描一次,不会重复扫描,节省资源;
// - 每扫完一个高度调用 params.HandleBlock(若非空)将 ScanBlockWithResult 的结果同步回调给外部系统;
// - 每轮结束后 sleep Interval 再次循环。
// 该方法只负责生产候选结果,入账/确认/重试策略由外部系统基于回调结果与 Verify 接口自行决定。
RunScanLoop(params ScanLoopParams) error
// ScanBlockPrioritize 插队扫描指定高度列表。
// 说明:
// - 将插队高度加入优先队列,RunScanLoop 会在主线扫描间隙优先处理这些高度;
// - 插队高度的扫描结果复用 RunScanLoop 的 handleBlock(如果 RunScanLoop 未运行则无回调);
// - 插队扫描不影响 RunScanLoop 的主线 cursor 推进逻辑;
// - 插队高度按升序处理,且去重;
// - 严格要求所有传入的高度都满足 confirmations 要求(height <= latest - confirmations),否则直接返回错误;
// - 调用方可根据错误信息调整高度后重试。
ScanBlockPrioritize(heights []uint64) error
}
BlockScanner 区块扫描器核心接口: - 扫块:按高度扫描区块(同步返回结果或仅返回 error) - 复核:按 txid 二次链上复核,并可与业务期望严格比对 - 持续扫描:以“外部系统维护游标”为前提,回调输出每个高度的扫描结果
说明: - 不包含内部持久化(DAI)与业务查询类能力(余额/地址交易等),这些应由外部系统或独立组件负责。
type ScanLoopParams ¶ added in v1.0.1
type ScanLoopParams struct {
StartHeight uint64 // 起始扫描高度,从 StartHeight+1 开始扫描
Confirmations uint64 // 确认数,仅用于计算 BlockHeader.Confirmations 供业务层参考
Interval time.Duration // 每轮扫描后的休眠间隔
HandleBlock func(res *types.BlockScanResult) // 每扫完一个高度的回调函数(可为 nil)
}
ScanLoopParams RunScanLoop 的参数结构体,后续添加新参数无需修改方法签名。
type TokenMetadataFunc ¶
type TokenMetadataFunc func(symbol, contractAddr string) *types.SmartContract
TokenMetadataFunc 根据链标识与合约地址查询代币/合约元数据(SmartContract),供扫块器在提取交易/回执时补充合约信息。