Skip to content

Commit 97f4935

Browse files
authored
Merge pull request #192 from fleet-sdk/mock-chain-reset
Add mock-chain parties reset and re-inclusion support
2 parents 071639b + 6db31b6 commit 97f4935

File tree

4 files changed

+66
-3
lines changed

4 files changed

+66
-3
lines changed

.changeset/hip-falcons-take.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@fleet-sdk/mock-chain": patch
3+
---
4+
5+
Add parties cleanup and re-inclusion

packages/mock-chain/src/mockChain.spec.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,47 @@ describe("Mock chain instantiation", () => {
9595
chain.reset();
9696

9797
// should clear and reset state to original values
98+
expect(chain.parties).to.have.length(2); // parties should not be cleared by default
9899
expect(chain.height).to.be.equal(creationHeight);
99100
expect(chain.timestamp).to.be.equal(creationTimestamp);
100101
expect(bob.utxos).to.have.length(0);
101102
expect(alice.utxos).to.have.length(0);
102103
});
103104

105+
it("Should reset the chain and clean up the parties", () => {
106+
const chain = new MockChain(234897);
107+
const creationHeight = chain.height;
108+
const creationTimestamp = chain.timestamp;
109+
110+
const bob = chain
111+
.newParty()
112+
.withBalance({ nanoergs: 1000000n })
113+
.withBalance({ nanoergs: 19827398237n });
114+
const alice = chain.newParty().withBalance({ nanoergs: 6000000n });
115+
116+
expect(bob.utxos).to.have.length(2);
117+
expect(alice.utxos).to.have.length(1);
118+
119+
chain.newBlocks(100);
120+
121+
expect(chain.height).to.be.greaterThan(creationHeight);
122+
expect(chain.timestamp).to.be.greaterThan(creationTimestamp);
123+
124+
// reset the chain
125+
chain.reset({ clearParties: true });
126+
127+
// should clear and reset state to original values
128+
expect(chain.parties).to.have.length(0); // parties should be cleared
129+
expect(chain.height).to.be.equal(creationHeight);
130+
expect(chain.timestamp).to.be.equal(creationTimestamp);
131+
expect(bob.utxos).to.have.length(0);
132+
expect(alice.utxos).to.have.length(0);
133+
134+
bob.addBalance({ nanoergs: 1000000n });
135+
expect(bob.utxos).to.have.length(1); // parties should be re-added after adding balance
136+
expect(chain.parties).to.have.length(1);
137+
});
138+
104139
it("Should simulate new blocks", () => {
105140
const blockTime = 120_000;
106141
const height = 100;

packages/mock-chain/src/mockChain.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ export type BlockState = {
4343
parameters: BlockchainParameters;
4444
};
4545

46+
export type ResetOptions = {
47+
clearParties?: boolean;
48+
};
49+
4650
export class MockChain {
4751
readonly #parties: MockChainParty[];
4852
readonly #tip: BlockState;
@@ -105,10 +109,14 @@ export class MockChain {
105109
}
106110
}
107111

108-
reset() {
112+
reset(options?: ResetOptions) {
109113
this.clearUTxOSet();
110114
this.#tip.height = this.#base.height;
111115
this.#tip.timestamp = this.#base.timestamp;
116+
117+
if (options?.clearParties) {
118+
this.#parties.length = 0;
119+
}
112120
}
113121

114122
newParty(name?: string): KeyedMockChainParty;
@@ -121,8 +129,18 @@ export class MockChain {
121129
);
122130
}
123131

124-
addParty(ergoTree: HexString, name?: string): NonKeyedMockChainParty {
125-
return this.#pushParty(new NonKeyedMockChainParty(this, ergoTree, name));
132+
addParty(contractOrParty: KeyedMockChainParty): KeyedMockChainParty;
133+
addParty(contractOrParty: NonKeyedMockChainParty, name?: string): NonKeyedMockChainParty;
134+
addParty(contractOrParty: HexString, name?: string): KeyedMockChainParty;
135+
addParty(
136+
contractOrParty: HexString | MockChainParty,
137+
name?: string
138+
): NonKeyedMockChainParty | KeyedMockChainParty {
139+
return this.#pushParty(
140+
typeof contractOrParty === "string"
141+
? new NonKeyedMockChainParty(this, contractOrParty, name)
142+
: contractOrParty
143+
);
126144
}
127145

128146
#pushParty<T extends MockChainParty>(party: T): T {

packages/mock-chain/src/party/mockChainParty.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ export abstract class MockChainParty {
6868
addUTxOs(utxos: MockUTxOInput): MockChainParty {
6969
this._utxos.add(utxos);
7070

71+
// Ensure the party is registered in the chain
72+
if (!this._chain.parties.includes(this)) {
73+
this._chain.addParty(this);
74+
}
75+
7176
return this;
7277
}
7378

0 commit comments

Comments
 (0)