Skip to content

Commit

Permalink
Configurable genesis balances (#1838)
Browse files Browse the repository at this point in the history
* Configurable genesis balances for val 0, other vals, other accounts

* Bad merge

* Better var name

* Fix up balances
  • Loading branch information
timmoreton authored Nov 23, 2019
1 parent 5bea6d3 commit a4efd0b
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 54 deletions.
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ ETHSTATS_DOCKER_IMAGE_TAG="cd037ea1e18848466452ba9890c1f1bcd3f61009"
ETHSTATS_TRUSTED_ADDRESSES="0x480b0e0A641AE45521377d4984d085a003934561,0xF523976B9FB2160e9a43D8Aee016b98ea8f57837"
ETHSTATS_BANNED_ADDRESSES="0xFd24A0699288E141A855bC8c0dd0400C5E89D5e5"

FAUCET_GENESIS_ACCOUNTS=2

GETH_NODE_DOCKER_IMAGE_REPOSITORY="us.gcr.io/celo-testnet/geth"
# When upgrading change this to latest commit hash from the master of the geth repo
# `geth $ git show | head -n 1`
Expand Down
2 changes: 2 additions & 0 deletions .env.alfajores
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ BLOCKSCOUT_WEB_REPLICAS=3
BLOCKSCOUT_DB_SUFFIX="7"
BLOCKSCOUT_SUBNETWORK_NAME="Alfajores"

FAUCET_GENESIS_ACCOUNTS=2

GETH_NODE_DOCKER_IMAGE_REPOSITORY="us.gcr.io/celo-testnet/geth"
# When upgrading change this to latest commit hash from the master of the geth repo
# `geth $ git show | head -n 1`
Expand Down
2 changes: 2 additions & 0 deletions .env.integration
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ BLOCKSCOUT_WEB_REPLICAS=3
BLOCKSCOUT_DB_SUFFIX="25"
BLOCKSCOUT_SUBNETWORK_NAME="Integration"

FAUCET_GENESIS_ACCOUNTS=2

GETH_NODE_DOCKER_IMAGE_REPOSITORY="us.gcr.io/celo-testnet/geth"
# When upgrading change this to latest commit hash from the master of the geth repo
# `geth $ git show | head -n 1`
Expand Down
5 changes: 5 additions & 0 deletions packages/celotool/src/lib/env-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ export enum envVar {
ETHSTATS_DOCKER_IMAGE_TAG = 'ETHSTATS_DOCKER_IMAGE_TAG',
ETHSTATS_TRUSTED_ADDRESSES = 'ETHSTATS_TRUSTED_ADDRESSES',
ETHSTATS_BANNED_ADDRESSES = 'ETHSTATS_BANNED_ADDRESSES',
FAUCET_GENESIS_ACCOUNTS = 'FAUCET_GENESIS_ACCOUNTS',
FAUCET_GENESIS_BALANCE = 'FAUCET_GENESIS_BALANCE',
ORACLE_GENESIS_BALANCE = 'ORACLE_GENESIS_BALANCE',
GETH_ACCOUNT_SECRET = 'GETH_ACCOUNT_SECRET',
GETH_BOOTNODE_DOCKER_IMAGE_REPOSITORY = 'GETH_BOOTNODE_DOCKER_IMAGE_REPOSITORY',
GETH_BOOTNODE_DOCKER_IMAGE_TAG = 'GETH_BOOTNODE_DOCKER_IMAGE_TAG',
Expand Down Expand Up @@ -71,6 +74,8 @@ export enum envVar {
TRANSACTION_METRICS_EXPORTER_DOCKER_IMAGE_REPOSITORY = 'TRANSACTION_METRICS_EXPORTER_DOCKER_IMAGE_REPOSITORY',
TRANSACTION_METRICS_EXPORTER_DOCKER_IMAGE_TAG = 'TRANSACTION_METRICS_EXPORTER_DOCKER_IMAGE_TAG',
TX_NODES = 'TX_NODES',
VALIDATOR_GENESIS_BALANCE = 'VALIDATOR_GENESIS_BALANCE',
VALIDATOR_ZERO_GENESIS_BALANCE = 'VALIDATOR_ZERO_GENESIS_BALANCE',
VALIDATORS = 'VALIDATORS',
VM_BASED = 'VM_BASED',
}
Expand Down
65 changes: 43 additions & 22 deletions packages/celotool/src/lib/generate_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
CONTRACT_OWNER_STORAGE_LOCATION,
GETH_CONFIG_OLD,
ISTANBUL_MIX_HASH,
OG_ACCOUNTS,
REGISTRY_ADDRESS,
TEMPLATE,
} from './genesis_constants'
Expand Down Expand Up @@ -39,6 +38,11 @@ export enum ConsensusType {
export interface Validator {
address: string
blsPublicKey: string
balance: string
}
export interface AccountAndBalance {
address: string
balance: string
}

export const MNEMONIC_ACCOUNT_TYPE_CHOICES = [
Expand Down Expand Up @@ -85,8 +89,15 @@ export const privateKeyToAddress = (privateKey: string) => {
export const privateKeyToStrippedAddress = (privateKey: string) =>
strip0x(privateKeyToAddress(privateKey))

const DEFAULT_BALANCE = '1000000000000000000000000'
const VALIDATOR_OG_SOURCE = 'og'
const validatorZeroBalance = fetchEnvOrFallback(
envVar.VALIDATOR_ZERO_GENESIS_BALANCE,
'100010011000000000000000000'
) // 100,010,011 CG
const validatorBalance = fetchEnvOrFallback(
envVar.VALIDATOR_GENESIS_BALANCE,
'10011000000000000000000'
) // 10,011 CG
const faucetBalance = fetchEnvOrFallback(envVar.FAUCET_GENESIS_BALANCE, '10011000000000000000000') // 10,000 CG

export const getPrivateKeysFor = (accountType: AccountType, mnemonic: string, n: number) =>
range(0, n).map((i) => generatePrivateKey(mnemonic, accountType, i))
Expand All @@ -103,6 +114,7 @@ export const getValidators = (mnemonic: string, n: number) => {
return {
address: strip0x(privateKeyToAddress(key)),
blsPublicKey: bls12377js.BLS.privateToPublicBytes(blsKeyBytes).toString('hex'),
balance: n === 0 ? validatorZeroBalance : validatorBalance,
}
})
}
Expand All @@ -116,16 +128,7 @@ export const getAddressFromEnv = (accountType: AccountType, n: number) => {
export const generateGenesisFromEnv = (enablePetersburg: boolean = true) => {
const mnemonic = fetchEnv(envVar.MNEMONIC)
const validatorEnv = fetchEnv(envVar.VALIDATORS)
const validators =
validatorEnv === VALIDATOR_OG_SOURCE
? OG_ACCOUNTS.map((account) => {
const blsKeyBytes = blsPrivateKeyToProcessedPrivateKey(account.privateKey)
return {
address: account.address,
blsPublicKey: bls12377js.BLS.privateToPublicBytes(blsKeyBytes).toString('hex'),
}
})
: getValidators(mnemonic, parseInt(validatorEnv, 10))
const validators = getValidators(mnemonic, parseInt(validatorEnv, 10))

const consensusType = fetchEnv(envVar.CONSENSUS_TYPE) as ConsensusType

Expand All @@ -144,16 +147,34 @@ export const generateGenesisFromEnv = (enablePetersburg: boolean = true) => {
const lookbackwindow = parseInt(fetchEnvOrFallback(envVar.LOOKBACK, '12'), 10)
const chainId = parseInt(fetchEnv(envVar.NETWORK_ID), 10)

// Assing DEFAULT ammount of gold to 2 faucet accounts
const faucetAddresses = getStrippedAddressesFor(AccountType.FAUCET, mnemonic, 2)
// Allocate faucet accounts
const numFaucetAccounts = parseInt(fetchEnvOrFallback(envVar.FAUCET_GENESIS_ACCOUNTS, '0'), 10)
const initialAccounts = getStrippedAddressesFor(
AccountType.FAUCET,
mnemonic,
numFaucetAccounts
).map((addr) => {
return {
address: addr,
balance: fetchEnvOrFallback(envVar.FAUCET_GENESIS_BALANCE, faucetBalance),
}
})

const oracleAddress = getStrippedAddressesFor(AccountType.PRICE_ORACLE, mnemonic, 1)
// Allocate oracle account(s)
initialAccounts.concat(
getStrippedAddressesFor(AccountType.PRICE_ORACLE, mnemonic, 1).map((addr) => {
return {
address: addr,
balance: fetchEnvOrFallback(envVar.ORACLE_GENESIS_BALANCE, '100000000000000000000'),
}
})
)

return generateGenesis({
validators,
consensusType,
blockTime,
initialAccounts: faucetAddresses.concat(oracleAddress),
initialAccounts,
epoch,
lookbackwindow,
chainId,
Expand Down Expand Up @@ -214,7 +235,7 @@ export const generateGenesis = ({
}: {
validators: Validator[]
consensusType?: ConsensusType
initialAccounts?: string[]
initialAccounts?: AccountAndBalance[]
blockTime: number
epoch: number
lookbackwindow: number
Expand Down Expand Up @@ -252,13 +273,13 @@ export const generateGenesis = ({

for (const validator of validators) {
genesis.alloc[validator.address] = {
balance: DEFAULT_BALANCE,
balance: validator.balance,
}
}

for (const address of otherAccounts) {
genesis.alloc[address] = {
balance: DEFAULT_BALANCE,
for (const account of otherAccounts) {
genesis.alloc[account.address] = {
balance: account.balance,
}
}

Expand Down
23 changes: 0 additions & 23 deletions packages/celotool/src/lib/genesis_constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,3 @@ export const CONTRACT_OWNER_STORAGE_LOCATION =
'0x34dc5a2556b2030988481969696f29fed38d45813d8003f6c70e5c16ac92ae0f'
export const ISTANBUL_MIX_HASH =
'0x63746963616c2062797a616e74696e65206661756c7420746f6c6572616e6365'

export const OG_ACCOUNTS = [
{
name: 'gethminer1',
privateKey: 'a2d2c843bb2c6b6aed146343b8aec7d23a7cc050f41c6217760d46095bfc49cd',
address: 'feE1a22F43BeeCB912B5a4912ba87527682ef0fC',
},
{
name: 'gethminer2',
privateKey: '898a61ff4d42360802c9897bc2df1298d2df9153cb761ca55b5dc1bb940f44dc',
address: '889F21CE69dcc25a4594f73230A55896d6703806',
},
{
name: 'gethminer3',
privateKey: '6005018fe530da09942a016921a185cabef0fbcc10e63fe2b45805b2957f6ec9',
address: '5372d2bbBaBaAf1495182E31cF13dB0d18463B0E',
},
{
name: 'gethminer4',
privateKey: '1310f2a9c32d52dbbabc28dbaa9ca4c5c826d59664320b597effa66155c72c61',
address: 'F71690ea7E0c67827d8968882FAC0c4cBBD65BCE',
},
]
10 changes: 1 addition & 9 deletions packages/celotool/src/lib/helm_deploy.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { entries, flatMap, range } from 'lodash'
import { entries, range } from 'lodash'
import sleep from 'sleep-promise'
import { getKubernetesClusterRegion, switchToClusterFromEnv } from './cluster'
import { EnvTypes, envVar, fetchEnv, fetchEnvOrFallback, isProduction } from './env-utils'
import { ensureAuthenticatedGcloudAccount } from './gcloud_utils'
import { generateGenesisFromEnv } from './generate_utils'
import { OG_ACCOUNTS } from './genesis_constants'
import { getStatefulSetReplicas, scaleResource } from './kubernetes'
import { execCmd, execCmdWithExitOnFailure, outputIncludes, switchToProjectFromEnv } from './utils'

Expand Down Expand Up @@ -507,12 +506,6 @@ async function helmParameters(celoEnv: string) {
]
: []

const gethAccountParameters = flatMap(OG_ACCOUNTS, (account) => [
`--set geth.account.${account.name}.name=${account.name}`,
`--set geth.account.${account.name}.privateKey=${account.privateKey}`,
`--set geth.account.${account.name}.address=${account.address}`,
])

return [
`--set domain.name=${fetchEnv('CLUSTER_DOMAIN_NAME')}`,
`--set geth.verbosity=${fetchEnvOrFallback('GETH_VERBOSITY', '4')}`,
Expand Down Expand Up @@ -554,7 +547,6 @@ async function helmParameters(celoEnv: string) {
)}`,
...productionTagOverrides,
...(await helmIPParameters(celoEnv)),
...gethAccountParameters,
]
}

Expand Down

0 comments on commit a4efd0b

Please sign in to comment.