Skip to content

Commit

Permalink
Update Moonbase runtime 2403
Browse files Browse the repository at this point in the history
  • Loading branch information
imstar15 committed Aug 5, 2023
1 parent 3d527bb commit f7e2664
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 39 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ MessageHash: 0xff304ed6aeab3e174ec667e9f69a18ebe23506836c4f53bd35aeb78503193453
| :--- | :----: |
| Polkadot | [v0.9.38](https://github.com/paritytech/polkadot/releases/tag/v0.9.38) |
| OAK-blockchain | [v2.0.0](https://github.com/OAK-Foundation/OAK-blockchain/releases/tag/v2.0.0) |
| Moonbeam | [runtime-2302](https://github.com/PureStake/moonbeam/releases/tag/runtime-2302) |
| Moonbeam | [runtime-2403](https://github.com/moonbeam-foundation/moonbeam/releases/tag/runtime-2403) |

### Steps & Logs

Expand Down
26 changes: 17 additions & 9 deletions src/common/moonbaseHelper.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import _ from 'lodash';
import { decodeAddress, blake2AsU8a } from '@polkadot/util-crypto';
import { TypeRegistry } from '@polkadot/types';
import { hexToU8a, u8aToHex } from '@polkadot/util';
import { ApiPromise, WsProvider } from '@polkadot/api';
import Keyring from '@polkadot/keyring';
import { BN } from 'bn.js';

import {
WEIGHT_REF_TIME_PER_SECOND, calculateXcmOverallWeight, getProxies, getProxyAccount,
WEIGHT_REF_TIME_PER_SECOND, calculateXcmOverallWeight, getProxies,
} from './utils';

// frame_support::weights::constants::WEIGHT_PER_SECOND
// https://github.com/paritytech/substrate/blob/2dff067e9f7f6f3cc4dbfdaaa97753eccc407689/frame/support/src/weights.rs#L39
// https://github.com/paritytech/substrate/blob/2dff067e9f7f6f3cc4dbfdaaa97753eccc407689/primitives/weights/src/lib.rs#L48
const WEIGHT_PER_SECOND = 1000000000000;

class MoonbaseHelper {
constructor(config) {
this.config = config;
Expand All @@ -27,9 +25,19 @@ class MoonbaseHelper {
this.keyring = new Keyring({ type: 'sr25519', ss58Format: this.config.ss58 });
};

getProxyAccount = (address, paraId, options) => {
const { accountKey20 } = getProxyAccount(this.api, paraId, address, options);
return accountKey20;
getProxyAccount = (address, paraId, { accountType = 'AccountId32' } = {}) => {
const decodedAddress = accountType === 'AccountKey20' ? hexToU8a(address) : decodeAddress(address);

// Calculate Hash Component
const registry = new TypeRegistry();
const toHash = new Uint8Array([
...new TextEncoder().encode('SiblingChain'),
...registry.createType('Compact<u32>', paraId).toU8a(),
...registry.createType('Compact<u32>', accountType.length + (accountType === 'AccountKey20' ? 20 : 32)).toU8a(),
...new TextEncoder().encode(accountType),
...decodedAddress,
]);
return u8aToHex(blake2AsU8a(toHash).slice(0, 32));
};

getProxies = async (address) => getProxies(this.api, address);
Expand Down
38 changes: 17 additions & 21 deletions src/common/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,39 +170,35 @@ export const listenEvents = async (api, section, method, timeout = undefined) =>
if (timeout) {
timeoutId = setTimeout(() => {
unsub();
resolve(false);
resolve(null);
}, timeout);
}

const listenSystemEvents = async () => {
unsub = await api.query.system.events((events) => {
let foundEvent = false;
// Loop through the Vec<EventRecord>
events.forEach((record) => {
// Extract the phase, event and the event types
const { event, phase } = record;
const { section: eventSection, method: eventMethod, typeDef: types } = event;

// console.log('section.method: ', `${section}.${method}`);
if (eventSection === section && eventMethod === method) {
foundEvent = true;
// Show what we are busy with
console.log(`\t${section}:${method}:: (phase=${phase.toString()})`);
// console.log(`\t\t${event.meta.documentation.toString()}`);

// Loop through each of the parameters, displaying the type and data
event.data.forEach((data, index) => {
console.log(`\t\t\t${types[index].type}: ${data.toString()}`);
});
}
const foundEvent = _.find(events, ({ event }) => {
const { section: eventSection, method: eventMethod } = event;
return eventSection === section && eventMethod === method;
});

if (foundEvent) {
const {
event: {
section: eventSection, method: eventMethod, typeDef: types, data: eventData,
}, phase,
} = foundEvent;
// Show what we are busy with
console.log(`\t${eventSection}:${eventMethod}:: (phase=${phase.toString()})`);
// Loop through each of the parameters, displaying the type and data
eventData.forEach((data, index) => {
console.log(`\t\t\t${types[index].type}: ${data.toString()}`);
});

unsub();
if (timeoutId) {
clearTimeout(timeoutId);
}
resolve(true);
resolve(foundEvent);
}
});
};
Expand Down
2 changes: 1 addition & 1 deletion src/config/moonbase-local.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import BN from 'bn.js';

const WEIGHT_REF_TIME = new BN('250000000');
const WEIGHT_PROOF_SIZE = new BN('0');
const WEIGHT_PROOF_SIZE = new BN('10000');

const assets = [
{
Expand Down
15 changes: 8 additions & 7 deletions src/moonbeam/moonbase-local.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { TuringDev, MoonbaseLocal } from '../config';
import TuringHelper from '../common/turingHelper';
import MoonbaseHelper from '../common/moonbaseHelper';
import {
sendExtrinsic, getDecimalBN, listenEvents, generateProvidedId, getHourlyTimestamp,
sendExtrinsic, getDecimalBN, listenEvents, getHourlyTimestamp, getTaskIdInTaskScheduledEvent,
// listenEvents, calculateTimeout,
} from '../common/utils';

Expand Down Expand Up @@ -50,10 +50,9 @@ const createEthereumXcmTransactThroughProxyExtrinsic = (parachainHelper, transac
};

const createAutomationTaskExtrinsic = ({
turingHelper, providedId, schedule, parachainId, scheduleFee, payloadExtrinsic, payloadExtrinsicWeight, overallWeight, fee, scheduleAs,
turingHelper, schedule, parachainId, scheduleFee, payloadExtrinsic, payloadExtrinsicWeight, overallWeight, fee, scheduleAs,
}) => {
const extrinsic = turingHelper.api.tx.automationTime.scheduleXcmpTaskThroughProxy(
providedId,
schedule,
{ V3: { parents: 1, interior: { X1: { Parachain: parachainId } } } },
scheduleFee,
Expand Down Expand Up @@ -237,7 +236,6 @@ const main = async () => {
const payloadExtrinsic = createEthereumXcmTransactThroughProxyExtrinsic(moonbaseHelper, aliceKeyPair.address);

console.log('\nb). Create an automation time task with the payload extrinsic ...');
const providedId = generateProvidedId();
const timestampNextHour = getHourlyTimestamp(1);

const payloadExtrinsicWeight = (await payloadExtrinsic.paymentInfo(aliceKeyPair.address)).weight;
Expand All @@ -246,7 +244,6 @@ const main = async () => {

const taskExtrinsic = createAutomationTaskExtrinsic({
turingHelper,
providedId,
schedule: { Fixed: { executionTimes: [0] } },
parachainId: moonbaseHelper.config.paraId,
scheduleFee: { V3: { parents: 1, interior: { X2: [{ Parachain: moonbaseHelper.config.paraId }, { PalletInstance: 3 }] } } },
Expand All @@ -268,11 +265,15 @@ const main = async () => {
feePerSecond,
keyPair: aliceKeyPair,
});
const taskId = await turingHelper.api.rpc.automationTime.generateTaskId(turingAddress, providedId);

// Listen TaskScheduled event on Turing chain
const taskScheduledEvent = await listenEvents(turingHelper.api, 'automationTime', 'TaskScheduled', 60000);
const taskId = getTaskIdInTaskScheduledEvent(taskScheduledEvent);
console.log('TaskId:', taskId);

// Listen XCM events on Moonbase side
const additionalWaitingTime = 5 * 60 * 1000;
console.log(`\n5. Keep Listening XCM events on ${parachainName} until ${moment(timestampNextHour).format('YYYY-MM-DD HH:mm:ss')}(${timestampNextHour}) to verify that the task(taskId: ${taskId}, providerId: ${providedId}) will be successfully executed ...`);
console.log(`\n5. Keep Listening XCM events on ${parachainName} until ${moment(timestampNextHour).format('YYYY-MM-DD HH:mm:ss')}(${timestampNextHour}) to verify that the task(taskId: ${taskId}) will be successfully executed ...`);
const timeout = timestampNextHour - moment().valueOf() + additionalWaitingTime;
const isTaskExecuted = await listenEvents(moonbaseHelper.api, 'ethereum', 'Executed', timeout);
if (!isTaskExecuted) {
Expand Down

0 comments on commit f7e2664

Please sign in to comment.