@@ -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}
0 commit comments