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/io"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
|
2022-06-27 14:47:13 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2021-07-28 09:21:04 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
|
2022-07-18 08:33:53 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/vmstate"
|
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)
|
|
|
|
}
|
|
|
|
|
2022-06-27 14:47:13 +00:00
|
|
|
bw := io.NewBufBinWriter()
|
|
|
|
if err := emitNewEpochCall(bw, wCtx, nmHash); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := wCtx.sendCommitteeTx(bw.Bytes(), -1, true); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return wCtx.awaitTx()
|
|
|
|
}
|
|
|
|
|
|
|
|
func emitNewEpochCall(bw *io.BufBinWriter, wCtx *initializeContext, nmHash util.Uint160) error {
|
2022-04-07 12:47:13 +00:00
|
|
|
res, err := invokeFunction(wCtx.Client, nmHash, "epoch", nil, nil)
|
2022-07-18 08:33:53 +00:00
|
|
|
if err != nil || res.State != vmstate.Halt.String() || len(res.Stack) == 0 {
|
2021-07-28 09:21:04 +00:00
|
|
|
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
|
2022-06-27 14:47:13 +00:00
|
|
|
wCtx.Command.Printf("Current epoch: %s, increase to %d.\n", bi, newEpoch)
|
2021-07-28 09:21:04 +00:00
|
|
|
|
|
|
|
// In NeoFS this is done via Notary contract. Here, however, we can form the
|
|
|
|
// transaction locally.
|
|
|
|
emit.AppCall(bw.BinWriter, nmHash, "newEpoch", callflag.All, newEpoch)
|
2022-06-27 14:47:13 +00:00
|
|
|
return bw.Err
|
2021-07-09 09:53:10 +00:00
|
|
|
}
|