Skip to content

Commit

Permalink
Merge pull request #4707 from Lezek123/apps-metaprotocol-pre-release
Browse files Browse the repository at this point in the history
Apps metaprotocol: Update from master and fix conflicts
  • Loading branch information
mnaamani authored Mar 22, 2023
2 parents b8c842e + aad51df commit f361c5a
Show file tree
Hide file tree
Showing 14 changed files with 623 additions and 16 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,6 @@ runtime-inputs/

.my_setup

devops/infrastructure
devops/infrastructure

joystream.tar.gz
339 changes: 339 additions & 0 deletions RUNTIME-CONTRIBUTING.md

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions scripts/runtime-code-shasum.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

# Compute a hash over files related to building joystream/node docker image

# Cargo workspace root
export WORKSPACE_ROOT=`cargo metadata --offline --no-deps --format-version 1 | jq .workspace_root -r`

cd ${WORKSPACE_ROOT}
# Assuming cargo workspace root is same as the git repo root
cd $(git rev-parse --show-toplevel)

TAR=tar
SED=sed
Expand Down
12 changes: 12 additions & 0 deletions scripts/runtime-code-tarball.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bash

# Assuming cargo workspace root is same as the git repo root
cd $(git rev-parse --show-toplevel)

tar -czf joystream.tar.gz \
Cargo.lock \
Cargo.toml \
runtime \
runtime-modules \
joystream-node.Dockerfile \
bin
7 changes: 4 additions & 3 deletions tests/network-tests/run-full-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ then
printf "***************************JOSYTREAM NODE LOGS****************************\n"
printf "**************************************************************************\n\n"
docker logs ${NODE_CONTAINER_ID} --tail 50

docker stop ${NODE_CONTAINER_ID}
docker rm ${NODE_CONTAINER_ID}

printf "\n\n\n"
printf "**************************************************************************\n"
printf "****************************HYDRA INDEXER LOGS****************************\n"
Expand All @@ -34,8 +36,7 @@ then
printf "**************************************************************************\n"
printf "*************************QUERY NODE PROCESSOR LOGS************************\n"
printf "**************************************************************************\n\n"
docker logs indexer --tail 50

docker logs processor --tail 50

docker-compose -f ../../docker-compose.yml down -v
}
Expand Down
2 changes: 2 additions & 0 deletions tests/network-tests/run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ CONTAINER_ID=$(./run-test-node-docker.sh)

function cleanup() {
docker logs ${CONTAINER_ID} --tail 15
docker stop ${CONTAINER_ID}
docker rm ${CONTAINER_ID}
docker-compose -f ../../docker-compose.yml down -v
}

