Skip to content

Commit

Permalink
deploy: subscribe to headers, not blocks
Browse files Browse the repository at this point in the history
Header subscription is available since NeoGo 0.105.0 and we don't need full
block data.

Signed-off-by: Roman Khimov <roman@nspcc.ru>
  • Loading branch information
roman-khimov committed Feb 20, 2025
1 parent 9cf9dc7 commit f42712e
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Changelog for NeoFS Contract
### Added

### Changed
- Deployment code uses header subscription instead of block subscription now (#471)

### Updated

Expand Down
4 changes: 2 additions & 2 deletions deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ type Blockchain interface {
// GetContractStateByHash may return non-nil state.Contract along with an error.
GetContractStateByHash(util.Uint160) (*state.Contract, error)

// SubscribeToNewBlocks opens stream of the new blocks persisted in the
// SubscribeToNewHeaders opens stream of the new headers persisted in the
// blockchain and returns channel to read them. The channel is closed only when
// connection to the blockchain is lost and there will be no more events. Caller
// subscribes once, regularly reads events from the channel and is resistant to
// event replay.
SubscribeToNewBlocks() (<-chan *block.Block, error)
SubscribeToNewHeaders() (<-chan *block.Header, error)

// SubscribeToNotaryRequests opens stream of the notary request events from the
// blockchain and returns channel to read them. The channel is closed only when
Expand Down
10 changes: 5 additions & 5 deletions deploy/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ func newBlockchainMonitor(l *zap.Logger, b Blockchain, chNewBlock chan<- struct{
return nil, fmt.Errorf("get current blockchain height: %w", err)
}

blockCh, err := b.SubscribeToNewBlocks()
headerCh, err := b.SubscribeToNewHeaders()
if err != nil {
return nil, fmt.Errorf("subscribe to new blocks of the chain: %w", err)
return nil, fmt.Errorf("subscribe to new headers of the chain: %w", err)
}

res := &blockchainMonitor{
Expand All @@ -70,7 +70,7 @@ func newBlockchainMonitor(l *zap.Logger, b Blockchain, chNewBlock chan<- struct{
go func() {
l.Info("listening to new blocks...")
for {
b, ok := <-blockCh
h, ok := <-headerCh
if !ok {
close(chNewBlock)
close(res.chConnLost)
Expand All @@ -79,7 +79,7 @@ func newBlockchainMonitor(l *zap.Logger, b Blockchain, chNewBlock chan<- struct{
return
}

res.height.Store(b.Index)
res.height.Store(h.Index)

select {
case chNewBlock <- struct{}{}:
Expand All @@ -89,7 +89,7 @@ func newBlockchainMonitor(l *zap.Logger, b Blockchain, chNewBlock chan<- struct{
default:
}

l.Info("new block arrived", zap.Uint32("height", b.Index))
l.Info("new block arrived", zap.Uint32("height", h.Index))
}
}()

Expand Down

0 comments on commit f42712e

Please sign in to comment.