forked from toncenter/tonweb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Slava Fomin II <[email protected]>
- Loading branch information
1 parent
bb9d452
commit 5111017
Showing
6 changed files
with
185 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
dist/types/providers/block-subscription/block-storage.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* A shardchain block definition. | ||
*/ | ||
export interface ShardBlock { | ||
workchain: number; | ||
shardId: string; | ||
shardBlockNumber: number; | ||
} | ||
/** | ||
* A storage for processed block numbers with | ||
* useful query functions. | ||
* | ||
* Used by `BlockSubscription`. | ||
*/ | ||
export interface BlockStorage { | ||
/** | ||
* Inserts new processed masterchain block number and | ||
* new unprocessed shardchain blocks numbers. | ||
* | ||
* Must be in single DB transaction. | ||
*/ | ||
insertBlocks(mcBlockNumber: number, shardBlocks: ShardBlock[]): Promise<void>; | ||
/** | ||
* Returns last processed masterchain block number. | ||
*/ | ||
getLastMasterchainBlockNumber(): (Promise<number | undefined>); | ||
/** | ||
* Marks the specified shardchain block number as processed and | ||
* inserts new unprocessed shardchain blocks numbers. | ||
* | ||
* Must be in single DB transaction. | ||
*/ | ||
setBlockProcessed(workchain: number, shardId: string, shardBlockNumber: number, prevShardBlocks: ShardBlock[]): Promise<void>; | ||
/** | ||
* Returns any unprocessed shard block numbers | ||
* (order is not important). | ||
*/ | ||
getUnprocessedShardBlock(): (Promise<ShardBlock | undefined>); | ||
} |
81 changes: 81 additions & 0 deletions
81
dist/types/providers/block-subscription/block-subscription.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import HttpProvider from '../index'; | ||
import { BlockStorage } from './block-storage'; | ||
export interface BlockSubscriptionOptions { | ||
/** | ||
* Start masterchain block number from which we start | ||
* to process blocks. If not specified, the subscription | ||
* starts from the last block of the network at the | ||
* time of launch. | ||
*/ | ||
startMcBlockNumber?: number; | ||
mcInterval?: number; | ||
shardsInterval?: number; | ||
} | ||
export declare type BlockHandler = ((blockHeader: any, blockShards?: any) => (Promise<void> | void)); | ||
export declare class BlockSubscription { | ||
/** | ||
* TonWeb HTTP provider. | ||
*/ | ||
private readonly provider; | ||
/** | ||
* Persistent storage for storing block numbers | ||
* that we have already processed. | ||
*/ | ||
private readonly storage; | ||
/** | ||
* Callback which is called for each block. | ||
* | ||
* Callback may throw an error, in this case the block | ||
* processing will be interrupted and block will not | ||
* be saved in the storage as processed. | ||
* | ||
* Shardchain blocks are processed OUT of chronological order. | ||
* Masterchain blocks are processed in chronological order. | ||
* | ||
* For masterchain `workchain === -1` and | ||
* `shardId === -9223372036854775808` | ||
*/ | ||
private readonly onBlock; | ||
/** | ||
* Start masterchain block number from which we start to process blocks. | ||
* if not specified, the subscription starts from the last block of the network at the time of launch. | ||
*/ | ||
private readonly options; | ||
private startMcBlockNumber; | ||
private readonly mcInterval; | ||
private readonly shardsInterval; | ||
private startLT?; | ||
private mcIntervalId?; | ||
private shardsIntervalId?; | ||
constructor( | ||
/** | ||
* TonWeb HTTP provider. | ||
*/ | ||
provider: HttpProvider, | ||
/** | ||
* Persistent storage for storing block numbers | ||
* that we have already processed. | ||
*/ | ||
storage: BlockStorage, | ||
/** | ||
* Callback which is called for each block. | ||
* | ||
* Callback may throw an error, in this case the block | ||
* processing will be interrupted and block will not | ||
* be saved in the storage as processed. | ||
* | ||
* Shardchain blocks are processed OUT of chronological order. | ||
* Masterchain blocks are processed in chronological order. | ||
* | ||
* For masterchain `workchain === -1` and | ||
* `shardId === -9223372036854775808` | ||
*/ | ||
onBlock: BlockHandler, | ||
/** | ||
* Start masterchain block number from which we start to process blocks. | ||
* if not specified, the subscription starts from the last block of the network at the time of launch. | ||
*/ | ||
options: BlockSubscriptionOptions); | ||
start(): Promise<void>; | ||
stop(): void; | ||
} |
44 changes: 44 additions & 0 deletions
44
dist/types/providers/block-subscription/in-memory-block-storage.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { BlockStorage, ShardBlock } from './block-storage'; | ||
export declare type LogFunction = (message: string) => void; | ||
/** | ||
* Simple in-memory implementation of the processed | ||
* block number storage. | ||
*/ | ||
export declare class InMemoryBlockStorage implements BlockStorage { | ||
private readonly logFunction; | ||
/** | ||
* @todo: should we use `Map` here? | ||
* Map of the processed masterchain blocks: | ||
* `key` is the block number, while | ||
* `value` reflects `isProcessed` state. | ||
*/ | ||
private readonly masterBlocks; | ||
/** | ||
* @todo: should we use `Map` here? | ||
* Map of the processed shardchain blocks: | ||
* The `key` should be constructed this way: | ||
* `${workchain}_${shardId}_${shardBlockNumber}` | ||
* and the `value` reflects `isProcessed` state. | ||
*/ | ||
private readonly shardBlocks; | ||
constructor(logFunction: LogFunction); | ||
insertBlocks(mcBlockNumber: number, shardBlockNumbers: ShardBlock[]): Promise<void>; | ||
getLastMasterchainBlockNumber(): Promise<number | undefined>; | ||
setBlockProcessed(workchain: number, shardId: string, shardBlockNumber: number, prevShardBlocks: ShardBlock[]): Promise<void>; | ||
getUnprocessedShardBlock(): (Promise<ShardBlock | undefined>); | ||
/** | ||
* Inserts new unprocessed shardchain block numbers. | ||
* Block number (workchain + shardId + shardBlockNumber) should be IGNORED if it is already in the storage. | ||
*/ | ||
private insertShardBlocks; | ||
/** | ||
* Generates unique key for identifying the specified | ||
* shardchain block. | ||
*/ | ||
private getShardBlockKey; | ||
/** | ||
* Parses the specified shardchain block key and returns | ||
* a shardchain block definition. | ||
*/ | ||
private parseShardBlockKey; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export { BlockSubscription, BlockSubscriptionOptions, BlockHandler, } from './block-subscription'; | ||
export { BlockStorage, ShardBlock, } from './block-storage'; | ||
export { InMemoryBlockStorage, LogFunction, } from './in-memory-block-storage'; |
11 changes: 11 additions & 0 deletions
11
dist/types/providers/block-subscription/sql-block-storage.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { BlockStorage, ShardBlock } from './block-storage'; | ||
/** | ||
* This is just a proof of concept of the | ||
* RDBMS (SQL) shardchain block storage implementation. | ||
*/ | ||
export declare class SqlBlockStorage implements BlockStorage { | ||
insertBlocks(mcBlockNumber: number, shardBlockNumbers: ShardBlock[]): Promise<void>; | ||
getLastMasterchainBlockNumber(): Promise<number | undefined>; | ||
setBlockProcessed(workchain: number, shardId: string, shardBlockNumber: number, prevShardBlocks: ShardBlock[]): Promise<void>; | ||
getUnprocessedShardBlock(): (Promise<ShardBlock | undefined>); | ||
} |