Skip to content

Commit 8b36479

Browse files
committed
fix: block heights in name_register.test.ts
1 parent 57c8da7 commit 8b36479

File tree

1 file changed

+46
-17
lines changed

1 file changed

+46
-17
lines changed

contrib/core-contract-tests/tests/bns/name_register.test.ts

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
1-
import { Cl } from "@stacks/transactions";
1+
import { Cl, ClarityType } from "@stacks/transactions";
22
import { beforeEach, describe, expect, it } from "vitest";
33
import { createHash } from "node:crypto";
4+
import { project } from '../clarigen-types'; // where your [types.output] was specified
5+
import { projectFactory } from '@clarigen/core';
6+
import { rovOk, mapGet } from '@clarigen/test';
7+
8+
const contracts = projectFactory(project, 'simnet');
9+
const contract = contracts.bns;
410

511
const accounts = simnet.getAccounts();
612
const alice = accounts.get("wallet_1")!;
713
const bob = accounts.get("wallet_2")!;
814
const charlie = accounts.get("wallet_3")!;
915

16+
function utf8ToBytes(str: string) {
17+
return new TextEncoder().encode(str);
18+
}
19+
1020
const cases = [
1121
{
1222
namespace: "blockstack",
@@ -120,7 +130,7 @@ describe("preorder namespace", () => {
120130
[Cl.buffer(ripemd160), Cl.uint(cases[1].value)],
121131
cases[1].namespaceOwner
122132
);
123-
expect(result).toBeOk(Cl.uint(144 + simnet.blockHeight));
133+
expect(result).toBeOk(Cl.uint(143 + simnet.blockHeight));
124134
});
125135
});
126136

@@ -136,7 +146,7 @@ describe("namespace reveal workflow", () => {
136146
[Cl.buffer(ripemd160), Cl.uint(cases[1].value)],
137147
cases[1].namespaceOwner
138148
);
139-
expect(result).toBeOk(Cl.uint(144 + simnet.blockHeight));
149+
expect(result).toBeOk(Cl.uint(143 + simnet.blockHeight));
140150
});
141151

