From 7b08c39e79c512ce9e68af1df0e44b4bd8fbf3bd Mon Sep 17 00:00:00 2001 From: Guillermo Rodriguez Date: Tue, 27 Aug 2024 14:15:05 -0300 Subject: [PATCH] Fix client ack on jumps & return generated block data on set_chain --- package.json | 2 +- src/chain.ts | 34 +++++++++++++++++++++++++--------- src/shipMocker.ts | 4 ++-- 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index 89be362..b420f1d 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@guilledk/leap-mock", "repository": "guilledk/leap-mock", - "version": "0.3.0", + "version": "0.4.0", "description": "", "main": "./build/shipMocker.js", "types": "./build/index.d.ts", diff --git a/src/chain.ts b/src/chain.ts index 63a89b8..e973fd6 100644 --- a/src/chain.ts +++ b/src/chain.ts @@ -23,15 +23,21 @@ export const ChainDescriptorSchema = z.object({ start_time: z.string().optional(), jumps: z.array(JumpInfoSchema).default([]), chain_start_block: z.number().default(1), - chain_end_block: z.number().default(1000), + chain_end_block: z.number().default(100), session_start_block: z.number().default(1), - session_stop_block: z.number().default(1000), + session_stop_block: z.number().default(100), http_port: z.number().default(8889), ship_port: z.number().default(18999) }); export type ChainDescriptor = z.infer; +export const SetChainResponseSchema = z.object({ + blocks: z.record(z.array(z.tuple([z.number(), z.string()]))) +}); + +export type SetChainResponse = z.infer; + export class MockChain { private descriptor: ChainDescriptor; @@ -70,7 +76,7 @@ export class MockChain { switch (requestType) { case "get_blocks_request_v0": - this.ackBlocks(ws, 1); + this.ackBlocks(ws, 10); break; case "get_status_request_v0": @@ -183,7 +189,9 @@ export class MockChain { console.log('Network is down'); } - async setChain(descriptor: ChainDescriptor) { + async setChain(descriptor: ChainDescriptor): Promise { + descriptor = ChainDescriptorSchema.parse(descriptor); + if (this.networkUp) await this.stopNetwork(); @@ -194,14 +202,20 @@ export class MockChain { this.jumps = new Map(descriptor.jumps.entries()); this.blockInfo = new Map(); + const resp = {}; - for(let j = 0; j <= descriptor.jumps.length; j++) { + for(let j = 0; j <= descriptor.jumps.length; j++) { const blocks: Map = new Map(); - for (let blockNum = descriptor.chain_start_block; blockNum <= descriptor.chain_end_block; blockNum++) - blocks.set(blockNum, randomHash()); + const blocks_resp = []; + for (let blockNum = descriptor.chain_start_block; blockNum <= descriptor.chain_end_block; blockNum++) { + const block: [number, string] = [blockNum, randomHash()] + blocks.set(blockNum, block[1]); + blocks_resp.push(block) + } this.blockInfo.set(j, blocks); - }; + resp[j] = blocks_resp; + } console.log(`CONTROL: set chain to:\n${JSON.stringify(descriptor, null, 4)}`); this.clientAckBlock = this.descriptor.session_start_block - 1; @@ -209,6 +223,8 @@ export class MockChain { console.log(`CONTROL: set session to ${descriptor.session_start_block}-${descriptor.session_stop_block}`); await this.startNetwork(); + + return SetChainResponseSchema.parse({blocks: resp}); } getBlockHash(blockNum: number) { @@ -338,7 +354,7 @@ export class MockChain { } setBlock(num: number) { - this.clientAckBlock = num + 1; + this.clientAckBlock = num + (this.lastSent - num); this.lastSent = num; this.forkDB.block_id = this.getBlockHash(num); this.forkDB.block_num = num; diff --git a/src/shipMocker.ts b/src/shipMocker.ts index 92e9319..4816df3 100644 --- a/src/shipMocker.ts +++ b/src/shipMocker.ts @@ -13,8 +13,8 @@ controlApp.use(bodyParser.json()); controlApp.post('/set_chain', async (req, res) => { const data: ChainDescriptor = req.body; - await chain.setChain(data); - res.json({ result: 'ok'}); + const chainInfo = await chain.setChain(data); + res.json({ result: chainInfo }); }); controlApp.listen(controlPort, () => {