From 0342e0eca7bee3c145f2ea86499a75ac587f0dba Mon Sep 17 00:00:00 2001 From: ouwenkg <2630582710@qq.com> Date: Thu, 12 Sep 2019 18:35:58 +0800 Subject: [PATCH 1/3] Add upgrade --- scripts/release.sh | 1 + scripts/upgrade/v1.0-to-v1.1.py | 120 ++++++++++++++++++++++++++++++++ scripts/upgrade/v1.0-to-v1.1.sh | 53 ++++++++++++++ 3 files changed, 174 insertions(+) create mode 100644 scripts/upgrade/v1.0-to-v1.1.py create mode 100644 scripts/upgrade/v1.0-to-v1.1.sh diff --git a/scripts/release.sh b/scripts/release.sh index 33e6343fa..85dea0772 100755 --- a/scripts/release.sh +++ b/scripts/release.sh @@ -54,6 +54,7 @@ cp -f scripts/create_cita_config.py target/install/scripts/ # 5) txtool cp -rf scripts/txtool target/install/scripts/ +cp -rf scripts/upgrade target/install/scripts/ # 6) docker env cp -f env.sh target/install/bin/cita-env diff --git a/scripts/upgrade/v1.0-to-v1.1.py b/scripts/upgrade/v1.0-to-v1.1.py new file mode 100644 index 000000000..e5d5e119f --- /dev/null +++ b/scripts/upgrade/v1.0-to-v1.1.py @@ -0,0 +1,120 @@ +#!/usr/bin/env python3 +# -*- coding:utf-8 -*- +""" +Amend the code of system contract. +""" + +import argparse +import functools +import json +import logging +import subprocess +import sys +import time +from jsonrpcclient.http_client import HTTPClient + +LATEST_VERSION = 2 +AMEND_ADDR = '0xffffffffffffffffffffffffffffffffff010002' +SYS_CONF = '0xffffffffffffffffffffffffffffffffff020000' +LOCAL = 'http://127.0.0.1:1337' + + +def send_tx(args): + """ + Send a transfer transaction to a node + + python3 make_tx.py \ + --code "" \ + --to 0xffffffffffffffffffffffffffffffffff010002 \ + --no-newcrypto + + python3 send_tx.py + + """ + kwargs = { + '--privkey': args.privkey, + '--to': AMEND_ADDR, + '--code': args.code, + '--value': str(args.value), + '--version': str(args.version), + } + args = functools.reduce( + lambda lst, kv: lst + list(kv), + kwargs.items(), + [], + ) + print(['python3', 'make_tx.py', *args, '--no-newcrypto']) + subprocess.call(['python3', 'make_tx.py', *args, '--no-newcrypto']) + subprocess.call(['python3', 'send_tx.py']) + with open('../output/transaction/hash') as fobj: + return fobj.read().strip() + + +def rpc_request(method, params, url=LOCAL): + """ Send a jsonrpc request to default url. """ + client = HTTPClient(url) + return client.request(method, params) + + +def get_receipt(tx_hash, url, retry=8): + """ Get receipt of a transaction """ + while retry > 0: + receipt = rpc_request('getTransactionReceipt', [tx_hash], url) + if receipt is not None: + return receipt + time.sleep(4) + retry -= 1 + + +def amend_code(addr, code, args): + """ Amend the code """ + try: + if code: + code = addr + code[2:] + print('code:', code) + args.code = code + tx_hash = send_tx(args) + receipt = get_receipt(tx_hash, args.url) + if receipt['errorMessage']: + logging.critical('amend code of %s error: %s', addr, + receipt['errorMessage']) + sys.exit(1) + except Exception as exception: + logging.critical('amend code of %s exception: %s', addr, exception) + sys.exit(1) + + +def parse_arguments(): + """ parse the arguments: chain_id, version, privkey, url """ + parser = argparse.ArgumentParser() + parser.add_argument( + "--version", + help="Tansaction version.", + default=LATEST_VERSION, + type=int) + parser.add_argument( + '--privkey', required=True, help='The admin private key.') + parser.add_argument('--url', required=True, help='The url of the chain.') + + args = parser.parse_args() + return args + + +def main(): + """ Load the genesis file and amend """ + args = parse_arguments() + + with open('../../genesis.json', 'r') as gene: + genesis = json.load(gene) + + alloc = genesis['alloc'] + for addr in alloc: + # amend code + args.value = 2 + if addr == SYS_CONF: + amend_code(addr, alloc[addr]['code'], args) + print(f'amend code successfully') + + +if __name__ == '__main__': + main() diff --git a/scripts/upgrade/v1.0-to-v1.1.sh b/scripts/upgrade/v1.0-to-v1.1.sh new file mode 100644 index 000000000..f34e800d9 --- /dev/null +++ b/scripts/upgrade/v1.0-to-v1.1.sh @@ -0,0 +1,53 @@ +#!/bin/bash + +# This is a updrade helper for CITA from version 1.0.0 to version 1.1.0. +# +# 1. why this bash needed? +# +# In cita 1.1.0, a interface named `setBlockInterval` was added in system contract. +# So, if you were using cita version 1.1 below and wanted to upgrade to version 1.1, this file is prepared for you. +# +# II. How to use this bach? +# +# 1. Download the cita version 1.1 release package from https://github.com/cryptape/cita/releases +# 2. Extract the package and copy directory bin/ and scripts/ to corresponding dir under your nodes. +# 3. Next, upgrade your node use this helper. Tap the comand below, remember to update the admin privkey +# and node url params. + +# ./scripts/upgrade/v1.0-to-v1.1.sh \ +# 0x5f0258a4778057a8a7d97809bd209055b2fbafa654ce7d31ec7191066b9225e6 \ +# http://127.0.0.1:1337 \ +set -e + +if [[ $(uname) == 'Darwin' ]] +then + SOURCE_DIR=$(realpath "$(dirname "$0")"/..) +else + SOURCE_DIR=$(readlink -f "$(dirname "$0")"/..) +fi + +if [ "$1" = "help" ]; then + echo "Admin private key, url as the params. + For example: \\ + bin/cita scripts/v1.0-to-v1.1.sh \\ + 0x5f0258a4778057a8a7d97809bd209055b2fbafa654ce7d31ec7191066b9225e6 \\ + http://127.0.0.1:1337" + exit 0 +fi + +echo "==> Create temp chain and get new genesis.json" +python3 scripts/create_cita_config.py create \ + --chain_name tmp \ + --super_admin "0x4b5ae4567ad5d9fb92bc9afd6a657e6fa13a2523" \ + --nodes "127.0.0.1:4000" + +cp tmp/0/genesis.json scripts/ + +echo "==> Send tx to amend code" +cd ./scripts/txtool/txtool +python3 "${SOURCE_DIR}"/upgrade/v1.0-to-v1.1.py --privkey "$1" --url "$2" +cd - + +echo "==> Clean temp data" +rm -rf tmp/ +rm -f "${SOURCE_DIR}"/genesis.json From 13ca96be64d29ad00ed8862fc3baa70df7ef968d Mon Sep 17 00:00:00 2001 From: ouwenkg <2630582710@qq.com> Date: Wed, 18 Sep 2019 15:48:40 +0800 Subject: [PATCH 2/3] Use utils --- scripts/amend_system_contracts.sh | 41 --------- scripts/release.sh | 6 +- .../utils.py} | 72 +-------------- scripts/upgrade/v1.0-to-v1.1.py | 87 +------------------ scripts/upgrade/v1.0-to-v1.1.sh | 6 +- 5 files changed, 13 insertions(+), 199 deletions(-) delete mode 100755 scripts/amend_system_contracts.sh rename scripts/{amend_system_contracts.py => upgrade/utils.py} (62%) mode change 100644 => 100755 scripts/upgrade/v1.0-to-v1.1.py mode change 100644 => 100755 scripts/upgrade/v1.0-to-v1.1.sh diff --git a/scripts/amend_system_contracts.sh b/scripts/amend_system_contracts.sh deleted file mode 100755 index d91cbcd8b..000000000 --- a/scripts/amend_system_contracts.sh +++ /dev/null @@ -1,41 +0,0 @@ -#!/bin/bash -set -e - -if [[ $(uname) == 'Darwin' ]] -then - SOURCE_DIR=$(realpath "$(dirname "$0")"/..) -else - SOURCE_DIR=$(readlink -f "$(dirname "$0")"/..) -fi - -if [ "$1" = "help" ]; then - echo "Admin private key, chain id, version, url as the params. - For example: \\ - bin/cita scripts/amend_system_contracts.sh \\ - 0x5f0258a4778057a8a7d97809bd209055b2fbafa654ce7d31ec7191066b9225e6 \\ - 1 \\ - 1 \\ - http://127.0.0.1:1337" - exit 0 -fi - -# Clean tmp files -rm -rf "${SOURCE_DIR}"/tmp -rm -f "${SOURCE_DIR}"/scripts/genesis.json - -# Just get the genensis.json -scripts/create_cita_config.py create \ - --chain_name tmp \ - --super_admin "0x4b5ae4567ad5d9fb92bc9afd6a657e6fa13a2523" \ - --nodes "127.0.0.1:4000" \ - --contract_arguments SysConfig.chainId="$2" \ - --contract_arguments VersionManager.version="$3" \ -&& cp tmp/0/genesis.json scripts/ \ -&& cd ./scripts/txtool/txtool \ -&& python3 "${SOURCE_DIR}"/scripts/amend_system_contracts.py \ - --privkey "$1" \ - --chain_id "$2" \ - --version "$3" \ - --url "$4" \ -&& rm -rf "${SOURCE_DIR}"/tmp \ -&& rm -f "${SOURCE_DIR}"/scripts/genesis.json diff --git a/scripts/release.sh b/scripts/release.sh index 85dea0772..7b8adc6b7 100755 --- a/scripts/release.sh +++ b/scripts/release.sh @@ -60,11 +60,7 @@ cp -rf scripts/upgrade target/install/scripts/ cp -f env.sh target/install/bin/cita-env cp -f scripts/cita_config.sh target/install/bin/cita-config -# 7) amend info of system contract -cp -f scripts/amend_system_contracts.sh target/install/scripts/ -cp -f scripts/amend_system_contracts.py target/install/scripts/ - -# 8) delete building container +# 7) delete building container docker container stop cita_build_container > /dev/null 2>&1 docker container rm cita_build_container > /dev/null 2>&1 diff --git a/scripts/amend_system_contracts.py b/scripts/upgrade/utils.py similarity index 62% rename from scripts/amend_system_contracts.py rename to scripts/upgrade/utils.py index 5fb405c9e..93f7ee68e 100644 --- a/scripts/amend_system_contracts.py +++ b/scripts/upgrade/utils.py @@ -1,25 +1,13 @@ -#!/usr/bin/env python3 -# -*- coding:utf-8 -*- -""" -Amend the code and storage of system contract. -""" -import argparse -import functools -import json -import logging -import subprocess import sys import time +import logging +import functools +import subprocess from jsonrpcclient.http_client import HTTPClient -LATEST_VERSION = 2 -AMEND_ADDR = '0xffffffffffffffffffffffffffffffffff010002' -SYS_CONF = '0xffffffffffffffffffffffffffffffffff020000' -NEW = '0xffffffffffffffffffffffffffffffffff020012' -KEY = '0x30' LOCAL = 'http://127.0.0.1:1337' - +AMEND_ADDR = '0xffffffffffffffffffffffffffffffffff010002' def send_tx(args): """ @@ -38,8 +26,6 @@ def send_tx(args): '--to': AMEND_ADDR, '--code': args.code, '--value': str(args.value), - '--version': str(args.version), - '--chain_id': str(args.chain_id), } args = functools.reduce( lambda lst, kv: lst + list(kv), @@ -52,13 +38,11 @@ def send_tx(args): with open('../output/transaction/hash') as fobj: return fobj.read().strip() - def rpc_request(method, params, url=LOCAL): """ Send a jsonrpc request to default url. """ client = HTTPClient(url) return client.request(method, params) - def get_receipt(tx_hash, url, retry=8): """ Get receipt of a transaction """ while retry > 0: @@ -68,7 +52,6 @@ def get_receipt(tx_hash, url, retry=8): time.sleep(4) retry -= 1 - def amend_code(addr, code, args): """ Amend the code """ try: @@ -86,7 +69,6 @@ def amend_code(addr, code, args): logging.critical('amend code of %s exception: %s', addr, exception) sys.exit(1) - def amend_storage(addr, key, val, args): """ Amend the storage: key and value """ try: @@ -103,49 +85,3 @@ def amend_storage(addr, key, val, args): logging.critical('amend storage of %s[%s] exception: %s', addr, key, exception) sys.exit(1) - - -def parse_arguments(): - """ parse the arguments: chain_id, version, privkey, url """ - parser = argparse.ArgumentParser() - parser.add_argument( - "--version", - help="Tansaction version.", - default=LATEST_VERSION, - type=int) - parser.add_argument( - '--privkey', required=True, help='The admin private key.') - parser.add_argument( - '--chain_id', required=True, help='The id of the chain.') - parser.add_argument('--url', required=True, help='The url of the chain.') - - args = parser.parse_args() - return args - - -def main(): - """ Load the genesis file and amend """ - args = parse_arguments() - - with open('../../genesis.json', 'r') as gene: - genesis = json.load(gene) - - alloc = genesis['alloc'] - - for addr in alloc: - # amend storage - args.value = 3 - storage = alloc[addr]['storage'] - for key in storage: - if addr == NEW: - amend_storage(addr, key, storage[key], args) - elif addr == SYS_CONF and key == KEY: - amend_storage(addr, key, storage[key], args) - - # amend code - args.value = 2 - amend_code(addr, alloc[addr]['code'], args) - - -if __name__ == '__main__': - main() diff --git a/scripts/upgrade/v1.0-to-v1.1.py b/scripts/upgrade/v1.0-to-v1.1.py old mode 100644 new mode 100755 index e5d5e119f..4430634fa --- a/scripts/upgrade/v1.0-to-v1.1.py +++ b/scripts/upgrade/v1.0-to-v1.1.py @@ -1,97 +1,16 @@ -#!/usr/bin/env python3 -# -*- coding:utf-8 -*- """ -Amend the code of system contract. +Amend the code of [System contract](0xffffffffffffffffffffffffffffffffff020000). """ import argparse -import functools import json -import logging -import subprocess -import sys -import time -from jsonrpcclient.http_client import HTTPClient +from utils import rpc_request, get_receipt, amend_code, send_tx -LATEST_VERSION = 2 -AMEND_ADDR = '0xffffffffffffffffffffffffffffffffff010002' SYS_CONF = '0xffffffffffffffffffffffffffffffffff020000' -LOCAL = 'http://127.0.0.1:1337' - - -def send_tx(args): - """ - Send a transfer transaction to a node - - python3 make_tx.py \ - --code "" \ - --to 0xffffffffffffffffffffffffffffffffff010002 \ - --no-newcrypto - - python3 send_tx.py - - """ - kwargs = { - '--privkey': args.privkey, - '--to': AMEND_ADDR, - '--code': args.code, - '--value': str(args.value), - '--version': str(args.version), - } - args = functools.reduce( - lambda lst, kv: lst + list(kv), - kwargs.items(), - [], - ) - print(['python3', 'make_tx.py', *args, '--no-newcrypto']) - subprocess.call(['python3', 'make_tx.py', *args, '--no-newcrypto']) - subprocess.call(['python3', 'send_tx.py']) - with open('../output/transaction/hash') as fobj: - return fobj.read().strip() - - -def rpc_request(method, params, url=LOCAL): - """ Send a jsonrpc request to default url. """ - client = HTTPClient(url) - return client.request(method, params) - - -def get_receipt(tx_hash, url, retry=8): - """ Get receipt of a transaction """ - while retry > 0: - receipt = rpc_request('getTransactionReceipt', [tx_hash], url) - if receipt is not None: - return receipt - time.sleep(4) - retry -= 1 - - -def amend_code(addr, code, args): - """ Amend the code """ - try: - if code: - code = addr + code[2:] - print('code:', code) - args.code = code - tx_hash = send_tx(args) - receipt = get_receipt(tx_hash, args.url) - if receipt['errorMessage']: - logging.critical('amend code of %s error: %s', addr, - receipt['errorMessage']) - sys.exit(1) - except Exception as exception: - logging.critical('amend code of %s exception: %s', addr, exception) - sys.exit(1) - def parse_arguments(): - """ parse the arguments: chain_id, version, privkey, url """ + """ parse the arguments: privkey, url """ parser = argparse.ArgumentParser() - parser.add_argument( - "--version", - help="Tansaction version.", - default=LATEST_VERSION, - type=int) parser.add_argument( '--privkey', required=True, help='The admin private key.') parser.add_argument('--url', required=True, help='The url of the chain.') diff --git a/scripts/upgrade/v1.0-to-v1.1.sh b/scripts/upgrade/v1.0-to-v1.1.sh old mode 100644 new mode 100755 index f34e800d9..ba43be791 --- a/scripts/upgrade/v1.0-to-v1.1.sh +++ b/scripts/upgrade/v1.0-to-v1.1.sh @@ -29,12 +29,16 @@ fi if [ "$1" = "help" ]; then echo "Admin private key, url as the params. For example: \\ - bin/cita scripts/v1.0-to-v1.1.sh \\ + bin/cita scripts/upgrade/v1.0-to-v1.1.sh \\ 0x5f0258a4778057a8a7d97809bd209055b2fbafa654ce7d31ec7191066b9225e6 \\ http://127.0.0.1:1337" exit 0 fi +echo "==> Prepare environment" +rm -rf tmp/ +rm -f "${SOURCE_DIR}"/genesis.json + echo "==> Create temp chain and get new genesis.json" python3 scripts/create_cita_config.py create \ --chain_name tmp \ From d69db92a283b73a35be330ee91203470192d0d40 Mon Sep 17 00:00:00 2001 From: ouwenkg <2630582710@qq.com> Date: Wed, 25 Sep 2019 11:28:16 +0800 Subject: [PATCH 3/3] Fix operation typo[skip travis] --- scripts/upgrade/v1.0-to-v1.1.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/upgrade/v1.0-to-v1.1.sh b/scripts/upgrade/v1.0-to-v1.1.sh index ba43be791..bd26579c4 100755 --- a/scripts/upgrade/v1.0-to-v1.1.sh +++ b/scripts/upgrade/v1.0-to-v1.1.sh @@ -7,11 +7,11 @@ # In cita 1.1.0, a interface named `setBlockInterval` was added in system contract. # So, if you were using cita version 1.1 below and wanted to upgrade to version 1.1, this file is prepared for you. # -# II. How to use this bach? +# II. How to use this bash? # # 1. Download the cita version 1.1 release package from https://github.com/cryptape/cita/releases # 2. Extract the package and copy directory bin/ and scripts/ to corresponding dir under your nodes. -# 3. Next, upgrade your node use this helper. Tap the comand below, remember to update the admin privkey +# 3. Next, upgrade your node use this helper. Tap the command below, remember to update the admin privkey # and node url params. # ./scripts/upgrade/v1.0-to-v1.1.sh \