From ea469438153b3be9fb35298e42e86343902ff233 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Thu, 2 Mar 2023 16:44:18 +0300 Subject: [PATCH] services: use buffered channels for block subscription Add a tiny buffer where possible to avoid Blockchain's blocking on new block addition. --- pkg/consensus/consensus.go | 3 ++- pkg/services/notary/notary.go | 7 ++++++- pkg/services/stateroot/service.go | 11 +++++++---- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/pkg/consensus/consensus.go b/pkg/consensus/consensus.go index a2833ac64..a4229e536 100644 --- a/pkg/consensus/consensus.go +++ b/pkg/consensus/consensus.go @@ -89,7 +89,8 @@ type service struct { messages chan Payload transactions chan *transaction.Transaction // blockEvents is used to pass a new block event to the consensus - // process. + // process. It has a tiny buffer in order to avoid Blockchain blocking + // on block addition under the high load. blockEvents chan *coreb.Block lastProposal []util.Uint256 wallet *wallet.Wallet diff --git a/pkg/services/notary/notary.go b/pkg/services/notary/notary.go index dc3d93a85..e3b6bea96 100644 --- a/pkg/services/notary/notary.go +++ b/pkg/services/notary/notary.go @@ -64,7 +64,12 @@ type ( mp *mempool.Pool // requests channel - reqCh chan mempoolevent.Event + reqCh chan mempoolevent.Event + // blocksCh is a channel used to receive block notifications from the + // Blockchain. It is not buffered intentionally, as it's important to keep + // the notary request pool in sync with the current blockchain heigh, thus, + // it's not recommended to use a large size of notary requests pool as it may + // slow down the block processing. blocksCh chan *block.Block stopCh chan struct{} done chan struct{} diff --git a/pkg/services/stateroot/service.go b/pkg/services/stateroot/service.go index 4dd21e42b..32de50124 100644 --- a/pkg/services/stateroot/service.go +++ b/pkg/services/stateroot/service.go @@ -63,9 +63,12 @@ type ( timePerBlock time.Duration maxRetries int relayExtensible RelayCallback - blockCh chan *block.Block - stopCh chan struct{} - done chan struct{} + // blockCh is a channel used to receive block notifications from the + // Blockchain. It has a tiny buffer in order to avoid Blockchain blocking + // on block addition under the high load. + blockCh chan *block.Block + stopCh chan struct{} + done chan struct{} } ) @@ -84,7 +87,7 @@ func New(cfg config.StateRoot, sm *stateroot.Module, log *zap.Logger, bc Ledger, chain: bc, log: log, incompleteRoots: make(map[uint32]*incompleteRoot), - blockCh: make(chan *block.Block), + blockCh: make(chan *block.Block, 1), stopCh: make(chan struct{}), done: make(chan struct{}), timePerBlock: bcConf.TimePerBlock,