Skip to content

Commit d21f405

Browse files
committed
feat: add parties reset and re-inclusion mechanism
1 parent d7f959e commit d21f405

File tree

4 files changed

+60
-3
lines changed

4 files changed

+60
-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: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ export type BlockState = {
4242
parameters: BlockchainParameters;
4343
};
4444

45+
export type ResetOptions = {
46+
clearParties?: boolean;
47+
};
48+
4549
export class MockChain {
4650
readonly #parties: MockChainParty[];
4751
readonly #tip: BlockState;
@@ -104,10 +108,14 @@ export class MockChain {
104108
}
105109
}
106110

107-
reset() {
111+
reset(options?: ResetOptions) {
108112
this.clearUTxOSet();
109113
this.#tip.height = this.#base.height;
110114
this.#tip.timestamp = this.#base.timestamp;
115+
116+
if (options?.clearParties) {
117+
this.#parties.length = 0;
118+
}
111119
}
112120

113121
newParty(name?: string): KeyedMockChainParty;
@@ -120,8 +128,12 @@ export class MockChain {
120128
);
121129
}
122130

123-
addParty(ergoTree: HexString, name?: string): NonKeyedMockChainParty {
124-
return this.#pushParty(new NonKeyedMockChainParty(this, ergoTree, name));
131+
addParty(contractOrParty: HexString | MockChainParty, name?: string): NonKeyedMockChainParty {
132+
return this.#pushParty(
133+
typeof contractOrParty === "string"
134+
? new NonKeyedMockChainParty(this, contractOrParty, name)
135+
: contractOrParty
136+
);
125137
}
126138

127139
#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)