chore: sorting transactions test

This commit is contained in:
ppedziwiatr
2021-12-28 11:39:45 +01:00
parent 9daa9f1d4c
commit b1a060a46b
3 changed files with 106 additions and 7 deletions

View File

@@ -1,6 +1,6 @@
{ {
"name": "redstone-smartweave", "name": "redstone-smartweave",
"version": "0.4.8", "version": "0.4.9",
"description": "An implementation of the SmartWeave smart contract protocol.", "description": "An implementation of the SmartWeave smart contract protocol.",
"main": "./lib/cjs/index.js", "main": "./lib/cjs/index.js",
"module": "./lib/esm/index.js", "module": "./lib/esm/index.js",

View File

@@ -22,32 +22,36 @@ async function main() {
logging: false // Enable network request logging logging: false // Enable network request logging
}); });
const contractTxId = '-8A6RexFkpfWwuyVO98wzSFZh0d6VJuI-buTJvlwOJQ'; const contractTxId = 'Daj-MNSnH55TDfxqC7v4eq0lKzVIwh98srUaWqyuZtY';
//const interactionsLoader = new FromFileInteractionsLoader(path.join(__dirname, 'data', 'interactions.json')); //const interactionsLoader = new FromFileInteractionsLoader(path.join(__dirname, 'data', 'interactions.json'));
// const smartweave = SmartWeaveWebFactory.memCachedBased(arweave).setInteractionsLoader(interactionsLoader).build(); // const smartweave = SmartWeaveWebFactory.memCachedBased(arweave).setInteractionsLoader(interactionsLoader).build();
const smartweave = SmartWeaveWebFactory const smartweaveR = SmartWeaveWebFactory
.memCachedBased(arweave, 1) .memCachedBased(arweave, 1)
.setInteractionsLoader(new RedstoneGatewayInteractionsLoader( .setInteractionsLoader(new RedstoneGatewayInteractionsLoader(
'https://gateway.redstone.finance') 'https://gateway.redstone.finance')
).build(); ).build();
const usedBefore = Math.round((process.memoryUsage().heapUsed / 1024 / 1024) * 100) / 100 const usedBefore = Math.round((process.memoryUsage().heapUsed / 1024 / 1024) * 100) / 100
const lootContract = smartweave.contract(contractTxId) const contractR = smartweaveR.contract(contractTxId);
.setEvaluationOptions({updateCacheForEachInteraction: true}); const {state, validity} = await contractR.readState();
const {state, validity} = await lootContract.readState();
const usedAfter = Math.round((process.memoryUsage().heapUsed / 1024 / 1024) * 100) / 100 const usedAfter = Math.round((process.memoryUsage().heapUsed / 1024 / 1024) * 100) / 100
logger.warn("Heap used in MB", { logger.warn("Heap used in MB", {
usedBefore, usedBefore,
usedAfter usedAfter
}); });
const smartweave = SmartWeaveWebFactory.memCached(arweave, 1);
const contract = smartweave.contract(contractTxId);
const result = await contract.readState();
//fs.writeFileSync(path.join(__dirname, 'data', 'validity.json'), JSON.stringify(validity)); //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', 'validity_old.json'), JSON.stringify(result.validity));
fs.writeFileSync(path.join(__dirname, 'data', 'state.json'), JSON.stringify(state)); fs.writeFileSync(path.join(__dirname, 'data', 'state_redstone.json'), JSON.stringify(state));
fs.writeFileSync(path.join(__dirname, 'data', 'state_arweave.json'), JSON.stringify(result.state));
// console.log('second read'); // console.log('second read');
// await lootContract.readState(); // await lootContract.readState();

View File

@@ -0,0 +1,95 @@
/* eslint-disable */
import Arweave from 'arweave';
import {
ArweaveGatewayInteractionsLoader,
BlockHeightInteractionsSorter,
Contract, DefaultEvaluationOptions, LexicographicalInteractionsSorter,
LoggerFactory,
SmartWeave,
SmartWeaveNodeFactory
} from '../src';
import {TsLogFactory} from '../src/logging/node/TsLogFactory';
import fs from 'fs';
import path from 'path';
import ArLocal from 'arlocal';
import {JWKInterface} from 'arweave/node/lib/wallet';
async function main() {
let contractSrc: string;
let wallet: JWKInterface;
let smartweave: SmartWeave;
LoggerFactory.use(new TsLogFactory());
LoggerFactory.INST.logLevel('error');
LoggerFactory.INST.logLevel('info', 'sorting');
const logger = LoggerFactory.INST.create('sorting');
const arlocal = new ArLocal(1985, false);
await arlocal.start();
const arweave = Arweave.init({
host: 'localhost',
port: 1985,
protocol: 'http'
});
try {
smartweave = SmartWeaveNodeFactory.memCached(arweave);
wallet = await arweave.wallets.generate();
const walletAddress = await arweave.wallets.getAddress(wallet);
await arweave.api.get(`/mint/${walletAddress}/1000000000000000`);
contractSrc = fs.readFileSync(
path.join(__dirname, '../src/__tests__/integration/', 'data/writing-contract.js'),
'utf8'
);
const contractTxId = await smartweave.createContract.deploy({
wallet,
initState: JSON.stringify({ticker: 'WRITING_CONTRACT'}),
src: contractSrc
});
const contract = smartweave.contract(contractTxId).connect(wallet).setEvaluationOptions({
ignoreExceptions: false,
internalWrites: true
});
await mine();
await contract.writeInteraction({function: 'writeContract', contractId: contractTxId, amount: 10});
await contract.writeInteraction({function: 'writeContract', contractId: contractTxId, amount: 10});
await contract.writeInteraction({function: 'writeContract', contractId: contractTxId, amount: 10});
await contract.writeInteraction({function: 'writeContract', contractId: contractTxId, amount: 10});
await contract.writeInteraction({function: 'writeContract', contractId: contractTxId, amount: 10});
await contract.writeInteraction({function: 'writeContract', contractId: contractTxId, amount: 10});
await contract.writeInteraction({function: 'writeContract', contractId: contractTxId, amount: 10});
await mine();
const interactionsLoader = new ArweaveGatewayInteractionsLoader(arweave);
const lexSorting = new LexicographicalInteractionsSorter(arweave);
const interactions = await interactionsLoader.load(contractTxId, 0, 100, new DefaultEvaluationOptions());
const sorted = await lexSorting.sort([...interactions]);
logger.info("\n\nLexicographical");
sorted.forEach(v => {
logger.info(`${v.node.id}: sortKey [${v.sortKey}]`);
});
const blockHeightSorting = new BlockHeightInteractionsSorter();
const interactions2 = await interactionsLoader.load(contractTxId, 0, 100, new DefaultEvaluationOptions());
const sorted2 = await blockHeightSorting.sort([...interactions2]);
logger.info("\n\nBlock height");
sorted2.forEach(v => {
logger.info(`${v.node.id}: sortKey [${v.sortKey}]`);
});
} finally {
await arlocal.stop();
}
async function mine() {
await arweave.api.get('mine');
}
}
main().catch((e) => console.error(e));