Skip to content

Commit

Permalink
fix: remove invoice hash testing from stacks-m2m-v2
Browse files Browse the repository at this point in the history
  • Loading branch information
whoabuddy committed Feb 21, 2024
1 parent bdd2b2c commit e79c58c
Showing 1 changed file with 0 additions and 194 deletions.
194 changes: 0 additions & 194 deletions tests/stacks-m2m-v2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,6 @@ const testResource = [
Cl.uint(defaultPrice),
];

// based on hex value printed to console in first run
// curious to see if this is the same every time
// before we have a local function to compute the same data
// (some 0x053637c68fd20a0bdeef9712f0eb6c2b5c041a7f0ee6a6aed601267c25a39cb6)
// const expectedBlock0Resource0 = Buffer.from("053637c68fd20a0bdeef9712f0eb6c2b5c041a7f0ee6a6aed601267c25a39cb6", "hex")
// incremented resource count (and others) to start at 1
// (some 0x38bc32acb79a15b7a0b04f4268eba0c5be61d17e0629aac508da25d8884b017e)
// changed hashing algorithm, now composed of: resource name, contract name, stacks block hash, bitcoin block hash, user pubkey
// (some 0x520b34a624e73b0533db4475f260febb048e7eb150f8773779b9fd9dab85d652)
// (some 0xffc41d187c6b7bdd89fec3b4cdf967f7ab81d1efb3358cee6df8f08c9d1c76e9)
const expectedBlock0Resource1 = Buffer.from(
"ffc41d187c6b7bdd89fec3b4cdf967f7ab81d1efb3358cee6df8f08c9d1c76e9",
"hex"
);

describe("Adding a resource", () => {
it("add-resource() fails if not called by deployer", async () => {
// ARRANGE
Expand Down Expand Up @@ -470,185 +455,6 @@ describe("Setting a Payment Address", () => {
});
});

describe("Generating an invoice hash", () => {
it("get-invoice-hash() returns none if resource is not found", async () => {
// ARRANGE
const simnet = await initSimnet();
const accounts = simnet.getAccounts();
const deployer = accounts.get("deployer")!;
// ACT
const response = simnet.callReadOnlyFn(
"stacks-m2m-v2",
"get-invoice-hash",
[
Cl.standardPrincipal(deployer), // user
Cl.uint(0), // resource index
Cl.uint(0), // block height
],
deployer
);
// ASSERT
expect(response.result).toBeNone();
});

it("get-invoice-hash() succeeds and returns the correct value", async () => {
// ARRANGE
const simnet = await initSimnet();
const accounts = simnet.getAccounts();
const deployer = accounts.get("deployer")!;
// ACT
simnet.callPublicFn(
"stacks-m2m-v2",
"add-resource",
testResource,
deployer
);
const response = simnet.callReadOnlyFn(
"stacks-m2m-v2",
"get-invoice-hash",
[
Cl.standardPrincipal(deployer), // user
Cl.uint(1), // resource index
Cl.uint(0), // block height
],
deployer
);
// ASSERT
expect(response.result).toBeSome(Cl.buffer(expectedBlock0Resource1));
});

it("get-invoice-hash() succeeds and returns the correct value after the chain progresses", async () => {
// ARRANGE
const simnet = await initSimnet();
const accounts = simnet.getAccounts();
const deployer = accounts.get("deployer")!;
// ACT
simnet.callPublicFn(
"stacks-m2m-v2",
"add-resource",
testResource,
deployer
);
simnet.mineEmptyBlocks(5000);
const response = simnet.callReadOnlyFn(
"stacks-m2m-v2",
"get-invoice-hash",
[
Cl.standardPrincipal(deployer), // user
Cl.uint(1), // resource index
Cl.uint(0), // block height
],
deployer
);
// ASSERT
expect(response.result).toBeSome(Cl.buffer(expectedBlock0Resource1));
});

it("get-invoice-hash() succeeds and generates unique values for different users at different block heights", async () => {
// ARRANGE
const simnet = await initSimnet();
const accounts = simnet.getAccounts();
const deployer = accounts.get("deployer")!;
const address1 = accounts.get("wallet_1")!;
const address2 = accounts.get("wallet_2")!;
const wallets = [deployer, address1, address2];

// ACT
// add a resource
simnet.callPublicFn(
"stacks-m2m-v2",
"add-resource",
testResource,
deployer
);
// progress the chain
simnet.mineEmptyBlocks(5000);
// create an array of invoice hashes for 500 blocks
const invoiceHashes = [];
for (const wallet of wallets) {
for (let i = 0; i < 500; i++) {
const response = simnet.callReadOnlyFn(
"stacks-m2m-v2",
"get-invoice-hash",
[
Cl.standardPrincipal(wallet), // user
Cl.uint(1), // resource index
Cl.uint(i), // block height
],
wallet
);
invoiceHashes.push(response.result);
}
}

// ASSERT
// check that each invoice hash is unique
const uniqueInvoiceHashes = new Set(invoiceHashes);
expect(uniqueInvoiceHashes.size).toEqual(invoiceHashes.length);
});

it("get-invoice-hash() succeeds and generates consistent values for different users at different block heights", async () => {
// ARRANGE
const simnet = await initSimnet();
const accounts = simnet.getAccounts();
const deployer = accounts.get("deployer")!;
const address1 = accounts.get("wallet_1")!;
const address2 = accounts.get("wallet_2")!;
const wallets = [deployer, address1, address2];

// ACT
// add a resource
simnet.callPublicFn(
"stacks-m2m-v2",
"add-resource",
testResource,
deployer
);
// progress the chain
simnet.mineEmptyBlocks(5000);
// create an array of invoice hashes for 500 blocks
const firstInvoiceHashes = [];
for (const wallet of wallets) {
for (let i = 0; i < 500; i++) {
const response = simnet.callReadOnlyFn(
"stacks-m2m-v2",
"get-invoice-hash",
[
Cl.standardPrincipal(wallet), // user
Cl.uint(1), // resource index
Cl.uint(i), // block height
],
wallet
);
firstInvoiceHashes.push(response.result);
}
}
// progress the chain
simnet.mineEmptyBlocks(5000);
// create an array of invoice hashes for 500 blocks
const secondInvoiceHashes = [];
for (const wallet of wallets) {
for (let i = 0; i < 500; i++) {
const response = simnet.callReadOnlyFn(
"stacks-m2m-v2",
"get-invoice-hash",
[
Cl.standardPrincipal(wallet), // user
Cl.uint(1), // resource index
Cl.uint(i), // block height
],
wallet
);
secondInvoiceHashes.push(response.result);
}
}

// ASSERT
// check that the arrays are equal
expect(firstInvoiceHashes).toEqual(secondInvoiceHashes);
});
});

describe("Paying an invoice", () => {
it("pay-invoice() fails if resource is not found", async () => {
// ARRANGE
Expand Down

0 comments on commit e79c58c

Please sign in to comment.