[#732] neofs-adm: get contract path on initialization

Simplifies code a bit.

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2021-07-29 16:23:36 +03:00 committed by Alex Vanin
parent 67b17cfb02
commit 6a40adcfca
5 changed files with 19 additions and 43 deletions

View file

@ -31,15 +31,10 @@ type initializeContext struct {
PollInterval time.Duration PollInterval time.Duration
Contracts map[string]*contractState Contracts map[string]*contractState
Command *cobra.Command Command *cobra.Command
ContractPath string
} }
func initializeSideChainCmd(cmd *cobra.Command, args []string) error { func initializeSideChainCmd(cmd *cobra.Command, args []string) error {
// contract path is not part of the config
contractsPath, err := cmd.Flags().GetString(contractsInitFlag)
if err != nil {
return err
}
initCtx, err := newInitializeContext(cmd, viper.GetViper()) initCtx, err := newInitializeContext(cmd, viper.GetViper())
if err != nil { if err != nil {
return fmt.Errorf("initialization error: %w", err) return fmt.Errorf("initialization error: %w", err)
@ -90,7 +85,7 @@ func initializeSideChainCmd(cmd *cobra.Command, args []string) error {
cmd.Println("endpoint:", viper.GetString(endpointFlag)) cmd.Println("endpoint:", viper.GetString(endpointFlag))
cmd.Println("alphabet-wallets:", viper.GetString(alphabetWalletsFlag)) cmd.Println("alphabet-wallets:", viper.GetString(alphabetWalletsFlag))
cmd.Println("contracts:", contractsPath) cmd.Println("contracts:", initCtx.ContractPath)
cmd.Println("epoch-duration:", viper.GetUint(epochDurationInitFlag)) cmd.Println("epoch-duration:", viper.GetUint(epochDurationInitFlag))
cmd.Println("max-object-size:", viper.GetUint(maxObjectSizeInitFlag)) cmd.Println("max-object-size:", viper.GetUint(maxObjectSizeInitFlag))
@ -129,6 +124,11 @@ func newInitializeContext(cmd *cobra.Command, v *viper.Viper) (*initializeContex
} }
} }
ctrPath, err := cmd.Flags().GetString(contractsInitFlag)
if err != nil {
return nil, fmt.Errorf("missing contracts path: %w", err)
}
initCtx := &initializeContext{ initCtx := &initializeContext{
Client: c, Client: c,
ConsensusAcc: consensusAcc, ConsensusAcc: consensusAcc,
@ -138,6 +138,7 @@ func newInitializeContext(cmd *cobra.Command, v *viper.Viper) (*initializeContex
PollInterval: time.Second, PollInterval: time.Second,
Command: cmd, Command: cmd,
Contracts: make(map[string]*contractState), Contracts: make(map[string]*contractState),
ContractPath: ctrPath,
} }
return initCtx, nil return initCtx, nil

View file

@ -71,12 +71,7 @@ type contractState struct {
} }
func (c *initializeContext) deployNNS() error { func (c *initializeContext) deployNNS() error {
ctrPath, err := c.Command.Flags().GetString(contractsInitFlag) cs, err := c.readContract(nnsContract)
if err != nil {
return fmt.Errorf("missing contracts path: %w", err)
}
cs, err := c.readContract(ctrPath, nnsContract)
if err != nil { if err != nil {
return err return err
} }
@ -114,15 +109,10 @@ func (c *initializeContext) deployNNS() error {
} }
func (c *initializeContext) deployContracts() error { func (c *initializeContext) deployContracts() error {
ctrPath, err := c.Command.Flags().GetString(contractsInitFlag)
if err != nil {
return fmt.Errorf("missing contracts path: %w", err)
}
mgmtHash, _ := c.Client.GetNativeContractHash(nativenames.Management) mgmtHash, _ := c.Client.GetNativeContractHash(nativenames.Management)
sender := c.CommitteeAcc.Contract.ScriptHash() sender := c.CommitteeAcc.Contract.ScriptHash()
for _, ctrName := range contractList { for _, ctrName := range contractList {
cs, err := c.readContract(ctrPath, ctrName) cs, err := c.readContract(ctrName)
if err != nil { if err != nil {
return err return err
} }
@ -138,7 +128,7 @@ func (c *initializeContext) deployContracts() error {
return err return err
} }
cs, err := c.readContract(ctrPath, alphabetContract) cs, err := c.readContract(alphabetContract)
if err != nil { if err != nil {
return err return err
} }
@ -202,12 +192,12 @@ func (c *initializeContext) deployContracts() error {
return c.awaitTx() return c.awaitTx()
} }
func (c *initializeContext) readContract(ctrPath, ctrName string) (*contractState, error) { func (c *initializeContext) readContract(ctrName string) (*contractState, error) {
if cs, ok := c.Contracts[ctrName]; ok { if cs, ok := c.Contracts[ctrName]; ok {
return cs, nil return cs, nil
} }
rawNef, err := ioutil.ReadFile(path.Join(ctrPath, ctrName, ctrName+"_contract.nef")) rawNef, err := ioutil.ReadFile(path.Join(c.ContractPath, ctrName, ctrName+"_contract.nef"))
if err != nil { if err != nil {
return nil, fmt.Errorf("can't read NEF file: %w", err) return nil, fmt.Errorf("can't read NEF file: %w", err)
} }
@ -215,7 +205,7 @@ func (c *initializeContext) readContract(ctrPath, ctrName string) (*contractStat
if err != nil { if err != nil {
return nil, fmt.Errorf("can't parse NEF file: %w", err) return nil, fmt.Errorf("can't parse NEF file: %w", err)
} }
rawManif, err := ioutil.ReadFile(path.Join(ctrPath, ctrName, "config.json")) rawManif, err := ioutil.ReadFile(path.Join(c.ContractPath, ctrName, "config.json"))
if err != nil { if err != nil {
return nil, fmt.Errorf("can't read manifest file: %w", err) return nil, fmt.Errorf("can't read manifest file: %w", err)
} }

View file

@ -21,12 +21,7 @@ const defaultNameServiceSysfee = 4000_0000
const defaultRegisterSysfee = 10_0000_0000 + defaultNameServiceDomainPrice const defaultRegisterSysfee = 10_0000_0000 + defaultNameServiceDomainPrice
func (c *initializeContext) setNNS() error { func (c *initializeContext) setNNS() error {
ctrPath, err := c.Command.Flags().GetString(contractsInitFlag) cs, err := c.readContract(nnsContract)
if err != nil {
return fmt.Errorf("missing contracts path: %w", err)
}
cs, err := c.readContract(ctrPath, nnsContract)
if err != nil { if err != nil {
return err return err
} }
@ -46,7 +41,7 @@ func (c *initializeContext) setNNS() error {
} }
} }
alphaCs, err := c.readContract(ctrPath, alphabetContract) alphaCs, err := c.readContract(alphabetContract)
if err != nil { if err != nil {
return fmt.Errorf("can't read alphabet contract: %w", err) return fmt.Errorf("can't read alphabet contract: %w", err)
} }
@ -79,7 +74,7 @@ func (c *initializeContext) setNNS() error {
} }
for _, ctrName := range contractList { for _, ctrName := range contractList {
cs, err := c.readContract(ctrPath, ctrName) cs, err := c.readContract(ctrName)
if err != nil { if err != nil {
return err return err
} }

View file

@ -82,12 +82,7 @@ func (c *initializeContext) transferNEOToAlphabetContracts() error {
return err return err
} }
ctrPath, err := c.Command.Flags().GetString(contractsInitFlag) cs, err := c.readContract(alphabetContract)
if err != nil {
return fmt.Errorf("missing contracts path: %w", err)
}
cs, err := c.readContract(ctrPath, alphabetContract)
if err != nil { if err != nil {
return fmt.Errorf("can't read alphabet contract: %w", err) return fmt.Errorf("can't read alphabet contract: %w", err)
} }

View file

@ -155,12 +155,7 @@ func (c *initializeContext) transferGASToProxy() error {
return fmt.Errorf("can't fetch %s hash: %w", nativenames.Gas, err) return fmt.Errorf("can't fetch %s hash: %w", nativenames.Gas, err)
} }
ctrPath, err := c.Command.Flags().GetString(contractsInitFlag) cs, err := c.readContract(proxyContract)
if err != nil {
return fmt.Errorf("missing contracts path: %w", err)
}
cs, err := c.readContract(ctrPath, proxyContract)
if err != nil { if err != nil {
return err return err
} }