|
| 1 | +// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. |
| 2 | +// See the file LICENSE for licensing terms. |
| 3 | + |
| 4 | +package simplex |
| 5 | + |
| 6 | +import ( |
| 7 | + "errors" |
| 8 | + "fmt" |
| 9 | + |
| 10 | + "github.com/ava-labs/simplex" |
| 11 | + "go.uber.org/zap" |
| 12 | + |
| 13 | + "github.com/ava-labs/avalanchego/ids" |
| 14 | + "github.com/ava-labs/avalanchego/message" |
| 15 | + "github.com/ava-labs/avalanchego/proto/pb/p2p" |
| 16 | + "github.com/ava-labs/avalanchego/snow/engine/common" |
| 17 | + "github.com/ava-labs/avalanchego/snow/networking/sender" |
| 18 | + "github.com/ava-labs/avalanchego/subnets" |
| 19 | + "github.com/ava-labs/avalanchego/utils/set" |
| 20 | +) |
| 21 | + |
| 22 | +var ( |
| 23 | + _ simplex.Communication = (*Comm)(nil) |
| 24 | + errNodeNotFound = errors.New("node not found in the validator list") |
| 25 | +) |
| 26 | + |
| 27 | +type Comm struct { |
| 28 | + logger simplex.Logger |
| 29 | + subnetID ids.ID |
| 30 | + chainID ids.ID |
| 31 | + // broadcastNodes are the nodes that should receive broadcast messages |
| 32 | + broadcastNodes set.Set[ids.NodeID] |
| 33 | + // allNodes are the IDs of all the nodes in the subnet |
| 34 | + allNodes []simplex.NodeID |
| 35 | + |
| 36 | + // sender is used to send messages to other nodes |
| 37 | + sender sender.ExternalSender |
| 38 | + msgBuilder message.OutboundMsgBuilder |
| 39 | +} |
| 40 | + |
| 41 | +func NewComm(config *Config) (*Comm, error) { |
| 42 | + if _, ok := config.Validators[config.Ctx.NodeID]; !ok { |
| 43 | + config.Log.Warn("Node is not a validator for the subnet", |
| 44 | + zap.Stringer("nodeID", config.Ctx.NodeID), |
| 45 | + zap.Stringer("chainID", config.Ctx.ChainID), |
| 46 | + zap.Stringer("subnetID", config.Ctx.SubnetID), |
| 47 | + ) |
| 48 | + return nil, fmt.Errorf("our %w: %s", errNodeNotFound, config.Ctx.NodeID) |
| 49 | + } |
| 50 | + |
| 51 | + broadcastNodes := set.NewSet[ids.NodeID](len(config.Validators) - 1) |
| 52 | + allNodes := make([]simplex.NodeID, 0, len(config.Validators)) |
| 53 | + // grab all the nodes that are validators for the subnet |
| 54 | + for _, vd := range config.Validators { |
| 55 | + allNodes = append(allNodes, vd.NodeID[:]) |
| 56 | + if vd.NodeID == config.Ctx.NodeID { |
| 57 | + continue // skip our own node ID |
| 58 | + } |
| 59 | + |
| 60 | + broadcastNodes.Add(vd.NodeID) |
| 61 | + } |
| 62 | + |
| 63 | + return &Comm{ |
| 64 | + subnetID: config.Ctx.SubnetID, |
| 65 | + broadcastNodes: broadcastNodes, |
| 66 | + allNodes: allNodes, |
| 67 | + logger: config.Log, |
| 68 | + sender: config.Sender, |
| 69 | + msgBuilder: config.OutboundMsgBuilder, |
| 70 | + chainID: config.Ctx.ChainID, |
| 71 | + }, nil |
| 72 | +} |
| 73 | + |
| 74 | +func (c *Comm) Nodes() []simplex.NodeID { |
| 75 | + return c.allNodes |
| 76 | +} |
| 77 | + |
| 78 | +func (c *Comm) Send(msg *simplex.Message, destination simplex.NodeID) { |
| 79 | + outboundMsg, err := c.simplexMessageToOutboundMessage(msg) |
| 80 | + if err != nil { |
| 81 | + c.logger.Error("Failed creating message", zap.Error(err)) |
| 82 | + return |
| 83 | + } |
| 84 | + |
| 85 | + dest, err := ids.ToNodeID(destination) |
| 86 | + if err != nil { |
| 87 | + c.logger.Error("Failed to convert destination NodeID", zap.Error(err)) |
| 88 | + return |
| 89 | + } |
| 90 | + |
| 91 | + c.sender.Send(outboundMsg, common.SendConfig{NodeIDs: set.Of(dest)}, c.subnetID, subnets.NoOpAllower) |
| 92 | +} |
| 93 | + |
| 94 | +func (c *Comm) Broadcast(msg *simplex.Message) { |
| 95 | + outboundMsg, err := c.simplexMessageToOutboundMessage(msg) |
| 96 | + if err != nil { |
| 97 | + c.logger.Error("Failed creating message", zap.Error(err)) |
| 98 | + return |
| 99 | + } |
| 100 | + |
| 101 | + c.sender.Send(outboundMsg, common.SendConfig{NodeIDs: c.broadcastNodes}, c.subnetID, subnets.NoOpAllower) |
| 102 | +} |
| 103 | + |
| 104 | +func (c *Comm) simplexMessageToOutboundMessage(msg *simplex.Message) (message.OutboundMessage, error) { |
| 105 | + var simplexMsg *p2p.Simplex |
| 106 | + switch { |
| 107 | + case msg.VerifiedBlockMessage != nil: |
| 108 | + bytes, err := msg.VerifiedBlockMessage.VerifiedBlock.Bytes() |
| 109 | + if err != nil { |
| 110 | + return nil, fmt.Errorf("failed to serialize block: %w", err) |
| 111 | + } |
| 112 | + simplexMsg = newBlockProposal(c.chainID, bytes, msg.VerifiedBlockMessage.Vote) |
| 113 | + case msg.VoteMessage != nil: |
| 114 | + simplexMsg = newVote(c.chainID, msg.VoteMessage) |
| 115 | + case msg.EmptyVoteMessage != nil: |
| 116 | + simplexMsg = newEmptyVote(c.chainID, msg.EmptyVoteMessage) |
| 117 | + case msg.FinalizeVote != nil: |
| 118 | + simplexMsg = newFinalizeVote(c.chainID, msg.FinalizeVote) |
| 119 | + case msg.Notarization != nil: |
| 120 | + simplexMsg = newNotarization(c.chainID, msg.Notarization) |
| 121 | + case msg.EmptyNotarization != nil: |
| 122 | + simplexMsg = newEmptyNotarization(c.chainID, msg.EmptyNotarization) |
| 123 | + case msg.Finalization != nil: |
| 124 | + simplexMsg = newFinalization(c.chainID, msg.Finalization) |
| 125 | + case msg.ReplicationRequest != nil: |
| 126 | + simplexMsg = newReplicationRequest(c.chainID, msg.ReplicationRequest) |
| 127 | + case msg.VerifiedReplicationResponse != nil: |
| 128 | + msg, err := newReplicationResponse(c.chainID, msg.VerifiedReplicationResponse) |
| 129 | + if err != nil { |
| 130 | + return nil, fmt.Errorf("failed to create replication response: %w", err) |
| 131 | + } |
| 132 | + simplexMsg = msg |
| 133 | + default: |
| 134 | + return nil, fmt.Errorf("unknown message type: %+v", msg) |
| 135 | + } |
| 136 | + |
| 137 | + return c.msgBuilder.SimplexMessage(simplexMsg) |
| 138 | +} |
0 commit comments