Skip to content

Commit

Permalink
test: stable stringify in tests, new regression testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
ppedziwiatr committed Jan 3, 2022
1 parent 936211d commit a33210b
Show file tree
Hide file tree
Showing 8 changed files with 11,048 additions and 1,016 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
"ts-node": "^10.2.1",
"tsc-alias": "1.3.10",
"tsconfig-paths": "^3.10.1",
"typescript": "4.5.2"
"typescript": "4.5.2",
"safe-stable-stringify": "2.3.1"
}
}
10 changes: 6 additions & 4 deletions src/__tests__/regression/read-state.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { interactRead, readContract } from 'smartweave';
import Arweave from 'arweave';
import { LoggerFactory, SmartWeaveNodeFactory } from '@smartweave';

const stringify = require('safe-stable-stringify')

function* chunks(arr, n) {
for (let i = 0; i < arr.length; i += n) {
// note: wrapping with an array to make it compatible with describe.each
Expand Down Expand Up @@ -34,10 +36,10 @@ describe.each(chunked)('.suite %#', (contracts: string[]) => {
async (contractTxId: string) => {
console.log('readContract', contractTxId);
const result = await readContract(arweave, contractTxId);
const resultString = JSON.stringify(result).trim();
const resultString = stringify(result).trim();
console.log('readState', contractTxId);
const result2 = await SmartWeaveNodeFactory.memCached(arweave, 1).contract(contractTxId).readState();
const result2String = JSON.stringify(result2.state).trim();
const result2String = stringify(result2.state).trim();
expect(result2String).toEqual(resultString);
},
600000
Expand All @@ -49,13 +51,13 @@ describe('readState', () => {
const contractTxId = 'CbGCxBJn6jLeezqDl1w3o8oCSeRCb-MmtZNKPodla-0';
const blockHeight = 707892;
const result = await readContract(arweave, contractTxId, blockHeight);
const resultString = JSON.stringify(result).trim();
const resultString = stringify(result).trim();

const result2 = await SmartWeaveNodeFactory.memCached(arweave, 1)
.contract(contractTxId)
.setEvaluationOptions({ updateCacheForEachInteraction: false })
.readState(blockHeight);
const result2String = JSON.stringify(result2.state).trim();
const result2String = stringify(result2.state).trim();

expect(result2String).toEqual(resultString);
});
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/regression/test-cases/read-state.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[
"Daj-MNSnH55TDfxqC7v4eq0lKzVIwh98srUaWqyuZtY",
"t9T7DIOGxx4VWXoCEeYYarFYeERTpWIC1V3y-BPZgKE",
"5pSyVjFI07z8mbLeQhYBMsQ4M_MPidXIGX6T77rnF2s",
"-8A6RexFkpfWwuyVO98wzSFZh0d6VJuI-buTJvlwOJQ",
"AVTqjPQGCCXim7Nl_gn3HMjE4k0Zi_eTFRJCNEVXZxw",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { ContractDefinitionLoader } from './ContractDefinitionLoader';
* in {@link ContractDefinitionLoader} - i.e. loads the definition from Arweave gateway.
*/
export class RedstoneGatewayContractDefinitionLoader extends ContractDefinitionLoader {
private readonly rLogger = LoggerFactory.INST.create('ContractDefinitionLoader');
private readonly rLogger = LoggerFactory.INST.create('RedstoneGatewayContractDefinitionLoader');

constructor(
private readonly baseUrl: string,
Expand Down
29 changes: 23 additions & 6 deletions tools/contract.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
/* eslint-disable */
import Arweave from 'arweave';
import {LoggerFactory, RedstoneGatewayInteractionsLoader, SmartWeaveWebFactory} from '../src';
import {
LoggerFactory, MemCache, RedstoneGatewayContractDefinitionLoader,
RedstoneGatewayInteractionsLoader,
SmartWeaveWebFactory
} from '../src';

import {TsLogFactory} from '../src/logging/node/TsLogFactory';
import fs from 'fs';
import path from 'path';
import {FromFileInteractionsLoader} from './FromFileInteractionsLoader';
import {SmartWeaveNodeFactory} from '../src/core/node/SmartWeaveNodeFactory';
import {readContract} from "smartweave";

const stringify = require('safe-stable-stringify')


const logger = LoggerFactory.INST.create('Contract');

// LoggerFactory.use(new TsLogFactory());
//LoggerFactory.use(new TsLogFactory());
LoggerFactory.INST.logLevel("error");
LoggerFactory.INST.logLevel("debug", "ArweaveGatewayInteractionsLoader");
LoggerFactory.INST.logLevel("debug", "HandlerBasedContract");
Expand All @@ -27,14 +36,19 @@ async function main() {
logging: false // Enable network request logging
});

const contractTxId = 'Daj-MNSnH55TDfxqC7v4eq0lKzVIwh98srUaWqyuZtY';
//const contractTxId = 't9T7DIOGxx4VWXoCEeYYarFYeERTpWIC1V3y-BPZgKE';
//const contractTxId = 'Daj-MNSnH55TDfxqC7v4eq0lKzVIwh98srUaWqyuZtY';
const contractTxId = 't9T7DIOGxx4VWXoCEeYYarFYeERTpWIC1V3y-BPZgKE'; //749180

//const interactionsLoader = new FromFileInteractionsLoader(path.join(__dirname, 'data', 'interactions.json'));

// const smartweave = SmartWeaveWebFactory.memCachedBased(arweave).setInteractionsLoader(interactionsLoader).build();
const smartweaveR = SmartWeaveWebFactory
.memCachedBased(arweave, 1).build();
.memCachedBased(arweave, 1)
.setInteractionsLoader(
new RedstoneGatewayInteractionsLoader("https://gateway.redstone.finance"))
.setDefinitionLoader(
new RedstoneGatewayContractDefinitionLoader("http://localhost:5666", arweave, new MemCache()))
.build();

/* const usedBefore = Math.round((process.memoryUsage().heapUsed / 1024 / 1024) * 100) / 100
const contractR = smartweaveR.contract(contractTxId);
Expand All @@ -51,11 +65,14 @@ async function main() {
});
const result = await contract.readState();

const result2 = await readContract(arweave, "t9T7DIOGxx4VWXoCEeYYarFYeERTpWIC1V3y-BPZgKE")


//fs.writeFileSync(path.join(__dirname, 'data', 'validity.json'), JSON.stringify(validity));

//fs.writeFileSync(path.join(__dirname, 'data', 'validity_old.json'), JSON.stringify(result.validity));
//fs.writeFileSync(path.join(__dirname, 'data', 'state_redstone.json'), JSON.stringify(state));
fs.writeFileSync(path.join(__dirname, 'data', 'state_new.json'), stringify(result.state).trim());
fs.writeFileSync(path.join(__dirname, 'data', 'state_old.json'), stringify(result2).trim());
//fs.writeFileSync(path.join(__dirname, 'data', 'state_arweave.json'), JSON.stringify(result.state));

// console.log('second read');
Expand Down
5,507 changes: 5,506 additions & 1 deletion tools/data/state_new.json

Large diffs are not rendered by default.

Loading

0 comments on commit a33210b

Please sign in to comment.