diff --git a/cmd/zetaclientd/start.go b/cmd/zetaclientd/start.go index 2a46dabdbd..a2513490b0 100644 --- a/cmd/zetaclientd/start.go +++ b/cmd/zetaclientd/start.go @@ -94,11 +94,6 @@ func Start(_ *cobra.Command, _ []string) error { Telemetry: telemetry, } - tss, err := zetatss.Setup(ctx, tssSetupProps, logger.Std) - if err != nil { - return errors.Wrap(err, "unable to setup TSS service") - } - isObserver, err := isObserverNode(ctx, zetacoreClient) switch { case err != nil: @@ -115,11 +110,23 @@ func Start(_ *cobra.Command, _ []string) error { graceful.ShutdownNow() }) - maintenance.NewShutdownListener(zetacoreClient, logger.Std).Listen(ctx, func() { + shutdownListener := maintenance.NewShutdownListener(zetacoreClient, logger.Std) + err = shutdownListener.RunPreStartCheck(ctx) + if err != nil { + return errors.Wrap(err, "pre start check failed") + } + shutdownListener.Listen(ctx, func() { logger.Std.Info().Msg("Shutdown listener received an action to shutdown zetaclientd.") graceful.ShutdownNow() }) + // This will start p2p communication so it should only happen after + // preflight checks have completed + tss, err := zetatss.Setup(ctx, tssSetupProps, logger.Std) + if err != nil { + return errors.Wrap(err, "unable to setup TSS service") + } + // CreateSignerMap: This creates a map of all signers for each chain. // Each signer is responsible for signing transactions for a particular chain signerMap, err := orchestrator.CreateSignerMap(ctx, tss, logger) diff --git a/docs/openapi/openapi.swagger.yaml b/docs/openapi/openapi.swagger.yaml index 694f839db6..611a0537ec 100644 --- a/docs/openapi/openapi.swagger.yaml +++ b/docs/openapi/openapi.swagger.yaml @@ -58115,6 +58115,11 @@ definitions: description: |- Offset from the zetacore block time to initiate signing. Should be calculated and set based on max(zetaclient_core_block_latency). + minimum_version: + type: string + description: |- + Minimum version of zetaclient that is allowed to run. This must be either + a valid semver string (v23.0.1) or empty. description: Flags for the top-level operation of zetaclient. observerPendingNonces: type: object diff --git a/proto/zetachain/zetacore/observer/operational.proto b/proto/zetachain/zetacore/observer/operational.proto index 62ff737074..89265adac1 100644 --- a/proto/zetachain/zetacore/observer/operational.proto +++ b/proto/zetachain/zetacore/observer/operational.proto @@ -16,4 +16,8 @@ message OperationalFlags { // Should be calculated and set based on max(zetaclient_core_block_latency). google.protobuf.Duration signer_block_time_offset = 2 [ (gogoproto.stdduration) = true ]; + + // Minimum version of zetaclient that is allowed to run. This must be either + // a valid semver string (v23.0.1) or empty. + string minimum_version = 3; } \ No newline at end of file diff --git a/typescript/zetachain/zetacore/observer/operational_pb.d.ts b/typescript/zetachain/zetacore/observer/operational_pb.d.ts index c9fc213927..dd2489b446 100644 --- a/typescript/zetachain/zetacore/observer/operational_pb.d.ts +++ b/typescript/zetachain/zetacore/observer/operational_pb.d.ts @@ -28,6 +28,14 @@ export declare class OperationalFlags extends Message { */ signerBlockTimeOffset?: Duration; + /** + * Minimum version of zetaclient that is allowed to run. This must be either + * a valid semver string (v23.0.1) or empty. + * + * @generated from field: string minimum_version = 3; + */ + minimumVersion: string; + constructor(data?: PartialMessage); static readonly runtime: typeof proto3; diff --git a/x/observer/types/errors.go b/x/observer/types/errors.go index fbc6261d76..aaebb297b4 100644 --- a/x/observer/types/errors.go +++ b/x/observer/types/errors.go @@ -71,4 +71,9 @@ var ( 1140, "signer block time offset exceeds limit", ) + ErrOperationalFlagsInvalidMinimumVersion = errorsmod.Register( + ModuleName, + 1141, + "minimum version is not a valid semver string", + ) ) diff --git a/x/observer/types/operational.go b/x/observer/types/operational.go index 4da766c53a..e90415acdf 100644 --- a/x/observer/types/operational.go +++ b/x/observer/types/operational.go @@ -4,6 +4,7 @@ import ( "time" cosmoserrors "cosmossdk.io/errors" + "golang.org/x/mod/semver" ) const ( @@ -23,5 +24,8 @@ func (f *OperationalFlags) Validate() error { return cosmoserrors.Wrapf(ErrOperationalFlagsSignerBlockTimeOffsetLimit, "(%s)", signerBlockTimeOffset) } } + if f.MinimumVersion != "" && !semver.IsValid(f.MinimumVersion) { + return ErrOperationalFlagsInvalidMinimumVersion + } return nil } diff --git a/x/observer/types/operational.pb.go b/x/observer/types/operational.pb.go index e5fb9c3281..7d5b88c058 100644 --- a/x/observer/types/operational.pb.go +++ b/x/observer/types/operational.pb.go @@ -35,6 +35,9 @@ type OperationalFlags struct { // Offset from the zetacore block time to initiate signing. // Should be calculated and set based on max(zetaclient_core_block_latency). SignerBlockTimeOffset *time.Duration `protobuf:"bytes,2,opt,name=signer_block_time_offset,json=signerBlockTimeOffset,proto3,stdduration" json:"signer_block_time_offset,omitempty"` + // Minimum version of zetaclient that is allowed to run. This must be either + // a valid semver string (v23.0.1) or empty. + MinimumVersion string `protobuf:"bytes,3,opt,name=minimum_version,json=minimumVersion,proto3" json:"minimum_version,omitempty"` } func (m *OperationalFlags) Reset() { *m = OperationalFlags{} } @@ -84,6 +87,13 @@ func (m *OperationalFlags) GetSignerBlockTimeOffset() *time.Duration { return nil } +func (m *OperationalFlags) GetMinimumVersion() string { + if m != nil { + return m.MinimumVersion + } + return "" +} + func init() { proto.RegisterType((*OperationalFlags)(nil), "zetachain.zetacore.observer.OperationalFlags") } @@ -93,25 +103,27 @@ func init() { } var fileDescriptor_ea3eed2ec55093b5 = []byte{ - // 282 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x44, 0x90, 0xc1, 0x4a, 0xec, 0x30, - 0x14, 0x86, 0x27, 0xf7, 0x8a, 0x8b, 0x8a, 0x22, 0x83, 0x42, 0x1d, 0x21, 0x0e, 0x82, 0x30, 0x20, - 0x93, 0x80, 0xbe, 0x41, 0x51, 0x71, 0x37, 0x30, 0xb8, 0x10, 0x37, 0x25, 0xed, 0x9c, 0xa6, 0xc1, - 0xb6, 0xa7, 0x24, 0xa9, 0xa8, 0x4f, 0x21, 0xb8, 0xf1, 0x91, 0x5c, 0xce, 0xd2, 0x9d, 0xd2, 0xbe, - 0x88, 0x4c, 0x32, 0x75, 0x76, 0x27, 0x27, 0xdf, 0x7f, 0x3e, 0xf8, 0x83, 0xe9, 0x2b, 0x58, 0x91, - 0xe6, 0x42, 0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52, 0x83, 0x49, 0xb6, 0x9d, 0xf6, 0xf2, 0x37, 0x00, 0x00, - 0xff, 0xff, 0xf0, 0xe0, 0x3c, 0x60, 0x67, 0x01, 0x00, 0x00, + // 308 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x44, 0x90, 0x41, 0x4a, 0x03, 0x31, + 0x14, 0x86, 0x1b, 0x2b, 0x82, 0x23, 0x56, 0x19, 0x14, 0xc6, 0x0a, 0xb1, 0x08, 0x62, 0x41, 0x9a, + 0x80, 0xde, 0xa0, 0xa8, 0xb8, 0x2b, 0x14, 0x11, 0x71, 0x33, 0xcc, 0xb4, 0xaf, 0x99, 0xe0, 0x24, + 0xaf, 0x24, 0x99, 0xa2, 0x9e, 0xc2, 0xa5, 0x87, 0xf1, 0x00, 0x2e, 0xbb, 0x74, 0xa7, 0xb4, 0x17, + 0x91, 0x26, 0xad, 0xdd, 0xbd, 0xfc, 0xf9, 0xff, 0xf7, 0x3d, 0xfe, 0xa8, 0xf3, 0x06, 0x2e, 0x1b, + 0x14, 0x99, 0xd4, 0xdc, 0x4f, 0x68, 0x80, 0x63, 0x6e, 0xc1, 0x4c, 0xc0, 0x70, 0x1c, 0x83, 0xc9, + 0x9c, 0x44, 0x9d, 0x95, 0x6c, 0x6c, 0xd0, 0x61, 0x7c, 0xfc, 0x6f, 0x67, 0x2b, 0x3b, 0x5b, 0xd9, + 0x9b, 0x07, 0x02, 0x05, 0x7a, 0x1f, 0x5f, 0x4c, 0x21, 0xd2, 0xa4, 0x02, 0x51, 0x94, 0xc0, 0xfd, + 0x2b, 0xaf, 0x46, 0x7c, 0x58, 0x85, 0xa5, 0xe1, 0xff, 0xf4, 0x93, 0x44, 0xfb, 0xbd, 0x35, 0xe8, + 0xb6, 0xcc, 0x84, 0x8d, 0xcf, 0xa2, 0x86, 0x01, 0xeb, 0x32, 0xe3, 0xd2, 0x02, 0xa4, 0x28, 0x5c, + 0x42, 0x5a, 0xa4, 0x5d, 0xef, 0xef, 0x2e, 0xd5, 0x3b, 0x2f, 0xc6, 0x8f, 0x51, 0x62, 0xa5, 0xd0, + 0x60, 0xd2, 0xbc, 0xc4, 0xc1, 0x73, 0xea, 0xa4, 0x82, 0x14, 0x47, 0x23, 0x0b, 0x2e, 0xd9, 0x68, + 0x91, 0xf6, 0xce, 0xe5, 0x11, 0x0b, 0x78, 0xb6, 0xc2, 0xb3, 0xeb, 0x25, 0xbe, 0xbb, 0xf9, 0xf1, + 0x73, 0x42, 0xfa, 0x87, 0x61, 0x41, 0x77, 0x91, 0xbf, 0x97, 0x0a, 0x7a, 0x3e, 0x1d, 0x9f, 0x47, + 0x7b, 0x4a, 0x6a, 0xa9, 0x2a, 0x95, 0x4e, 0xc0, 0x58, 0x89, 0x3a, 0xa9, 0xb7, 0x48, 0x7b, 0xbb, + 0xdf, 0x58, 0xca, 0x0f, 0x41, 0xed, 0xde, 0x7c, 0xcd, 0x28, 0x99, 0xce, 0x28, 0xf9, 0x9d, 0x51, + 0xf2, 0x3e, 0xa7, 0xb5, 0xe9, 0x9c, 0xd6, 0xbe, 0xe7, 0xb4, 0xf6, 0x74, 0x21, 0xa4, 0x2b, 0xaa, + 0x9c, 0x0d, 0x50, 0xf9, 0x6e, 0x3b, 0xa1, 0x66, 0x8d, 0x43, 0xe0, 0x2f, 0xeb, 0x92, 0xdd, 0xeb, + 0x18, 0x6c, 0xbe, 0xe5, 0xef, 0xbb, 0xfa, 0x0b, 0x00, 0x00, 0xff, 0xff, 0xe9, 0x88, 0xe2, 0xfd, + 0x90, 0x01, 0x00, 0x00, } func (m *OperationalFlags) Marshal() (dAtA []byte, err error) { @@ -134,6 +146,13 @@ func (m *OperationalFlags) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l + if len(m.MinimumVersion) > 0 { + i -= len(m.MinimumVersion) + copy(dAtA[i:], m.MinimumVersion) + i = encodeVarintOperational(dAtA, i, uint64(len(m.MinimumVersion))) + i-- + dAtA[i] = 0x1a + } if m.SignerBlockTimeOffset != nil { n1, err1 := github_com_cosmos_gogoproto_types.StdDurationMarshalTo(*m.SignerBlockTimeOffset, dAtA[i-github_com_cosmos_gogoproto_types.SizeOfStdDuration(*m.SignerBlockTimeOffset):]) if err1 != nil { @@ -176,6 +195,10 @@ func (m *OperationalFlags) Size() (n int) { l = github_com_cosmos_gogoproto_types.SizeOfStdDuration(*m.SignerBlockTimeOffset) n += 1 + l + sovOperational(uint64(l)) } + l = len(m.MinimumVersion) + if l > 0 { + n += 1 + l + sovOperational(uint64(l)) + } return n } @@ -269,6 +292,38 @@ func (m *OperationalFlags) Unmarshal(dAtA []byte) error { return err } iNdEx = postIndex + case 3: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field MinimumVersion", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowOperational + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthOperational + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthOperational + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.MinimumVersion = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex default: iNdEx = preIndex skippy, err := skipOperational(dAtA[iNdEx:]) diff --git a/x/observer/types/operational_test.go b/x/observer/types/operational_test.go index 79c8caf0dc..e80746999e 100644 --- a/x/observer/types/operational_test.go +++ b/x/observer/types/operational_test.go @@ -15,6 +15,10 @@ func TestOperationalFlags_Validate(t *testing.T) { of types.OperationalFlags errContains string }{ + { + name: "empty is valid", + of: types.OperationalFlags{}, + }, { name: "invalid restart height", of: types.OperationalFlags{ @@ -48,11 +52,18 @@ func TestOperationalFlags_Validate(t *testing.T) { }, errContains: types.ErrOperationalFlagsSignerBlockTimeOffsetLimit.Error(), }, + { + name: "minimum version valid", + of: types.OperationalFlags{ + MinimumVersion: "v1.1.1", + }, + }, { name: "all flags valid", of: types.OperationalFlags{ RestartHeight: 1, SignerBlockTimeOffset: ptr.Ptr(time.Second), + MinimumVersion: "v1.1.1", }, }, } diff --git a/zetaclient/maintenance/shutdown_listener.go b/zetaclient/maintenance/shutdown_listener.go index 0ac3b18d75..9efa5a39f1 100644 --- a/zetaclient/maintenance/shutdown_listener.go +++ b/zetaclient/maintenance/shutdown_listener.go @@ -2,12 +2,16 @@ package maintenance import ( "context" + "fmt" + "strings" "time" "cosmossdk.io/errors" "github.com/rs/zerolog" + "golang.org/x/mod/semver" "github.com/zeta-chain/node/pkg/bg" + "github.com/zeta-chain/node/pkg/constant" "github.com/zeta-chain/node/pkg/retry" observertypes "github.com/zeta-chain/node/x/observer/types" "github.com/zeta-chain/node/zetaclient/chains/interfaces" @@ -22,17 +26,32 @@ type ShutdownListener struct { logger zerolog.Logger lastRestartHeightMissed int64 + getVersion func() string } // NewShutdownListener creates a new ShutdownListener. func NewShutdownListener(client interfaces.ZetacoreClient, logger zerolog.Logger) *ShutdownListener { log := logger.With().Str("module", "shutdown_listener").Logger() return &ShutdownListener{ - client: client, - logger: log, + client: client, + logger: log, + getVersion: getVersionDefault, } } +// RunPreStartCheck runs any checks that must run before fully starting zetaclient. +// Specifically this should be run before any TSS P2P is started. +func (o *ShutdownListener) RunPreStartCheck(ctx context.Context) error { + operationalFlags, err := o.getOperationalFlagsWithRetry(ctx) + if err != nil { + return errors.Wrap(err, "unable to get initial operational flags") + } + if err := o.checkMinimumVersion(operationalFlags); err != nil { + return err + } + return nil +} + func (o *ShutdownListener) Listen(ctx context.Context, action func()) { var ( withLogger = bg.WithLogger(o.logger) @@ -43,12 +62,9 @@ func (o *ShutdownListener) Listen(ctx context.Context, action func()) { } func (o *ShutdownListener) waitForUpdate(ctx context.Context) error { - operationalFlags, err := retry.DoTypedWithBackoffAndRetry( - func() (observertypes.OperationalFlags, error) { return o.client.GetOperationalFlags(ctx) }, - retry.DefaultConstantBackoff(), - ) + operationalFlags, err := o.getOperationalFlagsWithRetry(ctx) if err != nil { - return errors.Wrap(err, "unable to get initial operational flags") + return errors.Wrap(err, "get initial operational flags") } if o.handleNewFlags(ctx, operationalFlags) { return nil @@ -74,8 +90,19 @@ func (o *ShutdownListener) waitForUpdate(ctx context.Context) error { } } +func (o *ShutdownListener) getOperationalFlagsWithRetry(ctx context.Context) (observertypes.OperationalFlags, error) { + return retry.DoTypedWithBackoffAndRetry( + func() (observertypes.OperationalFlags, error) { return o.client.GetOperationalFlags(ctx) }, + retry.DefaultConstantBackoff(), + ) +} + // handleNewFlags processes the flags and returns true if a shutdown should be signaled func (o *ShutdownListener) handleNewFlags(ctx context.Context, f observertypes.OperationalFlags) bool { + if err := o.checkMinimumVersion(f); err != nil { + o.logger.Error().Err(err).Msg("minimum version check") + return true + } if f.RestartHeight < 1 { return false } @@ -123,3 +150,29 @@ func (o *ShutdownListener) handleNewFlags(ctx context.Context, f observertypes.O } } } + +func (o *ShutdownListener) checkMinimumVersion(f observertypes.OperationalFlags) error { + if f.MinimumVersion != "" { + // we typically store the version without the required v prefix + currentVersion := ensurePrefix(o.getVersion(), "v") + if semver.Compare(currentVersion, f.MinimumVersion) == -1 { + return fmt.Errorf( + "current version (%s) is less than minimum version (%s)", + currentVersion, + f.MinimumVersion, + ) + } + } + return nil +} + +func getVersionDefault() string { + return constant.Version +} + +func ensurePrefix(s, prefix string) string { + if !strings.HasPrefix(s, prefix) { + return prefix + s + } + return s +} diff --git a/zetaclient/maintenance/shutdown_listener_test.go b/zetaclient/maintenance/shutdown_listener_test.go index 2b2c9128d1..c85c70cde0 100644 --- a/zetaclient/maintenance/shutdown_listener_test.go +++ b/zetaclient/maintenance/shutdown_listener_test.go @@ -100,8 +100,65 @@ func TestShutdownListener(t *testing.T) { return len(client.Calls) == 2 }, time.Second, time.Millisecond) assertChannelNotClosed(t, complete) - cancel() }) + + t.Run("minimum version ok", func(t *testing.T) { + client := mocks.NewZetacoreClient(t) + + listener := NewShutdownListener(client, logger) + listener.getVersion = func() string { + return "1.1.2" + } + + client.Mock.On("GetOperationalFlags", ctx).Return(observertypes.OperationalFlags{ + MinimumVersion: "v1.1.1", + }, nil) + + // pre start checks passed + err := listener.RunPreStartCheck(ctx) + require.NoError(t, err) + + // listener also does not shutdown + complete := make(chan interface{}) + listener.Listen(ctx, func() { + close(complete) + }) + + require.Eventually(t, func() bool { + return len(client.Calls) == 2 + }, time.Second, time.Millisecond) + assertChannelNotClosed(t, complete) + }) + + t.Run("minimum version failed", func(t *testing.T) { + client := mocks.NewZetacoreClient(t) + + listener := NewShutdownListener(client, logger) + listener.getVersion = func() string { + return "1.1.1" + } + + client.Mock.On("GetOperationalFlags", ctx).Return(observertypes.OperationalFlags{ + MinimumVersion: "v1.1.2", + }, nil) + + // pre start checks would return error + err := listener.RunPreStartCheck(ctx) + require.Error(t, err) + + // listener would also shutdown + complete := make(chan interface{}) + listener.Listen(ctx, func() { + close(complete) + }) + + require.Eventually(t, func() bool { + return len(client.Calls) == 2 + }, time.Second, time.Millisecond) + <-complete + }) + // avoid Log in goroutine after TestShutdownListener has completed + cancel() time.Sleep(time.Millisecond * 100) }