#!/bin/bash
set -e
BINARY_ROOT_PATH=$1
NODE_NAME=$2
PORT=$3
RPC_PORT=$4
NODE_COUNT=$5

echo "start-node CHAIN: $CHAIN"
echo "start-node KEYS_DIR: $KEYS_DIR"
echo "start-node LOCALNET_INIT_DIR: $LOCALNET_INIT_DIR"
echo "start-node NODE_NAME: $NODE_NAME"
echo "start-node PORT: $PORT"
echo "start-node RPC_PORT: $RPC_PORT"
echo "start-node NODE_COUNT: $NODE_COUNT"

DATETIME=$(date '+%Y-%m-%d_%H-%M-%S')
source $LOCALNET_INIT_DIR/env/eth.env
source $LOCALNET_INIT_DIR/env/arb.env
source $LOCALNET_INIT_DIR/env/node.env
source $LOCALNET_INIT_DIR/env/$NODE_COUNT/eth-aggkey.env
source $LOCALNET_INIT_DIR/env/$NODE_COUNT/dot-aggkey.env

$BINARY_ROOT_PATH/chainflip-node key insert --chain=$CHAIN --base-path=/tmp/chainflip/$NODE_NAME/chaindata --suri=0x$(cat $KEYS_DIR/LP_API) --key-type=lqpr --scheme=sr25519
$BINARY_ROOT_PATH/chainflip-node key insert --chain=$CHAIN --base-path=/tmp/chainflip/$NODE_NAME/chaindata --suri=0x$(cat $KEYS_DIR/BROKER_1) --key-type=brok --scheme=sr25519
$BINARY_ROOT_PATH/chainflip-node key insert --chain=$CHAIN --base-path=/tmp/chainflip/$NODE_NAME/chaindata --suri=0x$(cat $KEYS_DIR/$NODE_NAME/signing_key_file) --key-type=aura --scheme=sr25519
$BINARY_ROOT_PATH/chainflip-node key insert --chain=$CHAIN --base-path=/tmp/chainflip/$NODE_NAME/chaindata --suri=0x$(cat $KEYS_DIR/$NODE_NAME/signing_key_file) --key-type=gran --scheme=ed25519
RUST_LOG=runtime=debug $BINARY_ROOT_PATH/chainflip-node --chain=$CHAIN \
  --base-path=/tmp/chainflip/$NODE_NAME/chaindata \
  --node-key-file=$KEYS_DIR/$NODE_NAME/node_key_file \
  --validator \
  --max-runtime-instances 32 \
  --force-authoring \
  --rpc-cors=all \
  --unsafe-rpc-external \
  --rpc-methods=unsafe \
  --name=$NODE_NAME \
  --port=$PORT \
  --rpc-port=$RPC_PORT \
  --rpc-max-connections=500 \
  --rpc-max-request-size 1000 \
  --rpc-max-response-size 1000 \
  --blocks-pruning=archive \
  --state-pruning=archive \
  --bootnodes=/ip4/127.0.0.1/tcp/30333/p2p/12D3KooWFD3YMDyQocSQBAx6VCcUruYihi6xYufduzf321FnvvoY \
  --no-mdns \
  --trie-cache-size=0 > /tmp/chainflip/$NODE_NAME/chainflip-node.$DATETIME.log 2>&1 &
