Class: BSV::Transaction::ChainTrackers::Chaintracks
- Inherits:
-
BSV::Transaction::ChainTracker
- Object
- BSV::Transaction::ChainTracker
- BSV::Transaction::ChainTrackers::Chaintracks
- Defined in:
- lib/bsv/transaction/chain_trackers/chaintracks.rb
Overview
Chain tracker that verifies merkle roots using the Chaintracks API (Arcade/GorillaPool).
Delegates all HTTP communication to Network::Protocols::Chaintracks. The constructor signature and BSV::Transaction::ChainTracker contract are preserved.
Class Method Summary collapse
-
.default(testnet: false, **opts) ⇒ Chaintracks
Returns a Chaintracks instance using the GorillaPool provider default.
Instance Method Summary collapse
-
#current_height ⇒ Integer
Return the current blockchain height.
-
#initialize(url: nil, api_key: nil, http_client: nil, protocol: nil) ⇒ Chaintracks
constructor
A new instance of Chaintracks.
-
#valid_root_for_height?(root, height) ⇒ Boolean
Verify that a merkle root is valid for the given block height.
Constructor Details
#initialize(url: nil, api_key: nil, http_client: nil, protocol: nil) ⇒ Chaintracks
Returns a new instance of Chaintracks.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/bsv/transaction/chain_trackers/chaintracks.rb', line 34 def initialize(url: nil, api_key: nil, http_client: nil, protocol: nil) super() if protocol @protocol = protocol elsif url @url = url.chomp('/') @api_key = api_key @protocol = BSV::Network::Protocols::Chaintracks.new( base_url: @url, api_key: api_key, http_client: http_client ) else provider = BSV::Network::Providers::GorillaPool.default(api_key: api_key, http_client: http_client) @protocol = provider.protocol_for(:current_height) end end |
Class Method Details
.default(testnet: false, **opts) ⇒ Chaintracks
Returns a Chaintracks instance using the GorillaPool provider default.
24 25 26 27 28 |
# File 'lib/bsv/transaction/chain_trackers/chaintracks.rb', line 24 def self.default(testnet: false, **opts) provider = BSV::Network::Providers::GorillaPool.default(testnet: testnet, **opts) protocol = provider.protocol_for(:current_height) new(protocol: protocol) end |
Instance Method Details
#current_height ⇒ Integer
Return the current blockchain height.
79 80 81 82 83 84 85 86 87 |
# File 'lib/bsv/transaction/chain_trackers/chaintracks.rb', line 79 def current_height result = @protocol.call(:current_height) return result.data if result.success? raise BSV::Network::ChainProviderError.new( result..to_s, status_code: result.[:status_code] ) end |
#valid_root_for_height?(root, height) ⇒ Boolean
Verify that a merkle root is valid for the given block height.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/bsv/transaction/chain_trackers/chaintracks.rb', line 58 def valid_root_for_height?(root, height) result = @protocol.call(:get_block_header, height) return false if result.not_found? if result.error? raise BSV::Network::ChainProviderError.new( result..to_s, status_code: result.[:status_code] ) end merkle_root = result.data['merkleRoot'] return false unless merkle_root merkle_root.downcase == root.downcase end |