Skip to content

Commit

Permalink
mock-chain: add jump to
Browse files Browse the repository at this point in the history
  • Loading branch information
arobsn committed Jul 21, 2023
1 parent 4875f2a commit 3b6d3aa
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
22 changes: 22 additions & 0 deletions packages/mock-chain/src/mockChain.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
4 changes: 4 additions & 0 deletions packages/mock-chain/src/mockChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 3b6d3aa

Please sign in to comment.