forked from TrueCloudLab/frostfs-node
[#686] neofs-adm: implement morph force-new-epoch
Allow to force epoch change. Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
parent
9c1fb0b55e
commit
b95c16879d
2 changed files with 73 additions and 9 deletions
|
@ -1,13 +1,75 @@
|
||||||
package morph
|
package morph
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
nns "github.com/nspcc-dev/neo-go/examples/nft-nd-nns"
|
||||||
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
||||||
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
||||||
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
||||||
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
|
||||||
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
||||||
|
"github.com/nspcc-dev/neo-go/pkg/vm"
|
||||||
|
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
func forceNewEpochCmd(cmd *cobra.Command, args []string) error {
|
func forceNewEpochCmd(cmd *cobra.Command, args []string) error {
|
||||||
cmd.Println("endpoint:", viper.GetString(endpointFlag))
|
wCtx, err := newInitializeContext(cmd, viper.GetViper())
|
||||||
cmd.Println("alphabet-wallets:", viper.GetString(alphabetWalletsFlag))
|
if err != nil {
|
||||||
|
return fmt.Errorf("can't to initialize context: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
cs, err := wCtx.Client.GetContractStateByID(1)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("can't get NNS contract info: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
res, err := wCtx.Client.InvokeFunction(cs.Hash, "resolve", []smartcontract.Parameter{
|
||||||
|
{Type: smartcontract.StringType, Value: netmapContract + ".neofs"},
|
||||||
|
{Type: smartcontract.IntegerType, Value: int64(nns.TXT)},
|
||||||
|
}, nil)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("can't get netmap contract hash: %w", err)
|
||||||
|
}
|
||||||
|
if len(res.Stack) == 0 {
|
||||||
|
return errors.New("empty response from NNS")
|
||||||
|
}
|
||||||
|
|
||||||
|
var nmHash util.Uint160
|
||||||
|
bs, err := res.Stack[0].TryBytes()
|
||||||
|
if err == nil {
|
||||||
|
nmHash, err = util.Uint160DecodeStringLE(string(bs))
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("invalid response from NNS contract: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
res, err = wCtx.Client.InvokeFunction(nmHash, "epoch", []smartcontract.Parameter{}, []transaction.Signer{{
|
||||||
|
Account: wCtx.CommitteeAcc.Contract.ScriptHash(),
|
||||||
|
Scopes: transaction.Global, // Scope is important, as we have nested call to container contract.
|
||||||
|
}})
|
||||||
|
if err != nil || res.State != vm.HaltState.String() || len(res.Stack) == 0 {
|
||||||
|
return errors.New("can't fetch current epoch from the netmap contract")
|
||||||
|
}
|
||||||
|
|
||||||
|
bi, err := res.Stack[0].TryInteger()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("can't parse current epoch: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
newEpoch := bi.Int64() + 1
|
||||||
|
cmd.Printf("Current epoch: %s, increase to %d.\n", bi, newEpoch)
|
||||||
|
|
||||||
|
// In NeoFS this is done via Notary contract. Here, however, we can form the
|
||||||
|
// transaction locally.
|
||||||
|
bw := io.NewBufBinWriter()
|
||||||
|
emit.AppCall(bw.BinWriter, nmHash, "newEpoch", callflag.All, newEpoch)
|
||||||
|
if err := wCtx.sendCommitteeTx(bw.Bytes(), -1); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return wCtx.awaitTx()
|
||||||
}
|
}
|
||||||
|
|
|
@ -119,12 +119,14 @@ func newInitializeContext(cmd *cobra.Command, v *viper.Viper) (*initializeContex
|
||||||
return nil, fmt.Errorf("can't find consensus account: %w", err)
|
return nil, fmt.Errorf("can't find consensus account: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if viper.GetInt64(epochDurationInitFlag) <= 0 {
|
if cmd.Name() == "init" {
|
||||||
return nil, fmt.Errorf("epoch duration must be positive")
|
if viper.GetInt64(epochDurationInitFlag) <= 0 {
|
||||||
}
|
return nil, fmt.Errorf("epoch duration must be positive")
|
||||||
|
}
|
||||||
|
|
||||||
if viper.GetInt64(maxObjectSizeInitFlag) <= 0 {
|
if viper.GetInt64(maxObjectSizeInitFlag) <= 0 {
|
||||||
return nil, fmt.Errorf("max object size must be positive")
|
return nil, fmt.Errorf("max object size must be positive")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
initCtx := &initializeContext{
|
initCtx := &initializeContext{
|
||||||
|
@ -214,7 +216,7 @@ func (c *initializeContext) sendCommitteeTx(script []byte, sysFee int64) error {
|
||||||
tx, err := c.Client.CreateTxFromScript(script, c.CommitteeAcc, sysFee, 0, []client.SignerAccount{{
|
tx, err := c.Client.CreateTxFromScript(script, c.CommitteeAcc, sysFee, 0, []client.SignerAccount{{
|
||||||
Signer: transaction.Signer{
|
Signer: transaction.Signer{
|
||||||
Account: c.CommitteeAcc.Contract.ScriptHash(),
|
Account: c.CommitteeAcc.Contract.ScriptHash(),
|
||||||
Scopes: transaction.CalledByEntry,
|
Scopes: transaction.Global,
|
||||||
},
|
},
|
||||||
Account: c.CommitteeAcc,
|
Account: c.CommitteeAcc,
|
||||||
}})
|
}})
|
||||||
|
|
Loading…
Reference in a new issue