Skip to content

Commit 2248189

Browse files
committed
add additional registers merging
1 parent 2f63718 commit 2248189

File tree

2 files changed

+36
-18
lines changed

2 files changed

+36
-18
lines changed

packages/core/src/builder/outputBuilder.spec.ts

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,18 @@ describe("Additional registers", () => {
293293
builder = new OutputBuilder(SAFE_MIN_BOX_VALUE, ergoTreeHex, height);
294294
});
295295

296+
it("should merge additional registers", () => {
297+
builder.setAdditionalRegisters({ R4: "0101", R6: "0102" });
298+
expect(builder.additionalRegisters).toEqual({ R4: "0101", R6: "0102" });
299+
300+
expect(() => builder.build()).toThrow(InvalidRegistersPacking); // R5 is missing
301+
302+
builder.setAdditionalRegisters({ R5: "0103" });
303+
expect(builder.additionalRegisters).toEqual({ R4: "0101", R5: "0103", R6: "0102" });
304+
305+
expect(() => builder.build()).not.toThrow();
306+
});
307+
296308
it("Should bind registers properly", () => {
297309
builder.setAdditionalRegisters({
298310
R4: "0580c0fc82aa02",
@@ -335,28 +347,37 @@ describe("Additional registers", () => {
335347
* R9 without adding register R4 to R8, for example.
336348
*/
337349
it("Should throw if some register is skipped", () => {
350+
let outputBuilder = new OutputBuilder(SAFE_MIN_BOX_VALUE, ergoTreeHex, height);
338351
// R4 not included
339352
expect(() => {
340-
builder.setAdditionalRegisters({
341-
R5: "0580c0fc82aa02"
342-
} as NonMandatoryRegisters);
353+
outputBuilder
354+
.setAdditionalRegisters({
355+
R5: "0580c0fc82aa02"
356+
} as NonMandatoryRegisters)
357+
.build();
343358
}).toThrow(InvalidRegistersPacking);
344359

360+
outputBuilder = new OutputBuilder(SAFE_MIN_BOX_VALUE, ergoTreeHex, height);
345361
// R5 not included
346362
expect(() => {
347-
builder.setAdditionalRegisters({
348-
R4: "0580c0fc82aa02",
349-
R6: "0580c0fc82aa02"
350-
} as NonMandatoryRegisters);
363+
outputBuilder
364+
.setAdditionalRegisters({
365+
R4: "0580c0fc82aa02",
366+
R6: "0580c0fc82aa02"
367+
} as NonMandatoryRegisters)
368+
.build();
351369
}).toThrow(InvalidRegistersPacking);
352370

371+
outputBuilder = new OutputBuilder(SAFE_MIN_BOX_VALUE, ergoTreeHex, height);
353372
// R5 explicitly set to undefined
354373
expect(() => {
355-
builder.setAdditionalRegisters({
356-
R4: "0580c0fc82aa02",
357-
R5: undefined,
358-
R6: "0580c0fc82aa02"
359-
} as NonMandatoryRegisters);
374+
outputBuilder
375+
.setAdditionalRegisters({
376+
R4: "0580c0fc82aa02",
377+
R5: undefined,
378+
R6: "0580c0fc82aa02"
379+
} as NonMandatoryRegisters)
380+
.build();
360381
}).toThrow(InvalidRegistersPacking);
361382
});
362383
});

packages/core/src/builder/outputBuilder.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,17 +167,13 @@ export class OutputBuilder {
167167
}
168168

169169
setAdditionalRegisters(registers: NonMandatoryRegisters<ConstantInput>): OutputBuilder {
170-
const hexRegisters: NonMandatoryRegisters = {};
171170
for (const key in registers) {
172171
const r = registers[key as keyof NonMandatoryRegisters];
173172
if (!r) continue;
174173

175-
hexRegisters[key as keyof NonMandatoryRegisters] = typeof r === "string" ? r : r.toHex();
174+
this.#registers[key as keyof NonMandatoryRegisters] = typeof r === "string" ? r : r.toHex();
176175
}
177176

178-
if (!areRegistersDenselyPacked(hexRegisters)) throw new InvalidRegistersPacking();
179-
this.#registers = hexRegisters;
180-
181177
return this;
182178
}
183179

@@ -187,8 +183,9 @@ export class OutputBuilder {
187183
}
188184

189185
build(transactionInputs?: UnsignedInput[] | Box<Amount>[]): ErgoBoxCandidate {
190-
let tokens: TokenAmount<bigint>[];
186+
if (!areRegistersDenselyPacked(this.#registers)) throw new InvalidRegistersPacking();
191187

188+
let tokens: TokenAmount<bigint>[];
192189
if (this.minting) {
193190
const mintingTokenId = transactionInputs ? transactionInputs[0]?.boxId : undefined;
194191
tokens = this.assets.toArray(mintingTokenId);

0 commit comments

Comments
 (0)