Skip to content

Commit

Permalink
storage node tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Pandelis Symeonidis committed Nov 8, 2023
1 parent e7a2c20 commit 2edfba0
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 26 deletions.
84 changes: 59 additions & 25 deletions agoric/contract/test/swingsetTests/bootstrap/bootstrap-mint.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { E } from '@endo/eventual-send';
import { AmountMath } from '@agoric/ertp';
import { flow } from '../flow.js';
import { makeKreadUser } from './make-bootstrap-users.js';
import { makeCopyBag } from '@agoric/store';
import { makeCopyBag, mustMatch } from '@agoric/store';
import { TimestampRecordShape } from '@agoric/time';

export async function setupMintTests(context) {
const { contractAssets, paymentAsset } = context;
Expand Down Expand Up @@ -163,6 +164,7 @@ export async function mintExpectedFlow(context) {
paymentAsset,
users: { alice },
zoe,
getFromVStorage,
} = context;
const { message, give, offerArgs } = flow.mintCharacter.expected;

Expand Down Expand Up @@ -193,8 +195,13 @@ export async function mintExpectedFlow(context) {
offerArgs.name,
'New character is added to contract registry',
);
// console.log("STORAGE NODE: ")
// console.log(storageNode.getPath("kread.character"))
const vStorageCharacterData = getFromVStorage('kread.character');
assert.equal(vStorageCharacterData.name, offerArgs.name);
const vStorageInventoryData = getFromVStorage(
`kread.character.inventory-${offerArgs.name}`,
);

assert.equal(vStorageInventoryData.length, 3);

const payout = await E(userSeat).getPayout('Asset');
await E(purses.character).deposit(payout);
Expand Down Expand Up @@ -386,7 +393,7 @@ export async function mintNoCharactersAvailable(context) {
zoe,
} = context;
const { offerArgs, message, give } = flow.mintCharacter.noAvailability;

const mintCharacterInvitation = await E(
publicFacet,
).makeMintCharacterInvitation();
Expand Down Expand Up @@ -422,7 +429,7 @@ export async function mintNoCharactersAvailable(context) {

export async function mintInventoryCheck(context) {
/** @type {Context} */
const { publicFacet } = context;
const { publicFacet, getFromVStorage } = context;
const { offerArgs } = flow.mintCharacter.expected;

const characterInventory = await E(publicFacet).getCharacterInventory(
Expand All @@ -443,22 +450,16 @@ export async function mintInventoryCheck(context) {
'Two or more items have the same category',
);

assert.equal(
mappedInventory.filter((i) => i.rarity < 20).length,
2,
'No two common items',
);

assert.equal(
mappedInventory.filter((i) => i.rarity > 19).length,
1,
'No uncommon to legendary item found.',
const vStorageInventoryItems = getFromVStorage(
`kread.character.inventory-${offerArgs.name}`,
);
mustMatch(vStorageInventoryItems, characterInventory.items);
}

export async function mintItemExpectedFlow(context) {
/** @type {Context} */
const { creatorFacet, contractAssets, purses, zoe } = context;
const { creatorFacet, contractAssets, purses, zoe, getFromVStorage } =
context;
const { want, message } = flow.mintItem.expected;

const mintItemInvitation = await E(creatorFacet).makeMintItemInvitation();
Expand All @@ -483,11 +484,30 @@ export async function mintItemExpectedFlow(context) {
want.name,
'New Item was not added to character purse',
);

const vStorageItem = getFromVStorage('kread.item');
mustMatch(
harden(Object.keys(vStorageItem).sort()),
harden(['history', 'id', 'item']),
);

mustMatch(vStorageItem.item, want);
assert.equal(vStorageItem.id, 3);
mustMatch(
vStorageItem.history,
harden([
{
type: 'mint',
data: want,
timestamp: TimestampRecordShape,
},
]),
);
}

export async function mintSameItemSFT(context) {
/** @type {Context} */
const { creatorFacet, contractAssets, purses, zoe } = context;
const { creatorFacet, contractAssets, purses, zoe, getFromVStorage } = context;
const { want, message } = flow.mintItem.expected;

const mintItemInvitation = await E(creatorFacet).makeMintItemInvitation();
Expand Down Expand Up @@ -518,7 +538,15 @@ export async function mintSameItemSFT(context) {
2n,
'Supply of item not increased to 2',
);
assert.equal((await E(purses.item).getCurrentAmount()).value.payload.length, 1);
assert.equal(
(await E(purses.item).getCurrentAmount()).value.payload.length,
1,
);

const vStorageItem = getFromVStorage('kread.item');
mustMatch(vStorageItem.item, want)
assert.equal(vStorageItem.id, 4)

}

export async function mintItemMultipleFlow(context) {
Expand All @@ -545,13 +573,16 @@ export async function mintItemMultipleFlow(context) {

await E(purses.item).deposit(payout);

const totalItems = (await E(purses.item)
.getCurrentAmount())
.value.payload.reduce((acc, [_item, supply]) => {
return acc + supply;
}, 0n);
const totalItems = (
await E(purses.item).getCurrentAmount()
).value.payload.reduce((acc, [_item, supply]) => {
return acc + supply;
}, 0n);
assert.equal(totalItems, 4n);
assert.equal((await E(purses.item).getCurrentAmount()).value.payload.length, 2);
assert.equal(
(await E(purses.item).getCurrentAmount()).value.payload.length,
2,
);
}

export async function mintItemMultipleDifferentFlow(context) {
Expand All @@ -577,5 +608,8 @@ export async function mintItemMultipleDifferentFlow(context) {
const payout = await E(userSeat).getPayout('Asset');

await E(purses.item).deposit(payout);
assert.equal((await E(purses.item).getCurrentAmount()).value.payload.length, 4);
assert.equal(
(await E(purses.item).getCurrentAmount()).value.payload.length,
4,
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ import {
testFunctionalityAfterUpgrade,
testFunctionalityBeforeUpgrade,
} from './bootstrap-upgrade-v2.js';
import { unmarshalFromVstorage } from '@agoric/internal/src/lib-chainStorage.js';

const trace = makeTracer('kreadBootUpgrade');

Expand Down Expand Up @@ -161,6 +162,14 @@ export const buildRootObject = async () => {
assertPathSegment(candidate);
return candidate;
};
/**
* Reads the data from the vstorage at the given path
* Will throw an error if path doesnt exist
* @param {string} path
*/
const getFromVStorage = (path) => {
return unmarshalFromVstorage(storageKit.data, path, marshaller.fromCapData.bind(marshaller))
}

const committeeName = 'KREAd Committee';

Expand Down Expand Up @@ -288,7 +297,7 @@ export const buildRootObject = async () => {
} = terms;

context = {
storageNode: storageKit.rootNode,
getFromVStorage,
contractAssets: {
character: { issuer: characterIssuer, brand: characterBrand },
item: { issuer: itemIssuer, brand: itemBrand },
Expand Down

0 comments on commit 2edfba0

Please sign in to comment.