Skip to content

Commit

Permalink
Merge pull request #28 from subslice/chore/update-devphase
Browse files Browse the repository at this point in the history
Chore: update devphase
  • Loading branch information
h4x3rotab committed Jun 14, 2023
2 parents 7dc2f9b + 8756b50 commit 6d76832
Show file tree
Hide file tree
Showing 7 changed files with 294 additions and 96 deletions.
104 changes: 64 additions & 40 deletions phat/devphase.config.ts
Original file line number Diff line number Diff line change
@@ -1,96 +1,120 @@
import { ProjectConfigOptions } from 'devphase';
import { ProjectConfigOptions, StackSetupMode } from '@devphase/service';

const config : ProjectConfigOptions = {
/*
* Project directories
*/
directories: {
artifacts: 'artifacts',
contracts: 'contracts',
logs: 'logs',
scripts: 'scripts',
stacks: 'stacks',
tests: 'tests',
typings: 'typings'
},
/*
* Stack configuration
* {
* [componentName : string]: {
* binary: string, // path to binary
* workingDir: string, // working directory as above
* evns: {
* [name: string]: string,
* },
* args: {
* [name: string]: string,
* },
* timeout: number // start up timeout
* }
* }
*/
stack: {
blockTime: 500,
version: 'nightly-2023-02-20',
version: 'latest', // version which you want to pull from official repository (tag name) or "latest" one
blockTime: 6000, // default block time for direct stack running (may be overriden in testing mode)
setupOptions: {
mode: StackSetupMode.None,
workerUrl: 'http://localhost:{{stack.pruntime.port}}'
},
node: {
port: 39944,
port: 9944, // ws port
binary: '{{directories.stacks}}/{{stack.version}}/phala-node',
workingDir: '{{directories.stacks}}/.data/node',
dataDir: '{{directories.stacks}}/.data/node',
envs: {},
args: {
'--dev': true,
'--port': 33333,
'--ws-port': '{{stack.node.port}}',
'--rpc-port': 39933,
'--ws-external': true,
'--unsafe-ws-external': true,
'--rpc-methods': 'Unsafe',
'--block-millisecs': '{{stack.blockTime}}',
'--ws-port': '{{stack.node.port}}',
'--block-millisecs': '{{stack.blockTime}}', // override at runtime
},
timeout: 10000,
},
pruntime: {
port: 38000, // server port
port: 8000, // server port
binary: '{{directories.stacks}}/{{stack.version}}/pruntime',
workingDir: '{{directories.stacks}}/.data/pruntime',
envs: {
'RUST_LOG': 'debug,runtime=trace'
},
dataDir: '{{directories.stacks}}/.data/pruntime',
envs: {},
args: {
'--allow-cors': true,
'--cores': 0,
'--address': '0.0.0.0',
'--port': '{{stack.pruntime.port}}',
'--port': '{{stack.pruntime.port}}'
},
timeout: 2000,
},
pherry: {
gkMnemonic: '//Ferdie', // super user mnemonic
gkMnemonic: '//Alice', // gate keeper mnemonic
binary: '{{directories.stacks}}/{{stack.version}}/pherry',
workingDir: '{{directories.stacks}}/.data/pherry',
dataDir: '{{directories.stacks}}/.data/pherry',
envs: {},
args: {
'--no-wait': true,
'--mnemonic': '{{stack.pherry.gkMnemonic}}',
'--inject-key': '0000000000000000000000000000000000000000000000000000000000000001',
'--substrate-ws-endpoint': 'ws://localhost:{{stack.node.port}}',
'--pruntime-endpoint': 'http://localhost:{{stack.pruntime.port}}',
'--dev-wait-block-ms': '{{stack.blockTime}}',
'--attestation-provider': 'none',
'--dev-wait-block-ms': '{{stack.blockTime}}', // override at runtime
},
timeout: 5000,
timeout: 2000,
}
},
/**
* Networks configuration
* Default network is local and it can be changed using CLI argument
*/
networks: {
local: {
nodeUrl: 'ws://localhost:{{stack.node.port}}',
workerUrl: 'http://localhost:{{stack.pruntime.port}}',
blockTime: 500,
}
},
/**
* Testing configuration
*/
testing: {
testing: {
mocha: {}, // custom mocha configuration
spawnStack: true, // spawn runtime stack? or assume there is running one
stackLogOutput: true, // if specifed pipes output of all stack component to file (by default it is ignored)
envSetup: { // environment setup
blockTime: 100, // block time override for spawning local testnet
stackSetupConfig: { // environment setup
setup: {
// custom setup procedure callback; (devPhase) => Promise<void>
custom: undefined,
custom: undefined, // custom setup procedure callback; (devPhase) => Promise<void>
timeout: 120 * 1000,
},
teardown: {
// custom teardown procedure callback ; (devPhase) => Promise<void>
custom: undefined,
custom: undefined, // custom teardown procedure callback ; (devPhase) => Promise<void>
timeout: 10 * 1000,
}
},
stackLogOutput: false, // display stack output in console
},
/**
* Networks configuration
* Default network is local and it can be changed using CLI argument
*/
networks: {
local: {
nodeUrl: 'ws://localhost:{{stack.node.port}}',
nodeApiOptions: {},
workerUrl: 'http://localhost:{{stack.pruntime.port}}',
defaultClusterId: '0x0000000000000000000000000000000000000000000000000000000000000000', // set default cluster ID for further actions
blockTime: 6000, // network block time (may be overriden in testing mode)
},
poc5: {
nodeUrl: 'wss://poc5.phala.network/ws',
workerUrl: 'https://poc5.phala.network/tee-api-1',
defaultClusterId: '0x0000000000000000000000000000000000000000000000000000000000000001',
}
},
/**
* Accounts fallback configuration
Expand All @@ -109,4 +133,4 @@ const config : ProjectConfigOptions = {
}
};

export default config;
export default config;
3 changes: 2 additions & 1 deletion phat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"main": "index.js",
"license": "MIT",
"dependencies": {
"devphase": "^0.2.2",
"@devphase/cli": "^0.2.1",
"@devphase/service": "^0.2.1",
"dotenv": "^16.0.3"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion phat/tests/evm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { SampleOracle } from '@/typings/SampleOracle';
import { EvmTransactor } from '@/typings/EvmTransactor';
import * as PhalaSdk from '@phala/sdk';
import type { KeyringPair } from '@polkadot/keyring/types';
import { Contract, ContractType } from 'devphase';
import { Contract, ContractType } from '@devphase/service';

import 'dotenv/config';
import { LocalScheduler } from '@/typings/LocalScheduler';
Expand Down
48 changes: 34 additions & 14 deletions phat/tests/substrate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Sub0Factory } from '@/typings/Sub0Factory'
import * as PhalaSdk from '@phala/sdk';
import { ApiPromise } from '@polkadot/api';
import type { KeyringPair } from '@polkadot/keyring/types';
import { ContractType } from 'devphase';
import { ContractType } from '@devphase/service';

import 'dotenv/config';
import { LocalScheduler } from '@/typings/LocalScheduler';
Expand All @@ -13,8 +13,10 @@ async function delay(ms: number): Promise<void> {
}

describe('Substrate Offchain Rollup', () => {
const httpRpc: string = "http://127.0.0.1:39933";
const httpRpc: string = "http://localhost:39933";
const secretBob: string = "0x398f0c28f98885e046333d4a41c19cee4c37368a9832c6502f6cfd182e2aef89";
const defaultDelay: number = 10_000;
const defaultTimeout: number = 120_000;

let priceFeedFactory: SubPriceFeed.Factory;
let priceFeed: SubPriceFeed.Contract;
Expand All @@ -31,6 +33,12 @@ describe('Substrate Offchain Rollup', () => {
priceFeedFactory = await this.devPhase.getFactory('sub_price_feed');
sub0Factory = await this.devPhase.getFactory('sub0_factory');
priceFeedCodeHash = priceFeedFactory.metadata.source.hash;

// Set contract type which is required for deploying.
// The current devPhase generated factory does not have the contract type set.
priceFeedFactory.contractType = ContractType.InkCode;
sub0Factory.contractType = ContractType.InkCode;

await priceFeedFactory.deploy();
await sub0Factory.deploy();
expect(priceFeedCodeHash.startsWith('0x')).to.be.true;
Expand All @@ -47,41 +55,46 @@ describe('Substrate Offchain Rollup', () => {

describe('SubPriceFeed', () => {
before(async function() {
this.timeout(30_000);
this.timeout(defaultTimeout);

// Deploy contract
priceFeed = await priceFeedFactory.instantiate('default', [], {transferToCluster: 1e12});
console.log('SubPriceFeed deployed at', priceFeed.address.toString());
});

it('should has correct owners', async function() {
it('should have correct owners', async function() {
const feedOwner = await priceFeed.query.owner(certAlice, {});
expect(feedOwner.result.isOk).to.be.true;
expect(feedOwner.output.asOk.toString()).to.be.equal(alice.address.toString());
});

it('should be configurable', async function() {
it('can be configured', async function() {
this.timeout(defaultTimeout);

// Config the oracle
const feedConfig = await priceFeed.tx
.config(txConf, httpRpc, 100, secretBob as any, 'polkadot', 'usd')
.signAndSend(alice, {nonce: -1});
console.log('Feed configured', feedConfig.toHuman());
await delay(4*1000);
await delay(defaultDelay);

// Init the rollup on the blockchain
const init = await priceFeed.query.maybeInitRollup(certAlice, {});
console.log('Result: ', init.result.toHuman())
console.log('Output: ', init.output.toHuman())
expect(init.result.isOk).to.be.true;
expect(init.output.isOk).to.be.true;
expect(init.output.asOk.isOk).to.be.true;
});

it('can submit tx', async function() {
this.timeout(1000*30_000);
this.timeout(defaultTimeout);

const feed = await priceFeed.query.feedPrice(certAlice, {});
expect(feed.result.isOk).to.be.true;
expect(feed.output.isOk).to.be.true;
expect(feed.output.asOk.isOk).to.be.true;
await delay(3*1000);
await delay(defaultDelay);

// The response should be received on the blockchain
const receivedPrice = await api.query.phatOracle.priceFeeds.entries(alice.address);
Expand All @@ -91,19 +104,22 @@ describe('Substrate Offchain Rollup', () => {

describe('Sub0Factory', () => {
before(async function() {
this.timeout(30_000);
this.timeout(defaultTimeout);

// Deploy contract
sub0 = await sub0Factory.instantiate('default', [], {transferToCluster: 1e12});
console.log('Sub0Factory deployed at', sub0.address.toString());
});

it('should has correct owners', async function() {
it('should have correct owners', async function() {
const sub0Owner = await sub0.query.owner(certAlice, {});
expect(sub0Owner.result.isOk).to.be.true;
expect(sub0Owner.output.asOk.toString()).to.be.equal(alice.address.toString());
});

it('can be configured', async function() {
this.timeout(defaultTimeout);

// Config the oracle
const sub0Config = await sub0.tx
.config(txConf, httpRpc, 100, secretBob as any, priceFeedCodeHash)
Expand All @@ -119,13 +135,15 @@ describe('Substrate Offchain Rollup', () => {

let priceFeed1: SubPriceFeed.Contract;
it('can deploy price feeds', async function() {
this.timeout(defaultTimeout);

let deploy = await api.tx.utility.batchAll([
sub0.tx.deployPriceFeed(txConf, 'feed1', 'polkadot', 'usd'),
sub0.tx.deployPriceFeed(txConf, 'feed2', 'bitcoin', 'usd'),
]).signAndSend(alice, {nonce: -1});

console.log('PriceFeed1&2 deployed', deploy.toHuman());
await delay(5*1000);
await delay(defaultDelay);

let deployments = await sub0.query.getDeployments(certAlice, {});
expect(deployments.result.isOk).to.be.true;
Expand All @@ -140,24 +158,26 @@ describe('Substrate Offchain Rollup', () => {
});

it('can trigger a rollup', async function() {
this.timeout(defaultTimeout);

// Init the rollup on the blockchain
const init = await priceFeed1.query.maybeInitRollup(certAlice, {});
expect(init.result.isOk).to.be.true;
expect(init.output.isOk).to.be.true;
expect(init.output.asOk.isOk).to.be.true;
await delay(3*1000);
await delay(defaultDelay);

// Trigger a rollup
const feed = await priceFeed1.query.feedPrice(certAlice, {});
expect(feed.result.isOk).to.be.true;
expect(feed.output.isOk).to.be.true;
expect(feed.output.asOk.isOk).to.be.true;
await delay(3*1000);
await delay(defaultDelay);

// The response should be received on the blockchain
const receivedPrice = await api.query.phatOracle.priceFeeds.entries(alice.address);
expect(receivedPrice.length).to.be.equal(2); // 2 in totoal: 1 existing & 1 more
}).timeout(30000);
});

});

Expand Down
Loading

0 comments on commit 6d76832

Please sign in to comment.