Upgrade example with real wasm (via sudo now) (#562)

* Upgrade test with real wasm

* Update upgrade sample to use sudo
This commit is contained in:
Jaco Greeff
2019-01-10 11:37:51 +01:00
committed by GitHub
parent 9f1457c286
commit 07d61397d5
12 changed files with 26 additions and 24 deletions
+1 -1
View File
@@ -25,7 +25,7 @@
- [Make a transfer](examples/promise/07_transfer_dots/README.md)
- [Display system events](examples/promise/08_system_events/README.md)(
- [Transaction with events](examples/promise/09_transfer_events/README.md)
- [Upgrade with upgradeKey](examples/promise/10_upgrade_chain/README.md)
- [Upgrade via sudo](examples/promise/10_upgrade_chain/README.md)
<!-- These are hidden, allowing HTML generation but does not appear in the sidebar -->
@@ -11,8 +11,8 @@
},
"dependencies": {
"@polkadot/api": "^0.37.6",
"@polkadot/rpc-provider": "^0.37.6"
"@polkadot/api": "^0.38.6",
"@polkadot/rpc-provider": "^0.38.6"
},
"devDependencies": {
"rimraf": "^2.6.2"
@@ -10,7 +10,7 @@
"start": "node index.js"
},
"dependencies": {
"@polkadot/api": "^0.37.6"
"@polkadot/api": "^0.38.6"
},
"devDependencies": {
"rimraf": "^2.6.2"
@@ -10,7 +10,7 @@
"start": "node index.js"
},
"dependencies": {
"@polkadot/api": "^0.37.6"
"@polkadot/api": "^0.38.6"
},
"devDependencies": {
"rimraf": "^2.6.2"
@@ -10,7 +10,7 @@
"start": "node index.js"
},
"dependencies": {
"@polkadot/api": "^0.37.6"
"@polkadot/api": "^0.38.6"
},
"devDependencies": {
"rimraf": "^2.6.2"
@@ -10,7 +10,7 @@
"start": "node index.js"
},
"dependencies": {
"@polkadot/api": "^0.37.6"
"@polkadot/api": "^0.38.6"
},
"devDependencies": {
"rimraf": "^2.6.2"
@@ -10,7 +10,7 @@
"start": "node index.js"
},
"dependencies": {
"@polkadot/api": "^0.37.6"
"@polkadot/api": "^0.38.6"
},
"devDependencies": {
"rimraf": "^2.6.2"
@@ -10,7 +10,7 @@
"start": "node index.js"
},
"dependencies": {
"@polkadot/api": "^0.37.6"
"@polkadot/api": "^0.38.6"
},
"devDependencies": {
"rimraf": "^2.6.2"
@@ -1,5 +1,5 @@
# Chain upgrade
Performs a chain upgrade using the `upgradeKey` module. This may brick your chain, so us it as an educational sample. (use `substrate --dev purge-chain` to remove DB and recover).
Performs a chain upgrade using the `sudo` module. This may brick your chain, so us it as an educational sample. (use `substrate --dev purge-chain` to remove DB and recover).
[include](index.js)
+15 -13
View File
@@ -8,6 +8,8 @@ const testKeyring = require('@polkadot/keyring/testing');
// utility functions
const { compactAddLength, hexToU8a } = require('@polkadot/util');
const fs = require('fs');
async function main () {
// Initialise the provider to connect to the local node
const provider = new WsProvider('ws://127.0.0.1:9944');
@@ -16,29 +18,26 @@ async function main () {
const api = await ApiPromise.create(provider);
// retrieve the upgrade key from the chain state
const adminId = await api.query.upgradeKey.key();
const adminId = await api.query.sudo.key();
// get the nonce for the admin key
const adminNonce = await api.query.system.accountNonce(adminId);
// find the actual keypair in the keyring (if this is an changed value, the key
// needs to be added to the keyring before - this assumes we have defaults, i.e.
// Alice as the key - and this exists on the test keyring)
// Alice as the key - and this already exists on the test keyring)
const keyring = testKeyring.default();
const adminPair = keyring.getPair(adminId.toString());
console.log('Upgrading from', adminId.toString(), 'with nonce', adminNonce.toString());
// retrieve the runtime to upgrade to
const code = fs.readFileSync('./test.wasm').toString('hex');
const proposal = api.tx.consensus.setCode(`0x${code}`);
// create a properly encoded code array from the hex with length prefix - since we have a
// hex string, we can actuall;y pass it through directly (i.e. `.upgrade('0x...'))` below),
// however here we show how to pass an actual Uint8Array with encoded length
const code = compactAddLength(hexToU8a('0xdeadbeef'));
console.log(`Upgrading from ${adminId} with nonce ${adminNonce}, ${code.length / 2} bytes`);
// preform a chain upgrade, effectively bricking the chain, passing through
// a hex value, although a valid Uint8Array will also work here (in this case
// ensure it has a length prefix added, e.g. compact)
api.tx.upgradeKey
.upgrade(code)
// preform the actual chain upgrade via the sudo module
api.tx.sudo
.sudo(proposal)
.sign(adminPair, adminNonce)
.send(({ events = [], status, type }) => {
console.log('Transaction status:', type);
@@ -58,4 +57,7 @@ async function main () {
});
}
main().catch(console.error);
main().catch((error) => {
console.error(error);
process.exit(-1);
});
@@ -10,7 +10,7 @@
"start": "node index.js"
},
"dependencies": {
"@polkadot/api": "^0.37.6"
"@polkadot/api": "^0.38.6"
},
"devDependencies": {
"rimraf": "^2.6.2"
Binary file not shown.