Expand Down
1 change: 1 addition & 0 deletions utils/api-scripts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
output.wasm
7 changes: 4 additions & 3 deletions utils/api-scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,18 @@
},
"dependencies": {
"@joystream/types": "^1.0.0",
"@mongodb-js/zstd": "^1.1.0",
"@polkadot/api": "8.9.1",
"@polkadot/types": "8.9.1",
"@polkadot/keyring": "9.5.1",
"@polkadot/types": "8.9.1",
"@polkadot/util": "9.5.1",
"@polkadot/util-crypto": "9.5.1",
"@types/bn.js": "^5.1.0",
"bn.js": "^5.2.1"
},
"devDependencies": {
"typescript": "^4.4.3",
"ts-node": "^10.2.1"
"ts-node": "^10.2.1",
"typescript": "^4.4.3"
},
"volta": {
"extends": "../../package.json"
Expand Down
25 changes: 25 additions & 0 deletions utils/api-scripts/src/get-wasm-from-chain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { ApiPromise, WsProvider } from '@polkadot/api'
import fs from 'fs'
import { Bytes } from '@polkadot/types'

async function main() {
const outputFilePath = process.argv[2] || 'runtime.wasm'

const endpoint = process.env.WS_URI || 'ws://127.0.0.1:9944'
const provider = new WsProvider(endpoint)

const api: ApiPromise = new ApiPromise({ provider })
await api.isReadyOrError

const codeStorageKey = '0x3a636f6465' // hex(':code')
const runtime = await api.rpc.state.getStorage<Bytes>(codeStorageKey)

await api.disconnect()

const wasm = runtime.toU8a(true)
fs.writeFileSync(outputFilePath, wasm)
}

main()
.catch(console.error)
.finally(() => process.exit())
38 changes: 38 additions & 0 deletions utils/api-scripts/src/get-wasm-from-proposal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { ApiPromise, WsProvider } from '@polkadot/api'
import fs from 'fs'

// Fetch the proposed runtime from an active proposal
// If proposal has already been decided, executed or rejected the proposal details will not be
// available on chain, as they are cleaned up after the proposal is finalized.
async function main() {
const proposalId = parseInt(process.argv[2])
const outputFilePath = process.argv[3] || 'runtime.wasm'

const endpoint = process.env.WS_URI || 'ws://127.0.0.1:9944'
const provider = new WsProvider(endpoint)

const api: ApiPromise = new ApiPromise({ provider })
await api.isReadyOrError

const proposalCallCode = await api.query.proposalsEngine.dispatchableCallCode(proposalId)

await api.disconnect()

// attempt to decode as Call
const extrinsicCall = api.createType('Call', proposalCallCode.toHex())

const { method, section } = api.registry.findMetaCall(extrinsicCall.callIndex)

if (method !== 'executeRuntimeUpgradeProposal' || section !== 'joystreamUtility') {
throw new Error('This is not the proposal you are looking for!')
}

// The first arg to joystreamUtility.executeRuntimeUpgradeProposal(wasm) is the proposed wasm code
const wasm = extrinsicCall.args[0]

fs.writeFileSync(outputFilePath, wasm.toU8a(true))
}

main()
.catch(console.error)
.finally(() => process.exit())
102 changes: 102 additions & 0 deletions utils/api-scripts/src/helpers/runtimeVersion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
const BN = require('bn.js')
const types = require('@joystream/types')

async function getRuntimeVersionFromWasm(wasm) {
const memory = new WebAssembly.Memory({ initial: 21 })
// pointer into heap used to keep track of malloc'ed memory
// https://spec.polkadot.network/#defn-runtime-pointer
let allocPointer = 0

const module = await WebAssembly.instantiate(wasm, {
env: {
memory,
// Just a silly memory allocator :) sufficient to have Core_version() work.
// https://spec.polkadot.network/#id-ext_allocator_malloc
//! The runtime code must export a global symbol named `__heap_base` of type `i32`. Any memory
//! whose offset is below the value of `__heap_base` can be used at will by the program, while
//! any memory above `__heap_base` but below `__heap_base + heap_pages` (where `heap_pages` is
//! the value passed as parameter to [`HostVmPrototype::new`]) is available for use by the
//! implementation of `ext_allocator_malloc_version_1`.
ext_allocator_malloc_version_1: (size) => {
allocPointer += size
return module.instance.exports.__heap_base + allocPointer
},
// dummy runtime host functions for polkadot/substrate node
ext_hashing_blake2_128_version_1: () => {},
ext_hashing_blake2_256_version_1: () => {},
ext_hashing_twox_128_version_1: () => {},
ext_hashing_twox_64_version_1: () => {},
ext_allocator_free_version_1: () => {},
ext_storage_append_version_1: () => {},
ext_storage_clear_version_1: () => {},
ext_storage_clear_prefix_version_2: () => {},
ext_storage_commit_transaction_version_1: () => {},
ext_storage_exists_version_1: () => {},
ext_storage_get_version_1: () => {},
ext_storage_next_key_version_1: () => {},
ext_storage_read_version_1: () => {},
ext_storage_rollback_transaction_version_1: () => {},
ext_storage_root_version_2: () => {},
ext_storage_set_version_1: () => {},
ext_storage_start_transaction_version_1: () => {},
ext_misc_print_hex_version_1: () => {},
ext_misc_print_num_version_1: () => {},
ext_misc_print_utf8_version_1: () => {},
ext_misc_runtime_version_version_1: () => {},
ext_trie_blake2_256_ordered_root_version_2: () => {},
ext_offchain_is_validator_version_1: () => {},
ext_offchain_local_storage_clear_version_1: () => {},
ext_offchain_local_storage_compare_and_set_version_1: () => {},
ext_offchain_local_storage_get_version_1: () => {},
ext_offchain_local_storage_set_version_1: () => {},
ext_offchain_network_state_version_1: () => {},
ext_offchain_random_seed_version_1: () => {},
ext_offchain_submit_transaction_version_1: () => {},
ext_offchain_timestamp_version_1: () => {},
ext_offchain_index_set_version_1: () => {},
ext_crypto_ed25519_generate_version_1: () => {},
ext_crypto_ed25519_verify_version_1: () => {},
ext_crypto_finish_batch_verify_version_1: () => {},
ext_crypto_secp256k1_ecdsa_recover_compressed_version_2: () => {},
ext_crypto_sr25519_generate_version_1: () => {},
ext_crypto_sr25519_public_keys_version_1: () => {},
ext_crypto_sr25519_sign_version_1: () => {},
ext_crypto_sr25519_verify_version_2: () => {},
ext_crypto_start_batch_verify_version_1: () => {},
ext_logging_log_version_1: (a) => {},
ext_logging_max_level_version_1: () => {},
},
})

//! ## Runtime version
//!
//! Wasm files can contain so-called custom sections. A runtime can contain two custom sections
//! whose names are `"runtime_version"` and `"runtime_apis"`, in which case they must contain a
//! so-called runtime version.
//!
//! The runtime version contains important field that identifies a runtime.
//!
//! If no `"runtime_version"` and `"runtime_apis"` custom sections can be found, the
//! `Core_version` entry point is used as a fallback in order to obtain the runtime version. This
//! fallback mechanism is maintained for backwards compatibility purposes, but is considered
//! deprecated.
// Couldn't figure out how to find these "custom sections" so just using Core_version entry point.
const result = module.instance.exports.Core_version()

//! The function returns a 64 bits number. The 32 less significant bits represent a pointer to the
//! Wasm virtual machine's memory, and the 32 most significant bits a length. This pointer and
//! length designate a buffer containing the actual return value.
// https://spec.polkadot.network/#defn-runtime-pointer-size
const pointerSize = new BN(result)
const size = pointerSize.shrn(32).toNumber() // 32 most significant bits
const pointer = pointerSize.maskn(32).toNumber() // 32 least significant bits

// get data returned by the call from memory
const data = memory.buffer.slice(pointer, pointer + size)
// Decode data as `RuntimeVersion` struct
return types.createType('RuntimeVersion', Buffer.from(data))
}

module.exports = {
getRuntimeVersionFromWasm,
}
38 changes: 38 additions & 0 deletions utils/api-scripts/src/inspect-wasm-runtime-version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env node --experimental-wasm-bigint

// Run script with: node --experimental-wasm-bigint src/inspect-wasm-runtime-version.js ./runtime.wasm
const fs = require('fs')
const { decompress } = require('@mongodb-js/zstd')
const { getRuntimeVersionFromWasm } = require('./helpers/runtimeVersion')

// Reference from substrate:
// https://github.com/paritytech/substrate/blob/master/primitives/maybe-compressed-blob/src/lib.rs
const ZSTD_PREFIX = Buffer.from([82, 188, 83, 118, 70, 219, 142, 5])

function hasMagicPrefix(blob) {
const prefix = blob.subarray(0, 8)
return Buffer.compare(prefix, ZSTD_PREFIX) === 0
}

async function main() {
const inputWasmFilePath = process.argv[2] || 'runtime.wasm'

let wasm = fs.readFileSync(inputWasmFilePath)

if (hasMagicPrefix(wasm)) {
console.error('Decompressing WASM blob')
wasm = await decompress(Buffer.from(wasm.subarray(8)))
}

if (!WebAssembly.validate(wasm)) {
return console.error('Input wasm is not valid')
}

const runtimeVersion = await getRuntimeVersionFromWasm(wasm)

console.log(runtimeVersion.toHuman())
}

main()
.catch(console.error)
.finally(() => process.exit())
4 changes: 2 additions & 2 deletions utils/api-scripts/src/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import BN from 'bn.js'
import '@joystream/types'

async function main() {
// Initialise the provider to connect to the local node
const provider = new WsProvider('ws://127.0.0.1:9944')
const endpoint = process.env.WS_URI || 'ws://127.0.0.1:9944'
const provider = new WsProvider(endpoint)

// Create the API and wait until ready
let api: ApiPromise
Expand Down
54 changes: 51 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2350,6 +2350,54 @@
resolved "https://registry.yarnpkg.com/@microsoft/fetch-event-source/-/fetch-event-source-2.0.1.tgz#9ceecc94b49fbaa15666e38ae8587f64acce007d"
integrity sha512-W6CLUJ2eBMw3Rec70qrsEW0jOm/3twwJv21mrmj2yORiaVmVYGS4sSS5yUwvQc1ZlDLYGPnClVWmUUMagKNsfA==

"@mongodb-js/zstd-darwin-arm64@1.1.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@mongodb-js/zstd-darwin-arm64/-/zstd-darwin-arm64-1.1.0.tgz#24822d7117dc72dca9a9b8a5f7851e20a510ac8b"
integrity sha512-3XUa82KqWvtFEsx8WYwgdVCjC5Xnz77Hg54O3q7sw8LVPM/faFnGKGH2NSmNnucbabtEpLECz4iahK0bUotl4Q==

"@mongodb-js/zstd-darwin-x64@1.1.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@mongodb-js/zstd-darwin-x64/-/zstd-darwin-x64-1.1.0.tgz#9439e81c5762038a8733285ac0f5e7f368daa504"
integrity sha512-5veErRDyMHJbq/3cRSMrg6WkByXHhmhqZAK7Y4gn8owHcSIuC4ztOT+cH7AQWI7M47JkpcJX640cOMYYtWJbfg==

"@mongodb-js/zstd-linux-arm64-gnu@1.1.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@mongodb-js/zstd-linux-arm64-gnu/-/zstd-linux-arm64-gnu-1.1.0.tgz#b284472259e535eb1a70fccb1025d558cf2675cf"
integrity sha512-M5FRUlDUV3PvpAM/VmPLVczDJOZOaT7qxl1i6fXNGZ0DDmnSDigZBUmc+w3tQGKi0/VRZiwdyBZcQQgqBeKr7g==

"@mongodb-js/zstd-linux-arm64-musl@1.1.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@mongodb-js/zstd-linux-arm64-musl/-/zstd-linux-arm64-musl-1.1.0.tgz#3604b3183bd472b69e1936edc533eaad3778b1b5"
integrity sha512-3EdhHJKOd/8eXg4Q/ID+HGKUifIY0dqULPGpFveM7iBpV0QrpVOU7iDjVoFEROTqUubHN246Lmvj7VZ3z4F9+A==

"@mongodb-js/zstd-linux-x64-gnu@1.1.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@mongodb-js/zstd-linux-x64-gnu/-/zstd-linux-x64-gnu-1.1.0.tgz#99077b73c02e4d34f04ef7fae6bf66ef79e159a7"
integrity sha512-kNLvetxCFYx7QPEo/kR8h3ZApvjXtUYWuct9Mz52Rw67K3Cq5fJDjTAhroMsJCK2doNgjI1dxAQIhZziwnaMCg==

"@mongodb-js/zstd-linux-x64-musl@1.1.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@mongodb-js/zstd-linux-x64-musl/-/zstd-linux-x64-musl-1.1.0.tgz#3372c8bbef0a9dba310c4ecf4d1c2254bfe95fbb"
integrity sha512-lJxI/4kVPQUXWRxi4DOpBziO1y8EFuBGp7QLWzPgVhxY8boGYQhybZqj2L+KmLG3gsrh4dstQWhaIojDRM7UiQ==

"@mongodb-js/zstd-win32-x64-msvc@1.1.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@mongodb-js/zstd-win32-x64-msvc/-/zstd-win32-x64-msvc-1.1.0.tgz#ab4db0ce4fe4d0d150442366a6ef2fb251426fc4"
integrity sha512-nct2+7xL5FYeteH+nLCskYerHE7yaneqGJuR2/2n4GsUPMyIbyKwa0VOEAKP1QF/OrwLMswtdRUPtyI8q2PF9Q==

"@mongodb-js/zstd@^1.1.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@mongodb-js/zstd/-/zstd-1.1.0.tgz#f7cbaf2d700115155842322403f3684869026285"
integrity sha512-+dRj3tSgenHhdqZ1d9ii7PKrXerXOZgDKJE9E2IFxC2lOecgOnSnInAD1o6hCFmyJ5NDQrcsrJKxkOlptwA1mw==
optionalDependencies:
"@mongodb-js/zstd-darwin-arm64" "1.1.0"
"@mongodb-js/zstd-darwin-x64" "1.1.0"
"@mongodb-js/zstd-linux-arm64-gnu" "1.1.0"
"@mongodb-js/zstd-linux-arm64-musl" "1.1.0"
"@mongodb-js/zstd-linux-x64-gnu" "1.1.0"
"@mongodb-js/zstd-linux-x64-musl" "1.1.0"
"@mongodb-js/zstd-win32-x64-msvc" "1.1.0"

"@multiformats/base-x@^4.0.1":
version "4.0.1"
resolved "https://registry.yarnpkg.com/@multiformats/base-x/-/base-x-4.0.1.tgz#95ff0fa58711789d53aefb2590a8b7a4e715d121"
Expand Down Expand Up @@ -17950,9 +17998,9 @@ rx-lite@*, rx-lite@^4.0.8:
integrity sha1-Cx4Rr4vESDbwSmQH6S2kJGe3lEQ=

rxjs@7.5.5, rxjs@^6.3.3, rxjs@^6.4.0, rxjs@^6.6.0, rxjs@^6.6.3, rxjs@^7.2.0, rxjs@^7.4.0, rxjs@^7.5.5:
version "7.5.7"
resolved "https://registry.yarnpkg.com/rxjs/-/rxjs-7.5.7.tgz#2ec0d57fdc89ece220d2e702730ae8f1e49def39"
integrity sha512-z9MzKh/UcOqB3i20H6rtrlaE/CgjLOvheWK/9ILrbhROGTweAi1BaFsTT9FbwZi5Trr1qNRs+MXkhmR06awzQA==
version "7.8.0"
resolved "https://registry.yarnpkg.com/rxjs/-/rxjs-7.8.0.tgz#90a938862a82888ff4c7359811a595e14e1e09a4"
integrity sha512-F2+gxDshqmIub1KdvZkaEfGDwLNpPvk9Fs6LD/MyQxNgMds/WH9OdDDXOmxUZpME+iSK3rQCctkL0DYyytUqMg==
dependencies:
tslib "^2.1.0"

Expand Down

0 comments on commit f361c5a

Please sign in to comment.