Skip to content

Commit

Permalink
chore: fixes on docs and small interface changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ohager committed Feb 26, 2024
1 parent 8c03cb6 commit 42c54f6
Showing 1 changed file with 36 additions and 26 deletions.
62 changes: 36 additions & 26 deletions src/simulator-testbed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type Contract = CONTRACT;
*
* const bc = Testbed.Node.Blockchain;
* const tx = bc.transactions;
* const maps = testbed.getContractMaps().filter(({k1, k2, value}) => ...)
* const map = testbed.getContractMap().filter(({k1, k2, value}) => ...)
* ```
*
* __INITIALIZE CONTRACT__
Expand Down Expand Up @@ -86,12 +86,17 @@ type Contract = CONTRACT;
* expected result sets. This class is meant to be used with Test Runners like [Vitest](https://vitest.dev/) or [Jest](https://jestjs.io/).
*/
export class SimulatorTestbed {
Node: SimNode;
private node: SimNode;

/**
* Constructs a simulator testbed instance with or without given scenario
* @param scenario The initial scenario to be used.
*/
constructor(scenario?: TransactionObj[]) {
this.Node = new SimNode();
this.node = new SimNode();
if (scenario) {
const status = this.Node.setScenario(
this.toSimulatorTransactions(scenario),
const status = this.node.setScenario(
SimulatorTestbed.toSimulatorTransactions(scenario),
);
if (status.errorCode) {
throw new Error(
Expand All @@ -101,7 +106,12 @@ export class SimulatorTestbed {
}
}

private toSimulatorTransactions(scenario: TransactionObj[]): string {
/**
* Converts a scenario into a Simulator UI compatible string
* @param scenario Scenario Object
* @return string
*/
public static toSimulatorTransactions(scenario: TransactionObj[]): string {
return JSON.stringify(
scenario,
(key, value) => (typeof value === "bigint" ? value.toString() : value), // return everything else unchanged
Expand All @@ -123,7 +133,7 @@ export class SimulatorTestbed {
if (initializers) {
code = this.injectInitializerCode(code, initializers);
}
this.Node.loadSmartContract(code, 555n);
this.node.loadSmartContract(code, 555n);
return this;
}

Expand Down Expand Up @@ -151,7 +161,7 @@ ${code}`;
* @param {bigint} address - The contract address to select. Throws error if contract is not found.
*/
selectContract(address: bigint) {
if (!this.Node.Simulator.setCurrentSlotContract(address)) {
if (!this.node.Simulator.setCurrentSlotContract(address)) {
throw new Error("Invalid contract address.");
}
return this;
Expand All @@ -161,17 +171,17 @@ ${code}`;
* Retrieves the maps per contract.
*
* @param {bigint} address - The contract address (default: the last deployed).
* @return {any} The maps per slot.
* @return {any} The maps per contract.
*/
getContractMap(address?: bigint): MapObj[] {
if (!address) {
address = this.Node.Simulator.CurrentContract?.contract;
address = this.node.Simulator.CurrentContract?.contract;
}
if (!address) {
throw new Error("Contract not specified");
}
const BlockchainMap = this.Node.Blockchain.maps.find(
(M) => M.id === address,
const BlockchainMap = this.node.Blockchain.maps.find(
(map) => map.id === address,
);
return BlockchainMap?.map ?? [];
}
Expand Down Expand Up @@ -217,7 +227,7 @@ ${code}`;
*/
getAccount(accountId: bigint): AccountObj | null {
return (
this.Node.Blockchain.accounts.find((a) => a.id === accountId) || null
this.node.Blockchain.accounts.find((a) => a.id === accountId) || null
);
}

Expand All @@ -227,7 +237,7 @@ ${code}`;
* @return {BlockchainTransactionObj[]} - An array of BlockchainTransactionObj objects.
*/
getTransactions(): BlockchainTransactionObj[] {
return this.Node.Blockchain.transactions;
return this.node.Blockchain.transactions;
}

/**
Expand All @@ -237,19 +247,19 @@ ${code}`;
* @return {this} - Returns the current instance of the class.
*/
runScenario(scenario: TransactionObj[] = []) {
const scenarioStr = this.toSimulatorTransactions(scenario);
const status = this.Node.appendScenario(scenarioStr);
const scenarioStr = SimulatorTestbed.toSimulatorTransactions(scenario);
const status = this.node.appendScenario(scenarioStr);
if (status.errorCode) {
throw new Error(
"Appending transactions returned error: " + status.errorDescription,
);
}
const lastScenarioBlock = this.Node.scenarioTransactions.reduce(
const lastScenarioBlock = this.node.scenarioTransactions.reduce(
(p, c) => Math.max(c.blockheight ?? 0, p),
0,
);
this.Node.forgeUntilBlock(lastScenarioBlock + 2);
console.debug(`Blocks forged until height ${this.Node.forgeBlock()}.`);
this.node.forgeUntilBlock(lastScenarioBlock + 2);
console.debug(`Blocks forged until height ${this.node.forgeBlock()}.`);
return this;
}

Expand All @@ -262,8 +272,8 @@ ${code}`;
*/
getContract(address?: bigint): Contract {
const contract = !address
? this.Node.Simulator.getCurrentSlotContract()
: this.Node.Blockchain.Contracts.find((sc) => sc.contract === address);
? this.node.Simulator.getCurrentSlotContract()
: this.node.Blockchain.Contracts.find((sc) => sc.contract === address);
if (!contract) {
throw new Error("Invalid contract address");
}
Expand Down Expand Up @@ -307,7 +317,7 @@ ${code}`;
address?: bigint,
): BlockchainTransactionObj[] {
const contract = this.getContract(address);
return this.Node.Blockchain.transactions.filter(
return this.node.Blockchain.transactions.filter(
(tx) => tx.blockheight === blockheight && tx.sender === contract.contract,
);
}
Expand All @@ -328,18 +338,18 @@ ${code}`;
): BlockchainTransactionObj[] {
const contract = this.getContract(address);
transactions.forEach((tx) => {
tx.blockheight = this.Node.Blockchain.getCurrentBlock();
tx.blockheight = this.node.Blockchain.getCurrentBlock();
tx.recipient = contract.contract;
});
const status = this.Node.appendScenario(
this.toSimulatorTransactions(transactions),
const status = this.node.appendScenario(
SimulatorTestbed.toSimulatorTransactions(transactions),
);
if (status.errorCode) {
throw new Error(
"Appending transactions returned error: " + status.errorDescription,
);
}
const height = this.Node.forgeBlocks(2);
const height = this.node.forgeBlocks(2);
return this.getTransactionsSentByContract(height - 1);
}
}

0 comments on commit 42c54f6

Please sign in to comment.