Skip to content

Commit

Permalink
add possibility send multi boc
Browse files Browse the repository at this point in the history
  • Loading branch information
zakhar-petukhov committed Jul 31, 2023
1 parent afc81ec commit d2c0f32
Show file tree
Hide file tree
Showing 13 changed files with 300 additions and 101 deletions.
28 changes: 27 additions & 1 deletion api/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,32 @@
},
"description": "input parameters for contract get method"
},
"SendBoc": {
"content": {
"application/json": {
"schema": {
"properties": {
"batch": {
"items": {
"example": "te6ccgECBQEAARUAAkWIAWTtae+KgtbrX26Bep8JSq8lFLfGOoyGR/xwdjfvpvEaHg",
"type": "string"
},
"maxItems": 10,
"minItems": 1,
"type": "array"
},
"boc": {
"example": "te6ccgECBQEAARUAAkWIAWTtae+KgtbrX26Bep8JSq8lFLfGOoyGR/xwdjfvpvEaHg",
"type": "string"
}
},
"type": "object"
}
}
},
"description": "bag-of-cells serialized to base64",
"required": true
},
"TonConnectProof": {
"content": {
"application/json": {
Expand Down Expand Up @@ -4137,7 +4163,7 @@
"description": "Send message to blockchain",
"operationId": "sendMessage",
"requestBody": {
"$ref": "#/components/requestBodies/Boc"
"$ref": "#/components/requestBodies/SendBoc"
},
"responses": {
"200": {
Expand Down
20 changes: 19 additions & 1 deletion api/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ paths:
tags:
- Blockchain
requestBody:
$ref: "#/components/requestBodies/Boc"
$ref: "#/components/requestBodies/SendBoc"
responses:
'200':
description: "success"
Expand Down Expand Up @@ -2162,6 +2162,24 @@ components:
application/json:
schema:
type: object
SendBoc:
description: bag-of-cells serialized to base64
required: true
content:
application/json:
schema:
type: object
properties:
boc:
type: string
example: te6ccgECBQEAARUAAkWIAWTtae+KgtbrX26Bep8JSq8lFLfGOoyGR/xwdjfvpvEaHg
batch:
type: array
maxItems: 10
minItems: 1
items:
type: string
example: te6ccgECBQEAARUAAkWIAWTtae+KgtbrX26Bep8JSq8lFLfGOoyGR/xwdjfvpvEaHg
Boc:
description: bag-of-cells serialized to base64
required: true
Expand Down
9 changes: 9 additions & 0 deletions client/oas_client_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

77 changes: 37 additions & 40 deletions client/oas_json_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 14 additions & 3 deletions client/oas_schemas_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions client/oas_validators_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 33 additions & 7 deletions pkg/api/event_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,29 @@ func (h Handler) SendMessage(ctx context.Context, request *oas.SendMessageReq) e
if h.msgSender == nil {
return toError(http.StatusBadRequest, fmt.Errorf("msg sender is not configured"))
}
payload, err := base64.StdEncoding.DecodeString(request.Boc)
if err != nil {
return toError(http.StatusBadRequest, err)
if !request.Boc.IsSet() && len(request.Batch) == 0 {
return toError(http.StatusBadRequest, fmt.Errorf("boc not found"))
}
if request.Boc.IsSet() {
payload, err := sendMessage(ctx, request.Boc.Value, h.msgSender)
if err != nil {
sentry.Send("sending message", sentry.SentryInfoData{"payload": request.Boc}, sentry.LevelError)
return toError(http.StatusInternalServerError, err)
}
go h.addToMempool(payload)
}
if err := h.msgSender.SendMessage(ctx, payload); err != nil {
sentry.Send("sending message", sentry.SentryInfoData{"payload": request.Boc}, sentry.LevelError)
return toError(http.StatusInternalServerError, err)
if len(request.Batch) > 0 {
var msgsBoc []string
for _, msgBoc := range request.Batch {
payload, err := sendMessage(ctx, msgBoc, h.msgSender)
if err != nil {
msgsBoc = append(msgsBoc, base64.StdEncoding.EncodeToString(payload))
continue
}
go h.addToMempool(payload)
}
h.msgSender.MsgsBocAddToMempool(msgsBoc)
}
go h.addToMempool(payload)
return nil
}

Expand Down Expand Up @@ -415,3 +429,15 @@ func emulatedTreeToTrace(tree *txemulator.TxTree, accounts map[tongo.AccountID]t
}
return t, nil
}

func sendMessage(ctx context.Context, msgBoc string, msgSender messageSender) ([]byte, error) {
payload, err := base64.StdEncoding.DecodeString(msgBoc)
if err != nil {
return nil, err
}
err = msgSender.SendMessage(ctx, payload)
if err != nil {
return nil, err
}
return payload, nil
}
7 changes: 4 additions & 3 deletions pkg/api/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package api
import (
"context"
"crypto/ed25519"
"github.com/tonkeeper/tongo/boc"
"time"

"github.com/tonkeeper/tongo/boc"

"github.com/tonkeeper/tongo"
"github.com/tonkeeper/tongo/abi"
"github.com/tonkeeper/tongo/liteclient"
Expand Down Expand Up @@ -113,8 +114,8 @@ type chainState interface {

// messageSender provides a method to send a raw message to the blockchain.
type messageSender interface {
// SendMessage sends the given payload(a message) to the blockchain.
SendMessage(ctx context.Context, payload []byte) error
SendMessage(ctx context.Context, payload []byte) error // SendMessage sends the given payload(a message) to the blockchain.
MsgsBocAddToMempool(bocMsgs []string) // MsgsBocAddToMempool sends a list of boc to the cache for later sending
}

// executor runs any get methods
Expand Down
Loading

0 comments on commit d2c0f32

Please sign in to comment.