code.vegaprotocol.io/vega@v0.79.0/core/protocolupgrade/snapshot.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 protocolupgrade 17 18 import ( 19 "context" 20 "sort" 21 22 "code.vegaprotocol.io/vega/core/types" 23 eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1" 24 snappb "code.vegaprotocol.io/vega/protos/vega/snapshot/v1" 25 26 "github.com/golang/protobuf/proto" 27 ) 28 29 func (e *Engine) Namespace() types.SnapshotNamespace { 30 return types.ProtocolUpgradeSnapshot 31 } 32 33 func (e *Engine) Keys() []string { 34 return e.hashKeys 35 } 36 37 func (e *Engine) Stopped() bool { 38 return false 39 } 40 41 // get the serialised form and hash of the given key. 42 func (e *Engine) serialise() ([]byte, error) { 43 events := make([]*eventspb.ProtocolUpgradeEvent, 0, len(e.activeProposals)) 44 for _, evt := range e.events { 45 events = append(events, evt) 46 } 47 48 sort.SliceStable(events, func(i, j int) bool { 49 if events[i].VegaReleaseTag == events[j].VegaReleaseTag { 50 return events[i].UpgradeBlockHeight < events[j].UpgradeBlockHeight 51 } 52 return events[i].VegaReleaseTag < events[j].VegaReleaseTag 53 }) 54 55 payloadProtocolUpgradeProposals := &types.PayloadProtocolUpgradeProposals{ 56 Proposals: &snappb.ProtocolUpgradeProposals{ 57 ActiveProposals: events, 58 }, 59 } 60 61 if e.upgradeStatus.AcceptedReleaseInfo != nil { 62 payloadProtocolUpgradeProposals.Proposals.AcceptedProposal = &snappb.AcceptedProtocolUpgradeProposal{ 63 UpgradeBlockHeight: e.upgradeStatus.AcceptedReleaseInfo.UpgradeBlockHeight, 64 VegaReleaseTag: e.upgradeStatus.AcceptedReleaseInfo.VegaReleaseTag, 65 } 66 } 67 68 payload := types.Payload{ 69 Data: payloadProtocolUpgradeProposals, 70 } 71 72 data, err := proto.Marshal(payload.IntoProto()) 73 if err != nil { 74 return nil, err 75 } 76 77 return data, nil 78 } 79 80 func (e *Engine) GetState(k string) ([]byte, []types.StateProvider, error) { 81 state, err := e.serialise() 82 return state, nil, err 83 } 84 85 func (e *Engine) LoadState(ctx context.Context, p *types.Payload) ([]types.StateProvider, error) { 86 if e.Namespace() != p.Data.Namespace() { 87 return nil, types.ErrInvalidSnapshotNamespace 88 } 89 pl := p.Data.(*types.PayloadProtocolUpgradeProposals) 90 e.activeProposals = make(map[string]*protocolUpgradeProposal, len(pl.Proposals.ActiveProposals)) 91 e.events = make(map[string]*eventspb.ProtocolUpgradeEvent, len(pl.Proposals.ActiveProposals)) 92 93 for _, pue := range pl.Proposals.ActiveProposals { 94 ID := protocolUpgradeProposalID(pue.UpgradeBlockHeight, pue.VegaReleaseTag) 95 e.events[ID] = pue 96 } 97 98 for ID, evt := range e.events { 99 e.activeProposals[ID] = &protocolUpgradeProposal{ 100 vegaReleaseTag: evt.VegaReleaseTag, 101 blockHeight: evt.UpgradeBlockHeight, 102 accepted: make(map[string]struct{}, len(evt.Approvers)), 103 } 104 for _, approver := range evt.Approvers { 105 e.activeProposals[ID].accepted[approver] = struct{}{} 106 } 107 } 108 109 if pl.Proposals.AcceptedProposal != nil { 110 e.upgradeStatus.AcceptedReleaseInfo = &types.ReleaseInfo{ 111 UpgradeBlockHeight: pl.Proposals.AcceptedProposal.UpgradeBlockHeight, 112 VegaReleaseTag: pl.Proposals.AcceptedProposal.VegaReleaseTag, 113 } 114 } 115 116 return nil, nil 117 }