From 3b6d3aac75f9f9ec3f7d0c728f9632bfc433b5a2 Mon Sep 17 00:00:00 2001 From: arobsn Date: Fri, 21 Jul 2023 16:13:39 -0300 Subject: [PATCH] mock-chain: add jump to --- packages/mock-chain/src/mockChain.spec.ts | 22 ++++++++++++++++++++++ packages/mock-chain/src/mockChain.ts | 4 ++++ 2 files changed, 26 insertions(+) diff --git a/packages/mock-chain/src/mockChain.spec.ts b/packages/mock-chain/src/mockChain.spec.ts index 7257d641..f268b78a 100644 --- a/packages/mock-chain/src/mockChain.spec.ts +++ b/packages/mock-chain/src/mockChain.spec.ts @@ -45,6 +45,28 @@ describe("Mock chain instantiation", () => { expect(chain.parties).to.have.length(2); expect(chain.parties[1]).to.be.equal(alice); }); + + it("Should simulate new blocks", () => { + const blockTime = 120_000; + const height = 100; + const timestamp = new Date().getTime(); + + const chain = new MockChain({ height, timestamp }); + expect(chain.height).to.be.equal(height); + expect(chain.timestamp).to.be.equal(timestamp); + + chain.newBlock(); // +1 block + expect(chain.height).to.be.equal(height + 1); + expect(chain.timestamp).to.be.equal(timestamp + 1 * blockTime); + + chain.newBlocks(10); // +10 blocks + expect(chain.height).to.be.equal(111); + expect(chain.timestamp).to.be.equal(timestamp + 11 * blockTime); + + chain.jumpTo(250); // jump to block 250 + expect(chain.height).to.be.equal(250); + expect(chain.timestamp).to.be.equal(timestamp + 150 * blockTime); + }); }); describe("Contract execution and chain mocking", () => { diff --git a/packages/mock-chain/src/mockChain.ts b/packages/mock-chain/src/mockChain.ts index 06c88349..3cbd8d45 100644 --- a/packages/mock-chain/src/mockChain.ts +++ b/packages/mock-chain/src/mockChain.ts @@ -73,6 +73,10 @@ export class MockChain { this._timeStamp += BLOCK_TIME_MS * count; } + jumpTo(newHeight: number) { + this.newBlocks(newHeight - this._height); + } + newParty(name?: string): MockChainParty; newParty(params?: MockChainPartyParams): MockChainParty; newParty(nameOrParams?: string | MockChainPartyParams): MockChainParty {