Skip to content

Commit

Permalink
Merge pull request #857 from ouwenkg/upgrade-scripts
Browse files Browse the repository at this point in the history
Upgrade script
  • Loading branch information
AsceticBear committed Sep 25, 2019
2 parents 24f1649 + d69db92 commit 29897d2
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 114 deletions.
41 changes: 0 additions & 41 deletions scripts/amend_system_contracts.sh

This file was deleted.

7 changes: 2 additions & 5 deletions scripts/release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,13 @@ 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
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

Expand Down
72 changes: 4 additions & 68 deletions scripts/amend_system_contracts.py → scripts/upgrade/utils.py
Original file line number Diff line number Diff line change
@@ -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):
"""
Expand All @@ -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),
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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()
39 changes: 39 additions & 0 deletions scripts/upgrade/v1.0-to-v1.1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""
Amend the code of [System contract](0xffffffffffffffffffffffffffffffffff020000).
"""

import argparse
import json
from utils import rpc_request, get_receipt, amend_code, send_tx

SYS_CONF = '0xffffffffffffffffffffffffffffffffff020000'

def parse_arguments():
""" parse the arguments: privkey, url """
parser = argparse.ArgumentParser()
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()
57 changes: 57 additions & 0 deletions scripts/upgrade/v1.0-to-v1.1.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/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 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 command 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/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 \
--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

0 comments on commit 29897d2

Please sign in to comment.