code.vegaprotocol.io/vega@v0.79.0/datanode/networkhistory/protocol_upgrade_handler.go (about) 1 // Copyright (C) 2023 Gobalsky Labs Limited 2 // 3 // This program is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU Affero General Public License as 5 // published by the Free Software Foundation, either version 3 of the 6 // License, or (at your option) any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU Affero General Public License for more details. 12 // 13 // You should have received a copy of the GNU Affero General Public License 14 // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 16 package networkhistory 17 18 import ( 19 "context" 20 "fmt" 21 22 "code.vegaprotocol.io/vega/core/events" 23 "code.vegaprotocol.io/vega/datanode/service" 24 "code.vegaprotocol.io/vega/logging" 25 ) 26 27 type eventSender interface { 28 Send(events.Event) error 29 } 30 31 type ProtocolUpgradeHandler struct { 32 log *logging.Logger 33 protocolUpgradeService *service.ProtocolUpgrade 34 eventSender eventSender 35 createAndPublishSegment func(ctx context.Context, chainID string, toHeight int64) error 36 } 37 38 func NewProtocolUpgradeHandler( 39 log *logging.Logger, 40 protocolUpgradeService *service.ProtocolUpgrade, 41 eventSender eventSender, 42 createAndPublishSegment func(ctx context.Context, chainID string, toHeight int64) error, 43 ) *ProtocolUpgradeHandler { 44 return &ProtocolUpgradeHandler{ 45 log: log.Named("protocol-upgrade-handler"), 46 protocolUpgradeService: protocolUpgradeService, 47 createAndPublishSegment: createAndPublishSegment, 48 eventSender: eventSender, 49 } 50 } 51 52 func (t *ProtocolUpgradeHandler) OnProtocolUpgradeEvent(ctx context.Context, chainID string, 53 lastCommittedBlockHeight int64, 54 ) error { 55 if err := t.createAndPublishSegment(ctx, chainID, lastCommittedBlockHeight); err != nil { 56 t.log.Error("Failed to create and publish segment", logging.Error(err)) 57 return fmt.Errorf("failed to create and publish segment: %w", err) 58 } 59 60 t.log.Debug("Created and published segment", logging.Int64("last_committed_block_height", lastCommittedBlockHeight)) 61 62 t.protocolUpgradeService.SetProtocolUpgradeStarted() 63 64 if err := t.eventSender.Send(events.NewProtocolUpgradeDataNodeReady(ctx, lastCommittedBlockHeight)); err != nil { 65 t.log.Error("Failed to send data node upgrade event", logging.Error(err)) 66 return err 67 } 68 69 t.log.Debug("Notified Core about being ready for protocol upgrade", logging.Int64("last_committed_block_height", lastCommittedBlockHeight)) 70 71 return nil 72 } 73 74 func (t *ProtocolUpgradeHandler) GetProtocolUpgradeStarted() bool { 75 return t.protocolUpgradeService.GetProtocolUpgradeStarted() 76 }