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).
Queries the Chaintracks v2 block header endpoint to retrieve the merkle root for a given block height and compares it with the provided root.
Constant Summary collapse
- MAINNET_URL =
BSV::MAINNET_URL
- TESTNET_URL =
BSV::TESTNET_URL
Instance Method Summary collapse
-
#current_height ⇒ Integer
Return the current blockchain height.
-
#initialize(url: MAINNET_URL, api_key: nil, http_client: 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: MAINNET_URL, api_key: nil, http_client: nil) ⇒ Chaintracks
Returns a new instance of Chaintracks.
29 30 31 32 33 34 |
# File 'lib/bsv/transaction/chain_trackers/chaintracks.rb', line 29 def initialize(url: MAINNET_URL, api_key: nil, http_client: nil) super() @url = url.chomp('/') @api_key = api_key @http_client = http_client end |
Instance Method Details
#current_height ⇒ Integer
Return the current blockchain height.
55 56 57 58 59 |
# File 'lib/bsv/transaction/chain_trackers/chaintracks.rb', line 55 def current_height response = get('/chaintracks/v2/tip', not_found_returns_nil: false) data = JSON.parse(response.body) data['height'] end |
#valid_root_for_height?(root, height) ⇒ Boolean
Verify that a merkle root is valid for the given block height.
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/bsv/transaction/chain_trackers/chaintracks.rb', line 41 def valid_root_for_height?(root, height) response = get("/chaintracks/v2/header/height/#{height}") return false if response.nil? data = JSON.parse(response.body) merkle_root = data['merkleRoot'] return false unless merkle_root merkle_root.downcase == root.downcase end |