Skip to content

Commit

Permalink
move repeated vm check and file reading into func
Browse files Browse the repository at this point in the history
  • Loading branch information
felipemadero committed Oct 22, 2022
1 parent 333f897 commit a619539
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 48 deletions.
8 changes: 5 additions & 3 deletions network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ var (
)

type BlockchainSpec struct {
VmName string
Genesis []byte
SubnetId *string
VmName string
Genesis []byte
SubnetId *string
ChainConfig []byte
NetworkUpgrade []byte
}

// Network is an abstraction of an Avalanche network
Expand Down
99 changes: 54 additions & 45 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,33 +262,12 @@ func (s *server) Start(ctx context.Context, req *rpcpb.StartRequest) (*rpcpb.Sta
chainSpecs := []network.BlockchainSpec{}
if len(req.GetBlockchainSpecs()) > 0 {
s.log.Info("plugin-dir:", zap.String("plugin-dir", pluginDir))
for i := range req.GetBlockchainSpecs() {
spec := req.GetBlockchainSpecs()[i]
if spec.SubnetId != nil {
return nil, errors.New("blockchain subnet id must be nil if starting a new empty network")
}
vmName := spec.VmName
vmGenesisFilePath := spec.Genesis
s.log.Info("checking custom chain's VM ID before installation", zap.String("id", vmName))
vmID, err := utils.VMID(vmName)
if err != nil {
s.log.Warn("failed to convert VM name to VM ID", zap.String("vm-name", vmName), zap.Error(err))
return nil, ErrInvalidVMName
}
if err := utils.CheckPluginPaths(
filepath.Join(pluginDir, vmID.String()),
vmGenesisFilePath,
); err != nil {
return nil, err
}
b, err := os.ReadFile(vmGenesisFilePath)
for _, spec := range req.GetBlockchainSpecs() {
chainSpec, err := getNetworkBlockchainSpec(spec, true, pluginDir)
if err != nil {
return nil, err
}
chainSpecs = append(chainSpecs, network.BlockchainSpec{
VmName: vmName,
Genesis: b,
})
chainSpecs = append(chainSpecs, chainSpec)
}
}

Expand Down Expand Up @@ -432,6 +411,54 @@ func (s *server) waitChAndUpdateClusterInfo(waitMsg string, readyCh chan struct{
}
}

func getNetworkBlockchainSpec(
spec *rpcpb.BlockchainSpec,
isNewEmptyNetwork bool,
pluginDir string,
) (network.BlockchainSpec, error) {
if isNewEmptyNetwork && spec.SubnetId != nil {
return network.BlockchainSpec{}, errors.New("blockchain subnet id must be nil if starting a new empty network")
}
vmName := spec.VmName
s.log.Info("checking custom chain's VM ID before installation", zap.String("id", vmName))
vmID, err := utils.VMID(vmName)
if err != nil {
s.log.Warn("failed to convert VM name to VM ID", zap.String("vm-name", vmName), zap.Error(err))
return network.BlockchainSpec{}, ErrInvalidVMName
}
if err := utils.CheckPluginPaths(
filepath.Join(pluginDir, vmID.String()),
spec.Genesis,
); err != nil {
return network.BlockchainSpec{}, err
}
genesisBytes, err := os.ReadFile(spec.Genesis)
if err != nil {
return network.BlockchainSpec{}, err
}
var chainConfigBytes []byte
if spec.ChainConfig != "" {
chainConfigBytes, err = os.ReadFile(spec.ChainConfig)
if err != nil {
return network.BlockchainSpec{}, err
}
}
var networkUpgradeBytes []byte
if spec.NetworkUpgrade != "" {
networkUpgradeBytes, err = os.ReadFile(spec.NetworkUpgrade)
if err != nil {
return network.BlockchainSpec{}, err
}
}
return network.BlockchainSpec{
VmName: vmName,
Genesis: genesisBytes,
ChainConfig: chainConfigBytes,
NetworkUpgrade: networkUpgradeBytes,
SubnetId: spec.SubnetId,
}, nil
}

func (s *server) CreateBlockchains(ctx context.Context, req *rpcpb.CreateBlockchainsRequest) (*rpcpb.CreateBlockchainsResponse, error) {
// if timeout is too small or not set, default to 5-min
if deadline, ok := ctx.Deadline(); !ok || time.Until(deadline) < defaultStartTimeout {
Expand All @@ -453,30 +480,12 @@ func (s *server) CreateBlockchains(ctx context.Context, req *rpcpb.CreateBlockch
}

chainSpecs := []network.BlockchainSpec{}
for i := range req.GetBlockchainSpecs() {
vmName := req.GetBlockchainSpecs()[i].VmName
vmGenesisFilePath := req.GetBlockchainSpecs()[i].Genesis
s.log.Info("checking custom chain's VM ID before installation", zap.String("vm-id", vmName))
vmID, err := utils.VMID(vmName)
if err != nil {
s.log.Warn("failed to convert VM name to VM ID", zap.String("vm-name", vmName), zap.Error(err))
return nil, ErrInvalidVMName
}
if err := utils.CheckPluginPaths(
filepath.Join(s.network.pluginDir, vmID.String()),
vmGenesisFilePath,
); err != nil {
return nil, err
}
b, err := os.ReadFile(vmGenesisFilePath)
for _, spec := range req.GetBlockchainSpecs() {
chainSpec, err := getNetworkBlockchainSpec(spec, false, s.network.pluginDir)
if err != nil {
return nil, err
}
chainSpecs = append(chainSpecs, network.BlockchainSpec{
VmName: vmName,
Genesis: b,
SubnetId: req.GetBlockchainSpecs()[i].SubnetId,
})
chainSpecs = append(chainSpecs, chainSpec)
}

// check that defined subnets exist
Expand Down

0 comments on commit a619539

Please sign in to comment.