Skip to content

Commit

Permalink
/v2/blockchain/blocks/{block_id} to return value flow from block
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksej-paschenko committed Nov 23, 2023
1 parent e0ae116 commit 4d37ad6
Show file tree
Hide file tree
Showing 15 changed files with 2,410 additions and 169 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@

### 2023-10-22
- `/v2/blockchain/validators` method which returns the current validators and theirs stakes.

### 2023-10-23
- `/v2/blockchain/blocks/{block_id}` returns a value flow from a block.
85 changes: 85 additions & 0 deletions api/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -1174,6 +1174,41 @@
],
"type": "object"
},
"BlockCurrencyCollection": {
"properties": {
"grams": {
"example": 10000000000,
"format": "int64",
"type": "integer"
},
"other": {
"items": {
"properties": {
"id": {
"example": 13,
"format": "int64",
"type": "integer"
},
"value": {
"example": "10000000000",
"type": "string"
}
},
"required": [
"id",
"value"
],
"type": "object"
},
"type": "array"
}
},
"required": [
"grams",
"other"
],
"type": "object"
},
"BlockLimits": {
"properties": {
"bytes": {
Expand Down Expand Up @@ -1253,6 +1288,52 @@
],
"type": "object"
},
"BlockValueFlow": {
"properties": {
"burned": {
"$ref": "#/components/schemas/BlockCurrencyCollection"
},
"created": {
"$ref": "#/components/schemas/BlockCurrencyCollection"
},
"exported": {
"$ref": "#/components/schemas/BlockCurrencyCollection"
},
"fees_collected": {
"$ref": "#/components/schemas/BlockCurrencyCollection"
},
"fees_imported": {
"$ref": "#/components/schemas/BlockCurrencyCollection"
},
"from_prev_blk": {
"$ref": "#/components/schemas/BlockCurrencyCollection"
},
"imported": {
"$ref": "#/components/schemas/BlockCurrencyCollection"
},
"minted": {
"$ref": "#/components/schemas/BlockCurrencyCollection"
},
"recovered": {
"$ref": "#/components/schemas/BlockCurrencyCollection"
},
"to_next_blk": {
"$ref": "#/components/schemas/BlockCurrencyCollection"
}
},
"required": [
"from_prev_blk",
"to_next_blk",
"imported",
"exported",
"fees_collected",
"fees_imported",
"recovered",
"created",
"minted"
],
"type": "object"
},
"BlockchainAccountInspect": {
"properties": {
"code": {
Expand Down Expand Up @@ -1408,6 +1489,9 @@
"format": "int64",
"type": "integer"
},
"value_flow": {
"$ref": "#/components/schemas/BlockValueFlow"
},
"version": {
"example": 0,
"format": "int32",
Expand Down Expand Up @@ -1439,6 +1523,7 @@
"root_hash",
"file_hash",
"global_id",
"value_flow",
"version",
"after_merge",
"before_split",
Expand Down
63 changes: 62 additions & 1 deletion api/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2680,6 +2680,65 @@ components:
is_wallet:
type: boolean
example: true
BlockCurrencyCollection:
type: object
required:
- grams
- other
properties:
grams:
type: integer
format: int64
example: 10000000000
other:
type: array
items:
type: object
required:
- id
- value
properties:
id:
type: integer
format: int64
example: 13
value:
type: string
example: "10000000000"
BlockValueFlow:
type: object
required:
- from_prev_blk
- to_next_blk
- imported
- exported
- fees_collected
- fees_imported
- recovered
- created
- minted
properties:
from_prev_blk:
$ref: '#/components/schemas/BlockCurrencyCollection'
to_next_blk:
$ref: '#/components/schemas/BlockCurrencyCollection'
imported:
$ref: '#/components/schemas/BlockCurrencyCollection'
exported:
$ref: '#/components/schemas/BlockCurrencyCollection'
fees_collected:
$ref: '#/components/schemas/BlockCurrencyCollection'
burned:
$ref: '#/components/schemas/BlockCurrencyCollection'
fees_imported:
$ref: '#/components/schemas/BlockCurrencyCollection'
recovered:
$ref: '#/components/schemas/BlockCurrencyCollection'
created:
$ref: '#/components/schemas/BlockCurrencyCollection'
minted:
$ref: '#/components/schemas/BlockCurrencyCollection'

BlockchainBlock:
type: object
required:
Expand All @@ -2689,7 +2748,7 @@ components:
- root_hash
- file_hash
- global_id
# ValueFlow pgtype.JSONB
- value_flow
- version
- after_merge
- before_split
Expand All @@ -2710,6 +2769,8 @@ components:
- rand_seed
- created_by
properties:
value_flow:
$ref: '#/components/schemas/BlockValueFlow'
workchain_id:
type: integer
example: 0
Expand Down
27 changes: 27 additions & 0 deletions pkg/api/blockchain_converters.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,19 @@ func blockIdExtFromString(s string) (tongo.BlockIDExt, error) {
return id, nil
}

func convertValueFlow(collection core.CurrencyCollection) oas.BlockCurrencyCollection {
res := oas.BlockCurrencyCollection{
Grams: int64(collection.Grams),
Other: make([]oas.BlockCurrencyCollectionOtherItem, 0, len(collection.Other)),
}
for _, c := range collection.Other {
res.Other = append(res.Other, oas.BlockCurrencyCollectionOtherItem{
ID: c.ID,
Value: c.Value,
})
}
return res
}
func convertBlockHeader(b core.BlockHeader) oas.BlockchainBlock {
res := oas.BlockchainBlock{
WorkchainID: b.Workchain,
Expand All @@ -63,6 +76,20 @@ func convertBlockHeader(b core.BlockHeader) oas.BlockchainBlock {
OutMsgDescrLength: int64(b.BlockExtra.OutMsgDescrLength),
RandSeed: fmt.Sprintf("%x", b.BlockExtra.RandSeed),
CreatedBy: fmt.Sprintf("%x", b.BlockExtra.CreatedBy),
ValueFlow: oas.BlockValueFlow{
FromPrevBlk: convertValueFlow(b.ValueFlow.FromPrevBlk),
ToNextBlk: convertValueFlow(b.ValueFlow.ToNextBlk),
Imported: convertValueFlow(b.ValueFlow.Imported),
Exported: convertValueFlow(b.ValueFlow.Exported),
FeesCollected: convertValueFlow(b.ValueFlow.FeesCollected),
FeesImported: convertValueFlow(b.ValueFlow.FeesImported),
Recovered: convertValueFlow(b.ValueFlow.Recovered),
Created: convertValueFlow(b.ValueFlow.Created),
Minted: convertValueFlow(b.ValueFlow.Minted),
},
}
if b.ValueFlow.Burned != nil {
res.ValueFlow.Burned = oas.NewOptBlockCurrencyCollection(convertValueFlow(*b.ValueFlow.Burned))
}
if b.GenSoftware != nil {
res.GenSoftwareVersion.SetTo(int32(b.GenSoftware.Version))
Expand Down
39 changes: 39 additions & 0 deletions pkg/api/blockchain_handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/tonkeeper/opentonapi/pkg/litestorage"
"github.com/tonkeeper/opentonapi/pkg/oas"
pkgTesting "github.com/tonkeeper/opentonapi/pkg/testing"
"github.com/tonkeeper/tongo/config"
"github.com/tonkeeper/tongo/ton"
"go.uber.org/zap"
Expand Down Expand Up @@ -188,3 +189,41 @@ func TestHandler_GetBlockchainValidators(t *testing.T) {
}
require.Equal(t, validators.ElectAt, int64(curValidators.UtimeSince))
}

func TestHandler_GetBlockchainBlock(t *testing.T) {
var servers []config.LiteServer
if env, ok := os.LookupEnv("LITE_SERVERS"); ok {
var err error
servers, err = config.ParseLiteServersEnvVar(env)
require.Nil(t, err)
}
tests := []struct {
name string
blockID string
filenamePrefix string
}{
{
name: "block from masterchain, no burned",
blockID: "(-1,8000000000000000,34336028)",
filenamePrefix: "block-1-8000000000000000-34336028",
},
{
name: "block from masterchain with value burned",
blockID: "(-1,8000000000000000,34336196)",
filenamePrefix: "block-1-8000000000000000-34336196",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
logger := zap.L()
liteStorage, err := litestorage.NewLiteStorage(logger, litestorage.WithLiteServers(servers))
require.Nil(t, err)
h, err := NewHandler(logger, WithStorage(liteStorage), WithExecutor(liteStorage))
require.Nil(t, err)
params := oas.GetBlockchainBlockParams{BlockID: tt.blockID}
block, err := h.GetBlockchainBlock(context.Background(), params)
require.Nil(t, err)
pkgTesting.CompareResults(t, block, tt.filenamePrefix)
})
}
}
87 changes: 87 additions & 0 deletions pkg/api/testdata/block-1-8000000000000000-34336028.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
{
"value_flow": {
"from_prev_blk": {
"grams": 1917883300491494921,
"other": [
{
"id": 239,
"value": "664333333334"
},
{
"id": 4294967279,
"value": "998444444446"
}
]
},
"to_next_blk": {
"grams": 1917883303191494921,
"other": [
{
"id": 239,
"value": "664333333334"
},
{
"id": 4294967279,
"value": "998444444446"
}
]
},
"imported": {
"grams": 0,
"other": []
},
"exported": {
"grams": 0,
"other": []
},
"fees_collected": {
"grams": 2700000000,
"other": []
},
"fees_imported": {
"grams": 1000000000,
"other": []
},
"recovered": {
"grams": 2700000000,
"other": []
},
"created": {
"grams": 1700000000,
"other": []
},
"minted": {
"grams": 0,
"other": []
}
},
"workchain_id": -1,
"shard": "8000000000000000",
"seqno": 34336028,
"root_hash": "9933d579f836d4f0fd23cfd1e04baa476bcb15a7a6dd21ff9e198adf468199fb",
"file_hash": "04e5114839d6a2fd5ea239872b6a434f334998c3477b7837ad38994d32b64ed2",
"global_id": -239,
"version": 0,
"after_merge": false,
"before_split": false,
"after_split": false,
"want_split": false,
"want_merge": true,
"key_block": false,
"gen_utime": 1700756239,
"start_lt": 42747819000000,
"end_lt": 42747819000004,
"vert_seqno": 1,
"gen_catchain_seqno": 503952,
"min_ref_mc_seqno": 34336025,
"prev_key_block_seqno": 34333054,
"gen_software_version": 3,
"gen_software_capabilities": 46,
"prev_refs": [
"(-1,8000000000000000,34336027)"
],
"in_msg_descr_length": 1,
"out_msg_descr_length": 0,
"rand_seed": "0000000000000000b10000000000000000aeb23a857f000020eafd767d7f0000",
"created_by": "9efb2f376248f8b880c8c90b502b7939df2f3a43788ffa1c3c83706e34a87c3f"
}
Loading

0 comments on commit 4d37ad6

Please sign in to comment.