The core protocol of WoopChain
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
woop/p2p/stream/common/streammanager/interface_test.go

216 lines
4.4 KiB

package streammanager
import (
"context"
"errors"
"strconv"
"sync"
"sync/atomic"
sttypes "github.com/harmony-one/harmony/p2p/stream/types"
Release Candidate: dev -> main (#4319) * Rebase dev branch to current main branch (#4318) * add openssl compatibility on m2 chips using darwin (#4302) Adds support for OpenSSL on MacOS Ventura using m2 chips. * [dumpdb] ensure each cross link is dumped (#4311) * bump libp2p to version 0.24.0 and update its dependencies and relevant tests (#4315) * Removed legacy syncing peer provider. (#4260) * Removed legacy syncing peer provider. * Fix localnet. * Fix migrate version. * Rebased on main. * Fix formatting. * Remove blockchain dependency from engine. (#4310) * Consensus doesn't require anymore `Node` as a circular dependency. * Rebased upon main. * Removed engine beacon chain dependency. * Fixed nil error. * Fixed error. * bump libp2p to version 0.24.0 and update its dependencies and relevant tests * fix format, remove wrongly added configs * add back wrongly deleted comment * fix travis go checker Co-authored-by: Konstantin <355847+Frozen@users.noreply.github.com> Co-authored-by: “GheisMohammadi” <“Gheis.Mohammadi@gmail.com”> * bump libp2p to version 0.24.0 and update its dependencies and relevant tests (#4315) * Removed legacy syncing peer provider. (#4260) * Removed legacy syncing peer provider. * Fix localnet. * Fix migrate version. * Rebased on main. * Fix formatting. * Remove blockchain dependency from engine. (#4310) * Consensus doesn't require anymore `Node` as a circular dependency. * Rebased upon main. * Removed engine beacon chain dependency. * Fixed nil error. * Fixed error. * bump libp2p to version 0.24.0 and update its dependencies and relevant tests * fix format, remove wrongly added configs * add back wrongly deleted comment * fix travis go checker Co-authored-by: Konstantin <355847+Frozen@users.noreply.github.com> Co-authored-by: “GheisMohammadi” <“Gheis.Mohammadi@gmail.com”> * Fix for consensus stuck. (#4307) * Added check for block validity. * Starts new view change if block invalid. * Revert "Starts new view change if block invalid." This reverts commit e889fa5da2e0780f087ab7dae5106b96287706db. * staged dns sync v1.0 (#4316) * staged dns sync v1.0 * enabled stream downloader for localnet * fix code review issues * remove extra lock Co-authored-by: “GheisMohammadi” <“Gheis.Mohammadi@gmail.com”> * add description for closing client and change randomize process to ma… (#4276) * add description for closing client and change randomize process to make sure only online nodes are added to sync config * fix sync test * fix legacy limitNumPeers test * add WaitForEachPeerToConnect to node configs to make parallel peer connection optional Co-authored-by: “GheisMohammadi” <“Gheis.Mohammadi@gmail.com”> * Small fixes and code cleanup for network stack. (#4320) * staged dns sync v1.0 * enabled stream downloader for localnet * fix code review issues * remove extra lock * staged dns sync v1.0 * Fixed, code clean up and other. * Fixed, code clean up and other. * Fixed, code clean up and other. * Fix config. Co-authored-by: “GheisMohammadi” <“Gheis.Mohammadi@gmail.com”> * Fix not disable cache in archival mode (#4322) * Feature registry (#4324) * Registry for services. * Test. * Reverted comment. * Fix. * Slash fix (#4284) * Implementation of new slashing rate calculation * Write tests for then new slashing rate calculation * Add engine.applySlashing tests * fix #4059 Co-authored-by: Alex Brezas <abresas@gmail.com> Co-authored-by: Dimitris Lamprinos <pkakelas@gmail.com> * Bump github.com/aws/aws-sdk-go from 1.30.1 to 1.33.0 (#4325) (#4328) Bumps [github.com/aws/aws-sdk-go](https://github.com/aws/aws-sdk-go) from 1.30.1 to 1.33.0. - [Release notes](https://github.com/aws/aws-sdk-go/releases) - [Changelog](https://github.com/aws/aws-sdk-go/blob/v1.33.0/CHANGELOG.md) - [Commits](https://github.com/aws/aws-sdk-go/compare/v1.30.1...v1.33.0) --- updated-dependencies: - dependency-name: github.com/aws/aws-sdk-go dependency-type: direct:production ... Signed-off-by: dependabot[bot] <support@github.com> Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Bump github.com/btcsuite/btcd from 0.21.0-beta to 0.23.2 (#4327) (#4329) Bumps [github.com/btcsuite/btcd](https://github.com/btcsuite/btcd) from 0.21.0-beta to 0.23.2. - [Release notes](https://github.com/btcsuite/btcd/releases) - [Changelog](https://github.com/btcsuite/btcd/blob/master/CHANGES) - [Commits](https://github.com/btcsuite/btcd/compare/v0.21.0-beta...v0.23.2) --- updated-dependencies: - dependency-name: github.com/btcsuite/btcd dependency-type: indirect ... Signed-off-by: dependabot[bot] <support@github.com> Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: Max <82761650+MaxMustermann2@users.noreply.github.com> Co-authored-by: Gheis <36589218+GheisMohammadi@users.noreply.github.com> Co-authored-by: Konstantin <355847+Frozen@users.noreply.github.com> Co-authored-by: “GheisMohammadi” <“Gheis.Mohammadi@gmail.com”> Co-authored-by: Danny Willis <102543677+dannyposi@users.noreply.github.com> Co-authored-by: PeekPI <894646171@QQ.COM> Co-authored-by: Alex Brezas <abresas@gmail.com> Co-authored-by: Dimitris Lamprinos <pkakelas@gmail.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2 years ago
"github.com/libp2p/go-libp2p/core/network"
libp2p_peer "github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/protocol"
)
var _ StreamManager = &streamManager{}
var (
myPeerID = makePeerID(0)
testProtoID = sttypes.ProtoID("harmony/sync/unitest/0/1.0.0/1")
)
const (
defHardLoCap = 16 // discovery trigger immediately when size smaller than this number
defSoftLoCap = 32 // discovery trigger for routine check
defHiCap = 128 // Hard cap of the stream number
defDiscBatch = 16 // batch size for discovery
)
var defConfig = Config{
HardLoCap: defHardLoCap,
SoftLoCap: defSoftLoCap,
HiCap: defHiCap,
DiscBatch: defDiscBatch,
}
func newTestStreamManager() *streamManager {
pid := testProtoID
host := newTestHost()
pf := newTestPeerFinder(makeRemotePeers(100), emptyDelayFunc)
sm := newStreamManager(pid, host, pf, nil, defConfig)
host.sm = sm
return sm
}
type testStream struct {
id sttypes.StreamID
proto sttypes.ProtoID
closed bool
}
func newTestStream(id sttypes.StreamID, proto sttypes.ProtoID) *testStream {
return &testStream{id: id, proto: proto}
}
func (st *testStream) ID() sttypes.StreamID {
return st.id
}
func (st *testStream) ProtoID() sttypes.ProtoID {
return st.proto
}
func (st *testStream) WriteBytes([]byte) error {
return nil
}
func (st *testStream) ReadBytes() ([]byte, error) {
return nil, nil
}
func (st *testStream) FailedTimes() int {
return 0
}
func (st *testStream) AddFailedTimes() {
return
}
2 years ago
func (st *testStream) ResetFailedTimes() {
return
}
func (st *testStream) Close() error {
if st.closed {
return errors.New("already closed")
}
st.closed = true
return nil
}
func (st *testStream) CloseOnExit() error {
if st.closed {
return errors.New("already closed")
}
st.closed = true
return nil
}
func (st *testStream) ProtoSpec() (sttypes.ProtoSpec, error) {
return sttypes.ProtoIDToProtoSpec(st.ProtoID())
}
type testHost struct {
sm *streamManager
streams map[sttypes.StreamID]*testStream
lock sync.Mutex
errHook streamErrorHook
}
type streamErrorHook func(id sttypes.StreamID, err error)
func newTestHost() *testHost {
return &testHost{
streams: make(map[sttypes.StreamID]*testStream),
}
}
func (h *testHost) ID() libp2p_peer.ID {
return myPeerID
}
// NewStream mock the upper function logic. When stream setup and running protocol, the
// upper code logic will call StreamManager to add new stream
func (h *testHost) NewStream(ctx context.Context, p libp2p_peer.ID, pids ...protocol.ID) (network.Stream, error) {
if len(pids) == 0 {
return nil, errors.New("nil protocol ids")
}
var err error
stid := sttypes.StreamID(p)
defer func() {
if err != nil && h.errHook != nil {
h.errHook(stid, err)
}
}()
st := newTestStream(stid, sttypes.ProtoID(pids[0]))
h.lock.Lock()
h.streams[stid] = st
h.lock.Unlock()
err = h.sm.NewStream(st)
return nil, err
}
func makeStreamID(index int) sttypes.StreamID {
return sttypes.StreamID(strconv.Itoa(index))
}
func makePeerID(index int) libp2p_peer.ID {
return libp2p_peer.ID(strconv.Itoa(index))
}
func makeRemotePeers(size int) []libp2p_peer.ID {
ids := make([]libp2p_peer.ID, 0, size)
for i := 1; i != size+1; i++ {
ids = append(ids, makePeerID(i))
}
return ids
}
type testPeerFinder struct {
peerIDs []libp2p_peer.ID
curIndex int32
fpHook delayFunc
}
type delayFunc func(id libp2p_peer.ID) <-chan struct{}
func emptyDelayFunc(id libp2p_peer.ID) <-chan struct{} {
c := make(chan struct{})
go func() {
c <- struct{}{}
}()
return c
}
func newTestPeerFinder(ids []libp2p_peer.ID, fpHook delayFunc) *testPeerFinder {
return &testPeerFinder{
peerIDs: ids,
curIndex: 0,
fpHook: fpHook,
}
}
func (pf *testPeerFinder) FindPeers(ctx context.Context, ns string, peerLimit int) (<-chan libp2p_peer.AddrInfo, error) {
if peerLimit > len(pf.peerIDs) {
peerLimit = len(pf.peerIDs)
}
resC := make(chan libp2p_peer.AddrInfo)
go func() {
defer close(resC)
for i := 0; i != peerLimit; i++ {
// hack to prevent race
curIndex := atomic.LoadInt32(&pf.curIndex)
pid := pf.peerIDs[curIndex]
select {
case <-ctx.Done():
return
case <-pf.fpHook(pid):
}
resC <- libp2p_peer.AddrInfo{ID: pid}
atomic.AddInt32(&pf.curIndex, 1)
if int(atomic.LoadInt32(&pf.curIndex)) == len(pf.peerIDs) {
pf.curIndex = 0
}
}
}()
return resC, nil
}