Commit bc884145 authored by Matthew Sykes's avatar Matthew Sykes
Browse files

[FAB-9152] fix data race in TestManagerImpl



Introduce a RWMutex to the memory backed ledger to enable concurrent use
by tests.

Change-Id: I964ee2d6ff0fe6e8369702477631e7c0e0251cbc
Signed-off-by: default avatarMatthew Sykes <sykesmat@us.ibm.com>
parent c6d8eddd
......@@ -19,6 +19,7 @@ package ramledger
import (
"bytes"
"fmt"
"sync"
"github.com/hyperledger/fabric/common/flogging"
"github.com/hyperledger/fabric/common/ledger/blockledger"
......@@ -46,6 +47,7 @@ type simpleList struct {
}
type ramLedger struct {
lock sync.RWMutex
maxSize int
size int
oldest *simpleList
......@@ -76,6 +78,9 @@ func (cu *cursor) Close() {}
// Iterator returns an Iterator, as specified by a ab.SeekInfo message, and its
// starting block number
func (rl *ramLedger) Iterator(startPosition *ab.SeekPosition) (blockledger.Iterator, uint64) {
rl.lock.RLock()
defer rl.lock.RUnlock()
var list *simpleList
switch start := startPosition.Type.(type) {
case *ab.SeekPosition_Oldest:
......@@ -138,11 +143,16 @@ func (rl *ramLedger) Iterator(startPosition *ab.SeekPosition) (blockledger.Itera
// Height returns the number of blocks on the ledger
func (rl *ramLedger) Height() uint64 {
rl.lock.RLock()
defer rl.lock.RUnlock()
return rl.newest.block.Header.Number + 1
}
// Append appends a new block to the ledger
func (rl *ramLedger) Append(block *cb.Block) error {
rl.lock.Lock()
defer rl.lock.Unlock()
if block.Header.Number != rl.newest.block.Header.Number+1 {
return fmt.Errorf("Block number should have been %d but was %d",
rl.newest.block.Header.Number+1, block.Header.Number)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment