Skip to content

Commit

Permalink
Merge pull request #24 from LimeChain/modules/grandpa
Browse files Browse the repository at this point in the history
Grandpa commit
  • Loading branch information
Daniel-K-Ivanov authored Jun 7, 2021
2 parents 8616eaa + bdf3cd9 commit ed571d7
Show file tree
Hide file tree
Showing 27 changed files with 283 additions and 106 deletions.
26 changes: 11 additions & 15 deletions .github/workflows/compile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,27 @@ jobs:
- uses: actions/checkout@v2
- name: Install jq and curl
run: sudo apt-get update && sudo apt-get install -y jq curl make
- name: Cache node modules
uses: actions/cache@v2
env:
cache-name: cache-node-modules
with:
path: |
'**/node_modules'
'**/metadata/node_modules'
key: ${{ runner.os }}-modules-${{ env.cache-name }}-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-build-${{ env.cache-name }}-
${{ runner.os }}-build-
${{ runner.os }}-
- name: Compile runtime
run: bash .github/workflows/scripts/compile.sh
- name: Run the node and insert Aura keys
run: make run-node-demo spec=./raw-chain-spec.json detached=1 && bash .github/workflows/scripts/insert-aura.sh
run: |
make run-node \
NAME=node01 \
PORT=30333 \
WS-PORT=9944 \
RPC-PORT=9933 \
spec=raw-chain-spec.json detached=1 \
&& bash .github/workflows/scripts/insert-keys.sh \
&& docker restart node01
- name: Sleep for 10 seconds
uses: jakejarvis/wait-action@master
with:
time: '10s'
- name: Get latest header
id: check-header
run: |
echo "::set-output name=tx-output::$(bash .github/workflows/scripts/get-latest-header.sh)\n"
docker start node01 \
&& echo "::set-output name=tx-output::$(bash .github/workflows/scripts/get-latest-header.sh)\n"
- name: |
Check the latest header
If it contains number":"0x0",
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/scripts/compile.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
npm install -g yarn
sudo npm install -g yarn
yarn install
npm run build
node ./cli/dist/src/index.js spec --to=./chain-spec.json
node ./cli/dist/src/index.js spec --src=./chain-spec.json
npm uninstall -g yarn
sudo npm uninstall -g yarn
9 changes: 0 additions & 9 deletions .github/workflows/scripts/insert-aura.sh

This file was deleted.

18 changes: 18 additions & 0 deletions .github/workflows/scripts/insert-keys.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
lsof -n | grep LISTEN > /dev/null
curl --location --request POST '0.0.0.0:9933' \
--header 'Content-Type: application/json' \
--data-raw '{
"jsonrpc": "2.0",
"method": "author_insertKey",
"params": ["aura","clip organ olive upper oak void inject side suit toilet stick narrow","0x9effc1668ca381c242885516ec9fa2b19c67b6684c02a8a3237b6862e5c8cd7e"],
"id": 1
}'
lsof -n | grep LISTEN > /dev/null
curl --location --request POST '0.0.0.0:9933' \
--header 'Content-Type: application/json' \
--data-raw '{
"jsonrpc": "2.0",
"method": "author_insertKey",
"params": ["gran","clip organ olive upper oak void inject side suit toilet stick narrow","0xb48004c6e1625282313b07d1c9950935e86894a2e4f21fb1ffee9854d180c781"],
"id": 1
}'
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ endif
run-node:
ifdef help
@docker run -it limechain/as-substrate:node-v1 --help
else detached
@docker run -p ${RPC-PORT}:${RPC-PORT} -p ${WS-PORT}:${WS-PORT} -p ${PORT}:${PORT} -v "$(CURDIR)/${spec}":/raw-chain-spec.json -v /tmp:/tmp --name ${NAME} -d limechain/as-substrate:grandpa --base-path /tmp/${NAME} --port ${PORT} --ws-port ${WS-PORT} --rpc-port ${RPC-PORT} --execution Wasm --offchain-worker Never --validator --name=${NAME} ${OTHER}
else
docker run -p ${RPC-PORT}:${RPC-PORT} -p ${WS-PORT}:${WS-PORT} -p ${PORT}:${PORT} -v "$(CURDIR)/${spec}":/raw-chain-spec.json limechain/as-substrate:test --base-path /tmp/${NAME} --port ${PORT} --ws-port ${WS-PORT} --rpc-port ${RPC-PORT} --execution Wasm --offchain-worker Never --name=${NAME} ${OTHER}
@docker run -p ${RPC-PORT}:${RPC-PORT} -p ${WS-PORT}:${WS-PORT} -p ${PORT}:${PORT} -v "$(CURDIR)/${spec}":/raw-chain-spec.json -v /tmp:/tmp limechain/as-substrate:grandpa --base-path /tmp/${NAME} --port ${PORT} --ws-port ${WS-PORT} --rpc-port ${RPC-PORT} --execution Wasm --offchain-worker Never --validator --name=${NAME} ${OTHER}
endif
3 changes: 2 additions & 1 deletion assembly/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
export const __heap_base = 0;

