Skip to content

Commit ec2d078

Browse files
committed
Implement conformance handler for v0.0.4 tests
1 parent 6ea0d78 commit ec2d078

17 files changed

Lines changed: 1297 additions & 40 deletions

cmd/conformance-handler/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Conformance Handler Makefile
22

33
# Test suite configuration
4-
TEST_VERSION := 0.0.3
4+
TEST_VERSION := 0.0.4-alpha.pr14.4
55
TEST_REPO := stringintech/kernel-bindings-tests
66
TEST_DIR := .conformance-tests
77

cmd/conformance-handler/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@ The test suite is automatically downloaded for your platform (darwin_arm64, darw
2929
## Pinned Test Version
3030

3131
This handler is compatible with:
32-
- Test Suite Version: `0.0.3`
32+
- Test Suite Version: `0.0.4-alpha.pr14.4`
3333
- Test Repository: [stringintech/kernel-bindings-tests](https://github.com/stringintech/kernel-bindings-tests)

cmd/conformance-handler/block.go

Lines changed: 141 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,156 @@ func handleBlockCreate(registry *Registry, req Request) (Response, error) {
3737
return NewSuccessResponseWithRef(req.Ref), nil
3838
}
3939

40-
// handleBlockTreeEntryGetBlockHash gets the block hash from a block tree entry
41-
func handleBlockTreeEntryGetBlockHash(registry *Registry, req Request) (Response, error) {
40+
// handleBlockGetHash gets the hash of a block and stores it in the registry.
41+
func handleBlockGetHash(registry *Registry, req Request) (Response, error) {
4242
var params struct {
43-
BlockTreeEntry RefObject `json:"block_tree_entry"`
43+
Block RefObject `json:"block"`
4444
}
4545

4646
if err := json.Unmarshal(req.Params, &params); err != nil {
4747
return Response{}, fmt.Errorf("failed to parse params: %w", err)
4848
}
4949

50-
entry, err := registry.GetBlockTreeEntry(params.BlockTreeEntry.Ref)
50+
if req.Ref == "" {
51+
return Response{}, fmt.Errorf("ref field is required")
52+
}
53+
54+
block, err := registry.GetBlock(params.Block.Ref)
55+
if err != nil {
56+
return Response{}, err
57+
}
58+
59+
hash := block.Hash()
60+
registry.Store(req.Ref, hash)
61+
return NewSuccessResponseWithRef(req.Ref), nil
62+
}
63+
64+
// handleBlockGetHeader extracts the block header and stores it in the registry
65+
func handleBlockGetHeader(registry *Registry, req Request) (Response, error) {
66+
var params struct {
67+
Block RefObject `json:"block"`
68+
}
69+
70+
if err := json.Unmarshal(req.Params, &params); err != nil {
71+
return Response{}, fmt.Errorf("failed to parse params: %w", err)
72+
}
73+
74+
block, err := registry.GetBlock(params.Block.Ref)
75+
if err != nil {
76+
return Response{}, err
77+
}
78+
79+
header := block.GetHeader()
80+
registry.Store(req.Ref, header)
81+
82+
return NewSuccessResponseWithRef(req.Ref), nil
83+
}
84+
85+
// handleBlockCountTransactions returns the number of transactions in the block
86+
func handleBlockCountTransactions(registry *Registry, req Request) (Response, error) {
87+
var params struct {
88+
Block RefObject `json:"block"`
89+
}
90+
91+
if err := json.Unmarshal(req.Params, &params); err != nil {
92+
return Response{}, fmt.Errorf("failed to parse params: %w", err)
93+
}
94+
95+
block, err := registry.GetBlock(params.Block.Ref)
96+
if err != nil {
97+
return Response{}, err
98+
}
99+
100+
return NewSuccessResponse(block.CountTransactions()), nil
101+
}
102+
103+
// handleBlockGetTransactionAt retrieves the transaction at the given index and stores it in the registry
104+
func handleBlockGetTransactionAt(registry *Registry, req Request) (Response, error) {
105+
var params struct {
106+
Block RefObject `json:"block"`
107+
TransactionIndex uint64 `json:"transaction_index"`
108+
}
109+
110+
if err := json.Unmarshal(req.Params, &params); err != nil {
111+
return Response{}, fmt.Errorf("failed to parse params: %w", err)
112+
}
113+
114+
if req.Ref == "" {
115+
return Response{}, fmt.Errorf("ref field is required")
116+
}
117+
118+
block, err := registry.GetBlock(params.Block.Ref)
51119
if err != nil {
52120
return Response{}, err
53121
}
54122

55-
return NewSuccessResponse(entry.Hash().String()), nil
123+
txView, err := block.GetTransactionAt(params.TransactionIndex)
124+
if err != nil {
125+
return Response{}, err
126+
}
127+
128+
registry.Store(req.Ref, txView)
129+
130+
return NewSuccessResponseWithRef(req.Ref), nil
131+
}
132+
133+
// handleBlockToBytes returns the consensus-serialized block as a hex string
134+
func handleBlockToBytes(registry *Registry, req Request) (Response, error) {
135+
var params struct {
136+
Block RefObject `json:"block"`
137+
}
138+
139+
if err := json.Unmarshal(req.Params, &params); err != nil {
140+
return Response{}, fmt.Errorf("failed to parse params: %w", err)
141+
}
142+
143+
block, err := registry.GetBlock(params.Block.Ref)
144+
if err != nil {
145+
return Response{}, err
146+
}
147+
148+
data, err := block.Bytes()
149+
if err != nil {
150+
return NewEmptyErrorResponse(), nil
151+
}
152+
153+
return NewSuccessResponse(hex.EncodeToString(data)), nil
154+
}
155+
156+
// handleBlockCopy copies a block and stores the copy in the registry
157+
func handleBlockCopy(registry *Registry, req Request) (Response, error) {
158+
var params struct {
159+
Block RefObject `json:"block"`
160+
}
161+
162+
if err := json.Unmarshal(req.Params, &params); err != nil {
163+
return Response{}, fmt.Errorf("failed to parse params: %w", err)
164+
}
165+
166+
block, err := registry.GetBlock(params.Block.Ref)
167+
if err != nil {
168+
return Response{}, err
169+
}
170+
171+
blockCopy := block.Copy()
172+
registry.Store(req.Ref, blockCopy)
173+
174+
return NewSuccessResponseWithRef(req.Ref), nil
175+
}
176+
177+
// handleBlockDestroy destroys a block from the registry
178+
func handleBlockDestroy(registry *Registry, req Request) (Response, error) {
179+
var params struct {
180+
Block RefObject `json:"block"`
181+
}
182+
183+
if err := json.Unmarshal(req.Params, &params); err != nil {
184+
return Response{}, fmt.Errorf("failed to parse params: %w", err)
185+
}
186+
187+
if err := registry.Destroy(params.Block.Ref); err != nil {
188+
return Response{}, err
189+
}
190+
191+
return NewEmptySuccessResponse(), nil
56192
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package main
2+
3+
import (
4+
"encoding/hex"
5+
"encoding/json"
6+
"fmt"
7+
8+
"github.com/stringintech/go-bitcoinkernel/kernel"
9+
)
10+
11+
// handleBlockHashCreate creates a BlockHash from raw stored 32 bytes encoded as hex.
12+
func handleBlockHashCreate(registry *Registry, req Request) (Response, error) {
13+
var params struct {
14+
BlockHash string `json:"block_hash"`
15+
}
16+
17+
if err := json.Unmarshal(req.Params, &params); err != nil {
18+
return Response{}, fmt.Errorf("failed to parse params: %w", err)
19+
}
20+
21+
if req.Ref == "" {
22+
return Response{}, fmt.Errorf("ref field is required")
23+
}
24+
25+
var hashBytes [32]byte
26+
if len(params.BlockHash) != 64 {
27+
return Response{}, fmt.Errorf("block_hash must be exactly 64 hex characters")
28+
}
29+
if _, err := hex.Decode(hashBytes[:], []byte(params.BlockHash)); err != nil {
30+
return Response{}, fmt.Errorf("block_hash must be valid hex: %w", err)
31+
}
32+
33+
bh := kernel.NewBlockHash(hashBytes)
34+
registry.Store(req.Ref, bh)
35+
36+
return NewSuccessResponseWithRef(req.Ref), nil
37+
}
38+
39+
// handleBlockHashToBytes returns the raw stored 32-byte block hash as a hex string.
40+
func handleBlockHashToBytes(registry *Registry, req Request) (Response, error) {
41+
var params struct {
42+
BlockHash RefObject `json:"block_hash"`
43+
}
44+
45+
if err := json.Unmarshal(req.Params, &params); err != nil {
46+
return Response{}, fmt.Errorf("failed to parse params: %w", err)
47+
}
48+
49+
bh, err := registry.GetBlockHash(params.BlockHash.Ref)
50+
if err != nil {
51+
return Response{}, err
52+
}
53+
54+
raw := bh.Bytes()
55+
return NewSuccessResponse(fmt.Sprintf("%x", raw)), nil
56+
}
57+
58+
// handleBlockHashEquals checks if two block hashes are equal
59+
func handleBlockHashEquals(registry *Registry, req Request) (Response, error) {
60+
var params struct {
61+
Hash1 RefObject `json:"hash1"`
62+
Hash2 RefObject `json:"hash2"`
63+
}
64+
65+
if err := json.Unmarshal(req.Params, &params); err != nil {
66+
return Response{}, fmt.Errorf("failed to parse params: %w", err)
67+
}
68+
69+
bh, err := registry.GetBlockHash(params.Hash1.Ref)
70+
if err != nil {
71+
return Response{}, err
72+
}
73+
74+
bh2, err := registry.GetBlockHash(params.Hash2.Ref)
75+
if err != nil {
76+
return Response{}, err
77+
}
78+
79+
return NewSuccessResponse(bh.Equals(bh2)), nil
80+
}
81+
82+
// handleBlockHashCopy copies a block hash and stores the copy in the registry
83+
func handleBlockHashCopy(registry *Registry, req Request) (Response, error) {
84+
var params struct {
85+
BlockHash RefObject `json:"block_hash"`
86+
}
87+
88+
if err := json.Unmarshal(req.Params, &params); err != nil {
89+
return Response{}, fmt.Errorf("failed to parse params: %w", err)
90+
}
91+
92+
if req.Ref == "" {
93+
return Response{}, fmt.Errorf("ref field is required")
94+
}
95+
96+
bh, err := registry.GetBlockHash(params.BlockHash.Ref)
97+
if err != nil {
98+
return Response{}, err
99+
}
100+
101+
registry.Store(req.Ref, bh.Copy())
102+
103+
return NewSuccessResponseWithRef(req.Ref), nil
104+
}
105+
106+
// handleBlockHashDestroy destroys a block hash from the registry
107+
func handleBlockHashDestroy(registry *Registry, req Request) (Response, error) {
108+
var params struct {
109+
BlockHash RefObject `json:"block_hash"`
110+
}
111+
112+
if err := json.Unmarshal(req.Params, &params); err != nil {
113+
return Response{}, fmt.Errorf("failed to parse params: %w", err)
114+
}
115+
116+
if _, err := registry.GetBlockHash(params.BlockHash.Ref); err != nil {
117+
return Response{}, err
118+
}
119+
120+
if err := registry.Destroy(params.BlockHash.Ref); err != nil {
121+
return Response{}, err
122+
}
123+
124+
return NewEmptySuccessResponse(), nil
125+
}

0 commit comments

Comments
 (0)