Class: BSV::Transaction::ChainTrackers::Chaintracks

Inherits:
BSV::Transaction::ChainTracker show all
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.

Examples:

tracker = BSV::Transaction::ChainTrackers::Chaintracks.new
tracker.valid_root_for_height?('abcd...', 800_000)

With API key

tracker = BSV::Transaction::ChainTrackers::Chaintracks.new(api_key: 'my-key')
tracker.current_height

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url: nil, api_key: nil, http_client: nil, protocol: nil) ⇒ Chaintracks

Returns a new instance of Chaintracks.

Parameters:

  • url (String, nil) (defaults to: nil)

    base URL (legacy compat — prefer .default or protocol:)

  • api_key (String, nil) (defaults to: nil)

    optional Bearer API key

  • http_client (#request, nil) (defaults to: nil)

    injectable HTTP client for testing

  • protocol (BSV::Network::Protocols::Chaintracks, nil) (defaults to: nil)

    pre-configured protocol



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.

Parameters:

  • testnet (Boolean) (defaults to: false)

    when true, uses the testnet endpoint

  • opts (Hash)

    forwarded to the underlying protocol (e.g. api_key:, http_client:)

Returns:



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_heightInteger

Return the current blockchain height.

Returns:

  • (Integer)

Raises:



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.message.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.

Parameters:

  • root (String)

    merkle root as a hex string

  • height (Integer)

    block height

Returns:

  • (Boolean)

Raises:



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.message.to_s,
      status_code: result.[:status_code]
    )
  end

  merkle_root = result.data['merkleRoot']
  return false unless merkle_root

  merkle_root.downcase == root.downcase
end