Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fix/sync multiple tries #1864

Merged
merged 13 commits into from
Jun 5, 2020
30 changes: 18 additions & 12 deletions api/mock/cacherStub.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@ package mock

// CacherStub -
type CacherStub struct {
ClearCalled func()
PutCalled func(key []byte, value interface{}) (evicted bool)
GetCalled func(key []byte) (value interface{}, ok bool)
HasCalled func(key []byte) bool
PeekCalled func(key []byte) (value interface{}, ok bool)
HasOrAddCalled func(key []byte, value interface{}) (ok, evicted bool)
RemoveCalled func(key []byte)
RemoveOldestCalled func()
KeysCalled func() [][]byte
LenCalled func() int
MaxSizeCalled func() int
RegisterHandlerCalled func(func(key []byte, value interface{}))
ClearCalled func()
PutCalled func(key []byte, value interface{}) (evicted bool)
GetCalled func(key []byte) (value interface{}, ok bool)
HasCalled func(key []byte) bool
PeekCalled func(key []byte) (value interface{}, ok bool)
HasOrAddCalled func(key []byte, value interface{}) (ok, evicted bool)
RemoveCalled func(key []byte)
RemoveOldestCalled func()
KeysCalled func() [][]byte
LenCalled func() int
MaxSizeCalled func() int
RegisterHandlerCalled func(func(key []byte, value interface{}))
UnRegisterHandlerCalled func(func(key []byte, value interface{}))
}

// UnRegisterHandler -
func (cs *CacherStub) UnRegisterHandler(handler func(key []byte, value interface{})) {
cs.UnRegisterHandlerCalled(handler)
SebastianMarian marked this conversation as resolved.
Show resolved Hide resolved
}

// Clear -
Expand Down
7 changes: 5 additions & 2 deletions consensus/mock/cacherMock.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,11 @@ func (cm *CacherMock) MaxSize() int {
}

// RegisterHandler -
func (cm *CacherMock) RegisterHandler(func(key []byte, value interface{})) {
panic("implement me")
func (cm *CacherMock) RegisterHandler(func(key []byte, value interface{}), string) {
SebastianMarian marked this conversation as resolved.
Show resolved Hide resolved
}

// UnRegisterHandler -
func (cm *CacherMock) UnRegisterHandler(_ string) {
}

// IsInterfaceNil returns true if there is no value under the interface
Expand Down
11 changes: 11 additions & 0 deletions core/common.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package core

import (
"crypto/rand"
)

// EmptyChannel empties the given channel
func EmptyChannel(ch chan bool) int {
readsCnt := 0
Expand All @@ -12,3 +16,10 @@ func EmptyChannel(ch chan bool) int {
}
}
}

// UniqueIdentifier returns a unique string identifier
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might add ...of 32 bytes long
Since this will contain unprintable elements, I guess (for the sake of the re usability) to change the return type in []byte

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

func UniqueIdentifier() string {
buff := make([]byte, 32)
_, _ = rand.Read(buff)
return string(buff)
}
7 changes: 6 additions & 1 deletion data/mock/cacherMock.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,12 @@ func (cm *CacherMock) MaxSize() int {
}

