Skip to content

Commit

Permalink
refactor!: use calldata lib sdk, use snapshotting in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thepiwo committed Feb 7, 2022
1 parent b500968 commit 0c6d010
Show file tree
Hide file tree
Showing 11 changed files with 147 additions and 88 deletions.
65 changes: 33 additions & 32 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
},
"license": "ISC",
"dependencies": {
"@aeternity/aepp-sdk": "^9.0.1",
"@aeternity/aepp-sdk": "github:aeternity/aepp-sdk-js.git#develop",
"axios": "^0.24.0",
"commander": "^8.3.0",
"promisify-child-process": "^4.1.1",
Expand Down
28 changes: 13 additions & 15 deletions src/env/env.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
const {spawn, exec} = require("promisify-child-process");
const { spawn, exec } = require('promisify-child-process');

const {print, printError} = require('../utils/utils');
const {nodeConfiguration, compilerConfiguration, proxyConfiguration} = require('../config/node-config.json');
const { print, printError } = require('../utils/utils');
const { nodeConfiguration, compilerConfiguration, proxyConfiguration } = require('../config/node-config.json');

async function isEnvRunning() {
const info = await getInfo()
const info = await getInfo();

if (info) {
const containers = [nodeConfiguration.containerName, compilerConfiguration.containerName, proxyConfiguration.containerName];
return containers.every(containerName => {
const line = info.split('\n').find(line => line.includes(containerName))
return line && line.includes('Up')
})
return containers.every((containerName) => {
const line = info.split('\n').find((line) => line.includes(containerName));
return line && line.includes('Up');
});
}

return false;
Expand Down Expand Up @@ -55,8 +55,8 @@ async function stopEnv(running) {
async function startEnv(nodeVersion, compilerVersion) {
print('===== starting env =====');

await exec(`NODE_TAG=${nodeVersion} COMPILER_TAG=${compilerVersion} docker-compose pull`)
await exec(`NODE_TAG=${nodeVersion} COMPILER_TAG=${compilerVersion} docker-compose up -d`)
await exec(`NODE_TAG=${nodeVersion} COMPILER_TAG=${compilerVersion} docker-compose pull`);
await exec(`NODE_TAG=${nodeVersion} COMPILER_TAG=${compilerVersion} docker-compose up -d`);

print('===== Env was successfully started! =====');
}
Expand All @@ -70,18 +70,16 @@ async function printInfo(running) {
print(await getInfo());
}


async function getInfo() {
const info = await exec(`docker-compose ps`);
const info = await exec('docker-compose ps');

if (info && info.stdout) {
return info.stdout;
}

return null
return null;
}

module.exports = {
run
run,
};

27 changes: 13 additions & 14 deletions src/init/artifacts/test/exampleTests.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
const {assert} = require('chai');
const {Universal, MemoryAccount, Node} = require('@aeternity/aepp-sdk');
const {wallets, networks, utils} = require('@aeternity/aeproject');
const { assert } = require('chai');
const { utils } = require('@aeternity/aeproject');

const EXAMPLE_CONTRACT_SOURCE = './contracts/ExampleContract.aes';

describe('ExampleContract', () => {
let client;
let contract;

before(async () => {
const client = await Universal.compose({
deepProps: {Ae: {defaults: {interval: 50}}}
})({
nodes: [{name: 'node', instance: await Node({url: networks.devmode.nodeUrl, ignoreVersion: true})}],
compilerUrl: networks.devmode.compilerUrl,
accounts: [MemoryAccount({keypair: wallets[0]})]
});
client = await utils.getClient();

// a filesystem object must be passed to the compiler if the contract uses custom includes
const filesystem = utils.getFilesystem(EXAMPLE_CONTRACT_SOURCE);
Expand All @@ -23,15 +17,20 @@ describe('ExampleContract', () => {
const contract_content = utils.getContractContent(EXAMPLE_CONTRACT_SOURCE);

// initialize the contract instance
contract = await client.getContractInstance(contract_content, {filesystem});
contract = await client.getContractInstance(contract_content, { filesystem });
await contract.deploy();

// create a snapshot of the blockchain state
await utils.createSnapshot(client);
});

it('deploy ExampleContract', async () => {
await contract.deploy();
// after each test roll back to initial state
afterEach(async () => {
await utils.rollbackSnapshot(client);
});

it('call ExampleContract', async () => {
const {decodedResult} = await contract.methods.example(42)
const { decodedResult } = await contract.methods.example(42);
assert.equal(decodedResult, 42);
});
});
12 changes: 6 additions & 6 deletions src/init/init.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const {exec} = require('promisify-child-process');
const { exec } = require('promisify-child-process');

const constants = require('./constants.json');
const {print} = require('../utils/utils');
const { print } = require('../utils/utils');

const {copyFolderRecursiveSync, fileExists} = require('../utils/fs-utils');
const { copyFolderRecursiveSync, fileExists } = require('../utils/fs-utils');

async function run(update) {
if (update) {
Expand Down Expand Up @@ -48,12 +48,12 @@ const setupArtifacts = async () => {
const updateArtifacts = async () => {
print('===== creating project file and directory structure =====');

let fileSource = `${__dirname}${constants.updateArtifactsDir}`;
let destination = constants.artifactsDest;
const fileSource = `${__dirname}${constants.updateArtifactsDir}`;
const destination = constants.artifactsDest;

await copyFolderRecursiveSync(fileSource, destination);
};

module.exports = {
run
run,
};
2 changes: 1 addition & 1 deletion src/init/update-artifacts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"test": "mocha ./test/**/*.js --timeout 0 --exit"
},
"dependencies": {
"@aeternity/aepp-sdk": "^9.0.1"
"@aeternity/aepp-sdk": "github:aeternity/aepp-sdk-js.git#develop"
},
"devDependencies": {
"@aeternity/aeproject": "^3.0.5",
Expand Down
8 changes: 4 additions & 4 deletions src/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const wallets = require('./wallets.json');
const utils = require('./utils');

module.exports = {
utils: utils,
networks: networks,
wallets: wallets
}
utils,
networks,
wallets,
};
65 changes: 65 additions & 0 deletions src/lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
const fs = require('fs');
const path = require('path');
const http = require('http');
const { exec } = require('promisify-child-process');
const { Universal, MemoryAccount, Node } = require('@aeternity/aepp-sdk');

const networks = require('./networks.json');
const wallets = require('./wallets.json');

const getContractContent = (contractSource) => fs.readFileSync(contractSource, 'utf8');

Expand Down Expand Up @@ -40,7 +46,66 @@ const getFilesystem = (contractSource) => {
return filesystem;
};

async function get(url) {
return new Promise((resolve, reject) => {
const req = http.request(url, { method: 'GET' }, (res) => {
if (res.statusCode < 200 || res.statusCode > 299) {
return reject(new Error(`HTTP status code ${res.statusCode}`));
}

const body = [];
res.on('data', (chunk) => body.push(chunk));
res.on('end', () => resolve(Buffer.concat(body).toString()));
});

req.on('error', (err) => reject(err));

req.on('timeout', () => {
req.destroy();
reject(new Error('Request time out'));
});

req.end();
});
}

const getClient = async () => Universal.compose({
deepProps: { Ae: { defaults: { interval: 50 } } },
})({
nodes: [{ name: 'node', instance: await Node({ url: networks.devmode.nodeUrl, ignoreVersion: true }) }],
compilerUrl: networks.devmode.compilerUrl,
accounts: [MemoryAccount({ keypair: wallets[0] })],
});

const awaitKeyBlocks = async (client, n = 1) => {
const height = await client.height();
await get(`http://localhost:3001/emit_kb?n=${n}`);
await client.awaitHeight(height + n);
};

let snapshotHeight = -1;

const createSnapshot = async (client) => {
snapshotHeight = await client.height();
await awaitKeyBlocks(client, 1);
};

const rollbackSnapshot = async (client) => {
const currentBlockHeight = await client.height();
if (currentBlockHeight > snapshotHeight) {
// TODO replace with http api call
const cmd = `docker exec aeproject_node bin/aeternity db_rollback --height ${snapshotHeight}`;
await exec(cmd);
await awaitKeyBlocks(client, 1);
} else {
}
};

module.exports = {
getContractContent,
getFilesystem,
awaitKeyBlocks,
createSnapshot,
rollbackSnapshot,
getClient,
};
6 changes: 3 additions & 3 deletions src/test/test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const {print} = require("../utils/utils");
const {exec} = require("promisify-child-process");
const { exec } = require('promisify-child-process');
const { print } = require('../utils/utils');

const run = async () => {
const workingDirectory = process.cwd();
Expand All @@ -10,7 +10,7 @@ const run = async () => {
async function test() {
print('===== Starting Tests =====');

const child = exec(`npm test`)
const child = exec('npm test');

child.stdout.on('data', (out) => process.stdout.write(out));
child.stderr.on('data', (err) => process.stderr.write(err));
Expand Down
Loading

0 comments on commit 0c6d010

Please sign in to comment.