bitcoinkernel 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/CODE_OF_CONDUCT.md +132 -0
- data/LICENSE.txt +21 -0
- data/README.md +232 -0
- data/Rakefile +8 -0
- data/lib/bitcoinkernel/block.rb +71 -0
- data/lib/bitcoinkernel/block_hash.rb +52 -0
- data/lib/bitcoinkernel/block_tree_entry.rb +58 -0
- data/lib/bitcoinkernel/block_validation_state.rb +54 -0
- data/lib/bitcoinkernel/chain.rb +48 -0
- data/lib/bitcoinkernel/chain_parameters.rb +49 -0
- data/lib/bitcoinkernel/chainstate_manager.rb +54 -0
- data/lib/bitcoinkernel/chainstate_manager_options.rb +62 -0
- data/lib/bitcoinkernel/context.rb +26 -0
- data/lib/bitcoinkernel/context_options.rb +37 -0
- data/lib/bitcoinkernel/logging.rb +81 -0
- data/lib/bitcoinkernel/script_pubkey.rb +74 -0
- data/lib/bitcoinkernel/serializable.rb +33 -0
- data/lib/bitcoinkernel/transaction.rb +90 -0
- data/lib/bitcoinkernel/transaction_input.rb +28 -0
- data/lib/bitcoinkernel/transaction_out_point.rb +42 -0
- data/lib/bitcoinkernel/transaction_output.rb +44 -0
- data/lib/bitcoinkernel/txid.rb +42 -0
- data/lib/bitcoinkernel/validation_interface.rb +107 -0
- data/lib/bitcoinkernel/version.rb +5 -0
- data/lib/bitcoinkernel.rb +207 -0
- data/sig/bitcoinkernel.rbs +4 -0
- metadata +86 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 81fd6124378d97dca07b53f429d67010f042c40c75ddc6be286bb5435b1a367f
|
|
4
|
+
data.tar.gz: f20221f63791bf6f6b8489243bb7b30c3e89325c78527d1bfa17a317c9822be6
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 55e486721451529db4469257c9b204608d611b095b04390150a575f29e49bf9dab320b9c919f2b3898e4a5cc51b860ea3f356b158e6dc5c9bf233cef694ecb1c
|
|
7
|
+
data.tar.gz: cb72d62d2d6f8f55af9cb83c03406a7cdd27e28986abec5e35e14ee3163560674c09ee48386f43de1033c9e0733f270b6f9e5b31d09309b038b8ab5a1f5091ea
|
data/.rspec
ADDED
data/.ruby-gemset
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
bitcoinkernel
|
data/.ruby-version
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
ruby-4.0.0
|
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our
|
|
6
|
+
community a harassment-free experience for everyone, regardless of age, body
|
|
7
|
+
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
|
8
|
+
identity and expression, level of experience, education, socio-economic status,
|
|
9
|
+
nationality, personal appearance, race, caste, color, religion, or sexual
|
|
10
|
+
identity and orientation.
|
|
11
|
+
|
|
12
|
+
We pledge to act and interact in ways that contribute to an open, welcoming,
|
|
13
|
+
diverse, inclusive, and healthy community.
|
|
14
|
+
|
|
15
|
+
## Our Standards
|
|
16
|
+
|
|
17
|
+
Examples of behavior that contributes to a positive environment for our
|
|
18
|
+
community include:
|
|
19
|
+
|
|
20
|
+
* Demonstrating empathy and kindness toward other people
|
|
21
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
|
22
|
+
* Giving and gracefully accepting constructive feedback
|
|
23
|
+
* Accepting responsibility and apologizing to those affected by our mistakes,
|
|
24
|
+
and learning from the experience
|
|
25
|
+
* Focusing on what is best not just for us as individuals, but for the overall
|
|
26
|
+
community
|
|
27
|
+
|
|
28
|
+
Examples of unacceptable behavior include:
|
|
29
|
+
|
|
30
|
+
* The use of sexualized language or imagery, and sexual attention or advances of
|
|
31
|
+
any kind
|
|
32
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
|
33
|
+
* Public or private harassment
|
|
34
|
+
* Publishing others' private information, such as a physical or email address,
|
|
35
|
+
without their explicit permission
|
|
36
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
|
37
|
+
professional setting
|
|
38
|
+
|
|
39
|
+
## Enforcement Responsibilities
|
|
40
|
+
|
|
41
|
+
Community leaders are responsible for clarifying and enforcing our standards of
|
|
42
|
+
acceptable behavior and will take appropriate and fair corrective action in
|
|
43
|
+
response to any behavior that they deem inappropriate, threatening, offensive,
|
|
44
|
+
or harmful.
|
|
45
|
+
|
|
46
|
+
Community leaders have the right and responsibility to remove, edit, or reject
|
|
47
|
+
comments, commits, code, wiki edits, issues, and other contributions that are
|
|
48
|
+
not aligned to this Code of Conduct, and will communicate reasons for moderation
|
|
49
|
+
decisions when appropriate.
|
|
50
|
+
|
|
51
|
+
## Scope
|
|
52
|
+
|
|
53
|
+
This Code of Conduct applies within all community spaces, and also applies when
|
|
54
|
+
an individual is officially representing the community in public spaces.
|
|
55
|
+
Examples of representing our community include using an official email address,
|
|
56
|
+
posting via an official social media account, or acting as an appointed
|
|
57
|
+
representative at an online or offline event.
|
|
58
|
+
|
|
59
|
+
## Enforcement
|
|
60
|
+
|
|
61
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
62
|
+
reported to the community leaders responsible for enforcement at
|
|
63
|
+
[INSERT CONTACT METHOD].
|
|
64
|
+
All complaints will be reviewed and investigated promptly and fairly.
|
|
65
|
+
|
|
66
|
+
All community leaders are obligated to respect the privacy and security of the
|
|
67
|
+
reporter of any incident.
|
|
68
|
+
|
|
69
|
+
## Enforcement Guidelines
|
|
70
|
+
|
|
71
|
+
Community leaders will follow these Community Impact Guidelines in determining
|
|
72
|
+
the consequences for any action they deem in violation of this Code of Conduct:
|
|
73
|
+
|
|
74
|
+
### 1. Correction
|
|
75
|
+
|
|
76
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed
|
|
77
|
+
unprofessional or unwelcome in the community.
|
|
78
|
+
|
|
79
|
+
**Consequence**: A private, written warning from community leaders, providing
|
|
80
|
+
clarity around the nature of the violation and an explanation of why the
|
|
81
|
+
behavior was inappropriate. A public apology may be requested.
|
|
82
|
+
|
|
83
|
+
### 2. Warning
|
|
84
|
+
|
|
85
|
+
**Community Impact**: A violation through a single incident or series of
|
|
86
|
+
actions.
|
|
87
|
+
|
|
88
|
+
**Consequence**: A warning with consequences for continued behavior. No
|
|
89
|
+
interaction with the people involved, including unsolicited interaction with
|
|
90
|
+
those enforcing the Code of Conduct, for a specified period of time. This
|
|
91
|
+
includes avoiding interactions in community spaces as well as external channels
|
|
92
|
+
like social media. Violating these terms may lead to a temporary or permanent
|
|
93
|
+
ban.
|
|
94
|
+
|
|
95
|
+
### 3. Temporary Ban
|
|
96
|
+
|
|
97
|
+
**Community Impact**: A serious violation of community standards, including
|
|
98
|
+
sustained inappropriate behavior.
|
|
99
|
+
|
|
100
|
+
**Consequence**: A temporary ban from any sort of interaction or public
|
|
101
|
+
communication with the community for a specified period of time. No public or
|
|
102
|
+
private interaction with the people involved, including unsolicited interaction
|
|
103
|
+
with those enforcing the Code of Conduct, is allowed during this period.
|
|
104
|
+
Violating these terms may lead to a permanent ban.
|
|
105
|
+
|
|
106
|
+
### 4. Permanent Ban
|
|
107
|
+
|
|
108
|
+
**Community Impact**: Demonstrating a pattern of violation of community
|
|
109
|
+
standards, including sustained inappropriate behavior, harassment of an
|
|
110
|
+
individual, or aggression toward or disparagement of classes of individuals.
|
|
111
|
+
|
|
112
|
+
**Consequence**: A permanent ban from any sort of public interaction within the
|
|
113
|
+
community.
|
|
114
|
+
|
|
115
|
+
## Attribution
|
|
116
|
+
|
|
117
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
|
118
|
+
version 2.1, available at
|
|
119
|
+
[https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
|
|
120
|
+
|
|
121
|
+
Community Impact Guidelines were inspired by
|
|
122
|
+
[Mozilla's code of conduct enforcement ladder][Mozilla CoC].
|
|
123
|
+
|
|
124
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
|
125
|
+
[https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
|
|
126
|
+
[https://www.contributor-covenant.org/translations][translations].
|
|
127
|
+
|
|
128
|
+
[homepage]: https://www.contributor-covenant.org
|
|
129
|
+
[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
|
|
130
|
+
[Mozilla CoC]: https://github.com/mozilla/diversity
|
|
131
|
+
[FAQ]: https://www.contributor-covenant.org/faq
|
|
132
|
+
[translations]: https://www.contributor-covenant.org/translations
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 azuchi
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
# BitcoinKernel
|
|
2
|
+
|
|
3
|
+
Ruby bindings for [libbitcoinkernel](https://github.com/bitcoin/bitcoin/blob/master/doc/design/libraries.md#libbitcoinkernel), the Bitcoin Core consensus engine library.
|
|
4
|
+
|
|
5
|
+
## Requirements
|
|
6
|
+
|
|
7
|
+
- Ruby 3.0+
|
|
8
|
+
- libbitcoinkernel shared library
|
|
9
|
+
|
|
10
|
+
You need to build libbitcoinkernel from Bitcoin Core source:
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
git clone https://github.com/bitcoin/bitcoin.git
|
|
14
|
+
cd bitcoin
|
|
15
|
+
cmake -B build -DBUILD_KERNEL_LIB=ON -DBUILD_SHARED_LIBS=ON
|
|
16
|
+
cmake --build build
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Set the `LIB_BITCOINKERNEL_PATH` environment variable to point to the built library:
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
export LIB_BITCOINKERNEL_PATH=/path/to/bitcoin/build/src/kernel/libbitcoinkernel.so
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Installation
|
|
26
|
+
|
|
27
|
+
Add this line to your application's Gemfile:
|
|
28
|
+
|
|
29
|
+
```ruby
|
|
30
|
+
gem 'bitcoinkernel'
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
And then execute:
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
$ bundle install
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Or install it yourself as:
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
$ gem install bitcoinkernel
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Usage
|
|
46
|
+
|
|
47
|
+
### Basic Block Parsing
|
|
48
|
+
|
|
49
|
+
```ruby
|
|
50
|
+
require 'bitcoinkernel'
|
|
51
|
+
|
|
52
|
+
# Parse a block from raw bytes
|
|
53
|
+
block_hex = "0100000000000000..."
|
|
54
|
+
block = BitcoinKernel::Block.from_raw([block_hex].pack('H*'))
|
|
55
|
+
|
|
56
|
+
# Access block properties
|
|
57
|
+
puts block.block_hash.to_hex
|
|
58
|
+
puts "Transaction count: #{block.transaction_count}"
|
|
59
|
+
|
|
60
|
+
# Iterate transactions
|
|
61
|
+
block.transactions.each do |tx|
|
|
62
|
+
puts "Txid: #{tx.txid.to_hex}"
|
|
63
|
+
puts "Inputs: #{tx.input_count}, Outputs: #{tx.output_count}"
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# Serialize back to bytes
|
|
67
|
+
raw_bytes = block.to_bytes
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### Transaction Parsing
|
|
71
|
+
|
|
72
|
+
```ruby
|
|
73
|
+
# Parse a transaction
|
|
74
|
+
tx = BitcoinKernel::Transaction.from_raw(raw_tx_bytes)
|
|
75
|
+
|
|
76
|
+
# Access inputs
|
|
77
|
+
tx.inputs.each do |input|
|
|
78
|
+
out_point = input.out_point
|
|
79
|
+
puts "Spending #{out_point.txid.to_hex}:#{out_point.index}"
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# Access outputs
|
|
83
|
+
tx.outputs.each_with_index do |output, i|
|
|
84
|
+
puts "Output #{i}: #{output.amount} satoshis"
|
|
85
|
+
end
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### Script Verification
|
|
89
|
+
|
|
90
|
+
```ruby
|
|
91
|
+
# Verify a script
|
|
92
|
+
script = BitcoinKernel::ScriptPubkey.from_raw(script_bytes)
|
|
93
|
+
|
|
94
|
+
result = script.verify(
|
|
95
|
+
amount: 50_0000_0000, # in satoshis
|
|
96
|
+
tx: spending_tx,
|
|
97
|
+
input_index: 0,
|
|
98
|
+
flags: BitcoinKernel::ScriptFlags::ALL
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
puts result ? "Valid" : "Invalid"
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### Chainstate Management
|
|
105
|
+
|
|
106
|
+
```ruby
|
|
107
|
+
# Disable logging (optional)
|
|
108
|
+
BitcoinKernel::Logging.disable
|
|
109
|
+
|
|
110
|
+
# Create context with chain parameters
|
|
111
|
+
options = BitcoinKernel::ContextOptions.create
|
|
112
|
+
params = BitcoinKernel::ChainParameters.regtest # or .mainnet, .testnet, etc.
|
|
113
|
+
options.set_chainparams(params)
|
|
114
|
+
context = BitcoinKernel::Context.create(options)
|
|
115
|
+
|
|
116
|
+
# Create chainstate manager
|
|
117
|
+
cs_options = BitcoinKernel::ChainstateManagerOptions.create(
|
|
118
|
+
context: context,
|
|
119
|
+
data_directory: "/path/to/data",
|
|
120
|
+
blocks_directory: "/path/to/blocks"
|
|
121
|
+
)
|
|
122
|
+
cs_options.set_block_tree_db_in_memory
|
|
123
|
+
cs_options.set_chainstate_db_in_memory
|
|
124
|
+
|
|
125
|
+
manager = BitcoinKernel::ChainstateManager.create(cs_options)
|
|
126
|
+
|
|
127
|
+
# Process blocks
|
|
128
|
+
block = BitcoinKernel::Block.from_raw(block_bytes)
|
|
129
|
+
manager.process_block(block)
|
|
130
|
+
|
|
131
|
+
# Query the chain
|
|
132
|
+
chain = manager.active_chain
|
|
133
|
+
puts "Chain height: #{chain.height}"
|
|
134
|
+
|
|
135
|
+
entry = chain.entry_at(0)
|
|
136
|
+
puts "Genesis hash: #{entry.block_hash.to_hex}"
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
### Validation Interface
|
|
140
|
+
|
|
141
|
+
```ruby
|
|
142
|
+
# Create a custom validation interface
|
|
143
|
+
class MyValidationInterface < BitcoinKernel::ValidationInterface
|
|
144
|
+
def block_checked(block, state)
|
|
145
|
+
if state.valid?
|
|
146
|
+
puts "Block valid: #{block.block_hash.to_hex}"
|
|
147
|
+
else
|
|
148
|
+
puts "Block invalid"
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
def block_connected(block, entry)
|
|
153
|
+
puts "Block connected at height #{entry.height}"
|
|
154
|
+
end
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
# Use with context
|
|
158
|
+
vi = MyValidationInterface.new
|
|
159
|
+
options = BitcoinKernel::ContextOptions.create
|
|
160
|
+
options.set_validation_interface(vi)
|
|
161
|
+
context = BitcoinKernel::Context.create(options)
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
### Logging Configuration
|
|
165
|
+
|
|
166
|
+
```ruby
|
|
167
|
+
# Disable all logging
|
|
168
|
+
BitcoinKernel::Logging.disable
|
|
169
|
+
|
|
170
|
+
# Or configure logging options
|
|
171
|
+
BitcoinKernel::Logging.set_options(
|
|
172
|
+
timestamps: true,
|
|
173
|
+
time_micros: false,
|
|
174
|
+
threadnames: false,
|
|
175
|
+
sourcelocations: false,
|
|
176
|
+
category_levels: true
|
|
177
|
+
)
|
|
178
|
+
|
|
179
|
+
# Set log level for specific category
|
|
180
|
+
BitcoinKernel::Logging.set_level(
|
|
181
|
+
BitcoinKernel::Logging::Category::VALIDATION,
|
|
182
|
+
BitcoinKernel::Logging::Level::DEBUG
|
|
183
|
+
)
|
|
184
|
+
|
|
185
|
+
# Enable/disable categories
|
|
186
|
+
BitcoinKernel::Logging.enable_category(BitcoinKernel::Logging::Category::ALL)
|
|
187
|
+
BitcoinKernel::Logging.disable_category(BitcoinKernel::Logging::Category::MEMPOOL)
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
## API Reference
|
|
191
|
+
|
|
192
|
+
### Core Classes
|
|
193
|
+
|
|
194
|
+
| Class | Description |
|
|
195
|
+
|-------|-------------|
|
|
196
|
+
| `Block` | Bitcoin block with header and transactions |
|
|
197
|
+
| `BlockHash` | 32-byte block hash |
|
|
198
|
+
| `Transaction` | Bitcoin transaction |
|
|
199
|
+
| `Txid` | 32-byte transaction ID |
|
|
200
|
+
| `TransactionInput` | Transaction input |
|
|
201
|
+
| `TransactionOutput` | Transaction output with amount and script |
|
|
202
|
+
| `TransactionOutPoint` | Reference to a previous output (txid + index) |
|
|
203
|
+
| `ScriptPubkey` | Output script with verification support |
|
|
204
|
+
|
|
205
|
+
### Chain Management
|
|
206
|
+
|
|
207
|
+
| Class | Description |
|
|
208
|
+
|-------|-------------|
|
|
209
|
+
| `Context` | Kernel context for chain operations |
|
|
210
|
+
| `ContextOptions` | Configuration for context creation |
|
|
211
|
+
| `ChainParameters` | Network parameters (mainnet, testnet, regtest, etc.) |
|
|
212
|
+
| `ChainstateManager` | Manages blockchain state and validation |
|
|
213
|
+
| `ChainstateManagerOptions` | Configuration for chainstate manager |
|
|
214
|
+
| `Chain` | Active blockchain view |
|
|
215
|
+
| `BlockTreeEntry` | Entry in the block tree |
|
|
216
|
+
| `BlockValidationState` | Validation result for a block |
|
|
217
|
+
| `ValidationInterface` | Callbacks for validation events |
|
|
218
|
+
|
|
219
|
+
### Constants
|
|
220
|
+
|
|
221
|
+
| Module | Description |
|
|
222
|
+
|--------|-------------|
|
|
223
|
+
| `ChainType` | Network types (MAINNET, TESTNET, REGTEST, etc.) |
|
|
224
|
+
| `ScriptFlags` | Script verification flags |
|
|
225
|
+
| `ValidationMode` | Block validation modes |
|
|
226
|
+
| `BlockValidationResult` | Validation failure reasons |
|
|
227
|
+
| `Logging::Category` | Log categories |
|
|
228
|
+
| `Logging::Level` | Log levels (TRACE, DEBUG, INFO) |
|
|
229
|
+
|
|
230
|
+
## License
|
|
231
|
+
|
|
232
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module BitcoinKernel
|
|
4
|
+
# Represents a Bitcoin block.
|
|
5
|
+
class Block < FFI::AutoPointer
|
|
6
|
+
include Serializable
|
|
7
|
+
serialize_with :btck_block_to_bytes
|
|
8
|
+
|
|
9
|
+
# Create a Block from raw serialized data.
|
|
10
|
+
# @param [String] raw_block Serialized block data (binary string)
|
|
11
|
+
# @return [Block]
|
|
12
|
+
def self.from_raw(raw_block)
|
|
13
|
+
raw_ptr = FFI::MemoryPointer.new(:uint8, raw_block.bytesize)
|
|
14
|
+
raw_ptr.put_bytes(0, raw_block)
|
|
15
|
+
ptr = BitcoinKernel.btck_block_create(raw_ptr, raw_block.bytesize)
|
|
16
|
+
raise Error, "Failed to create block from raw data" if ptr.null?
|
|
17
|
+
new(ptr)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def self.release(ptr)
|
|
21
|
+
BitcoinKernel.btck_block_destroy(ptr)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# Create a copy of this block.
|
|
25
|
+
# @return [Block] A new Block instance with copied data
|
|
26
|
+
def copy
|
|
27
|
+
copied_ptr = BitcoinKernel.btck_block_copy(self)
|
|
28
|
+
raise Error, "Failed to copy block" if copied_ptr.null?
|
|
29
|
+
Block.new(copied_ptr)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# Compare two blocks for equality by their block hash.
|
|
33
|
+
# @param [Block] other The other block to compare
|
|
34
|
+
# @return [Boolean]
|
|
35
|
+
def ==(other)
|
|
36
|
+
return false unless other.is_a?(Block)
|
|
37
|
+
block_hash == other.block_hash
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Number of transactions in the block.
|
|
41
|
+
# @return [Integer]
|
|
42
|
+
def transaction_count
|
|
43
|
+
BitcoinKernel.btck_block_count_transactions(self)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# Get transaction at specified index.
|
|
47
|
+
# @param [Integer] index Transaction index
|
|
48
|
+
# @return [Transaction]
|
|
49
|
+
def transaction_at(index)
|
|
50
|
+
tx_ptr = BitcoinKernel.btck_block_get_transaction_at(self, index)
|
|
51
|
+
raise Error, "Transaction not found at index #{index}" if tx_ptr.null?
|
|
52
|
+
# Copy the transaction since the returned pointer is not owned
|
|
53
|
+
copied_ptr = BitcoinKernel.btck_transaction_copy(tx_ptr)
|
|
54
|
+
Transaction.new(copied_ptr)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# Get all transactions in the block.
|
|
58
|
+
# @return [Array<Transaction>]
|
|
59
|
+
def transactions
|
|
60
|
+
transaction_count.times.map { |i| transaction_at(i) }
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# Get the block hash.
|
|
64
|
+
# @return [BlockHash]
|
|
65
|
+
def block_hash
|
|
66
|
+
hash_ptr = BitcoinKernel.btck_block_get_hash(self)
|
|
67
|
+
raise Error, "Failed to get block hash" if hash_ptr.null?
|
|
68
|
+
BlockHash.new(hash_ptr)
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module BitcoinKernel
|
|
4
|
+
# Represents a block hash.
|
|
5
|
+
class BlockHash < FFI::AutoPointer
|
|
6
|
+
# Create a BlockHash from raw bytes.
|
|
7
|
+
# @param [String] bytes 32-byte hash (binary string)
|
|
8
|
+
# @return [BlockHash]
|
|
9
|
+
def self.from_bytes(bytes)
|
|
10
|
+
raise Error, "Block hash must be 32 bytes" unless bytes.bytesize == 32
|
|
11
|
+
raw_ptr = FFI::MemoryPointer.new(:uint8, 32)
|
|
12
|
+
raw_ptr.put_bytes(0, bytes)
|
|
13
|
+
ptr = BitcoinKernel.btck_block_hash_create(raw_ptr)
|
|
14
|
+
raise Error, "Failed to create block hash" if ptr.null?
|
|
15
|
+
new(ptr)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# Create a BlockHash from hex string.
|
|
19
|
+
# @param [String] hex 64-character hex string (little-endian, as commonly displayed)
|
|
20
|
+
# @return [BlockHash]
|
|
21
|
+
def self.from_hex(hex)
|
|
22
|
+
bytes = [hex].pack('H*').reverse
|
|
23
|
+
from_bytes(bytes)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def self.release(ptr)
|
|
27
|
+
BitcoinKernel.btck_block_hash_destroy(ptr)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Get the hash as raw bytes.
|
|
31
|
+
# @return [String] 32-byte binary string
|
|
32
|
+
def to_bytes
|
|
33
|
+
output = FFI::MemoryPointer.new(:uint8, 32)
|
|
34
|
+
BitcoinKernel.btck_block_hash_to_bytes(self, output)
|
|
35
|
+
output.read_bytes(32)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Get the hash as hex string (little-endian, as commonly displayed).
|
|
39
|
+
# @return [String]
|
|
40
|
+
def to_hex
|
|
41
|
+
to_bytes.reverse.unpack1('H*')
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# Compare two block hashes for equality.
|
|
45
|
+
# @param [BlockHash] other The other block hash to compare
|
|
46
|
+
# @return [Boolean]
|
|
47
|
+
def ==(other)
|
|
48
|
+
return false unless other.is_a?(BlockHash)
|
|
49
|
+
BitcoinKernel.btck_block_hash_equals(self, other) == 1
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module BitcoinKernel
|
|
4
|
+
# Represents an entry in the block tree.
|
|
5
|
+
#
|
|
6
|
+
# Note: This class does not inherit from FFI::AutoPointer because:
|
|
7
|
+
# - There is no btck_block_tree_entry_destroy function in the library
|
|
8
|
+
# - The pointer is not owned; it points to internal data of ChainstateManager
|
|
9
|
+
# - The lifetime depends on the parent ChainstateManager or Chain
|
|
10
|
+
class BlockTreeEntry
|
|
11
|
+
# @param [FFI::Pointer] ptr Pointer to btck_BlockTreeEntry
|
|
12
|
+
# @param [Boolean] owned Whether this object owns the pointer.
|
|
13
|
+
# BlockTreeEntry pointers from Chain/ChainstateManager are NOT owned.
|
|
14
|
+
def initialize(ptr, owned: false)
|
|
15
|
+
@ptr = ptr
|
|
16
|
+
@owned = owned
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# Get the height of this block in the chain.
|
|
20
|
+
# @return [Integer]
|
|
21
|
+
def height
|
|
22
|
+
BitcoinKernel.btck_block_tree_entry_get_height(@ptr)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# Get the block hash.
|
|
26
|
+
# @return [BlockHash]
|
|
27
|
+
def block_hash
|
|
28
|
+
ptr = BitcoinKernel.btck_block_tree_entry_get_block_hash(@ptr)
|
|
29
|
+
raise Error, "Failed to get block hash" if ptr.null?
|
|
30
|
+
# The returned pointer is not owned, but we need to copy it for safety
|
|
31
|
+
bytes = FFI::MemoryPointer.new(:uint8, 32)
|
|
32
|
+
BitcoinKernel.btck_block_hash_to_bytes(ptr, bytes)
|
|
33
|
+
BlockHash.from_bytes(bytes.read_bytes(32))
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Get the previous block tree entry.
|
|
37
|
+
# @return [BlockTreeEntry, nil] The previous entry, or nil if this is the genesis block
|
|
38
|
+
def previous
|
|
39
|
+
ptr = BitcoinKernel.btck_block_tree_entry_get_previous(@ptr)
|
|
40
|
+
return nil if ptr.null?
|
|
41
|
+
BlockTreeEntry.new(ptr, owned: false)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# Compare two block tree entries for equality.
|
|
45
|
+
# @param [BlockTreeEntry] other The other entry to compare
|
|
46
|
+
# @return [Boolean]
|
|
47
|
+
def ==(other)
|
|
48
|
+
return false unless other.is_a?(BlockTreeEntry)
|
|
49
|
+
BitcoinKernel.btck_block_tree_entry_equals(@ptr, other.to_ptr) == 1
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# Get the underlying pointer for FFI calls.
|
|
53
|
+
# @return [FFI::Pointer]
|
|
54
|
+
def to_ptr
|
|
55
|
+
@ptr
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module BitcoinKernel
|
|
4
|
+
# Represents the validation state of a block.
|
|
5
|
+
#
|
|
6
|
+
# Note: This class does not inherit from FFI::AutoPointer because:
|
|
7
|
+
# - There is no btck_block_validation_state_destroy function in the library
|
|
8
|
+
# - The pointer is not owned; it is passed to ValidationInterface callbacks
|
|
9
|
+
# - The lifetime is only valid during the callback execution
|
|
10
|
+
#
|
|
11
|
+
# This object is obtained through the ValidationInterface#block_checked callback.
|
|
12
|
+
class BlockValidationState
|
|
13
|
+
# @param [FFI::Pointer] ptr Pointer to btck_BlockValidationState
|
|
14
|
+
def initialize(ptr)
|
|
15
|
+
@ptr = ptr
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# Get the validation mode.
|
|
19
|
+
# @return [Integer] One of ValidationMode constants (VALID, INVALID, INTERNAL_ERROR)
|
|
20
|
+
def validation_mode
|
|
21
|
+
BitcoinKernel.btck_block_validation_state_get_validation_mode(@ptr)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# Check if the block is valid.
|
|
25
|
+
# @return [Boolean]
|
|
26
|
+
def valid?
|
|
27
|
+
validation_mode == ValidationMode::VALID
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Check if the block is invalid.
|
|
31
|
+
# @return [Boolean]
|
|
32
|
+
def invalid?
|
|
33
|
+
validation_mode == ValidationMode::INVALID
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Check if there was an internal error during validation.
|
|
37
|
+
# @return [Boolean]
|
|
38
|
+
def internal_error?
|
|
39
|
+
validation_mode == ValidationMode::INTERNAL_ERROR
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# Get the block validation result (reason for invalidity).
|
|
43
|
+
# @return [Integer] One of BlockValidationResult constants
|
|
44
|
+
def block_validation_result
|
|
45
|
+
BitcoinKernel.btck_block_validation_state_get_block_validation_result(@ptr)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# Get the underlying pointer for FFI calls.
|
|
49
|
+
# @return [FFI::Pointer]
|
|
50
|
+
def to_ptr
|
|
51
|
+
@ptr
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|