Skip to content

Commit

Permalink
refactor(service/state): CoreAccessor relies on header.Head inste…
Browse files Browse the repository at this point in the history
…ad of `header.Getter` (#1088)

* refactor(service/state): Remove unused header.Getter from state service and lint core_access.go imports

* refactor(header): Extract Head method from Getter into its own interface `Head`

* feat(header/sync): Syncer implements header.Head

* doc(header/sync): document Head method better

* refactor(service/state): CoreAccessor takes header.Head instead of header.Getter
  • Loading branch information
renaynay committed Sep 19, 2022
1 parent 1803ca0 commit b37a844
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 16 deletions.
11 changes: 9 additions & 2 deletions header/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ type Store interface {
// Getter contains the behavior necessary for a component to retrieve
// headers that have been processed during header sync.
type Getter interface {
// Head returns the ExtendedHeader of the chain head.
Head(context.Context) (*ExtendedHeader, error)
Head

// Get returns the ExtendedHeader corresponding to the given hash.
Get(context.Context, tmbytes.HexBytes) (*ExtendedHeader, error)
Expand All @@ -116,3 +115,11 @@ type Getter interface {
// GetRangeByHeight returns the given range [from:to) of ExtendedHeaders.
GetRangeByHeight(ctx context.Context, from, to uint64) ([]*ExtendedHeader, error)
}

// Head contains the behavior necessary for a component to retrieve
// the chain head. Note that "chain head" is subjective to the component
// reporting it.
type Head interface {
// Head returns the latest known header.
Head(context.Context) (*ExtendedHeader, error)
}
6 changes: 6 additions & 0 deletions header/sync/sync_head.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ import (
"github.com/celestiaorg/celestia-node/header"
)

// Head returns the Syncer's latest known header. It calls 'networkHead' in order to
// either return or eagerly fetch the most recent header.
func (s *Syncer) Head(ctx context.Context) (*header.ExtendedHeader, error) {
return s.networkHead(ctx)
}

// subjectiveHead returns the latest known local header that is not expired(within trusting period).
// If the header is expired, it is retrieved from a trusted peer without validation;
// in other words, an automatic subjective initialization is performed.
Expand Down
9 changes: 5 additions & 4 deletions node/state/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import (
"go.uber.org/fx"

apptypes "github.com/celestiaorg/celestia-app/x/payment/types"
"github.com/celestiaorg/celestia-node/header"

"github.com/celestiaorg/celestia-node/header/sync"
"github.com/celestiaorg/celestia-node/service/state"
)

Expand All @@ -14,9 +15,9 @@ func CoreAccessor(
coreIP,
coreRPC,
coreGRPC string,
) func(fx.Lifecycle, *apptypes.KeyringSigner, header.Store) (state.Accessor, error) {
return func(lc fx.Lifecycle, signer *apptypes.KeyringSigner, getter header.Store) (state.Accessor, error) {
ca := state.NewCoreAccessor(signer, getter, coreIP, coreRPC, coreGRPC)
) func(fx.Lifecycle, *apptypes.KeyringSigner, *sync.Syncer) (state.Accessor, error) {
return func(lc fx.Lifecycle, signer *apptypes.KeyringSigner, syncer *sync.Syncer) (state.Accessor, error) {
ca := state.NewCoreAccessor(signer, syncer, coreIP, coreRPC, coreGRPC)
lc.Append(fx.Hook{
OnStart: ca.Start,
OnStop: ca.Stop,
Expand Down
4 changes: 1 addition & 3 deletions node/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"go.uber.org/fx"

"github.com/celestiaorg/celestia-node/fraud"
"github.com/celestiaorg/celestia-node/header"
"github.com/celestiaorg/celestia-node/libs/fxutil"
"github.com/celestiaorg/celestia-node/node/core"
"github.com/celestiaorg/celestia-node/node/key"
Expand All @@ -32,10 +31,9 @@ func Service(
ctx context.Context,
lc fx.Lifecycle,
accessor state.Accessor,
store header.Store,
fservice fraud.Service,
) *state.Service {
serv := state.NewService(accessor, store)
serv := state.NewService(accessor)
lifecycleCtx := fxutil.WithLifecycle(ctx, lc)
lc.Append(fx.Hook{
OnStart: func(startCtx context.Context) error {
Expand Down
4 changes: 2 additions & 2 deletions service/state/core_access.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var log = logging.Logger("state")
// with a celestia-core node.
type CoreAccessor struct {
signer *apptypes.KeyringSigner
getter header.Getter
getter header.Head

queryCli banktypes.QueryClient
stakingCli stakingtypes.QueryClient
Expand All @@ -47,7 +47,7 @@ type CoreAccessor struct {
// connection.
func NewCoreAccessor(
signer *apptypes.KeyringSigner,
getter header.Getter,
getter header.Head,
coreIP,
rpcPort string,
grpcPort string,
Expand Down
6 changes: 1 addition & 5 deletions service/state/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/cosmos/cosmos-sdk/x/staking/types"

"github.com/celestiaorg/celestia-node/header"
"github.com/celestiaorg/nmt/namespace"
)

Expand All @@ -16,15 +15,12 @@ type Service struct {
cancel context.CancelFunc

accessor Accessor

getter header.Getter
}

// NewService constructs a new state Service.
func NewService(accessor Accessor, getter header.Getter) *Service {
func NewService(accessor Accessor) *Service {
return &Service{
accessor: accessor,
getter: getter,
}
}

Expand Down

0 comments on commit b37a844

Please sign in to comment.