Skip to content

Commit

Permalink
Fix client ack on jumps & return generated block data on set_chain
Browse files Browse the repository at this point in the history
  • Loading branch information
guilledk committed Aug 27, 2024
1 parent aa1a7dc commit 7b08c39
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
34 changes: 25 additions & 9 deletions src/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof ChainDescriptorSchema>;

export const SetChainResponseSchema = z.object({
blocks: z.record(z.array(z.tuple([z.number(), z.string()])))
});

export type SetChainResponse = z.infer<typeof SetChainResponseSchema>;


export class MockChain {
private descriptor: ChainDescriptor;
Expand Down Expand Up @@ -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":
Expand Down Expand Up @@ -183,7 +189,9 @@ export class MockChain {
console.log('Network is down');
}

async setChain(descriptor: ChainDescriptor) {
async setChain(descriptor: ChainDescriptor): Promise<SetChainResponse> {
descriptor = ChainDescriptorSchema.parse(descriptor);

if (this.networkUp)
await this.stopNetwork();

Expand All @@ -194,21 +202,29 @@ 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<number, string> = 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;
this.lastSent = this.clientAckBlock;
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) {
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/shipMocker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, () => {
Expand Down

0 comments on commit 7b08c39

Please sign in to comment.