// RegisterHandler -
func (cm *CacherMock) RegisterHandler(func(key []byte, value interface{})) {
func (cm *CacherMock) RegisterHandler(func(key []byte, value interface{}), string) {
SebastianMarian marked this conversation as resolved.
Show resolved Hide resolved
return
}

// UnRegisterHandler -
func (cm *CacherMock) UnRegisterHandler(string) {
SebastianMarian marked this conversation as resolved.
Show resolved Hide resolved
return
}

Expand Down
28 changes: 17 additions & 11 deletions data/mock/uint64CacherStub.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@ package mock

// Uint64CacherStub -
type Uint64CacherStub struct {
ClearCalled func()
PutCalled func(uint64, interface{}) bool
GetCalled func(uint64) (interface{}, bool)
HasCalled func(uint64) bool
PeekCalled func(uint64) (interface{}, bool)
HasOrAddCalled func(uint64, interface{}) (bool, bool)
RemoveCalled func(uint64)
RemoveOldestCalled func()
KeysCalled func() []uint64
LenCalled func() int
RegisterHandlerCalled func(handler func(nonce uint64))
ClearCalled func()
PutCalled func(uint64, interface{}) bool
GetCalled func(uint64) (interface{}, bool)
HasCalled func(uint64) bool
PeekCalled func(uint64) (interface{}, bool)
HasOrAddCalled func(uint64, interface{}) (bool, bool)
RemoveCalled func(uint64)
RemoveOldestCalled func()
KeysCalled func() []uint64
LenCalled func() int
RegisterHandlerCalled func(handler func(nonce uint64))
UnRegisterHandlerCalled func(func(key []byte, value interface{}))
}

// UnRegisterHandler -
func (ucs *Uint64CacherStub) UnRegisterHandler(handler func(key []byte, value interface{})) {
ucs.UnRegisterHandlerCalled(handler)
SebastianMarian marked this conversation as resolved.
Show resolved Hide resolved
}

// Clear -
Expand Down
9 changes: 8 additions & 1 deletion data/trie/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"sync"
"time"

"github.com/ElrondNetwork/elrond-go/core"
"github.com/ElrondNetwork/elrond-go/core/check"
"github.com/ElrondNetwork/elrond-go/data"
"github.com/ElrondNetwork/elrond-go/storage"
Expand All @@ -29,6 +30,7 @@ type trieSyncer struct {
requestHandler RequestHandler
interceptedNodes storage.Cacher
mutOperation sync.RWMutex
handlerID string
}

const maxNewMissingAddedPerTurn = 10
Expand Down Expand Up @@ -67,14 +69,19 @@ func NewTrieSyncer(
topic: topic,
shardId: shardId,
waitTimeBetweenRequests: time.Second,
handlerID: core.UniqueIdentifier(),
}
ts.interceptedNodes.RegisterHandler(ts.trieNodeIntercepted)
ts.interceptedNodes.RegisterHandler(ts.trieNodeIntercepted, ts.handlerID)

return ts, nil
}

// StartSyncing completes the trie, asking for missing trie nodes on the network
func (ts *trieSyncer) StartSyncing(rootHash []byte, ctx context.Context) error {
defer func() {
ts.interceptedNodes.UnRegisterHandler(ts.handlerID)
}()

if len(rootHash) == 0 {
return nil
}
Expand Down
32 changes: 19 additions & 13 deletions dataRetriever/mock/cacherStub.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@ package mock

// CacherStub -
type CacherStub struct {
ClearCalled func()
PutCalled func(key []byte, value interface{}, sizeInBytes int) (evicted bool)
GetCalled func(key []byte) (value interface{}, ok bool)
HasCalled func(key []byte) bool
PeekCalled func(key []byte) (value interface{}, ok bool)
HasOrAddCalled func(key []byte, value interface{}, sizeInBytes int) (ok, evicted bool)
RemoveCalled func(key []byte)
RemoveOldestCalled func()
KeysCalled func() [][]byte
LenCalled func() int
MaxSizeCalled func() int
RegisterHandlerCalled func(func(key []byte, value interface{}))
ClearCalled func()
PutCalled func(key []byte, value interface{}, sizeInBytes int) (evicted bool)
GetCalled func(key []byte) (value interface{}, ok bool)
HasCalled func(key []byte) bool
PeekCalled func(key []byte) (value interface{}, ok bool)
HasOrAddCalled func(key []byte, value interface{}, sizeInBytes int) (ok, evicted bool)
RemoveCalled func(key []byte)
RemoveOldestCalled func()
KeysCalled func() [][]byte
LenCalled func() int
MaxSizeCalled func() int
RegisterHandlerCalled func(func(key []byte, value interface{}))
UnRegisterHandlerCalled func(id string)
}

// UnRegisterHandler -
func (cs *CacherStub) UnRegisterHandler(id string) {
cs.UnRegisterHandlerCalled(id)
SebastianMarian marked this conversation as resolved.
Show resolved Hide resolved
}

// Clear -
Expand Down Expand Up @@ -67,7 +73,7 @@ func (cs *CacherStub) MaxSize() int {
}

// RegisterHandler -
func (cs *CacherStub) RegisterHandler(handler func(key []byte, value interface{})) {
func (cs *CacherStub) RegisterHandler(handler func(key []byte, value interface{}), _ string) {
cs.RegisterHandlerCalled(handler)
SebastianMarian marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down
34 changes: 21 additions & 13 deletions epochStart/mock/cacherStub.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,26 @@ package mock

// CacherStub -
type CacherStub struct {
ClearCalled func()
PutCalled func(key []byte, value interface{}, sizeInBytes int) (evicted bool)
GetCalled func(key []byte) (value interface{}, ok bool)
HasCalled func(key []byte) bool
PeekCalled func(key []byte) (value interface{}, ok bool)
HasOrAddCalled func(key []byte, value interface{}, sizeInBytes int) (ok, evicted bool)
RemoveCalled func(key []byte)
RemoveOldestCalled func()
KeysCalled func() [][]byte
LenCalled func() int
MaxSizeCalled func() int
RegisterHandlerCalled func(func(key []byte, value interface{}))
ClearCalled func()
PutCalled func(key []byte, value interface{}, sizeInBytes int) (evicted bool)
GetCalled func(key []byte) (value interface{}, ok bool)
HasCalled func(key []byte) bool
PeekCalled func(key []byte) (value interface{}, ok bool)
HasOrAddCalled func(key []byte, value interface{}, sizeInBytes int) (ok, evicted bool)
RemoveCalled func(key []byte)
RemoveOldestCalled func()
KeysCalled func() [][]byte
LenCalled func() int
MaxSizeCalled func() int
RegisterHandlerCalled func(func(key []byte, value interface{}))
UnRegisterHandlerCalled func(id string)
}

// UnRegisterHandler -
func (cs *CacherStub) UnRegisterHandler(id string) {
if cs.UnRegisterHandlerCalled != nil {
cs.UnRegisterHandlerCalled(id)
}
}

// Clear -
Expand Down Expand Up @@ -67,7 +75,7 @@ func (cs *CacherStub) MaxSize() int {
}

// RegisterHandler -
func (cs *CacherStub) RegisterHandler(handler func(key []byte, value interface{})) {
func (cs *CacherStub) RegisterHandler(handler func(key []byte, value interface{}), _ string) {
if cs.RegisterHandlerCalled != nil {
cs.RegisterHandlerCalled(handler)
}
Expand Down
2 changes: 1 addition & 1 deletion epochStart/shardchain/peerMiniBlocksSyncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func NewPeerMiniBlockSyncer(arguments ArgPeerMiniBlockSyncer) (*peerMiniBlockSyn

//TODO: change the registerHandler for the miniblockPool to call
//directly with hash and value - like func (sp *shardProcessor) receivedMetaBlock
p.miniBlocksPool.RegisterHandler(p.receivedMiniBlock)
p.miniBlocksPool.RegisterHandler(p.receivedMiniBlock, core.UniqueIdentifier())

return p, nil
}
Expand Down
32 changes: 19 additions & 13 deletions integrationTests/mock/cacherStub.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@ package mock

// CacherStub -
type CacherStub struct {
ClearCalled func()
PutCalled func(key []byte, value interface{}, sizeInBytes int) (evicted bool)
GetCalled func(key []byte) (value interface{}, ok bool)
HasCalled func(key []byte) bool
PeekCalled func(key []byte) (value interface{}, ok bool)
HasOrAddCalled func(key []byte, value interface{}, sizeInBytes int) (ok, evicted bool)
RemoveCalled func(key []byte)
RemoveOldestCalled func()
KeysCalled func() [][]byte
LenCalled func() int
MaxSizeCalled func() int
RegisterHandlerCalled func(func(key []byte, value interface{}))
ClearCalled func()
PutCalled func(key []byte, value interface{}, sizeInBytes int) (evicted bool)
GetCalled func(key []byte) (value interface{}, ok bool)
HasCalled func(key []byte) bool
PeekCalled func(key []byte) (value interface{}, ok bool)
HasOrAddCalled func(key []byte, value interface{}, sizeInBytes int) (ok, evicted bool)
RemoveCalled func(key []byte)
RemoveOldestCalled func()
KeysCalled func() [][]byte
LenCalled func() int
MaxSizeCalled func() int
RegisterHandlerCalled func(func(key []byte, value interface{}))
UnRegisterHandlerCalled func(id string)
}

// UnRegisterHandler -
func (cs *CacherStub) UnRegisterHandler(id string) {
cs.UnRegisterHandlerCalled(id)
SebastianMarian marked this conversation as resolved.
Show resolved Hide resolved
}

// Clear -
Expand Down Expand Up @@ -67,7 +73,7 @@ func (cs *CacherStub) MaxSize() int {
}

// RegisterHandler -
func (cs *CacherStub) RegisterHandler(handler func(key []byte, value interface{})) {
func (cs *CacherStub) RegisterHandler(handler func(key []byte, value interface{}), _ string) {
cs.RegisterHandlerCalled(handler)
SebastianMarian marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down