Skip to content

Commit

Permalink
add mutex to protect the hyperdrive shards
Browse files Browse the repository at this point in the history
  • Loading branch information
tok-kkk committed Sep 25, 2019
1 parent 6ba2ae2 commit 6d4e5fb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
9 changes: 9 additions & 0 deletions hyperdrive.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package hyperdrive

import (
"crypto/ecdsa"
"sync"

"github.com/renproject/hyperdrive/block"
"github.com/renproject/hyperdrive/process"
Expand Down Expand Up @@ -63,6 +64,7 @@ type Hyperdrive interface {
}

type hyperdrive struct {
mu *sync.Mutex
replicas map[Shard]Replica
}

Expand All @@ -73,13 +75,17 @@ func New(options Options, pStorage ProcessStorage, blockStorage BlockStorage, bl
replicas[shard] = replica.New(options, pStorage, blockStorage, blockIterator, validator, observer, broadcaster, shard, privKey)
}
return &hyperdrive{
mu: new(sync.Mutex),
replicas: replicas,
}
}

func (hyper *hyperdrive) Start() {
phi.ParForAll(hyper.replicas, func(shard Shard) {
hyper.mu.Lock()
replica := hyper.replicas[shard]
hyper.mu.Unlock()

replica.Start()
})
}
Expand All @@ -92,6 +98,9 @@ func (hyper *hyperdrive) Rebase(sigs Signatories) {
}

func (hyper *hyperdrive) HandleMessage(message Message) {
hyper.mu.Lock()
defer hyper.mu.Unlock()

replica, ok := hyper.replicas[message.Shard]
if !ok {
return
Expand Down
2 changes: 1 addition & 1 deletion process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ func (p *Process) handlePrevote(prevote *Prevote) {
if !prevote.blockHash.Equal(block.InvalidHash) {
prevoteDebugStr = prevote.blockHash.String()
}
p.logger.Debugf("received prevote=%v at height=%v and round=%v", prevoteDebugStr, prevote.height, prevote.round)
p.logger.Debugf("received prevote= %v at height=%v and round=%v", prevoteDebugStr, prevote.height, prevote.round)
n, _, _, firstTimeExceeding2F, firstTimeExceeding2FOnBlockHash := p.state.Prevotes.Insert(prevote)
if firstTimeExceeding2F && prevote.Height() == p.state.CurrentHeight && prevote.Round() == p.state.CurrentRound && p.state.CurrentStep == StepPrevote {
// upon 2f+1 Prevote{currentHeight, currentRound, *} while step = StepPrevote for the first time
Expand Down

0 comments on commit 6d4e5fb

Please sign in to comment.