|
| 1 | +#!/usr/bin/env bash |
| 2 | +NODEOS_RUNNING=$1 |
| 3 | + |
| 4 | +set -m |
| 5 | + |
| 6 | +# CAUTION: Never use these development keys for a production account! |
| 7 | +# Doing so will most certainly result in the loss of access to your account, these private keys are publicly known. |
| 8 | +SYSTEM_ACCOUNT_PRIVATE_KEY="5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3" |
| 9 | +SYSTEM_ACCOUNT_PUBLIC_KEY="EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV" |
| 10 | + |
| 11 | +EXAMPLE_ACCOUNT_PRIVATE_KEY="5JuH9fCXmU3xbj8nRmhPZaVrxxXrdPaRmZLW1cznNTmTQR2Kg5Z" |
| 12 | +EXAMPLE_ACCOUNT_PUBLIC_KEY="EOS7bxrQUTbQ4mqcoefhWPz1aFieN4fA9RQAiozRz7FrUChHZ7Rb8" |
| 13 | + |
| 14 | +R1_EXAMPLE_ACCOUNT_PRIVATE_KEY="PVT_R1_GrfEfbv5at9kbeHcGagQmvbFLdm6jqEpgE1wsGbrfbZNjpVgT" |
| 15 | +R1_EXAMPLE_ACCOUNT_PUBLIC_KEY="PUB_R1_4ztaVy8L9zbmzTdpfq5GcaFYwGwXTNmN3qW7qcgHMmfUZhpzQQ" |
| 16 | + |
| 17 | +ROOT_DIR="/opt" |
| 18 | +CONTRACTS_DIR="$ROOT_DIR/eosio/bin/contracts" |
| 19 | +BLOCKCHAIN_DATA_DIR=/root/.local/share |
| 20 | +BLOCKCHAIN_CONFIG_DIR=/opt/eosio/bin/config-dir |
| 21 | +WALLET_DIR="/root/eosio-wallet/" |
| 22 | + |
| 23 | +mkdir -p $ROOT_DIR/bin |
| 24 | + |
| 25 | +# Set PATH |
| 26 | +PATH="$PATH:$ROOT_DIR/bin:$ROOT_DIR/bin/scripts" |
| 27 | +CONFIG_DIR="$ROOT_DIR/bin/config-dir" |
| 28 | + |
| 29 | +function start_wallet { |
| 30 | + echo "Starting the wallet" |
| 31 | + rm -rf $WALLET_DIR |
| 32 | + mkdir -p $WALLET_DIR |
| 33 | + nohup keosd --unlock-timeout 999999999 --wallet-dir $WALLET_DIR --http-server-address 127.0.0.1:8900 2>&1 & |
| 34 | + sleep 1s |
| 35 | + wallet_password=$(cleos wallet create --to-console | awk 'FNR > 3 { print $1 }' | tr -d '"') |
| 36 | + echo $wallet_password > "$CONFIG_DIR"/keys/default_wallet_password.txt |
| 37 | + |
| 38 | + cleos wallet import --private-key $SYSTEM_ACCOUNT_PRIVATE_KEY |
| 39 | +} |
| 40 | + |
| 41 | +function post_preactivate { |
| 42 | + curl -X POST http://127.0.0.1:8888/v1/producer/schedule_protocol_feature_activations -d '{"protocol_features_to_activate": ["0ec7e080177b2c02b278d5088611686b49d739925a92d9bfcacd7fc6b74053bd"]}' |
| 43 | +} |
| 44 | + |
| 45 | +# $1 feature disgest to activate |
| 46 | +function activate_feature { |
| 47 | + cleos push action eosio activate '["'"$1"'"]' -p eosio |
| 48 | + if [ $? -ne 0 ]; then |
| 49 | + exit 1 |
| 50 | + fi |
| 51 | +} |
| 52 | + |
| 53 | +# $1 account name |
| 54 | +# $2 contract directory |
| 55 | +# $3 wasm file name |
| 56 | +# $4 abi file name |
| 57 | +function setcode { |
| 58 | + retry_count="4" |
| 59 | + |
| 60 | + while [ $retry_count -gt 0 ]; do |
| 61 | + cleos set code $1 $2 -p $1@active |
| 62 | + if [ $? -eq 0 ]; then |
| 63 | + break |
| 64 | + fi |
| 65 | + |
| 66 | + echo "setcode failed retrying..." |
| 67 | + sleep 1s |
| 68 | + retry_count=$[$retry_count-1] |
| 69 | + done |
| 70 | + |
| 71 | + if [ $retry_count -eq 0 ]; then |
| 72 | + echo "setcode failed too many times, bailing." |
| 73 | + exit 1 |
| 74 | + fi |
| 75 | +} |
| 76 | + |
| 77 | +# $1 account name |
| 78 | +# $2 contract directory |
| 79 | +# $3 abi file name |
| 80 | +function setabi { |
| 81 | + retry_count="4" |
| 82 | + |
| 83 | + while [ $retry_count -gt 0 ]; do |
| 84 | + cleos set abi $1 $2 -p $1@active |
| 85 | + if [ $? -eq 0 ]; then |
| 86 | + break |
| 87 | + fi |
| 88 | + |
| 89 | + echo "setcode failed retrying..." |
| 90 | + sleep 1s |
| 91 | + retry_count=$[$retry_count-1] |
| 92 | + done |
| 93 | + |
| 94 | + if [ $retry_count -eq 0 ]; then |
| 95 | + echo "setcode failed too many times, bailing." |
| 96 | + exit 1 |
| 97 | + fi |
| 98 | +} |
| 99 | + |
| 100 | +# $1 - account name |
| 101 | +# $2 - public key |
| 102 | +# $3 - private key |
| 103 | +function create_account { |
| 104 | + cleos wallet import --private-key $3 |
| 105 | + cleos create account eosio $1 $2 |
| 106 | +} |
| 107 | + |
| 108 | +# Move into the executable directory |
| 109 | +cd $ROOT_DIR/bin/ |
| 110 | +mkdir -p $CONFIG_DIR |
| 111 | +mkdir -p $BLOCKCHAIN_DATA_DIR |
| 112 | +mkdir -p $BLOCKCHAIN_CONFIG_DIR |
| 113 | + |
| 114 | +if [ -z "$NODEOS_RUNNING" ]; then |
| 115 | + echo "Starting the chain for setup" |
| 116 | + nodeos -e -p eosio \ |
| 117 | + --data-dir $BLOCKCHAIN_DATA_DIR \ |
| 118 | + --config-dir $BLOCKCHAIN_CONFIG_DIR \ |
| 119 | + --http-validate-host=false \ |
| 120 | + --plugin eosio::producer_api_plugin \ |
| 121 | + --plugin eosio::chain_api_plugin \ |
| 122 | + --plugin eosio::http_plugin \ |
| 123 | + --http-server-address=0.0.0.0:8888 \ |
| 124 | + --access-control-allow-origin=* \ |
| 125 | + --contracts-console \ |
| 126 | + --max-transaction-time=100000 \ |
| 127 | + --verbose-http-errors & |
| 128 | +fi |
| 129 | + |
| 130 | +mkdir -p "$CONFIG_DIR"/keys |
| 131 | + |
| 132 | +sleep 1s |
| 133 | + |
| 134 | +echo "Waiting for the chain to finish startup" |
| 135 | +until curl localhost:8888/v1/chain/get_info |
| 136 | +do |
| 137 | + echo "Still waiting" |
| 138 | + sleep 1s |
| 139 | +done |
| 140 | + |
| 141 | +# Sleep for 2s to allow time for 4 blocks to be created so we have blocks to reference when sending transactions |
| 142 | +sleep 2s |
| 143 | +echo "Creating accounts and deploying contracts" |
| 144 | + |
| 145 | +start_wallet |
| 146 | + |
| 147 | +# preactivate concensus upgrades |
| 148 | +post_preactivate |
| 149 | + |
| 150 | +sleep 1s |
| 151 | +cleos wallet unlock --password $(cat "$CONFIG_DIR"/keys/default_wallet_password.txt) || true |
| 152 | +setabi eosio $CONTRACTS_DIR/boot/boot.abi |
| 153 | +setcode eosio $CONTRACTS_DIR/boot/boot.wasm |
| 154 | +sleep 2s |
| 155 | +cleos push action eosio boot "[]" -p eosio@active |
| 156 | + |
| 157 | +sleep 1s |
| 158 | +cleos wallet unlock --password $(cat "$CONFIG_DIR"/keys/default_wallet_password.txt) || true |
| 159 | +setcode eosio $CONTRACTS_DIR/system/system.wasm |
| 160 | +setabi eosio $CONTRACTS_DIR/system/system.abi |
| 161 | + |
| 162 | +# token |
| 163 | +sleep 1s |
| 164 | +cleos wallet unlock --password $(cat "$CONFIG_DIR"/keys/default_wallet_password.txt) || true |
| 165 | +create_account eosio.token $SYSTEM_ACCOUNT_PUBLIC_KEY $SYSTEM_ACCOUNT_PRIVATE_KEY |
| 166 | +create_account bob $EXAMPLE_ACCOUNT_PUBLIC_KEY $EXAMPLE_ACCOUNT_PRIVATE_KEY |
| 167 | +create_account alice $EXAMPLE_ACCOUNT_PUBLIC_KEY $EXAMPLE_ACCOUNT_PRIVATE_KEY |
| 168 | +create_account bobr1 $R1_EXAMPLE_ACCOUNT_PUBLIC_KEY $R1_EXAMPLE_ACCOUNT_PRIVATE_KEY |
| 169 | +create_account alicer1 $R1_EXAMPLE_ACCOUNT_PUBLIC_KEY $R1_EXAMPLE_ACCOUNT_PRIVATE_KEY |
| 170 | + |
| 171 | +sleep 1s |
| 172 | +cleos set abi eosio.token $CONTRACTS_DIR/token/token.abi -p eosio.token@active -p eosio@active |
| 173 | +cleos set code eosio.token $CONTRACTS_DIR/token/token.wasm -p eosio.token@active -p eosio@active |
| 174 | + |
| 175 | +cleos push action eosio.token create '["bob", "10000000000.0000 SYS"]' -p eosio.token |
| 176 | +cleos push action eosio.token issue '["bob", "5000000000.0000 SYS", "Half of available supply"]' -p bob |
| 177 | +cleos push action eosio.token transfer '["bob", "alice", "1000000.0000 SYS", "memo"]' -p bob |
| 178 | +cleos push action eosio.token transfer '["bob", "bobr1", "1000000.0000 SYS", "memo"]' -p bob |
| 179 | +cleos push action eosio.token transfer '["bob", "alicer1", "1000000.0000 SYS", "memo"]' -p bob |
| 180 | + |
| 181 | +cleos push action eosio init "[]" -p eosio@active |
| 182 | + |
| 183 | +echo "All done initializing the blockchain" |
| 184 | + |
| 185 | +if [[ -z $NODEOS_RUNNING ]]; then |
| 186 | + echo "Shut down Nodeos, sleeping for 2 seconds to allow time for at least 4 blocks to be created after deploying contracts" |
| 187 | + sleep 2s |
| 188 | + kill %1 |
| 189 | + fg %1 |
| 190 | +fi |
0 commit comments