Skip to content

Commit

Permalink
Merge branch 'development' into eclesio/refactor-peerstate
Browse files Browse the repository at this point in the history
  • Loading branch information
EclesioMeloJunior committed Feb 7, 2022
2 parents a93fafe + fbd13d2 commit 858a72b
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 184 deletions.
5 changes: 3 additions & 2 deletions dot/network/inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ func (s *Service) readStream(stream libp2pnetwork.Stream, decoder messageDecoder
s.streamManager.logNewStream(stream)

peer := stream.Conn().RemotePeer()
msgBytes := s.bufPool.get()
defer s.bufPool.put(msgBytes)
buffer := s.bufPool.Get().(*[]byte)
defer s.bufPool.Put(buffer)
msgBytes := *buffer

for {
n, err := readStream(stream, msgBytes[:])
Expand Down
10 changes: 5 additions & 5 deletions dot/network/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,11 +427,11 @@ func (s *Service) readHandshake(stream libp2pnetwork.Stream, decoder HandshakeDe
hsC := make(chan *handshakeReader)

go func() {
msgBytes := s.bufPool.get()
defer func() {
s.bufPool.put(msgBytes)
close(hsC)
}()
defer close(hsC)

buffer := s.bufPool.Get().(*[]byte)
defer s.bufPool.Put(buffer)
msgBytes := *buffer

tot, err := readStream(stream, msgBytes[:])
if err != nil {
Expand Down
43 changes: 0 additions & 43 deletions dot/network/pool.go

This file was deleted.

114 changes: 0 additions & 114 deletions dot/network/pool_test.go

This file was deleted.

16 changes: 6 additions & 10 deletions dot/network/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ type Service struct {
host *host
mdns *mdns
gossip *gossip
bufPool *sizedBufferPool
bufPool *sync.Pool
streamManager *streamManager

notificationsProtocols map[byte]*notificationsProtocol // map of sub-protocol msg ID to protocol info
Expand Down Expand Up @@ -182,16 +182,12 @@ func NewService(cfg *Config) (*Service, error) {
return nil, err
}

// pre-allocate pool of buffers used to read from streams.
// initially allocate as many buffers as likely necessary which is the number of inbound streams we will have,
// which should equal the average number of peers times the number of notifications protocols, which is currently 3.
preAllocateInPool := cfg.MinPeers * 3
poolSize := cfg.MaxPeers * 3
if cfg.noPreAllocate { // testing
preAllocateInPool = 0
poolSize = cfg.MinPeers * 3
bufPool := &sync.Pool{
New: func() interface{} {
b := make([]byte, maxMessageSize)
return &b
},
}
bufPool := newSizedBufferPool(preAllocateInPool, poolSize)

network := &Service{
ctx: ctx,
Expand Down
1 change: 0 additions & 1 deletion lib/trie/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ func (t *Trie) loadProof(proofHashToNode map[string]Node, n Node) {
if !ok {
continue
}
delete(proofHashToNode, proofHash)

branch.Children[i] = node
t.loadProof(proofHashToNode, node)
Expand Down
82 changes: 73 additions & 9 deletions lib/trie/proof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,10 @@ func testGenerateProof(t *testing.T, entries []Pair, keys [][]byte) ([]byte, [][
value := trie.Get(key)
require.NotNil(t, value)

itemFromDB := Pair{
items[idx] = Pair{
Key: key,
Value: value,
}
items[idx] = itemFromDB
}

return root, proof, items
Expand All @@ -95,23 +94,23 @@ func TestVerifyProof_ShouldReturnTrue(t *testing.T) {
{Key: []byte("alpha"), Value: make([]byte, 32)},
{Key: []byte("bravo"), Value: []byte("bravo")},
{Key: []byte("do"), Value: []byte("verb")},
{Key: []byte("dog"), Value: []byte("puppy")},
{Key: []byte("doge"), Value: make([]byte, 32)},
{Key: []byte("dogea"), Value: []byte("puppy")},
{Key: []byte("dogeb"), Value: []byte("puppy")},
{Key: []byte("horse"), Value: []byte("stallion")},
{Key: []byte("house"), Value: []byte("building")},
}

keys := [][]byte{
[]byte("do"),
[]byte("dog"),
[]byte("doge"),
[]byte("dogea"),
[]byte("dogeb"),
}

root, proof, pl := testGenerateProof(t, entries, keys)
root, proof, pairs := testGenerateProof(t, entries, keys)
v, err := VerifyProof(proof, root, pairs)

v, err := VerifyProof(proof, root, pl)
require.True(t, v)
require.NoError(t, err)
require.True(t, v)
}

func TestVerifyProof_ShouldReturnDuplicateKeysError(t *testing.T) {
Expand Down Expand Up @@ -158,3 +157,68 @@ func TestVerifyProof_ShouldReturnTrueWithouCompareValues(t *testing.T) {
require.True(t, v)
require.NoError(t, err)
}

func TestBranchNodes_SameHash_DiferentPaths_GenerateAndVerifyProof(t *testing.T) {
value := []byte("somevalue")
entries := []Pair{
{Key: []byte("d"), Value: value},
{Key: []byte("b"), Value: value},
{Key: []byte("dxyz"), Value: value},
{Key: []byte("bxyz"), Value: value},
{Key: []byte("dxyzi"), Value: value},
{Key: []byte("bxyzi"), Value: value},
}

keys := [][]byte{
[]byte("d"),
[]byte("b"),
[]byte("dxyz"),
[]byte("bxyz"),
[]byte("dxyzi"),
[]byte("bxyzi"),
}

root, proof, pairs := testGenerateProof(t, entries, keys)

ok, err := VerifyProof(proof, root, pairs)
require.NoError(t, err)
require.True(t, ok)
}

func TestLeafNodes_SameHash_DifferentPaths_GenerateAndVerifyProof(t *testing.T) {
tmp := t.TempDir()

memdb, err := chaindb.NewBadgerDB(&chaindb.Config{
InMemory: true,
DataDir: tmp,
})
require.NoError(t, err)

var (
value = []byte("somevalue")
key1 = []byte("worlda")
key2 = []byte("worldb")
)

tt := NewEmptyTrie()
tt.Put(key1, value)
tt.Put(key2, value)

err = tt.Store(memdb)
require.NoError(t, err)

hash, err := tt.Hash()
require.NoError(t, err)

proof, err := GenerateProof(hash.ToBytes(), [][]byte{key1, key2}, memdb)
require.NoError(t, err)

pairs := []Pair{
{Key: key1, Value: value},
{Key: key2, Value: value},
}

ok, err := VerifyProof(proof, hash.ToBytes(), pairs)
require.NoError(t, err)
require.True(t, ok)
}

0 comments on commit 858a72b

Please sign in to comment.