code.vegaprotocol.io/vega@v0.79.0/datanode/sqlsubscribers/node.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 sqlsubscribers 17 18 import ( 19 "context" 20 "time" 21 22 "code.vegaprotocol.io/vega/core/events" 23 "code.vegaprotocol.io/vega/datanode/entities" 24 eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1" 25 26 "github.com/pkg/errors" 27 ) 28 29 type ValidatorUpdateEvent interface { 30 events.Event 31 ValidatorUpdate() eventspb.ValidatorUpdate 32 } 33 34 type ValidatorRankingScoreEvent interface { 35 events.Event 36 ValidatorRankingEvent() eventspb.ValidatorRankingEvent 37 } 38 39 type ValidatorRewardScoreEvent interface { 40 events.Event 41 ValidatorScoreEvent() eventspb.ValidatorScoreEvent 42 } 43 44 type NodeStore interface { 45 UpsertNode(context.Context, *entities.Node) error 46 UpsertRanking(context.Context, *entities.RankingScore, *entities.RankingScoreAux) error 47 UpsertScore(context.Context, *entities.RewardScore, *entities.RewardScoreAux) error 48 UpdatePublicKey(context.Context, *entities.KeyRotation) error 49 AddNodeAnnouncedEvent(context.Context, string, time.Time, *entities.ValidatorUpdateAux) error 50 UpdateEthereumAddress(ctx context.Context, kr entities.EthereumKeyRotation) error 51 } 52 53 type Node struct { 54 subscriber 55 store NodeStore 56 } 57 58 func NewNode(store NodeStore) *Node { 59 return &Node{ 60 store: store, 61 } 62 } 63 64 func (*Node) Types() []events.Type { 65 return []events.Type{ 66 events.ValidatorUpdateEvent, 67 events.ValidatorRankingEvent, 68 events.ValidatorScoreEvent, 69 events.KeyRotationEvent, 70 events.EthereumKeyRotationEvent, 71 } 72 } 73 74 func (n *Node) Push(ctx context.Context, evt events.Event) error { 75 switch e := evt.(type) { 76 case ValidatorUpdateEvent: 77 return n.consumeUpdate(ctx, e) 78 case ValidatorRankingScoreEvent: 79 return n.consumeRankingScore(ctx, e) 80 case ValidatorRewardScoreEvent: 81 return n.consumeRewardScore(ctx, e) 82 case KeyRotationEvent: 83 return n.consumeKeyRotation(ctx, e) 84 case EthereumKeyRotationEvent: 85 return n.consumeEthereumKeyRotation(ctx, e) 86 default: 87 return errors.Errorf("unknown event type %s", e.Type().String()) 88 } 89 } 90 91 func (n *Node) consumeUpdate(ctx context.Context, event ValidatorUpdateEvent) error { 92 node, aux, err := entities.NodeFromValidatorUpdateEvent(event.ValidatorUpdate(), entities.TxHash(event.TxHash()), n.vegaTime) 93 if err != nil { 94 return errors.Wrap(err, "converting validator update event proto to database entity failed") 95 } 96 97 if err := errors.Wrap(n.store.UpsertNode(ctx, &node), "inserting node to SQL store failed"); err != nil { 98 return err 99 } 100 return errors.Wrap(n.store.AddNodeAnnouncedEvent(ctx, node.ID.String(), node.VegaTime, &aux), "inserting node to SQL store failed") 101 } 102 103 func (n *Node) consumeRankingScore(ctx context.Context, event ValidatorRankingScoreEvent) error { 104 ranking, aux, err := entities.RankingScoreFromRankingEvent(event.ValidatorRankingEvent(), entities.TxHash(event.TxHash()), n.vegaTime) 105 if err != nil { 106 return errors.Wrap(err, "converting ranking score event proto to database entity failed") 107 } 108 109 return errors.Wrap(n.store.UpsertRanking(ctx, &ranking, &aux), "inserting ranking score to SQL store failed") 110 } 111 112 func (n *Node) consumeRewardScore(ctx context.Context, event ValidatorRewardScoreEvent) error { 113 reward, aux, err := entities.RewardScoreFromScoreEvent(event.ValidatorScoreEvent(), entities.TxHash(event.TxHash()), n.vegaTime) 114 if err != nil { 115 return errors.Wrap(err, "converting reward score event proto to database entity failed") 116 } 117 118 return errors.Wrap(n.store.UpsertScore(ctx, &reward, &aux), "inserting reward score to SQL store failed") 119 } 120 121 func (n *Node) consumeKeyRotation(ctx context.Context, event KeyRotationEvent) error { 122 keyRotation := event.KeyRotation() 123 record, err := entities.KeyRotationFromProto(&keyRotation, entities.TxHash(event.TxHash()), n.vegaTime) 124 if err != nil { 125 return errors.Wrap(err, "converting key rotation proto to database entity failed") 126 } 127 128 return errors.Wrap(n.store.UpdatePublicKey(ctx, record), "Updating public key to SQL store failed") 129 } 130 131 func (n *Node) consumeEthereumKeyRotation(ctx context.Context, event EthereumKeyRotationEvent) error { 132 keyRotation := event.EthereumKeyRotation() 133 record, err := entities.EthereumKeyRotationFromProto(&keyRotation, entities.TxHash(event.TxHash()), n.vegaTime, 134 event.Sequence()) 135 if err != nil { 136 return errors.Wrap(err, "converting ethereum key rotation proto to database entity failed") 137 } 138 139 return errors.Wrap(n.store.UpdateEthereumAddress(ctx, record), "Updating public key to SQL store failed") 140 } 141 142 func (n *Node) Name() string { 143 return "Node" 144 }