Skip to content

Commit 5477357

Browse files
committed
add DownloadBlockchainBlockBoc handler
1 parent 40f9c15 commit 5477357

12 files changed

+484
-41
lines changed

api/openapi.json

+42-4
Original file line numberDiff line numberDiff line change
@@ -6691,7 +6691,7 @@
66916691
},
66926692
"/v2/accounts/{account_id}/events/emulate": {
66936693
"post": {
6694-
"description": "Emulate sending message to blockchain",
6694+
"description": "Emulate sending message to retrieve account-specific events",
66956695
"operationId": "emulateMessageToAccountEvent",
66966696
"parameters": [
66976697
{
@@ -7683,6 +7683,44 @@
76837683
]
76847684
}
76857685
},
7686+
"/v2/blockchain/blocks/{block_id}/boc": {
7687+
"get": {
7688+
"description": "Download blockchain block BOC",
7689+
"operationId": "downloadBlockchainBlockBoc",
7690+
"parameters": [
7691+
{
7692+
"$ref": "#/components/parameters/blockchainBlockIDParameter"
7693+
}
7694+
],
7695+
"responses": {
7696+
"200": {
7697+
"content": {
7698+
"application/octet-stream": {
7699+
"schema": {
7700+
"format": "binary",
7701+
"type": "string"
7702+
}
7703+
}
7704+
},
7705+
"description": "Block BOC file",
7706+
"headers": {
7707+
"Content-Disposition": {
7708+
"schema": {
7709+
"example": "attachment; filename=\"block.boc\"",
7710+
"type": "string"
7711+
}
7712+
}
7713+
}
7714+
},
7715+
"default": {
7716+
"$ref": "#/components/responses/Error"
7717+
}
7718+
},
7719+
"tags": [
7720+
"Blockchain"
7721+
]
7722+
}
7723+
},
76867724
"/v2/blockchain/blocks/{block_id}/transactions": {
76877725
"get": {
76887726
"description": "Get transactions from block",
@@ -8181,7 +8219,7 @@
81818219
},
81828220
"/v2/events/emulate": {
81838221
"post": {
8184-
"description": "Emulate sending message to blockchain",
8222+
"description": "Emulate sending message to retrieve general blockchain events",
81858223
"operationId": "emulateMessageToEvent",
81868224
"parameters": [
81878225
{
@@ -10720,7 +10758,7 @@
1072010758
},
1072110759
"/v2/traces/emulate": {
1072210760
"post": {
10723-
"description": "Emulate sending message to blockchain",
10761+
"description": "Emulate sending message to retrieve with a detailed execution trace",
1072410762
"operationId": "emulateMessageToTrace",
1072510763
"parameters": [
1072610764
{
@@ -10823,7 +10861,7 @@
1082310861
},
1082410862
"/v2/wallet/emulate": {
1082510863
"post": {
10826-
"description": "Emulate sending message to blockchain",
10864+
"description": "Emulate sending message to retrieve the resulting wallet state",
1082710865
"operationId": "emulateMessageToWallet",
1082810866
"parameters": [
1082910867
{

api/openapi.yml

+23
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,29 @@ paths:
168168
$ref: '#/components/schemas/BlockchainBlock'
169169
'default':
170170
$ref: '#/components/responses/Error'
171+
/v2/blockchain/blocks/{block_id}/boc:
172+
get:
173+
description: Download blockchain block BOC
174+
operationId: downloadBlockchainBlockBoc
175+
tags:
176+
- Blockchain
177+
parameters:
178+
- $ref: '#/components/parameters/blockchainBlockIDParameter'
179+
responses:
180+
'200':
181+
description: Block BOC file
182+
content:
183+
application/octet-stream:
184+
schema:
185+
type: string
186+
format: binary
187+
headers:
188+
Content-Disposition:
189+
schema:
190+
type: string
191+
example: 'attachment; filename="block.boc"'
192+
'default':
193+
$ref: '#/components/responses/Error'
171194
/v2/blockchain/masterchain/{masterchain_seqno}/shards:
172195
get:
173196
description: Get blockchain block shards

pkg/api/blockchain_handlers.go

+20
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package api
22

33
import (
4+
"bytes"
45
"context"
56
"encoding/json"
67
"errors"
@@ -72,6 +73,25 @@ func (h *Handler) GetBlockchainBlock(ctx context.Context, params oas.GetBlockcha
7273
return &res, nil
7374
}
7475

76+
func (h *Handler) DownloadBlockchainBlockBoc(ctx context.Context, params oas.DownloadBlockchainBlockBocParams) (*oas.DownloadBlockchainBlockBocOKHeaders, error) {
77+
blockID, err := ton.ParseBlockID(params.BlockID)
78+
if err != nil {
79+
return nil, toError(http.StatusBadRequest, err)
80+
}
81+
82+
bocBytes, err := h.blockSource.GetBlockchainBlock(ctx, blockID.Workchain, blockID.Shard, blockID.Seqno)
83+
if err != nil {
84+
return nil, toError(http.StatusInternalServerError, err)
85+
}
86+
87+
return &oas.DownloadBlockchainBlockBocOKHeaders{
88+
ContentDisposition: oas.NewOptString(fmt.Sprintf(`attachment; filename="block_%s.boc"`, params.BlockID)),
89+
Response: oas.DownloadBlockchainBlockBocOK{
90+
Data: bytes.NewReader(bocBytes),
91+
},
92+
}, nil
93+
}
94+
7595
func (h *Handler) GetBlockchainMasterchainShards(ctx context.Context, params oas.GetBlockchainMasterchainShardsParams) (r *oas.BlockchainBlockShards, _ error) {
7696
shards, err := h.storage.GetBlockShards(ctx, ton.BlockID{Shard: 0x8000000000000000, Seqno: uint32(params.MasterchainSeqno), Workchain: -1})
7797
if errors.Is(err, core.ErrEntityNotFound) {

pkg/api/handler.go

+8
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ type Handler struct {
4747
metaCache metadataCache
4848
tonConnect *tonconnect.Server
4949
verifierSource verifierSource
50+
blockSource blockSource
5051

5152
// mempoolEmulate contains results of emulation of messages that are in the mempool.
5253
mempoolEmulate mempoolEmulate
@@ -139,6 +140,12 @@ func WithRatesSource(source ratesSource) Option {
139140
}
140141
}
141142

143+
func WithBlocksSource(source blockSource) Option {
144+
return func(o *Options) {
145+
o.blockSource = source
146+
}
147+
}
148+
142149
func WithTonConnectSecret(tonConnectSecret string) Option {
143150
return func(o *Options) {
144151
o.tonConnectSecret = tonConnectSecret
@@ -238,6 +245,7 @@ func NewHandler(logger *zap.Logger, opts ...Option) (*Handler, error) {
238245
score: options.score,
239246
ratesSource: rates.InitCalculator(options.ratesSource),
240247
verifierSource: options.verifier,
248+
blockSource: options.blockSource,
241249
metaCache: metadataCache{
242250
collectionsCache: cache.NewLRUCache[tongo.AccountID, tep64.Metadata](10000, "nft_metadata_cache"),
243251
jettonsCache: cache.NewLRUCache[tongo.AccountID, tep64.Metadata](10000, "jetton_metadata_cache"),

pkg/api/interfaces.go

+4
Original file line numberDiff line numberDiff line change
@@ -213,3 +213,7 @@ type mempoolEmulate struct {
213213
traces cache.Cache[ton.Bits256, *core.Trace]
214214
accountsTraces cache.Cache[tongo.AccountID, []ton.Bits256]
215215
}
216+
217+
type blockSource interface {
218+
GetBlockchainBlock(ctx context.Context, workchain int32, shard uint64, seqno uint32) ([]byte, error)
219+
}

pkg/oas/oas_handlers_gen.go

+121-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)