2021-07-09 09:53:10 +00:00
|
|
|
package morph
|
|
|
|
|
|
|
|
import (
|
2021-07-28 09:21:04 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2023-08-10 12:03:16 +00:00
|
|
|
"strings"
|
2021-07-28 09:21:04 +00:00
|
|
|
|
2024-02-01 06:43:51 +00:00
|
|
|
util2 "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/modules/morph/util"
|
2021-07-28 09:21:04 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
2023-10-24 10:06:30 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/management"
|
2022-09-05 12:45:38 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/unwrap"
|
2021-07-28 09:21:04 +00:00
|
|
|
"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"
|
2021-07-09 09:53:10 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
2023-04-26 08:24:40 +00:00
|
|
|
func forceNewEpochCmd(cmd *cobra.Command, _ []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)
|
|
|
|
}
|
|
|
|
|
2023-10-24 10:06:30 +00:00
|
|
|
r := management.NewReader(wCtx.ReadOnlyInvoker)
|
|
|
|
cs, err := r.GetContractByID(1)
|
2021-07-28 09:21:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't get NNS contract info: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-02-01 06:43:51 +00:00
|
|
|
nmHash, err := util2.NNSResolveHash(wCtx.ReadOnlyInvoker, cs.Hash, domainOf(netmapContract))
|
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
|
|
|
|
}
|
|
|
|
|
2024-02-07 16:44:28 +00:00
|
|
|
if err = wCtx.sendConsensusTx(bw.Bytes()); err == nil {
|
|
|
|
err = wCtx.awaitTx()
|
2022-06-27 14:47:13 +00:00
|
|
|
}
|
2024-02-07 16:44:28 +00:00
|
|
|
if err != nil && strings.Contains(err.Error(), "invalid epoch") {
|
|
|
|
cmd.Println("Epoch has already ticked.")
|
|
|
|
return nil
|
2023-08-10 12:03:16 +00:00
|
|
|
}
|
2024-02-07 16:44:28 +00:00
|
|
|
return err
|
2022-06-27 14:47:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func emitNewEpochCall(bw *io.BufBinWriter, wCtx *initializeContext, nmHash util.Uint160) error {
|
2022-09-05 12:45:38 +00:00
|
|
|
curr, err := unwrap.Int64(wCtx.ReadOnlyInvoker.Call(nmHash, "epoch"))
|
2021-07-28 09:21:04 +00:00
|
|
|
if err != nil {
|
2022-09-05 12:45:38 +00:00
|
|
|
return errors.New("can't fetch current epoch from the netmap contract")
|
2021-07-28 09:21:04 +00:00
|
|
|
}
|
|
|
|
|
2022-09-05 12:45:38 +00:00
|
|
|
newEpoch := curr + 1
|
|
|
|
wCtx.Command.Printf("Current epoch: %d, increase to %d.\n", curr, 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
|
|
|
}
|