Skip to content

Commit

Permalink
fix node killing and start operator flow + cleanup scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
marc-aurele-besner committed Jun 10, 2024
1 parent dd74bf4 commit 985a657
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 69 deletions.
19 changes: 4 additions & 15 deletions scripts/download.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ TAG="latest" # "tags/gemini-3h-2024-may-06" # Tag of the release to download or
DOWNLOAD_DIR="downloads"
EXECUTABLE_DIR="executables"

# Delete the executable directory
rm -r "$EXECUTABLE_DIR"

# Create directories if they do not exist
mkdir -p "$DOWNLOAD_DIR"
mkdir -p "$EXECUTABLE_DIR"
Expand Down Expand Up @@ -51,18 +54,4 @@ chmod +x "$EXECUTABLE_DIR/farmer"
# Delete the downloads directory
rmdir "$DOWNLOAD_DIR"

echo "Downloaded and unzipped the latest node and farmer assets."

# # Run node in the background
# echo "Running node in the background..."
# ./executables/node run --dev --farmer --timekeeper --base-path executables/node-temp --name "localhost-node" --rpc-rate-limit 1000 --rpc-max-connections 10000 --state-pruning archive-canonical --blocks-pruning 512 --rpc-cors all --force-synced --force-authoring -- --domain-id 1 --operator-id 1 --state-pruning archive-canonical --blocks-pruning 512 --rpc-cors all &

# # Wait for 10 seconds before starting farmer
# echo "Waiting for 10 seconds before starting farmer..."
# sleep 10

# # Run farmer
# echo "Running farmer in the background..."
# ./executables/farmer farm path=executables/farmer-temp,size=1GiB --reward-address 5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY --node-rpc-url ws://127.0.0.1:9944 &

# echo "Both node and farmer are running in parallel."
echo "Downloaded and unzipped the latest node and farmer assets."
99 changes: 47 additions & 52 deletions scripts/run-dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,87 +5,81 @@ const { u8aToHex } = require('@polkadot/util')
const { createType } = require('@polkadot/types')

let runner = {
download: null,
node: null,
farmer: null,
}
let operatorRegistered = false

