Skip to content

Commit

Permalink
Ensure DHT protocol ID matches Starknet spec
Browse files Browse the repository at this point in the history
Use dht.V1ProtocolOverride to set exact protocol ID format for DHT.
Add test to verify protocol ID format for different networks:
- /starknet/kad/SN_SEPOLIA/1.0.0 for Sepolia
- /starknet/kad/SN_MAIN/1.0.0 for Mainnet

The change ensures that DHT protocol IDs strictly follow Starknet
specification without additional libp2p prefixes or suffixes.
  • Loading branch information
wojciechos committed Nov 28, 2024
1 parent 71bd8c4 commit 49b2002
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
7 changes: 4 additions & 3 deletions p2p/p2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func NewWithHost(p2phost host.Host, peers string, feederNode bool, bc *blockchai
}
}

p2pdht, err := makeDHT(p2phost, peersAddrInfoS)
p2pdht, err := MakeDHT(p2phost, peersAddrInfoS, snNetwork.L2ChainID)
if err != nil {
return nil, err
}
Expand All @@ -165,9 +165,10 @@ func NewWithHost(p2phost host.Host, peers string, feederNode bool, bc *blockchai
return s, nil
}

func makeDHT(p2phost host.Host, addrInfos []peer.AddrInfo) (*dht.IpfsDHT, error) {
func MakeDHT(p2phost host.Host, addrInfos []peer.AddrInfo, chainID string) (*dht.IpfsDHT, error) {
protocolID := starknet.DHTPID(chainID)
return dht.New(context.Background(), p2phost,
dht.ProtocolPrefix(starknet.Prefix),
dht.V1ProtocolOverride(protocolID),
dht.BootstrapPeers(addrInfos...),
dht.RoutingTableRefreshPeriod(routingTableRefreshPeriod),
dht.Mode(dht.ModeServer),
Expand Down
34 changes: 34 additions & 0 deletions p2p/p2p_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/libp2p/go-libp2p/core/protocol"
mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"
"github.com/multiformats/go-multiaddr"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -206,3 +207,36 @@ func TestLoadAndPersistPeers(t *testing.T) {
)
require.NoError(t, err)
}

func TestMakeDHTProtocolName(t *testing.T) {
net, err := mocknet.FullMeshLinked(1)
require.NoError(t, err)
testHost := net.Hosts()[0]

testCases := []struct {
name string
network *utils.Network
expected string
}{
{
name: "sepolia network",
network: &utils.Sepolia,
expected: "/starknet/kad/SN_SEPOLIA/1.0.0",
},
{
name: "mainnet network",
network: &utils.Mainnet,
expected: "/starknet/kad/SN_MAIN/1.0.0",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
dht, err := p2p.MakeDHT(testHost, nil, tc.network.L2ChainID)
require.NoError(t, err)

protocols := dht.Host().Mux().Protocols()
assert.Contains(t, protocols, protocol.ID(tc.expected), "protocol list: %v", protocols)
})
}
}
4 changes: 4 additions & 0 deletions p2p/starknet/ids.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ func ClassesPID() protocol.ID {
func StateDiffPID() protocol.ID {
return Prefix + "/state_diffs/0.1.0-rc.0"
}

func DHTPID(chainID string) protocol.ID {
return protocol.ID(Prefix + "/kad/" + chainID + "/1.0.0")
}

0 comments on commit 49b2002

Please sign in to comment.