Skip to content

Commit

Permalink
Merge pull request #374 from ava-labs/chain-config-per-node
Browse files Browse the repository at this point in the history
Chain config per node
  • Loading branch information
felipemadero authored Dec 20, 2022
2 parents a6fa34a + 583c5b6 commit e4312c3
Show file tree
Hide file tree
Showing 6 changed files with 426 additions and 392 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,27 @@ curl -X POST -k http://localhost:8081/v1/control/createblockchains -d '{"pluginD
avalanche-network-runner control create-blockchains '[{"vm_name":"'$VM_NAME'","genesis":"'$GENESIS_PATH'", "subnet_id": "'$SUBNET_ID'", "chain_config": "'$CHAIN_CONFIG_PATH'", "network_upgrade": "'$NETWORK_UPGRADE_PATH'", "subnet_config": "'$SUBNET_CONFIG_PATH'"}]' --plugin-dir $PLUGIN_DIR
```
Chain config can also be defined on a per node basis. For that, a per node chain config file is needed, which is a JSON that specifies the chain config per node. For example, given the following as the contents of the file with path `$PER_NODE_CHAIN_CONFIG`:
```json
{
"node1": {"rpc-tx-fee-cap": 101},
"node2": {"rpc-tx-fee-cap": 102},
"node3": {"rpc-tx-fee-cap": 103},
"node4": {"rpc-tx-fee-cap": 104},
"node5": {"rpc-tx-fee-cap": 105}
}
```
Then a blockchain with different chain configs per node can be created with this command:
```bash
curl -X POST -k http://localhost:8081/v1/control/createblockchains -d '{"pluginDir":"'$PLUGIN_DIR'","blockchainSpecs":[{"vm_name":"'$VM_NAME'","genesis":"'$GENESIS_PATH'", "subnet_id": "'$SUBNET_ID'", "per_node_chain_config": "'$PER_NODE_CHAIN_CONFIG'", "network_upgrade": "'$NETWORK_UPGRADE_PATH'", "subnet_config": "'$SUBNET_CONFIG_PATH'"}]}'
# or
avalanche-network-runner control create-blockchains '[{"vm_name":"'$VM_NAME'","genesis":"'$GENESIS_PATH'", "subnet_id": "'$SUBNET_ID'", "per_node_chain_config": "'$PER_NODE_CHAIN_CONFIG'", "network_upgrade": "'$NETWORK_UPGRADE_PATH'", "subnet_config": "'$SUBNET_CONFIG_PATH'"}]' --plugin-dir $PLUGIN_DIR
```
To remove (stop) a node:
```bash
Expand Down
57 changes: 13 additions & 44 deletions local/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,8 @@ func (ln *localNetwork) installCustomChains(
if err != nil {
return nil, err
}
blockchainFilesCreated, err := ln.createBlockchainConfigFiles(chainSpecs, blockchainTxs, ln.log)
if err != nil {
return nil, err
}

blockchainFilesCreated := ln.setBlockchainConfigFiles(chainSpecs, blockchainTxs, ln.log)

if numSubnetsToCreate > 0 || blockchainFilesCreated {
// we need to restart if there are new subnets or if there are new network config files
Expand Down Expand Up @@ -771,73 +769,44 @@ func createBlockchainTxs(
return blockchainTxs, nil
}

func (ln *localNetwork) createBlockchainConfigFiles(
func (ln *localNetwork) setBlockchainConfigFiles(
chainSpecs []network.BlockchainSpec,
blockchainTxs []*txs.Tx,
log logging.Logger,
) (bool, error) {
) bool {
fmt.Println()
created := false
log.Info(logging.Green.Wrap("creating config files for each custom chain"))
for i, chainSpec := range chainSpecs {
chainAlias := blockchainTxs[i].ID().String()

// create config, network upgrade and subnet config files
if chainSpec.ChainConfig != nil {
// update config info. set defaults and node specifics
if chainSpec.ChainConfig != nil || len(chainSpec.PerNodeChainConfig) != 0 {
created = true
ln.chainConfigFiles[chainAlias] = string(chainSpec.ChainConfig)
for nodeName := range ln.nodes {
nodeRootDir := getNodeDir(ln.rootDir, nodeName)
chainConfigDir := filepath.Join(nodeRootDir, chainConfigSubDir)
chainConfigPath := filepath.Join(chainConfigDir, chainAlias, configFileName)
if err := createFileAndWrite(chainConfigPath, chainSpec.ChainConfig); err != nil {
return false, fmt.Errorf("couldn't write chain config file at %q: %w", chainConfigPath, err)
if cfg, ok := chainSpec.PerNodeChainConfig[nodeName]; ok {
ln.nodes[nodeName].config.ChainConfigFiles[chainAlias] = string(cfg)
} else {
delete(ln.nodes[nodeName].config.ChainConfigFiles, chainAlias)
}
}
}
if chainSpec.NetworkUpgrade != nil {
created = true
for nodeName := range ln.nodes {
nodeRootDir := getNodeDir(ln.rootDir, nodeName)
chainConfigDir := filepath.Join(nodeRootDir, chainConfigSubDir)
chainUpgradePath := filepath.Join(chainConfigDir, chainAlias, upgradeConfigFileName)
if err := createFileAndWrite(chainUpgradePath, chainSpec.NetworkUpgrade); err != nil {
return false, fmt.Errorf("couldn't write network upgrade file at %q: %w", chainUpgradePath, err)
}
}
}
if chainSpec.SubnetConfig != nil {
created = true
for nodeName := range ln.nodes {
nodeRootDir := getNodeDir(ln.rootDir, nodeName)
subnetConfigDir := filepath.Join(nodeRootDir, subnetConfigSubDir)
subnetConfigPath := filepath.Join(subnetConfigDir, *chainSpec.SubnetID+".json")
if err := createFileAndWrite(subnetConfigPath, chainSpec.SubnetConfig); err != nil {
return false, fmt.Errorf("couldn't write chain config file at %q: %w", subnetConfigPath, err)
}
}
}
// update config info for snapshopt/restart purposes
// put into defaults and reset node specifics
if chainSpec.ChainConfig != nil {
ln.chainConfigFiles[chainAlias] = string(chainSpec.ChainConfig)
for nodeName := range ln.nodes {
delete(ln.nodes[nodeName].config.ChainConfigFiles, chainAlias)
}
}
if chainSpec.NetworkUpgrade != nil {
ln.upgradeConfigFiles[chainAlias] = string(chainSpec.NetworkUpgrade)
for nodeName := range ln.nodes {
delete(ln.nodes[nodeName].config.UpgradeConfigFiles, chainAlias)
}
}
if chainSpec.SubnetConfig != nil {
created = true
ln.subnetConfigFiles[*chainSpec.SubnetID] = string(chainSpec.SubnetConfig)
for nodeName := range ln.nodes {
delete(ln.nodes[nodeName].config.SubnetConfigFiles, *chainSpec.SubnetID)
}
}
}
return created, nil
return created
}

func (*localNetwork) createBlockchains(
Expand Down
15 changes: 8 additions & 7 deletions network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ var (
)

type BlockchainSpec struct {
VMName string
Genesis []byte
SubnetID *string
ChainConfig []byte
NetworkUpgrade []byte
SubnetConfig []byte
BlockchainAlias string
VMName string
Genesis []byte
SubnetID *string
ChainConfig []byte
NetworkUpgrade []byte
SubnetConfig []byte
BlockchainAlias string
PerNodeChainConfig map[string][]byte
}

// Network is an abstraction of an Avalanche network
Expand Down
Loading

0 comments on commit e4312c3

Please sign in to comment.