From 2977e1cf9c0338d1a1d6bf8ccd74d97486bd9abf Mon Sep 17 00:00:00 2001 From: millken Date: Tue, 12 Jul 2022 16:28:20 +0800 Subject: [PATCH 1/5] Fix #3520 --- blockchain/integrity/integrity_test.go | 13 ++++--------- blockchain/pubsubmanager.go | 7 ++++--- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/blockchain/integrity/integrity_test.go b/blockchain/integrity/integrity_test.go index a635aa6aa7..4aecc1da05 100644 --- a/blockchain/integrity/integrity_test.go +++ b/blockchain/integrity/integrity_test.go @@ -11,7 +11,7 @@ import ( "encoding/hex" "fmt" "math/big" - "sync" + "sync/atomic" "testing" "time" @@ -832,22 +832,17 @@ func TestBlockchain_MintNewBlock_PopAccount(t *testing.T) { } type MockSubscriber struct { - counter int - mu sync.RWMutex + counter int32 } func (ms *MockSubscriber) ReceiveBlock(blk *block.Block) error { - ms.mu.Lock() tsfs, _ := classifyActions(blk.Actions) - ms.counter += len(tsfs) - ms.mu.Unlock() + atomic.AddInt32(&ms.counter, int32(len(tsfs))) return nil } func (ms *MockSubscriber) Counter() int { - ms.mu.RLock() - defer ms.mu.RUnlock() - return ms.counter + return int(atomic.LoadInt32(&ms.counter)) } func TestConstantinople(t *testing.T) { diff --git a/blockchain/pubsubmanager.go b/blockchain/pubsubmanager.go index 5c251174e4..cf1f09b300 100644 --- a/blockchain/pubsubmanager.go +++ b/blockchain/pubsubmanager.go @@ -93,9 +93,10 @@ func (ps *pubSub) RemoveBlockListener(s BlockCreationSubscriber) error { // SendBlockToSubscribers sends block to every subscriber by using buffer channel func (ps *pubSub) SendBlockToSubscribers(blk *block.Block) { - ps.lock.Lock() - defer ps.lock.Unlock() - for _, elem := range ps.blocklisteners { + ps.lock.RLock() + defer ps.lock.RUnlock() + blocklisteners := ps.blocklisteners + for _, elem := range blocklisteners { elem.pendingBlksBuffer <- blk } } From 5d07d3fb22e8a4419bb46d448f79a701256337a9 Mon Sep 17 00:00:00 2001 From: millken Date: Tue, 12 Jul 2022 16:38:53 +0800 Subject: [PATCH 2/5] Fix #3520 --- blockchain/integrity/integrity_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/blockchain/integrity/integrity_test.go b/blockchain/integrity/integrity_test.go index 4aecc1da05..3cd7b30823 100644 --- a/blockchain/integrity/integrity_test.go +++ b/blockchain/integrity/integrity_test.go @@ -1137,6 +1137,8 @@ func TestLoadBlockchainfromDB(t *testing.T) { height := bc.TipHeight() fmt.Printf("Open blockchain pass, height = %d\n", height) require.NoError(addTestingTsfBlocks(cfg, bc, dao, ap)) + //make sure pubsub is completed + time.Sleep(time.Millisecond * 200) require.NoError(bc.Stop(ctx)) require.Equal(24, ms.Counter()) From 47bff0a9ec5defef30369d615baf7ef47b1d83ed Mon Sep 17 00:00:00 2001 From: millken Date: Wed, 13 Jul 2022 13:29:22 +0800 Subject: [PATCH 3/5] fix comments --- blockchain/pubsubmanager.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/blockchain/pubsubmanager.go b/blockchain/pubsubmanager.go index cf1f09b300..1503e1bbcd 100644 --- a/blockchain/pubsubmanager.go +++ b/blockchain/pubsubmanager.go @@ -95,8 +95,7 @@ func (ps *pubSub) RemoveBlockListener(s BlockCreationSubscriber) error { func (ps *pubSub) SendBlockToSubscribers(blk *block.Block) { ps.lock.RLock() defer ps.lock.RUnlock() - blocklisteners := ps.blocklisteners - for _, elem := range blocklisteners { + for _, elem := range ps.blocklisteners { elem.pendingBlksBuffer <- blk } } From 1bb6f41493280d2196206fc23a9279ea5e7072b6 Mon Sep 17 00:00:00 2001 From: millken Date: Thu, 14 Jul 2022 10:40:37 +0800 Subject: [PATCH 4/5] Update test, fix comments --- blockchain/integrity/integrity_test.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/blockchain/integrity/integrity_test.go b/blockchain/integrity/integrity_test.go index 3cd7b30823..901d9a52cc 100644 --- a/blockchain/integrity/integrity_test.go +++ b/blockchain/integrity/integrity_test.go @@ -1138,8 +1138,11 @@ func TestLoadBlockchainfromDB(t *testing.T) { fmt.Printf("Open blockchain pass, height = %d\n", height) require.NoError(addTestingTsfBlocks(cfg, bc, dao, ap)) //make sure pubsub is completed - time.Sleep(time.Millisecond * 200) - require.NoError(bc.Stop(ctx)) + err = testutil.WaitUntil(200*time.Millisecond, 3*time.Second, func() (bool, error) { + err = bc.Stop(ctx) + return err == nil, err + }) + require.NoError(err) require.Equal(24, ms.Counter()) // Load a blockchain from DB From f94a64a167915ea147ccaa094f2ad395ea582f07 Mon Sep 17 00:00:00 2001 From: millken Date: Thu, 14 Jul 2022 16:07:27 +0800 Subject: [PATCH 5/5] fix issue in comments --- blockchain/integrity/integrity_test.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/blockchain/integrity/integrity_test.go b/blockchain/integrity/integrity_test.go index 901d9a52cc..8bf890d14a 100644 --- a/blockchain/integrity/integrity_test.go +++ b/blockchain/integrity/integrity_test.go @@ -1139,11 +1139,10 @@ func TestLoadBlockchainfromDB(t *testing.T) { require.NoError(addTestingTsfBlocks(cfg, bc, dao, ap)) //make sure pubsub is completed err = testutil.WaitUntil(200*time.Millisecond, 3*time.Second, func() (bool, error) { - err = bc.Stop(ctx) - return err == nil, err + return 24 == ms.Counter(), nil }) require.NoError(err) - require.Equal(24, ms.Counter()) + require.NoError(bc.Stop(ctx)) // Load a blockchain from DB bc = blockchain.NewBlockchain(