-
Notifications
You must be signed in to change notification settings - Fork 4
/
blockchain
executable file
·170 lines (141 loc) · 5.36 KB
/
blockchain
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/bin/bash
function join_by { local IFS="$1"; shift; echo "$*"; }
VERBOSITY=3
ACCOUNT_PASSWORDS=(account0 account1 account2)
PASSWORD_FILE=res/password.pass
DATA_DIR=`pwd`/.geth_data/
GETH_PARAMS=(--identity "FincontractsTestNet" --maxpeers 0 --rpc --rpcport "8545" --rpccorsdomain "*" --datadir $DATA_DIR --port "30303" --nodiscover --rpcapi "db,eth,net,web3,personal" --networkid 1900 --verbosity $VERBOSITY --nat "none")
MINE_PARAMS=(--mine --minerthreads 1)
UNLOCK_PARAMS=(--unlock $(join_by , ${!ACCOUNT_PASSWORDS[@]}) --password res/password.pass)
GENESISFILE=res/genesis.json
LOGFILE=${DATA_DIR}/geth.log
SOLC_HELPER=contracts/solc_helper/solc_helper
CONTRACT_BIN=contracts/bin
PROGRAMNAME=$0
function usage {
echo "USAGE:"
echo -e "\t$PROGRAMNAME command"
echo
echo "COMMANDS:"
echo
echo -e "\t setup \t Sets up private blockchain chain, includes 'deploy' command"
echo -e "\t deploy \t Compiles and deploys contracts "
echo -e "\t start \t Starts geth node in the background"
echo -e "\t restart \t Restarts geth node"
echo -e "\t stop \t Stops geth node"
echo -e "\t attach \t Attaches to the running geth node"
echo -e "\t help \t Displays this message"
echo
exit 1
}
function setup {
killall geth
echo "Removing old blockchain data"
rm -rf ${DATA_DIR}
echo "Setting up test blockchain"
mkdir -p ${DATA_DIR}
make_accounts
compile_contracts
}
function make_accounts {
rm -f $PASSWORD_FILE
cp $GENESISFILE $GENESISFILE.tmp
for password in ${ACCOUNT_PASSWORDS[@]}; do
echo "Creating an account with password:" $password
echo $password > ${password}.tmp
echo $password >> $PASSWORD_FILE
ACCOUNT="0x$(geth --datadir $DATA_DIR --password ${password}.tmp account new | cut -d "{" -f2 | cut -d "}" -f1)"
rm ${password}.tmp
echo "New account created:" $ACCOUNT
echo -e "\tAllocating ether..."
jq '.alloc += { '\"${ACCOUNT}\"' : { "balance": "20000000000000000000" } }' $GENESISFILE.tmp > tmp && mv tmp $GENESISFILE.tmp
done
geth "${GETH_PARAMS[@]}" init $GENESISFILE.tmp
rm $GENESISFILE.tmp
}
function compile_contract {
CONTRACT_NAME=$1
CONTRACT_SRC=$2
OUTPUT_DEPLOYMENT_SCRIPT=$3
$SOLC_HELPER \
--contract $CONTRACT_NAME \
--gas 3000000 \
--value 0 \
--output $OUTPUT_DEPLOYMENT_SCRIPT \
$CONTRACT_SRC
}
function compile_contracts {
mkdir -p $CONTRACT_BIN
rm -r $CONTRACT_BIN/*
echo "Compiling contract..."
CONTRACT_SRC=contracts/fincontracts/marketplace.sol
compile_contract Gateway $CONTRACT_SRC $CONTRACT_BIN/gateway_deploy.js
compile_contract GatewayTrue $CONTRACT_SRC $CONTRACT_BIN/gatewaytrue_deploy.js
compile_contract GatewayFalse $CONTRACT_SRC $CONTRACT_BIN/gatewayfalse_deploy.js
compile_contract GatewayBool $CONTRACT_SRC $CONTRACT_BIN/gatewaybool_deploy.js
compile_contract GatewayInteger $CONTRACT_SRC $CONTRACT_BIN/gatewayint_deploy.js
compile_contract FincontractMarketplace $CONTRACT_SRC $CONTRACT_BIN/marketplace_deploy.js
}
function start {
echo "Starting geth in background"
nohup geth "${GETH_PARAMS[@]}" "${MINE_PARAMS[@]}" "${UNLOCK_PARAMS[@]}" &> $LOGFILE&
}
function create_instantiation_script {
CONTRACT=$(basename $1 | cut -d_ -f1 )
CONTRACT_ABI=${CONTRACT_BIN}/${CONTRACT}_deploy_abi.js
CONTRACT_ABI_VAR=$(cat $CONTRACT_ABI | grep -o "\w*Compiled\w*")
CONTRACT_INST_VAR=$(echo ${CONTRACT_ABI_VAR} | sed 's/Compiled*//')
CONTRACT_ABI_VAR=${CONTRACT_ABI_VAR}.${CONTRACT_INST_VAR}.abi
CONTRACT_INST_SCRIPT=${CONTRACT_BIN}/${CONTRACT}_inst.js
CONTRACT_INST_MODULE=${CONTRACT_BIN}/${CONTRACT}.js
CONTRACT_INST_SCRIPT_CODE="var ${CONTRACT_INST_VAR} = eth.contract(${CONTRACT_ABI_VAR})"
CONTRACT_ABI="{$(cat ${CONTRACT_ABI} | cut -d{ -f2- | cut -d";" -f1)"
CONTRACT_ABI="$(echo ${CONTRACT_ABI} | jq ".${CONTRACT_INST_VAR}.abi")"
CONTRACT_INST_VAR="${CONTRACT_INST_VAR} = exports.${CONTRACT_INST_VAR}"
CONTRACT_INST_MODULE_CODE="var ${CONTRACT_INST_VAR} = function(web3){return web3.eth.contract(${CONTRACT_ABI})"
if [ ! -z ${2} ]; then
CONTRACT_INST_SCRIPT_CODE="${CONTRACT_INST_SCRIPT_CODE}.at(\"${2}\")"
CONTRACT_INST_MODULE_CODE="${CONTRACT_INST_MODULE_CODE}.at(\"${2}\")"
fi
echo "${CONTRACT_INST_SCRIPT_CODE}" > $CONTRACT_INST_SCRIPT
echo "${CONTRACT_INST_MODULE_CODE};};" > $CONTRACT_INST_MODULE
echo " Contract instantiation script created at ${CONTRACT_INST_SCRIPT}"
echo " Contract instantiation module created at ${CONTRACT_INST_MODULE}"
}
function deploy {
compile_contracts
echo "Deploying all contracts from $CONTRACT_BIN"
for FILE in $CONTRACT_BIN/*_deploy.js; do
echo -e "Deploying $FILE"
ADDRESS=$(geth "${GETH_PARAMS[@]}" "${MINE_PARAMS[@]}" "${UNLOCK_PARAMS[@]}" \
js $FILE 2>$LOGFILE | grep "Contract mined! Address" | cut -d: -f2
)
if [ -z $ADDRESS ]; then
echo " Deploying failed. Is node NOT running and init?";
else
echo " Address is" $ADDRESS
fi
create_instantiation_script $FILE $ADDRESS
done
}
if [[ $1 == "setup" ]]; then
setup
deploy
start
elif [[ $1 == "attach" ]]; then
echo "Attaching to session..."
geth "${GETH_PARAMS[@]}" attach ipc://$DATA_DIR/geth.ipc
elif [[ $1 == "start" ]]; then
start
elif [[ $1 == "restart" ]]; then
killall geth && start
elif [[ $1 == "stop" ]]; then
killall geth
elif [[ $1 == "deploy" ]]; then
killall geth
deploy
elif [[ $1 == "help" ]]; then
usage
else
usage
fi