export * from "./runtime/api/aura-api";
export * from "./runtime/api/core";
export * from "./runtime/api/block-builder";
export * from "./runtime/api/core";
export * from "./runtime/api/grandpa-api";
export * from "./runtime/api/others";
1 change: 0 additions & 1 deletion assembly/pallets/aura/aura.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ export class Aura {

/**
* @description Creates Aura inherent
* NOTE: Draft implementation
* @param data Inherent data
*/
static _createInherent(data: InherentData<ByteArray>): u8[] {
Expand Down
34 changes: 34 additions & 0 deletions assembly/pallets/grandpa/grandpa.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { ByteArray } from "as-scale-codec";
import { Storage, Utils } from "subsembly-core";

/**
* @descritption Storage entries for Grandpa
*/
export namespace GrandpaStorageEntries {
}

/**
* @description Class representing GRANDPA module
*/
export class Grandpa {
public static readonly GRANDPA_AUTHORITIES: string = ":grandpa_authorities";

/**
* @description Get GRANDPA authorities from the storage
* concatenated list of Grandpa authorities
*/
static _authorities(): u8[] {
const entry = Storage.get(Utils.stringsToBytes([this.GRANDPA_AUTHORITIES], false));
const auths = entry.isSome() ? (<ByteArray>entry.unwrap()).unwrap() : [0];
// first byte is option byte
return auths;
}

/**
* @description Set GRANDPA authorities
* @param auths list of Grandpa authorities
*/
static _setAuthorities(auths: u8[]): void {
Storage.set(Utils.stringsToBytes([this.GRANDPA_AUTHORITIES], false), auths);
}
}
2 changes: 1 addition & 1 deletion assembly/pallets/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from "./aura/aura";
export * from "./balances/balances";
export * from "./grandpa/grandpa";
export * from "./timestamp/timestamp";
export * from "./transaction-payment/transaction-payment";

5 changes: 3 additions & 2 deletions assembly/runtime/api/grandpa-api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Serialiser } from 'subsembly-core';

import { Grandpa } from '../../pallets/grandpa/grandpa';

/**
* @description Attempt to extract a pending set-change signal from a digest.
Expand All @@ -25,5 +25,6 @@ export function GrandpaApi_grandpa_forced_change(data: i32, len: i32): u64 {
* @param len i32 length (in bytes) of the arguments passed
*/
export function GrandpaApi_grandpa_authorities(data: i32, len: i32): u64 {
return Serialiser.serialiseResult([]);
const authorities = Grandpa._authorities();
return Serialiser.serialiseResult(authorities.slice(1));
}
6 changes: 0 additions & 6 deletions assembly/runtime/api/index.ts

This file was deleted.

2 changes: 1 addition & 1 deletion cli/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 cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "CLI tool for Subsembly",
"main": "dist/src/index.js",
"scripts": {
"test": "mocha 'test/**/*.js' --timeout 10000 && cd ./src/commands/spec/builder && npm run test",
"test": "npx mocha 'test/**/*.js' --timeout 10000 && cd ./src/commands/spec/builder && npm run test",
"dev": "ts-node src/index.ts",
"build-spec": "npm install --prefix ./src/commands/spec/builder && npm run asbuild --prefix ./src/commands/spec/builder",
"copy-wasm": "mkdir -p ./dist/src/commands/spec/builder/build/ && cp ./src/commands/spec/builder/build/build-spec.wasm ./dist/src/commands/spec/builder/build/",
Expand Down
3 changes: 2 additions & 1 deletion cli/src/commands/spec/builder/assembly/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { u128 } from 'as-bignum';
import { BytesReader, CompactInt, UInt128 } from 'as-scale-codec';
import { BytesReader, CompactInt, UInt128, UInt64 } from 'as-scale-codec';
import { AccountData, AccountId, Utils } from 'subsembly-core';

export type Balance = UInt128;
export type Weight = UInt64;

/**
* @description Gets the AccountData converted to the bytes
Expand Down
7 changes: 6 additions & 1 deletion cli/src/commands/spec/builder/customSpec-template.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@
},
"aura": {
"authorities": [
"5H49oi57ktRnYTbhVtKpGGk79rB9QXNcApYELLWcKa9W8nfs"
"5FfBQ3kwXrbdyoqLPvcXRp7ikWydXawpNs2Ceu3WwFdhZ8W4"
]
},
"grandpa": {
"authorities": [
["5G9NWJ5P9uk7am24yCKeLZJqXWW6hjuMyRJDmw4ofqxG8Js2", 1]
]
}
}
Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/spec/builder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"asbuild:optimized": "asc assembly/index.ts -b build/build-spec.wasm -t build/build-spec.wat --runtime full --optimize",
"asbuild": "npm run asbuild:optimized"
},
"author": "",
"author": "Dastanbek Samatov",
"license": "ISC",
"dependencies": {
"@assemblyscript/loader": "^0.17.9",
Expand Down
5 changes: 5 additions & 0 deletions cli/src/commands/spec/builder/src/genesis-builder.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const Balances = require("./modules/balances");
const System = require("./modules/system");
const Aura = require('./modules/aura');
const Grandpa = require('./modules/grandpa');

/**
* Class for the genesis configuration
Expand Down Expand Up @@ -40,6 +41,10 @@ class GenesisBuilder {
const rawAura = Aura.toRaw(genesisConfig.genesis.runtime.aura.authorities);
Object.assign(rawGenesis.raw.top, rawAura);
}
if(genesisConfig.genesis.runtime.grandpa) {
const rawGrandpa = Grandpa.toRaw(genesisConfig.genesis.runtime.grandpa.authorities);
Object.assign(rawGenesis.raw.top, rawGrandpa);
}
rawGenesis['raw']["childrenDefault"] = {};
return rawGenesis;
}
Expand Down
29 changes: 2 additions & 27 deletions cli/src/commands/spec/builder/src/modules/aura.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
const { u8aToHex } = require('@polkadot/util');
const { Keyring } = require('@polkadot/api');
const {
__newArray, __getArray,
UInt8Array_ID, getAccountIdBytes
} = require('../wasm-loader');
const Utils = require('../utils');

class Aura {
Expand All @@ -22,7 +18,7 @@ class Aura {
* @param authorities list of authorities
*/
static toRaw(authorities){
validateIsArray(authorities);
Utils.validateIsArray(authorities, "Aura: Invalid or no Aura array provided");

if (authorities.length === 0){
throw new Error("Aura: Array of authorities is empty");
Expand All @@ -36,32 +32,11 @@ class Aura {
const keyringInstance = keyring.addFromAddress(element);
rawAuthorities = rawAuthorities.concat(Array.from(keyringInstance.publicKey));
});
const auths = getAuthoritiesBytes(rawAuthorities);
const auths = Utils.getAuthoritiesBytes(rawAuthorities);
return {
[key]: u8aToHex(auths)
}
}
}

/**
* Validates whether the provided parameter is array. Throws otherwise
* @param {*} arr
*/
function validateIsArray (arr) {
if (!Array.isArray(arr)) {
throw new Error("Aura: Invalid or no authorities array provided");
}
}

/**
* Get scale encoded list of Aura authorities
* @param authorities list of authorities
*/
const getAuthoritiesBytes = (authorities) => {
const aPtr = __newArray(UInt8Array_ID, authorities);
const auths = getAccountIdBytes(aPtr);
const result = __getArray(auths);
return result;
}

module.exports = Aura;
34 changes: 12 additions & 22 deletions cli/src/commands/spec/builder/src/modules/balances.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,43 +20,33 @@ class Balances {
* @param balances array with accountId and balances
*/
static toRaw(balancesArray) {
validateIsArray(balancesArray);
Utils.validateIsArray(balancesArray, "Balances: Invalid or no balances array provided");

const rawBalances = {};
const keyring = new Keyring({ type: 'sr25519' });
const typeReg = new TypeRegistry();
balancesArray.forEach(balanceArray => {
validateIsArray(balanceArray);
Utils.validateIsArray(balanceArray, "Balances: Invalid or no balances array provided");
const keyringInstance = keyring.addFromAddress(balanceArray[0]);
const key = Utils.getHashKey(this.MODULE_PREFIX, this.MODULE_KEY, keyringInstance.publicKey);
const systemKey = Utils.getHashKey(System.MODULE_PREFIX, System.MODULE_KEY, keyringInstance.publicKey);
const accData = accDataToHex(typeReg.createType("U128", balanceArray[1].toString()).toU8a());
const accData = this.accDataToHex(typeReg.createType("U128", balanceArray[1].toString()).toU8a());
const accInfo = System.getAccountInfo(accData);
rawBalances[key] = accData;
rawBalances[systemKey] = accInfo;
});
return rawBalances;
}
}

/**
* Validates whether the provided parameter is array. Throws otherwise
* @param {*} arr
*/
function validateIsArray (arr) {
if (!Array.isArray(arr)) {
throw new Error("Balances: Invalid or no balances array provided");
/**
*
* @param value encodes AccountData instance to hex
*/
static accDataToHex = (value) => {
const accData = getAccountDataBytes(__newArray(UInt8Array_ID, value));
const res = __getArray(accData);
return u8aToHex(res);
}
}

/**
*
* @param value encodes AccountData instance to hex
*/
const accDataToHex = (value) => {
const accData = getAccountDataBytes(__newArray(UInt8Array_ID, value));
const res = __getArray(accData);
return u8aToHex(res);

}

module.exports = Balances
Loading

0 comments on commit ed571d7

Please sign in to comment.