2021-07-09 09:53:10 +00:00
|
|
|
package morph
|
|
|
|
|
|
|
|
import (
|
2021-07-28 09:21:04 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
2021-11-29 13:45:25 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2021-07-28 09:21:04 +00:00
|
|
|
"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/vm"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
|
2021-07-09 09:53:10 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
func forceNewEpochCmd(cmd *cobra.Command, args []string) error {
|
2021-07-28 09:21:04 +00:00
|
|
|
wCtx, err := newInitializeContext(cmd, viper.GetViper())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't to initialize context: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cs, err := wCtx.Client.GetContractStateByID(1)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't get NNS contract info: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-08-11 08:48:01 +00:00
|
|
|
nmHash, err := nnsResolveHash(wCtx.Client, cs.Hash, netmapContract+".neofs")
|
2021-07-28 09:21:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't get netmap contract hash: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-11-29 13:45:25 +00:00
|
|
|
signer := transaction.Signer{
|
2021-07-28 09:21:04 +00:00
|
|
|
Account: wCtx.CommitteeAcc.Contract.ScriptHash(),
|
|
|
|
Scopes: transaction.Global, // Scope is important, as we have nested call to container contract.
|
2021-11-29 13:45:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
groupKey, err := nnsResolveKey(wCtx.Client, cs.Hash, groupKeyDomain)
|
|
|
|
if err != nil {
|
|
|
|
cmd.Println("Can't resolve neofs contract group key, using Global scope")
|
|
|
|
} else {
|
|
|
|
signer.Scopes = transaction.CustomGroups
|
|
|
|
signer.AllowedGroups = keys.PublicKeys{groupKey}
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := wCtx.Client.InvokeFunction(nmHash, "epoch", []smartcontract.Parameter{}, nil)
|
2021-07-28 09:21:04 +00:00
|
|
|
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
|
|
|
|
}
|
2021-07-09 09:53:10 +00:00
|
|
|
|
2021-07-28 09:21:04 +00:00
|
|
|
return wCtx.awaitTx()
|
2021-07-09 09:53:10 +00:00
|
|
|
}
|