142152
it("should reveal a namespace", () => {
@@ -180,7 +190,7 @@ describe("namespace reveal workflow", () => {
180190
[Cl.buffer(ripemd160), Cl.uint(100)],
181191
bob
182192
);
183-
expect(preorder.result).toBeOk(Cl.uint(144 + simnet.blockHeight));
193+
expect(preorder.result).toBeOk(Cl.uint(141 + simnet.blockHeight));
184194

185195
const register = simnet.callPublicFn(
186196
"bns",
@@ -286,6 +296,7 @@ describe("name revealing workflow", () => {
286296
const merged = new TextEncoder().encode(`${name}.${cases[0].namespace}${cases[0].salt}`);
287297
const sha256 = createHash("sha256").update(merged).digest();
288298
const ripemd160 = createHash("ripemd160").update(sha256).digest();
299+
simnet.mineEmptyBlock();
289300
simnet.callPublicFn("bns", "name-preorder", [Cl.buffer(ripemd160), Cl.uint(2559999)], bob);
290301

291302
const { result } = simnet.callPublicFn(
@@ -328,6 +339,7 @@ describe("name revealing workflow", () => {
328339
const merged = new TextEncoder().encode(`${name}.${cases[0].namespace}${cases[0].salt}`);
329340
const sha256 = createHash("sha256").update(merged).digest();
330341
const ripemd160 = createHash("ripemd160").update(sha256).digest();
342+
simnet.mineEmptyBlock();
331343
simnet.callPublicFn("bns", "name-preorder", [Cl.buffer(ripemd160), Cl.uint(2560000)], bob);
332344

333345
const register = simnet.callPublicFn(
@@ -366,8 +378,8 @@ describe("name revealing workflow", () => {
366378
Cl.tuple({
367379
owner: Cl.standardPrincipal(bob),
368380
["zonefile-hash"]: Cl.bufferFromUtf8(cases[0].zonefile),
369-
["lease-ending-at"]: Cl.some(Cl.uint(16)),
370-
["lease-started-at"]: Cl.uint(6),
381+
["lease-ending-at"]: Cl.some(Cl.uint(14)),
382+
["lease-started-at"]: Cl.uint(4),
371383
})
372384
);
373385
});
@@ -377,6 +389,7 @@ describe("name revealing workflow", () => {
377389
const merged = new TextEncoder().encode(`${name}.${cases[0].namespace}${cases[0].salt}`);
378390
const sha256 = createHash("sha256").update(merged).digest();
379391
const ripemd160 = createHash("ripemd160").update(sha256).digest();
392+
simnet.mineEmptyBlock();
380393
simnet.callPublicFn("bns", "name-preorder", [Cl.buffer(ripemd160), Cl.uint(2560000)], bob);
381394
simnet.callPublicFn(
382395
"bns",
@@ -415,11 +428,11 @@ describe("register a name again before and after expiration", () => {
415428
simnet.callPublicFn(
416429
"bns",
417430
"namespace-preorder",
418-
[Cl.buffer(ripemd160NS), Cl.uint(cases[0].value)],
431+
[Cl.buffer(Uint8Array.from(ripemd160NS)), Cl.uint(cases[0].value)],
419432
cases[0].namespaceOwner
420433
);
421434

422-
simnet.callPublicFn(
435+
const revealResult = simnet.callPublicFn(
423436
"bns",
424437
"namespace-reveal",
425438
[
@@ -431,22 +444,34 @@ describe("register a name again before and after expiration", () => {
431444
],
432445
cases[0].namespaceOwner
433446
);
447+
expect(revealResult.result).toBeOk(Cl.bool(true));
434448

435-
simnet.callPublicFn(
449+
const readyResult = simnet.callPublicFn(
436450
"bns",
437451
"namespace-ready",
438452
[Cl.bufferFromUtf8(cases[0].namespace)],
439453
cases[0].namespaceOwner
440454
);
441-
455+
expect(readyResult.result).toBeOk(Cl.bool(true));
442456
// register bob.blockstack
443457
const name = "bob";
444458
const merged = new TextEncoder().encode(`${name}.${cases[0].namespace}${cases[0].salt}`);
445459
const sha256 = createHash("sha256").update(merged).digest();
446460
const ripemd160 = createHash("ripemd160").update(sha256).digest();
447-
simnet.callPublicFn("bns", "name-preorder", [Cl.buffer(ripemd160), Cl.uint(2560000)], bob);
461+
simnet.mineEmptyBlocks(1);
462+
const preorderResult = simnet.callPublicFn("bns", "name-preorder", [Cl.buffer(Uint8Array.from(ripemd160)), Cl.uint(2560000)], bob);
463+
expect(preorderResult.result.type).toBe(ClarityType.ResponseOk);
448464

449-
simnet.callPublicFn(
465+
const namespace = rovOk(contract.getNamespaceProperties(utf8ToBytes(cases[0].namespace)))
466+
expect(namespace).toBeTruthy();
467+
468+
const preorder = mapGet(contract.identifier, contract.maps.namePreorders, {
469+
buyer: bob,
470+
hashedSaltedFqn: Uint8Array.from(ripemd160),
471+
})
472+
expect(preorder).toBeTruthy();
473+
474+
const registerResult = simnet.callPublicFn(
450475
"bns",
451476
"name-register",
452477
[
@@ -457,6 +482,7 @@ describe("register a name again before and after expiration", () => {
457482
],
458483
bob
459484
);
485+
expect(registerResult.result).toBeOk(Cl.bool(true));
460486
});
461487

462488
it("fails if someone else tries to register it", () => {
@@ -465,13 +491,15 @@ describe("register a name again before and after expiration", () => {
465491
const merged = new TextEncoder().encode(`${name}.${cases[0].namespace}${salt}`);
466492
const sha256 = createHash("sha256").update(merged).digest();
467493
const ripemd160 = createHash("ripemd160").update(sha256).digest();
494+
let blockHeight = simnet.blockHeight;
495+
console.log("blockHeight", blockHeight);
468496
const preorder = simnet.callPublicFn(
469497
"bns",
470498
"name-preorder",
471-
[Cl.buffer(ripemd160), Cl.uint(2560000)],
499+
[Cl.buffer(Uint8Array.from(ripemd160)), Cl.uint(2560000)],
472500
charlie
473501
);
474-
expect(preorder.result).toBeOk(Cl.uint(144 + simnet.blockHeight));
502+
expect(preorder.result).toBeOk(Cl.uint(138 + simnet.blockHeight));
475503

476504
const register = simnet.callPublicFn(
477505
"bns",
@@ -484,6 +512,7 @@ describe("register a name again before and after expiration", () => {
484512
],
485513
charlie
486514
);
515+
console.log(simnet.blockHeight);
487516
expect(register.result).toBeErr(Cl.int(2004));
488517
});
489518

@@ -541,7 +570,7 @@ describe("register a name again before and after expiration", () => {
541570
[Cl.buffer(ripemd160), Cl.uint(2560000)],
542571
bob
543572
);
544-
expect(preorder.result).toBeOk(Cl.uint(144 + simnet.blockHeight));
573+
expect(preorder.result).toBeOk(Cl.uint(138 + simnet.blockHeight));
545574

546575
const register = simnet.callPublicFn(
547576
"bns",
@@ -589,8 +618,8 @@ describe("register a name again before and after expiration", () => {
589618
Cl.tuple({
590619
owner: Cl.standardPrincipal(charlie),
591620
["zonefile-hash"]: Cl.bufferFromAscii("CHARLIE"),
592-
["lease-ending-at"]: Cl.some(Cl.uint(5029)),
593-
["lease-started-at"]: Cl.uint(5019),
621+
["lease-ending-at"]: Cl.some(Cl.uint(5025)),
622+
["lease-started-at"]: Cl.uint(5015),
594623
})
595624
);
596625
});

0 commit comments

Comments
 (0)