Skip to content

Commit

Permalink
feat: sharable outbound peers (#210)
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Gianelloni <wolf31o2@blinklabs.io>
  • Loading branch information
wolf31o2 authored Nov 2, 2024
1 parent 2cf9ba3 commit 6b7040e
Show file tree
Hide file tree
Showing 11 changed files with 185 additions and 45 deletions.
2 changes: 1 addition & 1 deletion chainsync/chainsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (s *State) AddBlock(block ledger.Block, blockType uint) error {
// TODO: figure out something for Byron. this won't work, since the
// block number isn't stored in the block itself
blockNumber := block.BlockNumber()
// Uodate metrics
// Update metrics
s.metrics.blockNum.Set(float64(blockNumber))
s.metrics.slotNum.Set(float64(slotNumber))
// Generate event
Expand Down
41 changes: 29 additions & 12 deletions outbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type outboundPeer struct {
Address string
ReconnectCount int
ReconnectDelay time.Duration
sharable bool
}

func (n *Node) startOutboundConnections() {
Expand All @@ -48,7 +49,7 @@ func (n *Node) startOutboundConnections() {
"component", "network",
"role", "client",
)
var tmpHosts []string
var tmpPeers []outboundPeer
for _, host := range n.config.topologyConfig.BootstrapPeers {
n.config.logger.Debug(
fmt.Sprintf(
Expand All @@ -59,9 +60,14 @@ func (n *Node) startOutboundConnections() {
"component", "network",
"role", "client",
)
tmpHosts = append(
tmpHosts,
net.JoinHostPort(host.Address, strconv.Itoa(int(host.Port))),
tmpPeers = append(
tmpPeers,
outboundPeer{
Address: net.JoinHostPort(
host.Address,
strconv.Itoa(int(host.Port)),
),
},
)
}
for _, localRoot := range n.config.topologyConfig.LocalRoots {
Expand All @@ -75,9 +81,15 @@ func (n *Node) startOutboundConnections() {
"component", "network",
"role", "client",
)
tmpHosts = append(
tmpHosts,
net.JoinHostPort(host.Address, strconv.Itoa(int(host.Port))),
tmpPeers = append(
tmpPeers,
outboundPeer{
Address: net.JoinHostPort(
host.Address,
strconv.Itoa(int(host.Port)),
),
sharable: localRoot.Advertise,
},
)
}
}
Expand All @@ -92,15 +104,20 @@ func (n *Node) startOutboundConnections() {
"component", "network",
"role", "client",
)
tmpHosts = append(
tmpHosts,
net.JoinHostPort(host.Address, strconv.Itoa(int(host.Port))),
tmpPeers = append(
tmpPeers,
outboundPeer{
Address: net.JoinHostPort(
host.Address,
strconv.Itoa(int(host.Port)),
),
sharable: publicRoot.Advertise,
},
)
}
}
// Start outbound connections
for _, host := range tmpHosts {
tmpPeer := outboundPeer{Address: host}
for _, tmpPeer := range tmpPeers {
go func(peer outboundPeer) {
if err := n.createOutboundConnection(peer); err != nil {
n.config.logger.Error(
Expand Down
40 changes: 38 additions & 2 deletions peersharing.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
package node

import (
"fmt"
"net"
"strconv"

opeersharing "github.com/blinklabs-io/gouroboros/protocol/peersharing"
)

Expand All @@ -34,6 +38,38 @@ func (n *Node) peersharingShareRequest(
ctx opeersharing.CallbackContext,
amount int,
) ([]opeersharing.PeerAddress, error) {
// TODO: add hooks for getting peers to share
return []opeersharing.PeerAddress{}, nil
peers := []opeersharing.PeerAddress{}
var cnt int
for _, peer := range n.outboundConns {
cnt++
if cnt > amount {
break
}
if cnt > len(n.outboundConns) {
break
}
if peer.sharable {
host, port, err := net.SplitHostPort(peer.Address)
if err != nil {
// Skip on error
n.config.logger.Debug("failed to split peer address, skipping")
continue
}
portNum, err := strconv.ParseUint(port, 10, 16)
if err != nil {
// Skip on error
n.config.logger.Debug("failed to parse peer port, skipping")
continue
}
n.config.logger.Debug(
fmt.Sprintf("adding peer for sharing: %s", peer.Address),
)
peers = append(peers, opeersharing.PeerAddress{
IP: net.ParseIP(host),
Port: uint16(portNum),
},
)
}
}
return peers, nil
}
4 changes: 3 additions & 1 deletion state/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ func (ci *ChainIterator) Next(blocking bool) (*ChainIteratorResult, error) {
return nil, nil
}
// Wait for new block or a rollback
blockSubId, blockChan := ci.ls.config.EventBus.Subscribe(ChainBlockEventType)
blockSubId, blockChan := ci.ls.config.EventBus.Subscribe(
ChainBlockEventType,
)
rollbackSubId, rollbackChan := ci.ls.config.EventBus.Subscribe(
ChainRollbackEventType,
)
Expand Down
20 changes: 16 additions & 4 deletions state/eras/allegra.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,32 @@ func DecodePParamsUpdateAllegra(data []byte) (any, error) {
func PParamsUpdateAllegra(currentPParams any, pparamsUpdate any) (any, error) {
allegraPParams, ok := currentPParams.(allegra.AllegraProtocolParameters)
if !ok {
return nil, fmt.Errorf("current PParams (%T) is not expected type", currentPParams)
return nil, fmt.Errorf(
"current PParams (%T) is not expected type",
currentPParams,
)
}
allegraPParamsUpdate, ok := pparamsUpdate.(allegra.AllegraProtocolParameterUpdate)
if !ok {
return nil, fmt.Errorf("PParams update (%T) is not expected type", pparamsUpdate)
return nil, fmt.Errorf(
"PParams update (%T) is not expected type",
pparamsUpdate,
)
}
allegraPParams.Update(&allegraPParamsUpdate)
return allegraPParams, nil
}

func HardForkAllegra(nodeConfig *cardano.CardanoNodeConfig, prevPParams any) (any, error) {
func HardForkAllegra(
nodeConfig *cardano.CardanoNodeConfig,
prevPParams any,
) (any, error) {
shelleyPParams, ok := prevPParams.(shelley.ShelleyProtocolParameters)
if !ok {
return nil, fmt.Errorf("previous PParams (%T) are not expected type", prevPParams)
return nil, fmt.Errorf(
"previous PParams (%T) are not expected type",
prevPParams,
)
}
ret := allegra.UpgradePParams(shelleyPParams)
return ret, nil
Expand Down
20 changes: 16 additions & 4 deletions state/eras/alonzo.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,32 @@ func DecodePParamsUpdateAlonzo(data []byte) (any, error) {
func PParamsUpdateAlonzo(currentPParams any, pparamsUpdate any) (any, error) {
alonzoPParams, ok := currentPParams.(alonzo.AlonzoProtocolParameters)
if !ok {
return nil, fmt.Errorf("current PParams (%T) is not expected type", currentPParams)
return nil, fmt.Errorf(
"current PParams (%T) is not expected type",
currentPParams,
)
}
alonzoPParamsUpdate, ok := pparamsUpdate.(alonzo.AlonzoProtocolParameterUpdate)
if !ok {
return nil, fmt.Errorf("PParams update (%T) is not expected type", pparamsUpdate)
return nil, fmt.Errorf(
"PParams update (%T) is not expected type",
pparamsUpdate,
)
}
alonzoPParams.Update(&alonzoPParamsUpdate)
return alonzoPParams, nil
}

func HardForkAlonzo(nodeConfig *cardano.CardanoNodeConfig, prevPParams any) (any, error) {
func HardForkAlonzo(
nodeConfig *cardano.CardanoNodeConfig,
prevPParams any,
) (any, error) {
maryPParams, ok := prevPParams.(mary.MaryProtocolParameters)
if !ok {
return nil, fmt.Errorf("previous PParams (%T) are not expected type", prevPParams)
return nil, fmt.Errorf(
"previous PParams (%T) are not expected type",
prevPParams,
)
}
ret := alonzo.UpgradePParams(maryPParams)
alonzoGenesis, err := nodeConfig.AlonzoGenesis()
Expand Down
20 changes: 16 additions & 4 deletions state/eras/babbage.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,32 @@ func DecodePParamsUpdateBabbage(data []byte) (any, error) {
func PParamsUpdateBabbage(currentPParams any, pparamsUpdate any) (any, error) {
babbagePParams, ok := currentPParams.(babbage.BabbageProtocolParameters)
if !ok {
return nil, fmt.Errorf("current PParams (%T) is not expected type", currentPParams)
return nil, fmt.Errorf(
"current PParams (%T) is not expected type",
currentPParams,
)
}
babbagePParamsUpdate, ok := pparamsUpdate.(babbage.BabbageProtocolParameterUpdate)
if !ok {
return nil, fmt.Errorf("PParams update (%T) is not expected type", pparamsUpdate)
return nil, fmt.Errorf(
"PParams update (%T) is not expected type",
pparamsUpdate,
)
}
babbagePParams.Update(&babbagePParamsUpdate)
return babbagePParams, nil
}

func HardForkBabbage(nodeConfig *cardano.CardanoNodeConfig, prevPParams any) (any, error) {
func HardForkBabbage(
nodeConfig *cardano.CardanoNodeConfig,
prevPParams any,
) (any, error) {
alonzoPParams, ok := prevPParams.(alonzo.AlonzoProtocolParameters)
if !ok {
return nil, fmt.Errorf("previous PParams (%T) are not expected type", prevPParams)
return nil, fmt.Errorf(
"previous PParams (%T) are not expected type",
prevPParams,
)
}
ret := babbage.UpgradePParams(alonzoPParams)
return ret, nil
Expand Down
20 changes: 16 additions & 4 deletions state/eras/conway.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,32 @@ func DecodePParamsUpdateConway(data []byte) (any, error) {
func PParamsUpdateConway(currentPParams any, pparamsUpdate any) (any, error) {
conwayPParams, ok := currentPParams.(conway.ConwayProtocolParameters)
if !ok {
return nil, fmt.Errorf("current PParams (%T) is not expected type", currentPParams)
return nil, fmt.Errorf(
"current PParams (%T) is not expected type",
currentPParams,
)
}
conwayPParamsUpdate, ok := pparamsUpdate.(conway.ConwayProtocolParameterUpdate)
if !ok {
return nil, fmt.Errorf("PParams update (%T) is not expected type", pparamsUpdate)
return nil, fmt.Errorf(
"PParams update (%T) is not expected type",
pparamsUpdate,
)
}
conwayPParams.Update(&conwayPParamsUpdate)
return conwayPParams, nil
}

func HardForkConway(nodeConfig *cardano.CardanoNodeConfig, prevPParams any) (any, error) {
func HardForkConway(
nodeConfig *cardano.CardanoNodeConfig,
prevPParams any,
) (any, error) {
babbagePParams, ok := prevPParams.(babbage.BabbageProtocolParameters)
if !ok {
return nil, fmt.Errorf("previous PParams (%T) are not expected type", prevPParams)
return nil, fmt.Errorf(
"previous PParams (%T) are not expected type",
prevPParams,
)
}
ret := conway.UpgradePParams(babbagePParams)
conwayGenesis, err := nodeConfig.ConwayGenesis()
Expand Down
20 changes: 16 additions & 4 deletions state/eras/mary.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,32 @@ func DecodePParamsUpdateMary(data []byte) (any, error) {
func PParamsUpdateMary(currentPParams any, pparamsUpdate any) (any, error) {
maryPParams, ok := currentPParams.(mary.MaryProtocolParameters)
if !ok {
return nil, fmt.Errorf("current PParams (%T) is not expected type", currentPParams)
return nil, fmt.Errorf(
"current PParams (%T) is not expected type",
currentPParams,
)
}
maryPParamsUpdate, ok := pparamsUpdate.(mary.MaryProtocolParameterUpdate)
if !ok {
return nil, fmt.Errorf("PParams update (%T) is not expected type", pparamsUpdate)
return nil, fmt.Errorf(
"PParams update (%T) is not expected type",
pparamsUpdate,
)
}
maryPParams.Update(&maryPParamsUpdate)
return maryPParams, nil
}

func HardForkMary(nodeConfig *cardano.CardanoNodeConfig, prevPParams any) (any, error) {
func HardForkMary(
nodeConfig *cardano.CardanoNodeConfig,
prevPParams any,
) (any, error) {
allegraPParams, ok := prevPParams.(allegra.AllegraProtocolParameters)
if !ok {
return nil, fmt.Errorf("previous PParams (%T) are not expected type", prevPParams)
return nil, fmt.Errorf(
"previous PParams (%T) are not expected type",
prevPParams,
)
}
ret := mary.UpgradePParams(allegraPParams)
return ret, nil
Expand Down
15 changes: 12 additions & 3 deletions state/eras/shelley.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,26 @@ func DecodePParamsUpdateShelley(data []byte) (any, error) {
func PParamsUpdateShelley(currentPParams any, pparamsUpdate any) (any, error) {
shelleyPParams, ok := currentPParams.(shelley.ShelleyProtocolParameters)
if !ok {
return nil, fmt.Errorf("current PParams (%T) is not expected type", currentPParams)
return nil, fmt.Errorf(
"current PParams (%T) is not expected type",
currentPParams,
)
}
shelleyPParamsUpdate, ok := pparamsUpdate.(shelley.ShelleyProtocolParameterUpdate)
if !ok {
return nil, fmt.Errorf("PParams update (%T) is not expected type", pparamsUpdate)
return nil, fmt.Errorf(
"PParams update (%T) is not expected type",
pparamsUpdate,
)
}
shelleyPParams.Update(&shelleyPParamsUpdate)
return shelleyPParams, nil
}

func HardForkShelley(nodeConfig *cardano.CardanoNodeConfig, prevPParams any) (any, error) {
func HardForkShelley(
nodeConfig *cardano.CardanoNodeConfig,
prevPParams any,
) (any, error) {
// There's no Byron protocol parameters to upgrade from, so this is mostly
// a dummy call for consistency
ret := shelley.UpgradePParams(nil)
Expand Down
Loading

0 comments on commit 6b7040e

Please sign in to comment.