Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.18

require (
github.com/celestiaorg/go-cnc v0.3.0
github.com/celestiaorg/go-header v0.1.0
github.com/celestiaorg/go-header v0.2.1
github.com/dgraph-io/badger/v3 v3.2103.5
github.com/go-kit/kit v0.12.0
github.com/gogo/protobuf v1.3.3
Expand Down Expand Up @@ -156,6 +156,7 @@ require (
go.etcd.io/bbolt v1.3.6 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/otel v1.11.2 // indirect
go.opentelemetry.io/otel/metric v0.34.0 // indirect
go.opentelemetry.io/otel/trace v1.11.2 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/dig v1.15.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ github.com/celestiaorg/go-cnc v0.3.0 h1:eAVPNHGpx+2sBO7NZyQ1+VW8rzf6W4FQDlSq6aqS
github.com/celestiaorg/go-cnc v0.3.0/go.mod h1:zYzvHudSd1iNPuHBMyvZ1YvWou5aT9JXgtch9Tkaf70=
github.com/celestiaorg/go-header v0.1.0 h1:K/atYWwZ/bjMLJ/Apy0dokbREa8BGgxUMiMjhRHlF4E=
github.com/celestiaorg/go-header v0.1.0/go.mod h1:AR7GQ1519TDLEFxRC0rt9emq1IvhU+Nf+1Ufe3JI3nA=
github.com/celestiaorg/go-header v0.2.0 h1:UnufpDXQGLpP40SyiwfZLRT7alKLmo3lraPaJtsV8qI=
github.com/celestiaorg/go-header v0.2.0/go.mod h1:6XKf0yhoEQqfKQTZnyTZjTjF5jH5Wq9uO9AvDMkdYbs=
github.com/celestiaorg/go-header v0.2.1 h1:h6EiEcrA7K9dg5bRNe7aNQ13rDAL4/wRB5jujMGP1Ho=
github.com/celestiaorg/go-header v0.2.1/go.mod h1:6XKf0yhoEQqfKQTZnyTZjTjF5jH5Wq9uO9AvDMkdYbs=
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/tendermint v0.34.22-0.20221202214355-3605c597500d h1:OH9dp6WWotp53aG58xSdLWd+F1Znf3DhA0BadyJO4Aw=
Expand Down Expand Up @@ -1055,6 +1059,8 @@ go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0=
go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo=
go.opentelemetry.io/otel v1.11.2 h1:YBZcQlsVekzFsFbjygXMOXSs6pialIZxcjfO/mBDmR0=
go.opentelemetry.io/otel v1.11.2/go.mod h1:7p4EUV+AqgdlNV9gL97IgUZiVR3yrFXYo53f9BM3tRI=
go.opentelemetry.io/otel/metric v0.34.0 h1:MCPoQxcg/26EuuJwpYN1mZTeCYAUGx8ABxfW07YkjP8=
go.opentelemetry.io/otel/metric v0.34.0/go.mod h1:ZFuI4yQGNCupurTXCwkeD/zHBt+C2bR7bw5JqUm/AP8=
go.opentelemetry.io/otel/trace v1.11.2 h1:Xf7hWSF2Glv0DE3MH7fBHvtpSBsjcBUe5MYAmZM/+y0=
go.opentelemetry.io/otel/trace v1.11.2/go.mod h1:4N+yC7QEz7TTsG9BSRLNAa63eg5E06ObSbKPmxQ/pKA=
go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
Expand Down
21 changes: 14 additions & 7 deletions node/header_exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (hExService *HeaderExchangeService) initOrAppendHeaderStore(ctx context.Con
if header.Height() == hExService.genesis.InitialHeight {
err = hExService.headerStore.Init(ctx, header)
} else {
_, err = hExService.headerStore.Append(ctx, header)
err = hExService.headerStore.Append(ctx, header)
}
return err
}
Expand Down Expand Up @@ -110,7 +110,7 @@ func (hExService *HeaderExchangeService) Start() error {
var err error
// have to do the initializations here to utilize the p2p node which is created on start
ps := hExService.p2p.PubSub()
hExService.sub = goheaderp2p.NewSubscriber[*types.SignedHeader](ps, pubsub.DefaultMsgIdFn)
hExService.sub = goheaderp2p.NewSubscriber[*types.SignedHeader](ps, pubsub.DefaultMsgIdFn, hExService.genesis.ChainID)
if err = hExService.sub.Start(hExService.ctx); err != nil {
return fmt.Errorf("error while starting subscriber: %w", err)
}
Expand All @@ -131,7 +131,7 @@ func (hExService *HeaderExchangeService) Start() error {
}

peerIDs := hExService.p2p.PeerIDs()
if hExService.ex, err = newP2PExchange(hExService.p2p.Host(), peerIDs, network, hExService.p2p.ConnectionGater()); err != nil {
if hExService.ex, err = newP2PExchange(hExService.p2p.Host(), peerIDs, network, hExService.genesis.ChainID, hExService.p2p.ConnectionGater()); err != nil {
return err
}
if err = hExService.ex.Start(hExService.ctx); err != nil {
Expand Down Expand Up @@ -203,17 +203,24 @@ func newP2PServer(
network string,
opts ...goheaderp2p.Option[goheaderp2p.ServerParameters],
) (*goheaderp2p.ExchangeServer[*types.SignedHeader], error) {
return goheaderp2p.NewExchangeServer[*types.SignedHeader](host, store, network, opts...)
opts = append(opts,
goheaderp2p.WithNetworkID[goheaderp2p.ServerParameters](network),
)
return goheaderp2p.NewExchangeServer[*types.SignedHeader](host, store, opts...)
}

func newP2PExchange(
host host.Host,
peers []peer.ID,
network string,
network, chainID string,
conngater *conngater.BasicConnectionGater,
opts ...goheaderp2p.Option[goheaderp2p.ClientParameters],
) (*goheaderp2p.Exchange[*types.SignedHeader], error) {
return goheaderp2p.NewExchange[*types.SignedHeader](host, peers, network, conngater, opts...)
opts = append(opts,
goheaderp2p.WithNetworkID[goheaderp2p.ClientParameters](network),
goheaderp2p.WithChainID[goheaderp2p.ClientParameters](chainID),
)
return goheaderp2p.NewExchange[*types.SignedHeader](host, peers, conngater, opts...)
}

// newSyncer constructs new Syncer for headers.
Expand All @@ -223,5 +230,5 @@ func newSyncer(
sub header.Subscriber[*types.SignedHeader],
opt goheadersync.Options,
) (*sync.Syncer[*types.SignedHeader], error) {
return sync.NewSyncer(ex, store, sub, opt)
return sync.NewSyncer[*types.SignedHeader](ex, store, sub, opt)
}
60 changes: 16 additions & 44 deletions types/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,60 +75,32 @@ func (h *Header) Time() time.Time {
return time.Unix(int64(h.BaseHeader.Time), 0)
}

func (h *Header) VerifyAdjacent(untrst header.Header) error {
func (h *Header) Verify(untrst header.Header) error {
untrstH, ok := untrst.(*Header)
if !ok {
return &header.VerifyError{
Reason: fmt.Errorf("%T is not of type %T", untrst, h),
}
}

if untrstH.Height() != h.Height()+1 {
return &header.VerifyError{
Reason: fmt.Errorf("headers must be adjacent in height: trusted %d, untrusted %d", h.Height(), untrstH.Height()),
}
// if the header type is wrong, something very bad is going on
// and is a programmer bug
panic(fmt.Errorf("%T is not of type %T", untrst, h))
Comment thread
Wondertan marked this conversation as resolved.
}

// sanity check fields
if err := verifyNewHeaderAndVals(h, untrstH); err != nil {
return &header.VerifyError{Reason: err}
}

// Check the validator hashes are the same
// TODO: next validator set is not available
if !bytes.Equal(untrstH.AggregatorsHash[:], h.AggregatorsHash[:]) {
return &header.VerifyError{
Reason: fmt.Errorf("expected old header next validators (%X) to match those from new header (%X)",
h.AggregatorsHash,
untrstH.AggregatorsHash,
),
}
}

return nil

}

func (h *Header) VerifyNonAdjacent(untrst header.Header) error {
untrstH, ok := untrst.(*Header)
if !ok {
return &header.VerifyError{
Reason: fmt.Errorf("%T is not of type %T", untrst, h),
}
}
// perform actual verification
if untrstH.Height() == h.Height()+1 {
return &header.VerifyError{
Reason: fmt.Errorf(
"headers must be non adjacent in height: trusted %d, untrusted %d",
h.Height(),
untrstH.Height(),
),
// Check the validator hashes are the same in the case headers are adjacent
// TODO: next validator set is not available
if !bytes.Equal(untrstH.AggregatorsHash[:], h.AggregatorsHash[:]) {
return &header.VerifyError{
Reason: fmt.Errorf("expected old header next validators (%X) to match those from new header (%X)",
h.AggregatorsHash,
untrstH.AggregatorsHash,
),
}
}
}

if err := verifyNewHeaderAndVals(h, untrstH); err != nil {
return &header.VerifyError{Reason: err}
}

// TODO: There must be a way to verify non-adjacent headers
// Ensure that untrusted commit has enough of trusted commit's power.
// err := h.ValidatorSet.VerifyCommitLightTrusting(eh.ChainID, untrst.Commit, light.DefaultTrustLevel)
// if err != nil {
Expand Down
20 changes: 6 additions & 14 deletions types/signed_header.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,15 @@ func (sH *SignedHeader) IsZero() bool {
return sH == nil
}

func (sH *SignedHeader) VerifyAdjacent(untrst header.Header) error {
func (sH *SignedHeader) Verify(untrst header.Header) error {
// Explicit type checks are required due to embedded Header which also does the explicit type check
untrstH, ok := untrst.(*SignedHeader)
if !ok {
return &header.VerifyError{
Reason: fmt.Errorf("%T is not of type %T", untrst, sH),
}
// if the header type is wrong, something very bad is going on
Comment thread
Wondertan marked this conversation as resolved.
// and is a programmer bug
panic(fmt.Errorf("%T is not of type %T", untrst, untrstH))
}
return sH.Header.VerifyAdjacent(&untrstH.Header)
return sH.Header.Verify(&untrstH.Header)
Comment thread
Wondertan marked this conversation as resolved.
}

func (sH *SignedHeader) VerifyNonAdjacent(untrst header.Header) error {
untrstH, ok := untrst.(*SignedHeader)
if !ok {
return &header.VerifyError{
Reason: fmt.Errorf("%T is not of type %T", untrst, sH),
}
}
return sH.Header.VerifyNonAdjacent(&untrstH.Header)
}
var _ header.Header = &SignedHeader{}