function downloadNodeAndFarmer() {
console.log('First lets download the node and farmer')
const download = spawn('bash', ['scripts/download.sh'])

download.stdout.on('data', (data) => {
runner.download = spawn('bash', ['scripts/download.sh'], { detached: true })
runner.download.stdout.on('data', (data) => {
const message = data.toString()
console.log('\x1b[33m', 'Download: ', '\x1b[0m', message)

if (message.includes('Downloaded and unzipped the latest node and farmer assets.'))
runner.node = runSimpleNode()
})
download.stderr.on('data', (data) => console.error(`First script error: ${data}`))
download.on('close', (code) => console.log(`First script exited with code ${code}`))
return download
runner.download.stderr.on('data', (data) => {
const message = data.toString()
if (message.includes('%') || message.includes('--:--') || message.includes('Time Current'))
console.log('\x1b[33m', 'Download: ', '\x1b[0m', message)
else console.error('\x1b[31m', 'Download error: ', '\x1b[0m', message)
})
runner.download.on('close', (code) => {
console.log(`First script exited with code ${code}`)
if (code === 0) runSimpleNode()
else console.error('\x1b[31m', 'Download script failed with code: ', '\x1b[0m', code)
})
}

function runSimpleNode() {
console.log('Now lets start a simple node.')
const node = spawn('bash', ['scripts/run-node.sh'])
let farmerStarted = false

node.stdout.on('data', (data) => {
runner.node = spawn('bash', ['scripts/run-node.sh'], { detached: true })
runner.node.stdout.on('data', (data) => {
const message = data.toString()
console.log('\x1b[36m', 'Node: ', '\x1b[0m', message)

if (!farmerStarted && message.includes('Idle (0 peers)')) {
farmerStarted = true
runner.farmer = runFarmer()
}
if (runner.farmer === null && message.includes('Idle (0 peers)')) runFarmer()
})
node.stderr.on('data', (data) => console.error('\x1b[31m', 'Node error: ', '\x1b[0m', data))
node.on('close', (code) => console.log('\x1b[31m', 'Node exited with code: ', '\x1b[0m', code))
return node
runner.node.stderr.on('data', (data) =>
console.error('\x1b[31m', 'Node error: ', '\x1b[0m', data.toString()),
)
runner.node.on('close', (code) =>
console.log('\x1b[31m', 'Node exited with code: ', '\x1b[0m', code),
)
}

function runFarmer() {
console.log('Now lets start the farmer.')
const farmer = spawn('bash', ['scripts/run-farmer.sh'])

farmer.stdout.on('data', (data) => {
runner.farmer = spawn('bash', ['scripts/run-farmer.sh'], { detached: true })
runner.farmer.stdout.on('data', (data) => {
const message = data.toString()
console.log('\x1b[35m', 'Farmer: ', '\x1b[0m', message)

if (!operatorRegistered && message.includes('Successfully signed reward hash')) {
operatorRegistered = true
registerOperator()
}
})
farmer.stderr.on('data', (data) => console.error('\x1b[31m', 'Farmer error: ', '\x1b[0m', data))
farmer.on('close', (code) =>
runner.farmer.stderr.on('data', (data) =>
console.error('\x1b[31m', 'Farmer error: ', '\x1b[0m', data.toString()),
)
runner.farmer.on('close', (code) =>
console.log('\x1b[31m', 'Farmer exited with code: ', '\x1b[0m', code),
)
return farmer
}

function runOperatorNode() {
console.log('Now lets start a operator node.')
const operator = spawn('bash', ['scripts/run-operator.sh'])
let farmerStarted = false

operator.stdout.on('data', (data) => {
runner.operator = spawn('bash', ['scripts/run-operator.sh'])
runner.operator.stdout.on('data', (data) => {
const message = data.toString()
console.log('\x1b[32m', 'Operator: ', '\x1b[0m', message)

if (!farmerStarted && message.includes('Idle (0 peers)')) {
farmerStarted = true
runner.farmer = runFarmer()
}
if (runner.farmer === null && message.includes('Idle (0 peers)')) runFarmer()
})
operator.stderr.on('data', (data) =>
console.error('\x1b[31m', 'Operator error: ', '\x1b[0m', data),
runner.operator.stderr.on('data', (data) =>
console.error('\x1b[31m', 'Operator error: ', '\x1b[0m', data.toString()),
)
operator.on('close', (code) =>
runner.operator.on('close', (code) =>
console.log('\x1b[31m', 'Operator exited with code: ', '\x1b[0m', code),
)
return operator
}

async function registerOperator() {
Expand All @@ -95,13 +89,12 @@ async function registerOperator() {
const files = await readdir(keystorePath)
if (files.length === 0) throw new Error('No files found in keystore directory.')

// Read the contents of the first file in the directory
// Read the contents of the first file in the directory and extract and clean the seed
const seedFile = await readFile(`${keystorePath}/${files[0]}`, 'utf-8')
const seed = seedFile.trim().replace(/"/g, '') // Extract and clean the seed
const seed = seedFile.trim().replace(/"/g, '')

// Create the provider
// Create the provider and the API instance
const provider = new WsProvider('ws://127.0.0.1:9944/ws')
// Create the API instance
const api = await ApiPromise.create({ provider })

const AliceKeyring = new Keyring({ type: 'sr25519' })
Expand All @@ -110,9 +103,8 @@ async function registerOperator() {
const Alice = AliceKeyring.addFromUri('//Alice')
const Operator = OperatorKeyring.addFromUri(seed)

const message = createType(api.registry, 'AccountId', Alice.address)
const signingKey = u8aToHex(Operator.publicKey)
const signature = Operator.sign(message.toU8a())
const signature = Operator.sign(createType(api.registry, 'AccountId', Alice.address).toU8a())

const tx = await api.tx.domains.registerOperator(
'0',
Expand All @@ -138,14 +130,18 @@ async function registerOperator() {
// Wait for 12 seconds before killing the node and farmer to make sure the operator is registered
setTimeout(() => {
console.log('\x1b[33m', 'Registering operator: ', '\x1b[0m', 'Killing node and farmer.')
runner.node.kill()
runner.farmer.kill()

process.kill(-runner.node.pid)
process.kill(-runner.farmer.pid)
runner.node = null
runner.farmer = null

console.log('\x1b[33m', 'Registering operator: ', '\x1b[0m', 'Node and farmer killed.')

// Wait for 2 seconds before starting the operator node
setTimeout(() => {
runner.node = runOperatorNode()
}, 2000)
}, 5000)
}, 12000)

resolve()
Expand All @@ -157,9 +153,8 @@ async function registerOperator() {
) {
console.log('\x1b[31m', 'Registering operator: ', '\x1b[0m', 'Transaction failed')
reject(new Error('Transaction failed'))
} else {
} else
console.log('\x1b[33m', 'Registering operator: ', '\x1b[0m', 'Status of tx: ' + status.type)
}
})
})

Expand Down
2 changes: 0 additions & 2 deletions scripts/run-operator.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

echo -e "${YELLOW}Create keystore...${NC}\n"

# Instructions for setting variables
echo -e "Using base-path: ${GREEN}$BASE_PATH${NC}"
echo -e "Using domain-id: ${GREEN}$DOMAIN_ID${NC}\n"
Expand Down

0 comments on commit 985a657

Please sign in to comment.