Skip to content

Commit

Permalink
feat: add p2p exchange option to bootstrap use persistent peerstore /…
Browse files Browse the repository at this point in the history
… bootstrap from previously seen peers
  • Loading branch information
derrandz committed Apr 28, 2023
1 parent 2d5b385 commit 6a68df9
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 7 deletions.
4 changes: 2 additions & 2 deletions das/daser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ func (m *mockGetter) generateHeaders(t *testing.T, bServ blockservice.BlockServi
m.head = int64(startHeight + endHeight)
}

func (m *mockGetter) Head(context.Context) (*header.ExtendedHeader, error) {
func (m *mockGetter) Head(context.Context, ...libhead.Option) (*header.ExtendedHeader, error) {
return m.headers[m.head], nil
}

Expand Down Expand Up @@ -354,7 +354,7 @@ func (m benchGetterStub) GetByHeight(context.Context, uint64) (*header.ExtendedH

type getterStub struct{}

func (m getterStub) Head(context.Context) (*header.ExtendedHeader, error) {
func (m getterStub) Head(context.Context, ...libhead.Option) (*header.ExtendedHeader, error) {
return &header.ExtendedHeader{RawHeader: header.RawHeader{Height: 1}}, nil
}

Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ go 1.20

replace github.com/ipfs/go-verifcid => github.com/celestiaorg/go-verifcid v0.0.1-lazypatch

replace github.com/celestiaorg/go-header => github.com/derrandz/go-header v0.0.0-20230428151923-2da10fd92f76

require (
cosmossdk.io/math v1.0.0-beta.3
github.com/BurntSushi/toml v1.2.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,6 @@ github.com/celestiaorg/dagstore v0.0.0-20230413141458-735ab09a15d6 h1:/yCwMCoOPc
github.com/celestiaorg/dagstore v0.0.0-20230413141458-735ab09a15d6/go.mod h1:ta/DlqIH10bvhwqJIw51Nq3QU4XVMp6pz3f0Deve9fM=
github.com/celestiaorg/go-fraud v0.1.0 h1:v6mZvlmf2J5ELZfPnrtmmOvKbaYIUs/erDWPO8NbZyY=
github.com/celestiaorg/go-fraud v0.1.0/go.mod h1:yoNM35cKMAkt5Mi/Qx3Wi9bnPilLi8n6RpHZVglTUDs=
github.com/celestiaorg/go-header v0.2.6 h1:f1Mlyu+EfDpkuzO3SWU5dow+ga2vLQ7hNuvsOe//z64=
github.com/celestiaorg/go-header v0.2.6/go.mod h1:i9OpY70+PJ1xPw1IgMfF0Pk6vBD6VWPmjY3bgubJBcU=
github.com/celestiaorg/go-libp2p-messenger v0.2.0 h1:/0MuPDcFamQMbw9xTZ73yImqgTO3jHV7wKHvWD/Irao=
github.com/celestiaorg/go-libp2p-messenger v0.2.0/go.mod h1:s9PIhMi7ApOauIsfBcQwbr7m+HBzmVfDIS+QLdgzDSo=
github.com/celestiaorg/go-verifcid v0.0.1-lazypatch h1:9TSe3w1cmJmbWlweCwCTIZkan7jV8M+KwglXpdD+UG8=
Expand Down Expand Up @@ -492,6 +490,8 @@ github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0/go.mod h1:DZGJHZMqrU4JJqFAWUS2U
github.com/decred/dcrd/lru v1.0.0/go.mod h1:mxKOwFd7lFjN2GZYsiz/ecgqR6kkYAl+0pz0tEMk218=
github.com/deepmap/oapi-codegen v1.6.0/go.mod h1:ryDa9AgbELGeB+YEXE1dR53yAjHwFvE9iAUlWl9Al3M=
github.com/deepmap/oapi-codegen v1.8.2/go.mod h1:YLgSKSDv/bZQB7N4ws6luhozi3cEdRktEqrX88CvjIw=
github.com/derrandz/go-header v0.0.0-20230428151923-2da10fd92f76 h1:R35DAv5yMA8gk3Nplb/FwrIUycTL/uP782u8njbJXiQ=
github.com/derrandz/go-header v0.0.0-20230428151923-2da10fd92f76/go.mod h1:i9OpY70+PJ1xPw1IgMfF0Pk6vBD6VWPmjY3bgubJBcU=
github.com/desertbit/timer v0.0.0-20180107155436-c41aec40b27f h1:U5y3Y5UE0w7amNe7Z5G/twsBW0KEalRQXZzf8ufSh9I=
github.com/desertbit/timer v0.0.0-20180107155436-c41aec40b27f/go.mod h1:xH/i4TFMt8koVQZ6WFms69WAsDWr2XsYL3Hkl7jkoLE=
github.com/dgraph-io/badger v1.5.5-0.20190226225317-8115aed38f8f/go.mod h1:VZxzAIRPHRVNRKRo6AXrX9BJegn6il06VMTZVJYCIjQ=
Expand Down
14 changes: 12 additions & 2 deletions nodebuilder/header/constructors.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import (

"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/peerstore"
p2p_peerstore "github.com/libp2p/go-libp2p/core/peerstore"
"github.com/libp2p/go-libp2p/p2p/net/conngater"
"go.uber.org/fx"

libfraud "github.com/celestiaorg/go-fraud"
libhead "github.com/celestiaorg/go-header"
"github.com/celestiaorg/go-header/p2p"
"github.com/celestiaorg/go-header/p2p/peerstore"
"github.com/celestiaorg/go-header/store"
"github.com/celestiaorg/go-header/sync"

Expand All @@ -29,6 +30,7 @@ func newP2PExchange(
host host.Host,
conngater *conngater.BasicConnectionGater,
cfg Config,
peerstore peerstore.Peerstore,
) (libhead.Exchange[*header.ExtendedHeader], error) {
peers, err := cfg.trustedPeers(bpeers)
if err != nil {
Expand All @@ -37,12 +39,20 @@ func newP2PExchange(
ids := make([]peer.ID, len(peers))
for index, peer := range peers {
ids[index] = peer.ID
host.Peerstore().AddAddrs(peer.ID, peer.Addrs, peerstore.PermanentAddrTTL)
host.Peerstore().AddAddrs(peer.ID, peer.Addrs, p2p_peerstore.PermanentAddrTTL)
}
peerlist, err := peerstore.Load(context.Background())
if err != nil {
log.Info("Error loading peerlist from disk", "err", err)
} else {
log.Info("Loaded peerlist from disk", peerlist)
}

exchange, err := p2p.NewExchange[*header.ExtendedHeader](host, ids, conngater,
p2p.WithParams(cfg.Client),
p2p.WithNetworkID[p2p.ClientParameters](network.String()),
p2p.WithChainID(network.String()),
p2p.WithPeerPersistence[p2p.ClientParameters](peerstore),
)
if err != nil {
return nil, err
Expand Down
4 changes: 4 additions & 0 deletions nodebuilder/header/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

libhead "github.com/celestiaorg/go-header"
"github.com/celestiaorg/go-header/p2p"
"github.com/celestiaorg/go-header/p2p/peerstore"
"github.com/celestiaorg/go-header/store"
"github.com/celestiaorg/go-header/sync"

Expand Down Expand Up @@ -89,6 +90,9 @@ func ConstructModule(tp node.Type, cfg *Config) fx.Option {
return server.Stop(ctx)
}),
)),
fx.Provide(func(ds datastore.Batching) peerstore.Peerstore {
return peerstore.NewPeerStore(ds)
}),
)

switch tp {
Expand Down
3 changes: 2 additions & 1 deletion state/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/celestiaorg/celestia-app/testutil/testfactory"
"github.com/celestiaorg/celestia-app/testutil/testnode"
blobtypes "github.com/celestiaorg/celestia-app/x/blob/types"
libhead "github.com/celestiaorg/go-header"

"github.com/celestiaorg/celestia-node/core"
"github.com/celestiaorg/celestia-node/header"
Expand Down Expand Up @@ -95,7 +96,7 @@ type localHeader struct {
client rpcclient.Client
}

func (l localHeader) Head(ctx context.Context) (*header.ExtendedHeader, error) {
func (l localHeader) Head(ctx context.Context, _ ...libhead.Option) (*header.ExtendedHeader, error) {
latest, err := l.client.Block(ctx, nil)
if err != nil {
return nil, err
Expand Down

0 comments on commit 6a68df9

Please sign in to comment.