Skip to content

Commit

Permalink
deploy: Stop monitor after non system contract deploy
Browse files Browse the repository at this point in the history
Signed-off-by: Evgenii Baidakov <[email protected]>
  • Loading branch information
smallhive committed Apr 18, 2024
1 parent d7835ce commit 2f5b8ab
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
2 changes: 2 additions & 0 deletions deploy/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ func Contract(
return util.Uint160{}, fmt.Errorf("init blockchain monitor: %w", err)
}

defer monitor.stop()

syncPrm := syncNeoFSContractPrm{
logger: prm.Logger,
blockchain: prm.Blockchain,
Expand Down
12 changes: 12 additions & 0 deletions deploy/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type blockchainMonitor struct {
height atomic.Uint32

chConnLost chan struct{}
chExit chan struct{}
}

// newBlockchainMonitor constructs and runs monitor for the given Blockchain.
Expand All @@ -61,6 +62,7 @@ func newBlockchainMonitor(l *zap.Logger, b Blockchain, chNewBlock chan<- struct{
blockchain: b,
blockInterval: time.Duration(ver.Protocol.MillisecondsPerBlock) * time.Millisecond,
chConnLost: make(chan struct{}),
chExit: make(chan struct{}),
}

res.height.Store(initialBlock)
Expand All @@ -72,6 +74,7 @@ func newBlockchainMonitor(l *zap.Logger, b Blockchain, chNewBlock chan<- struct{
if !ok {
close(chNewBlock)
close(res.chConnLost)
close(res.chExit)
l.Info("new blocks channel is closed, listening stopped")
return
}
Expand All @@ -80,6 +83,9 @@ func newBlockchainMonitor(l *zap.Logger, b Blockchain, chNewBlock chan<- struct{

select {
case chNewBlock <- struct{}{}:
case <-res.chExit:
l.Info("monitoring new blocks channel is closed, listening stopped")
return
default:
}

Expand All @@ -95,6 +101,12 @@ func (x *blockchainMonitor) currentHeight() uint32 {
return x.height.Load()
}

// currentHeight returns current blockchain height.
func (x *blockchainMonitor) stop() {
x.chExit <- struct{}{}
close(x.chExit)
}

// waitForNextBlock blocks until blockchainMonitor encounters new block on the
// chain, underlying connection with the [Blockchain] is lost or provided
// context is done (returns context error).
Expand Down

0 comments on commit 2f5b8ab

Please sign in to comment.