From 0b6da0ff84db533e1a4ad6a70786648dce77bb3d Mon Sep 17 00:00:00 2001 From: GarmashAlex Date: Tue, 7 Jan 2025 22:00:50 +0300 Subject: [PATCH 1/8] Update fetcher.go --- core/fetcher.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/core/fetcher.go b/core/fetcher.go index f2b160e108..101ba2e04d 100644 --- a/core/fetcher.go +++ b/core/fetcher.go @@ -1,3 +1,5 @@ +// Package core provides fundamental blockchain interaction functionality, +// including block fetching, subscription handling, and validator set management. package core import ( From 2ae10dccfbd244bbde08c630c4b65895288300ac Mon Sep 17 00:00:00 2001 From: GarmashAlex Date: Tue, 7 Jan 2025 22:01:11 +0300 Subject: [PATCH 2/8] Update fetcher.go --- core/fetcher.go | 1 + 1 file changed, 1 insertion(+) diff --git a/core/fetcher.go b/core/fetcher.go index 101ba2e04d..50d4de84d6 100644 --- a/core/fetcher.go +++ b/core/fetcher.go @@ -14,6 +14,7 @@ import ( libhead "github.com/celestiaorg/go-header" ) +// Constants for event subscription const newBlockSubscriber = "NewBlock/Events" var ( From 7ef6ed595cc81f06c0c96a41e789a50c8bf08d5c Mon Sep 17 00:00:00 2001 From: GarmashAlex Date: Tue, 7 Jan 2025 22:02:02 +0300 Subject: [PATCH 3/8] Update fetcher.go --- core/fetcher.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/core/fetcher.go b/core/fetcher.go index 50d4de84d6..10ea178c03 100644 --- a/core/fetcher.go +++ b/core/fetcher.go @@ -18,7 +18,9 @@ import ( const newBlockSubscriber = "NewBlock/Events" var ( + // log is the package-level logger log = logging.Logger("core") + // newDataSignedBlockQuery defines the query string for subscribing to signed block events newDataSignedBlockQuery = types.QueryForEvent(types.EventSignedBlock).String() ) From 3cc8654464c5445b4033f5a90fab4284dc89b993 Mon Sep 17 00:00:00 2001 From: GarmashAlex Date: Tue, 7 Jan 2025 22:02:54 +0300 Subject: [PATCH 4/8] Update fetcher.go --- core/fetcher.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/core/fetcher.go b/core/fetcher.go index 10ea178c03..0abb199d14 100644 --- a/core/fetcher.go +++ b/core/fetcher.go @@ -24,6 +24,8 @@ var ( newDataSignedBlockQuery = types.QueryForEvent(types.EventSignedBlock).String() ) +// BlockFetcher provides functionality to fetch blocks, commits, and validator sets +// from a Tendermint Core node. It also supports subscribing to new block events. type BlockFetcher struct { client Client From 7cca178a829012aeb540d236d23c3665da358f9a Mon Sep 17 00:00:00 2001 From: GarmashAlex Date: Tue, 7 Jan 2025 22:03:23 +0300 Subject: [PATCH 5/8] Update fetcher.go --- core/fetcher.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/core/fetcher.go b/core/fetcher.go index 0abb199d14..90450d3998 100644 --- a/core/fetcher.go +++ b/core/fetcher.go @@ -40,7 +40,9 @@ func NewBlockFetcher(client Client) *BlockFetcher { } } -// GetBlockInfo queries Core for additional block information, like Commit and ValidatorSet. +// GetBlockInfo queries Core for additional block information, including Commit and ValidatorSet. +// If height is nil, it fetches information for the latest block. +// Returns an error if either the commit or validator set cannot be retrieved. func (f *BlockFetcher) GetBlockInfo(ctx context.Context, height *int64) (*types.Commit, *types.ValidatorSet, error) { commit, err := f.Commit(ctx, height) if err != nil { From 11e691b876a202af3fca68517be6291d157c5c57 Mon Sep 17 00:00:00 2001 From: GarmashAlex Date: Tue, 7 Jan 2025 22:03:55 +0300 Subject: [PATCH 6/8] Update fetcher.go --- core/fetcher.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/fetcher.go b/core/fetcher.go index 90450d3998..5c109a7c40 100644 --- a/core/fetcher.go +++ b/core/fetcher.go @@ -46,7 +46,7 @@ func NewBlockFetcher(client Client) *BlockFetcher { func (f *BlockFetcher) GetBlockInfo(ctx context.Context, height *int64) (*types.Commit, *types.ValidatorSet, error) { commit, err := f.Commit(ctx, height) if err != nil { - return nil, nil, fmt.Errorf("core/fetcher: getting commit at height %d: %w", height, err) + return nil, nil, fmt.Errorf("failed to get commit at height %v: %w", height, err) } // If a nil `height` is given as a parameter, there is a chance From 860cc1f266a69001057634dea632a9a3205d54a9 Mon Sep 17 00:00:00 2001 From: GarmashAlex Date: Tue, 7 Jan 2025 22:04:25 +0300 Subject: [PATCH 7/8] Update fetcher.go --- core/fetcher.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/fetcher.go b/core/fetcher.go index 5c109a7c40..1e1c02f16e 100644 --- a/core/fetcher.go +++ b/core/fetcher.go @@ -56,7 +56,7 @@ func (f *BlockFetcher) GetBlockInfo(ctx context.Context, height *int64) (*types. // prevent this potential inconsistency. valSet, err := f.ValidatorSet(ctx, &commit.Height) if err != nil { - return nil, nil, fmt.Errorf("core/fetcher: getting validator set at height %d: %w", height, err) + return nil, nil, fmt.Errorf("failed to get validator set at height %v: %w", height, err) } return commit, valSet, nil From 7151335fc95594321164efb01f6e540e64ccb793 Mon Sep 17 00:00:00 2001 From: GarmashAlex Date: Tue, 7 Jan 2025 22:06:58 +0300 Subject: [PATCH 8/8] docs: improve documentation and error handling in core/fetcher.go --- core/fetcher.go | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/core/fetcher.go b/core/fetcher.go index 1e1c02f16e..4add99fe9c 100644 --- a/core/fetcher.go +++ b/core/fetcher.go @@ -59,31 +59,30 @@ func (f *BlockFetcher) GetBlockInfo(ctx context.Context, height *int64) (*types. return nil, nil, fmt.Errorf("failed to get validator set at height %v: %w", height, err) } - return commit, valSet, nil -} - -// GetBlock queries Core for a `Block` at the given height. +// GetBlock retrieves a block at the specified height from Core. +// If height is nil, it fetches the latest block. func (f *BlockFetcher) GetBlock(ctx context.Context, height *int64) (*types.Block, error) { res, err := f.client.Block(ctx, height) if err != nil { - return nil, err + return nil, fmt.Errorf("failed to get block: %w", err) } if res != nil && res.Block == nil { - return nil, fmt.Errorf("core/fetcher: block not found, height: %d", height) + return nil, fmt.Errorf("block not found at height %v", height) } return res.Block, nil } +// GetBlockByHash retrieves a block with the specified hash from Core. func (f *BlockFetcher) GetBlockByHash(ctx context.Context, hash libhead.Hash) (*types.Block, error) { res, err := f.client.BlockByHash(ctx, hash) if err != nil { - return nil, err + return nil, fmt.Errorf("failed to get block by hash: %w", err) } if res != nil && res.Block == nil { - return nil, fmt.Errorf("core/fetcher: block not found, hash: %s", hash.String()) + return nil, fmt.Errorf("block not found with hash %s", hash.String()) } return res.Block, nil @@ -132,12 +131,12 @@ func (f *BlockFetcher) ValidatorSet(ctx context.Context, height *int64) (*types. return types.NewValidatorSet(vals), nil } -// SubscribeNewBlockEvent subscribes to new block events from Core, returning -// a new block event channel on success. +// SubscribeNewBlockEvent subscribes to new block events from Core. +// Returns a channel that receives new block events and an error if the subscription fails. +// The subscription can be cancelled using UnsubscribeNewBlockEvent or when the context is cancelled. func (f *BlockFetcher) SubscribeNewBlockEvent(ctx context.Context) (<-chan types.EventDataSignedBlock, error) { - // start the client if not started yet if !f.client.IsRunning() { - return nil, errors.New("client not running") + return nil, errors.New("client is not running") } ctx, cancel := context.WithCancel(ctx) @@ -146,7 +145,7 @@ func (f *BlockFetcher) SubscribeNewBlockEvent(ctx context.Context) (<-chan types eventChan, err := f.client.Subscribe(ctx, newBlockSubscriber, newDataSignedBlockQuery) if err != nil { - return nil, err + return nil, fmt.Errorf("failed to subscribe to new blocks: %w", err) } signedBlockCh := make(chan types.EventDataSignedBlock) @@ -159,7 +158,7 @@ func (f *BlockFetcher) SubscribeNewBlockEvent(ctx context.Context) (<-chan types return case newEvent, ok := <-eventChan: if !ok { - log.Errorw("fetcher: new blocks subscription channel closed unexpectedly") + log.Errorw("new blocks subscription channel closed unexpectedly") return } signedBlock := newEvent.Data.(types.EventDataSignedBlock) @@ -196,3 +195,4 @@ func (f *BlockFetcher) IsSyncing(ctx context.Context) (bool, error) { } return resp.SyncInfo.CatchingUp, nil } +