Skip to content

Commit

Permalink
Add CanNamespaceBeRegisteredTrue invariant
Browse files Browse the repository at this point in the history
This commit:
- Adds the CanNamespaceBeRegisteredTrue invariant. This checks that can-namespace-be-registered always returns (ok true) when namespace is not yet registered or its launchability period has passed since its reveal.
- Adds the burnBlockHeight to the model.
- Adds the newly generated invariant to the invariants list.
  • Loading branch information
BowTiedRadone committed Jul 17, 2024
1 parent 1240283 commit b670ec3
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
3 changes: 3 additions & 0 deletions tests/BNS-V2.stateful.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { GetBnsFromIdNone } from "./state/GetBnsFromIdNone.ts";
import { GetPrimaryNameNone } from "./state/GetPrimaryNameNone.ts";
import { GetNamespacePropertiesErr } from "./state/GetNamespacePropertiesErr.ts";
import { GetNamespacePrice } from "./state/GetNamespacePrice.ts";
import { CanNamespaceBeRegisteredTrue } from "./state/CanNamespaceBeRegistered.ts";

it("executes BNS-V2 state interactions", async () => {
const excludedAccounts = ["faucet", "deployer"];
Expand All @@ -23,9 +24,11 @@ it("executes BNS-V2 state interactions", async () => {
GetPrimaryNameNone(filteredAccounts),
GetNamespacePropertiesErr(filteredAccounts),
GetNamespacePrice(filteredAccounts),
CanNamespaceBeRegisteredTrue(filteredAccounts),
];

const model = {
burnBlockHeight: 0,
lastTokenId: 0,
owners: new Map(),
indexToName: new Map(),
Expand Down
57 changes: 57 additions & 0 deletions tests/state/CanNamespaceBeRegistered.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import fc from "fast-check";
import { Model } from "./types";
import { Simnet } from "@hirosystems/clarinet-sdk";
import { expect } from "vitest";
import { Cl } from "@stacks/transactions";
import { encoder, prettyConsoleLog } from "../BNS-V2.helper";

const NAMESPACE_LAUNCHABILITY_TTL = 52595;

export const CanNamespaceBeRegisteredTrue = (
accounts: Map<string, string>,
) =>
fc
.record({
sender: fc.constantFrom(...accounts),
namespace: fc.string({ maxLength: 20 }),
})
.map((r) => ({
check: (model: Readonly<Model>) => {
const namespace = model.namespaces.get(r.namespace);

if (
!namespace || namespace.launchedAt === undefined ||
namespace.revealedAt === undefined
) {
return true;
}

return model.burnBlockHeight >
namespace.revealedAt + NAMESPACE_LAUNCHABILITY_TTL;
},
run: (_model: Model, real: Simnet) => {
const [wallet, address] = r.sender;
const namespaceBuff = encoder.encode(r.namespace);
// Act
const { result: canRegisterNamespaceOptional } = real.callReadOnlyFn(
"BNS-V2",
"can-namespace-be-registered",
[Cl.buffer(namespaceBuff)],
address,
);

// Assert
expect(canRegisterNamespaceOptional).toBeOk(Cl.bool(true));

prettyConsoleLog(
"Ӿ tx-sender",
wallet,
"✓",
"can-namespace-be-registered",
`namespace: "${r.namespace}"`,
"response: (ok true)",
);
},
toString: () =>
`can-namespace-be-registered namespace "${r.namespace}" response (ok true)`,
}));
1 change: 1 addition & 0 deletions tests/state/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export type Model = {
burnBlockHeight: number;
lastTokenId: number;
owners: Map<number, string>;
indexToName: Map<number, Name>;
Expand Down

0 comments on commit b670ec3

Please sign in to comment.