From 7d2296b24ccadbc031855f10a1fee50a0f2446f9 Mon Sep 17 00:00:00 2001
From: number571
Date: Mon, 25 Nov 2024 16:12:46 +0700
Subject: [PATCH 01/17] update
---
CHANGELOG.md | 8 +++++++-
build/build.go | 2 +-
test/result/badge_coverage.svg | 2 +-
test/result/coverage.svg | 8 ++++----
4 files changed, 13 insertions(+), 7 deletions(-)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 045c1c8e4..d5e1b4ca3 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,10 +2,16 @@
-## v1.7.6~
+## v1.7.7~
*??? ??, ????*
+
+
+## v1.7.6
+
+*November 25, 2024*
+
### IMPROVEMENTS
- Update `pkg/network/anonymity/queue`: add GetConsumersCap
diff --git a/build/build.go b/build/build.go
index a97c4533f..3fe547087 100644
--- a/build/build.go
+++ b/build/build.go
@@ -1,3 +1,3 @@
package build
-const CVersion = "v1.7.6~"
+const CVersion = "v1.7.7~"
diff --git a/test/result/badge_coverage.svg b/test/result/badge_coverage.svg
index 9fd7bd14b..2eb8e6b26 100644
--- a/test/result/badge_coverage.svg
+++ b/test/result/badge_coverage.svg
@@ -1 +1 @@
-
\ No newline at end of file
+
\ No newline at end of file
diff --git a/test/result/coverage.svg b/test/result/coverage.svg
index 9d8842712..4f6a78a3e 100644
--- a/test/result/coverage.svg
+++ b/test/result/coverage.svg
@@ -7,7 +7,7 @@
>
-
+
-
+
-
+
-
+
Date: Wed, 27 Nov 2024 13:12:26 +0700
Subject: [PATCH 02/17] update
---
pkg/network/anonymity/queue/queue.go | 2 +-
pkg/network/anonymity/queue/settings.go | 4 ++--
test/result/badge_coverage.svg | 2 +-
test/result/coverage.svg | 8 ++++----
4 files changed, 8 insertions(+), 8 deletions(-)
diff --git a/pkg/network/anonymity/queue/queue.go b/pkg/network/anonymity/queue/queue.go
index 30968953a..3a6dd7209 100644
--- a/pkg/network/anonymity/queue/queue.go
+++ b/pkg/network/anonymity/queue/queue.go
@@ -150,7 +150,7 @@ func (p *sQBProblemProcessor) EnqueueMessage(pPubKey asymmetric.IPubKey, pBytes
hash := pPubKey.GetHasher().ToString()
v, ok := p.fMainPool.fConsumers[hash]
if !ok {
- v = uint64(len(p.fMainPool.fConsumers)+1) % p.fSettings.GetConsumersCap()
+ v = uint64(len(p.fMainPool.fConsumers)) % p.fSettings.GetConsumersCap()
p.fMainPool.fConsumers[hash] = v
}
p.fMainPool.fMutex.Unlock()
diff --git a/pkg/network/anonymity/queue/settings.go b/pkg/network/anonymity/queue/settings.go
index bc079b23c..a9c78ae8c 100644
--- a/pkg/network/anonymity/queue/settings.go
+++ b/pkg/network/anonymity/queue/settings.go
@@ -40,9 +40,9 @@ func (p *sSettings) mustNotNull() ISettings {
panic(`p.FQueuePoolCap[0] == 0 || p.FQueuePoolCap[1] == 0`)
}
if p.FConsumersCap == 0 {
- panic(`p.FConsumersLen == 0`)
+ panic(`p.FConsumersCap == 0`)
}
- // p.FParallel, p.FNetworkMask, p.FWorkSizeBits, p.FRandQueuePeriod, p.FLimitVoidSizeBytes can be = 0
+ // p.FNetworkMask can be = 0
return p
}
diff --git a/test/result/badge_coverage.svg b/test/result/badge_coverage.svg
index 2eb8e6b26..9fd7bd14b 100644
--- a/test/result/badge_coverage.svg
+++ b/test/result/badge_coverage.svg
@@ -1 +1 @@
-
\ No newline at end of file
+
\ No newline at end of file
diff --git a/test/result/coverage.svg b/test/result/coverage.svg
index 4f6a78a3e..9d8842712 100644
--- a/test/result/coverage.svg
+++ b/test/result/coverage.svg
@@ -7,7 +7,7 @@
>
-
+
-
+
-
+
-
+
Date: Wed, 27 Nov 2024 22:14:46 +0700
Subject: [PATCH 03/17] update
---
pkg/network/anonymity/queue/queue.go | 4 ++--
test/result/badge_coverage.svg | 2 +-
test/result/coverage.svg | 8 ++++----
3 files changed, 7 insertions(+), 7 deletions(-)
diff --git a/pkg/network/anonymity/queue/queue.go b/pkg/network/anonymity/queue/queue.go
index 3a6dd7209..63470afc4 100644
--- a/pkg/network/anonymity/queue/queue.go
+++ b/pkg/network/anonymity/queue/queue.go
@@ -119,13 +119,13 @@ func (p *sQBProblemProcessor) runMainPoolFiller(pCtx context.Context, pCancel fu
pWG.Done()
pCancel()
}()
- for i := uint64(0); ; i++ {
+ for i := uint64(0); ; i = (i + 1) % p.fSettings.GetConsumersCap() {
select {
case <-pCtx.Done():
return
case <-time.After(p.fSettings.GetQueuePeriod()):
break // next consumer
- case msg := <-p.fMainPool.fRawQueue[i%p.fSettings.GetConsumersCap()]:
+ case msg := <-p.fMainPool.fRawQueue[i]:
if err := p.pushMessage(pCtx, p.fMainPool.fQueue, msg); err != nil {
return
}
diff --git a/test/result/badge_coverage.svg b/test/result/badge_coverage.svg
index 9fd7bd14b..2eb8e6b26 100644
--- a/test/result/badge_coverage.svg
+++ b/test/result/badge_coverage.svg
@@ -1 +1 @@
-
\ No newline at end of file
+
\ No newline at end of file
diff --git a/test/result/coverage.svg b/test/result/coverage.svg
index 9d8842712..4f6a78a3e 100644
--- a/test/result/coverage.svg
+++ b/test/result/coverage.svg
@@ -7,7 +7,7 @@
>
-
+
-
+
-
+
-
+
Date: Thu, 28 Nov 2024 08:14:30 +0700
Subject: [PATCH 04/17] update
---
pkg/network/anonymity/queue/queue.go | 2 +-
test/result/coverage.svg | 6 +++---
2 files changed, 4 insertions(+), 4 deletions(-)
diff --git a/pkg/network/anonymity/queue/queue.go b/pkg/network/anonymity/queue/queue.go
index 63470afc4..7bca910e8 100644
--- a/pkg/network/anonymity/queue/queue.go
+++ b/pkg/network/anonymity/queue/queue.go
@@ -54,7 +54,7 @@ func NewQBProblemProcessor(pSettings ISettings, pClient client.IClient) IQBProbl
fClient: pClient,
fMainPool: &sMainPool{
fQueue: make(chan net_message.IMessage, queuePoolCap[0]*consumersCap),
- fConsumers: make(map[string]uint64, 64),
+ fConsumers: make(map[string]uint64, 128),
fRawQueue: func() map[uint64]chan []byte {
m := make(map[uint64]chan []byte, consumersCap)
for i := uint64(0); i < consumersCap; i++ {
diff --git a/test/result/coverage.svg b/test/result/coverage.svg
index 4f6a78a3e..b02a6d16a 100644
--- a/test/result/coverage.svg
+++ b/test/result/coverage.svg
@@ -336,7 +336,7 @@
-
+
-
+
-
+
Date: Sun, 1 Dec 2024 20:40:47 +0700
Subject: [PATCH 05/17] update
---
README.md | 3 +++
1 file changed, 3 insertions(+)
diff --git a/README.md b/README.md
index 5c6758bf9..85c5c85ea 100644
--- a/README.md
+++ b/README.md
@@ -55,6 +55,9 @@
+
+
+
About project
From 7b2d0dbd183457c55d9d1e679005ad5c28e27d1b Mon Sep 17 00:00:00 2001
From: number571
Date: Thu, 5 Dec 2024 00:14:09 +0700
Subject: [PATCH 06/17] update
---
pkg/{network => }/anonymity/action.go | 0
pkg/{network => }/anonymity/anonymity.go | 119 +++---
pkg/{network => }/anonymity/anonymity_test.go | 168 ++++----
pkg/{network => }/anonymity/doc.go | 0
pkg/anonymity/errors.go | 32 ++
.../anonymity/examples/echo/construct.go | 70 ++--
.../anonymity/examples/echo/main.go | 11 +-
.../anonymity/examples/echo/main_test.go | 0
.../anonymity/examples/ping-pong/construct.go | 70 ++--
.../anonymity/examples/ping-pong/main.go | 12 +-
.../anonymity/examples/ping-pong/main_test.go | 0
pkg/{network => }/anonymity/head.go | 0
pkg/{network => }/anonymity/logger/doc.go | 0
.../anonymity/logger/log_builder.go | 0
.../anonymity/logger/logger_test.go | 0
pkg/{network => }/anonymity/logger/types.go | 0
pkg/{network => }/anonymity/queue/doc.go | 0
pkg/{network => }/anonymity/queue/errors.go | 2 +-
.../anonymity/queue/examples/queue/main.go | 2 +-
.../queue/examples/queue/main_test.go | 0
pkg/{network => }/anonymity/queue/queue.go | 0
.../anonymity/queue/queue_test.go | 0
pkg/{network => }/anonymity/queue/settings.go | 0
pkg/{network => }/anonymity/queue/types.go | 0
pkg/{network => }/anonymity/settings.go | 0
pkg/{network => }/anonymity/types.go | 9 +-
pkg/network/anonymity/errors.go | 32 --
test/result/badge_codelines.svg | 2 +-
test/result/badge_coverage.svg | 2 +-
test/result/coverage.svg | 390 +++++++++---------
30 files changed, 501 insertions(+), 420 deletions(-)
rename pkg/{network => }/anonymity/action.go (100%)
rename pkg/{network => }/anonymity/anonymity.go (84%)
rename pkg/{network => }/anonymity/anonymity_test.go (78%)
rename pkg/{network => }/anonymity/doc.go (100%)
create mode 100644 pkg/anonymity/errors.go
rename pkg/{network => }/anonymity/examples/echo/construct.go (57%)
rename pkg/{network => }/anonymity/examples/echo/main.go (84%)
rename pkg/{network => }/anonymity/examples/echo/main_test.go (100%)
rename pkg/{network => }/anonymity/examples/ping-pong/construct.go (57%)
rename pkg/{network => }/anonymity/examples/ping-pong/main.go (85%)
rename pkg/{network => }/anonymity/examples/ping-pong/main_test.go (100%)
rename pkg/{network => }/anonymity/head.go (100%)
rename pkg/{network => }/anonymity/logger/doc.go (100%)
rename pkg/{network => }/anonymity/logger/log_builder.go (100%)
rename pkg/{network => }/anonymity/logger/logger_test.go (100%)
rename pkg/{network => }/anonymity/logger/types.go (100%)
rename pkg/{network => }/anonymity/queue/doc.go (100%)
rename pkg/{network => }/anonymity/queue/errors.go (86%)
rename pkg/{network => }/anonymity/queue/examples/queue/main.go (97%)
rename pkg/{network => }/anonymity/queue/examples/queue/main_test.go (100%)
rename pkg/{network => }/anonymity/queue/queue.go (100%)
rename pkg/{network => }/anonymity/queue/queue_test.go (100%)
rename pkg/{network => }/anonymity/queue/settings.go (100%)
rename pkg/{network => }/anonymity/queue/types.go (100%)
rename pkg/{network => }/anonymity/settings.go (100%)
rename pkg/{network => }/anonymity/types.go (77%)
delete mode 100644 pkg/network/anonymity/errors.go
diff --git a/pkg/network/anonymity/action.go b/pkg/anonymity/action.go
similarity index 100%
rename from pkg/network/anonymity/action.go
rename to pkg/anonymity/action.go
diff --git a/pkg/network/anonymity/anonymity.go b/pkg/anonymity/anonymity.go
similarity index 84%
rename from pkg/network/anonymity/anonymity.go
rename to pkg/anonymity/anonymity.go
index e15a09fa6..955771c86 100644
--- a/pkg/network/anonymity/anonymity.go
+++ b/pkg/anonymity/anonymity.go
@@ -7,19 +7,17 @@ import (
"sync"
"time"
+ "github.com/number571/go-peer/pkg/anonymity/queue"
"github.com/number571/go-peer/pkg/client/message"
"github.com/number571/go-peer/pkg/crypto/asymmetric"
"github.com/number571/go-peer/pkg/crypto/hashing"
"github.com/number571/go-peer/pkg/crypto/random"
"github.com/number571/go-peer/pkg/logger"
- "github.com/number571/go-peer/pkg/network"
- "github.com/number571/go-peer/pkg/network/anonymity/queue"
- "github.com/number571/go-peer/pkg/network/conn"
"github.com/number571/go-peer/pkg/payload"
"github.com/number571/go-peer/pkg/state"
"github.com/number571/go-peer/pkg/storage/database"
- anon_logger "github.com/number571/go-peer/pkg/network/anonymity/logger"
+ anon_logger "github.com/number571/go-peer/pkg/anonymity/logger"
net_message "github.com/number571/go-peer/pkg/network/message"
)
@@ -32,8 +30,9 @@ type sNode struct {
fState state.IState
fSettings ISettings
fLogger logger.ILogger
+ fProducer IProducerF
+ fConsumer IConsumerF
fKVDatavase database.IKVDatabase
- fNetwork network.INode
fQueue queue.IQBProblemProcessor
fMapPubKeys asymmetric.IMapPubKeys
fHandleRoutes map[uint32]IHandlerF
@@ -43,8 +42,9 @@ type sNode struct {
func NewNode(
pSett ISettings,
pLogger logger.ILogger,
+ pProducer IProducerF,
+ pConsumer IConsumerF,
pKVDatavase database.IKVDatabase,
- pNetwork network.INode,
pQueue queue.IQBProblemProcessor,
pMapPubKeys asymmetric.IMapPubKeys,
) INode {
@@ -52,8 +52,9 @@ func NewNode(
fState: state.NewBoolState(),
fSettings: pSett,
fLogger: pLogger,
+ fProducer: pProducer,
+ fConsumer: pConsumer,
fKVDatavase: pKVDatavase,
- fNetwork: pNetwork,
fQueue: pQueue,
fMapPubKeys: pMapPubKeys,
fHandleRoutes: make(map[uint32]IHandlerF, 64),
@@ -62,48 +63,84 @@ func NewNode(
}
func (p *sNode) Run(pCtx context.Context) error {
- networkMask := p.fQueue.GetSettings().GetNetworkMask()
-
- enableFunc := func() error {
- p.fNetwork.HandleFunc(networkMask, p.networkHandler)
- return nil
- }
- if err := p.fState.Enable(enableFunc); err != nil {
+ if err := p.fState.Enable(nil); err != nil {
return errors.Join(ErrRunning, err)
}
+ defer func() { _ = p.fState.Disable(nil) }()
- defer func() {
- disableFunc := func() error {
- p.fNetwork.HandleFunc(networkMask, nil)
- return nil
- }
- _ = p.fState.Disable(disableFunc)
+ chCtx, cancel := context.WithCancel(pCtx)
+ defer cancel()
+
+ const N = 3
+
+ errs := [N]error{}
+ wg := &sync.WaitGroup{}
+ wg.Add(N)
+
+ go func() {
+ defer func() { wg.Done(); cancel() }()
+ errs[0] = p.fQueue.Run(chCtx)
+ }()
+ go func() {
+ defer func() { wg.Done(); cancel() }()
+ errs[1] = p.runConsumer(chCtx)
+ }()
+ go func() {
+ defer func() { wg.Done(); cancel() }()
+ errs[2] = p.runProducer(chCtx)
}()
- chErr := make(chan error)
- go func() { chErr <- p.fQueue.Run(pCtx) }()
+ wg.Wait()
+
+ select {
+ case <-pCtx.Done():
+ return pCtx.Err()
+ default:
+ for _, err := range errs {
+ if err != nil {
+ return err
+ }
+ }
+ panic("closed without errors")
+ }
+}
+
+func (p *sNode) runProducer(pCtx context.Context) error {
+ serviceName := p.fSettings.GetServiceName()
for {
select {
case <-pCtx.Done():
return pCtx.Err()
- case err := <-chErr:
- return errors.Join(ErrProcessRun, err)
default:
netMsg := p.fQueue.DequeueMessage(pCtx)
if netMsg == nil {
// context done
- break
+ continue
}
- logBuilder := anon_logger.NewLogBuilder(p.fSettings.GetServiceName())
-
- // update logger state
- p.enrichLogger(logBuilder, netMsg).
+ // create logger state
+ logBuilder := p.enrichLogger(anon_logger.NewLogBuilder(serviceName), netMsg).
WithPubKey(p.fQueue.GetClient().GetPrivKey().GetPubKey())
// internal logger
- _, _ = p.storeHashWithBroadcast(pCtx, logBuilder, netMsg)
+ _, _ = p.storeHashWithProduce(pCtx, logBuilder, netMsg)
+ }
+ }
+}
+
+func (p *sNode) runConsumer(pCtx context.Context) error {
+ for {
+ select {
+ case <-pCtx.Done():
+ return pCtx.Err()
+ default:
+ netMsg, err := p.fConsumer(pCtx)
+ if err != nil {
+ continue
+ }
+ // internal logger
+ _ = p.messageHandler(pCtx, netMsg)
}
}
}
@@ -120,10 +157,6 @@ func (p *sNode) GetKVDatabase() database.IKVDatabase {
return p.fKVDatavase
}
-func (p *sNode) GetNetworkNode() network.INode {
- return p.fNetwork
-}
-
func (p *sNode) GetMessageQueue() queue.IQBProblemProcessor {
return p.fQueue
}
@@ -202,17 +235,11 @@ func (p *sNode) recvResponse(pCtx context.Context, pActionKey string) ([]byte, e
}
}
-func (p *sNode) networkHandler(
- pCtx context.Context,
- _ network.INode, // used as p.fNetwork
- pConn conn.IConn,
- pNetMsg net_message.IMessage,
-) error {
+func (p *sNode) messageHandler(pCtx context.Context, pNetMsg net_message.IMessage) error {
logBuilder := anon_logger.NewLogBuilder(p.fSettings.GetServiceName())
// update logger state
- p.enrichLogger(logBuilder, pNetMsg).
- WithConn(pConn)
+ p.enrichLogger(logBuilder, pNetMsg)
client := p.fQueue.GetClient()
encMsg := pNetMsg.GetPayload().GetBody()
@@ -225,10 +252,10 @@ func (p *sNode) networkHandler(
}
// try store hash of message
- if ok, err := p.storeHashWithBroadcast(pCtx, logBuilder, pNetMsg); !ok {
+ if ok, err := p.storeHashWithProduce(pCtx, logBuilder, pNetMsg); !ok {
// internal logger
if err != nil {
- return errors.Join(ErrStoreHashWithBroadcast, err)
+ return errors.Join(ErrStoreHashWithProduce, err)
}
// hash already exist in database
return nil
@@ -372,7 +399,7 @@ func (p *sNode) enrichLogger(pLogBuilder anon_logger.ILogBuilder, pNetMsg net_me
WithSize(size)
}
-func (p *sNode) storeHashWithBroadcast(
+func (p *sNode) storeHashWithProduce(
pCtx context.Context,
pLogBuilder anon_logger.ILogBuilder,
pNetMsg net_message.IMessage,
@@ -387,7 +414,7 @@ func (p *sNode) storeHashWithBroadcast(
}
// redirect message to another nodes
- if err := p.fNetwork.BroadcastMessage(pCtx, pNetMsg); err != nil {
+ if err := p.fProducer(pCtx, pNetMsg); err != nil {
// some connections can return errors
p.fLogger.PushWarn(pLogBuilder.WithType(anon_logger.CLogBaseBroadcast))
return true, nil
diff --git a/pkg/network/anonymity/anonymity_test.go b/pkg/anonymity/anonymity_test.go
similarity index 78%
rename from pkg/network/anonymity/anonymity_test.go
rename to pkg/anonymity/anonymity_test.go
index 00e10bf94..dd4fef331 100644
--- a/pkg/network/anonymity/anonymity_test.go
+++ b/pkg/anonymity/anonymity_test.go
@@ -11,6 +11,7 @@ import (
"testing"
"time"
+ "github.com/number571/go-peer/pkg/anonymity/queue"
"github.com/number571/go-peer/pkg/client"
"github.com/number571/go-peer/pkg/crypto/asymmetric"
"github.com/number571/go-peer/pkg/crypto/hashing"
@@ -18,13 +19,12 @@ import (
"github.com/number571/go-peer/pkg/encoding"
"github.com/number571/go-peer/pkg/logger"
"github.com/number571/go-peer/pkg/network"
- "github.com/number571/go-peer/pkg/network/anonymity/queue"
"github.com/number571/go-peer/pkg/payload"
"github.com/number571/go-peer/pkg/storage/cache"
"github.com/number571/go-peer/pkg/storage/database"
testutils "github.com/number571/go-peer/test/utils"
- anon_logger "github.com/number571/go-peer/pkg/network/anonymity/logger"
+ anon_logger "github.com/number571/go-peer/pkg/anonymity/logger"
"github.com/number571/go-peer/pkg/network/conn"
net_message "github.com/number571/go-peer/pkg/network/message"
)
@@ -53,8 +53,8 @@ func TestError(t *testing.T) {
func TestNodeSettings(t *testing.T) {
t.Parallel()
- node, cancels := testNewNodeWithDB(time.Minute, "", &tsDatabase{})
- defer testFreeNodes([]INode{node}, []context.CancelFunc{cancels}, 9)
+ node, networkNode, cancels := testNewNodeWithDB(time.Minute, "", &tsDatabase{})
+ defer testFreeNodes([]INode{node}, []network.INode{networkNode}, []context.CancelFunc{cancels}, 9)
sett := node.GetSettings()
if sett.GetFetchTimeout() != time.Minute {
@@ -99,12 +99,12 @@ func TestComplexFetchPayload(t *testing.T) {
t.Parallel()
addresses := [2]string{testutils.TgAddrs[2], testutils.TgAddrs[3]}
- nodes, cancels := testNewNodes(t, time.Minute, addresses, 0)
+ nodes, networkNodes, cancels := testNewNodes(t, time.Minute, addresses, 0)
if nodes[0] == nil {
t.Error("nodes is null")
return
}
- defer testFreeNodes(nodes[:], cancels[:], 0)
+ defer testFreeNodes(nodes[:], networkNodes[:], cancels[:], 0)
wg := sync.WaitGroup{}
wg.Add(tcIter)
@@ -142,12 +142,12 @@ func TestF2FWithoutFriends(t *testing.T) {
// 3 seconds for wait
addresses := [2]string{testutils.TgAddrs[10], testutils.TgAddrs[11]}
- nodes, cancels := testNewNodes(t, 3*time.Second, addresses, 1)
+ nodes, networkNodes, cancels := testNewNodes(t, 3*time.Second, addresses, 1)
if nodes[0] == nil {
t.Error("nodes is null")
return
}
- defer testFreeNodes(nodes[:], cancels[:], 1)
+ defer testFreeNodes(nodes[:], networkNodes[:], cancels[:], 1)
nodes[0].GetMapPubKeys().DelPubKey(nodes[1].GetMessageQueue().GetClient().GetPrivKey().GetPubKey())
nodes[1].GetMapPubKeys().DelPubKey(nodes[0].GetMessageQueue().GetClient().GetPrivKey().GetPubKey())
@@ -171,12 +171,12 @@ func TestFetchPayload(t *testing.T) {
t.Parallel()
addresses := [2]string{testutils.TgAddrs[12], testutils.TgAddrs[13]}
- nodes, cancels := testNewNodes(t, time.Minute, addresses, 4)
+ nodes, networkNodes, cancels := testNewNodes(t, time.Minute, addresses, 4)
if nodes[0] == nil {
t.Error("nodes is null")
return
}
- defer testFreeNodes(nodes[:], cancels[:], 4)
+ defer testFreeNodes(nodes[:], networkNodes[:], cancels[:], 4)
nodes[1].HandleFunc(
tcHead,
@@ -217,12 +217,12 @@ func TestBroadcastPayload(t *testing.T) {
t.Parallel()
addresses := [2]string{testutils.TgAddrs[14], testutils.TgAddrs[15]}
- nodes, cancels := testNewNodes(t, time.Minute, addresses, 3)
+ nodes, networkNodes, cancels := testNewNodes(t, time.Minute, addresses, 3)
if nodes[0] == nil {
t.Error("nodes is null")
return
}
- defer testFreeNodes(nodes[:], cancels[:], 3)
+ defer testFreeNodes(nodes[:], networkNodes[:], cancels[:], 3)
chResult := make(chan string)
nodes[1].HandleFunc(
@@ -272,12 +272,12 @@ func TestEnqueuePayload(t *testing.T) {
t.Parallel()
addresses := [2]string{testutils.TgAddrs[16], testutils.TgAddrs[17]}
- nodes, cancels := testNewNodes(t, time.Minute, addresses, 8)
+ nodes, networkNodes, cancels := testNewNodes(t, time.Minute, addresses, 8)
if nodes[0] == nil {
t.Error("nodes is null")
return
}
- defer testFreeNodes(nodes[:], cancels[:], 8)
+ defer testFreeNodes(nodes[:], networkNodes[:], cancels[:], 8)
node := nodes[0].(*sNode)
pubKey := nodes[1].GetMessageQueue().GetClient().GetPrivKey().GetPubKey()
@@ -317,11 +317,11 @@ func TestEnqueuePayload(t *testing.T) {
func TestHandleWrapper(t *testing.T) {
t.Parallel()
- _node, cancel := testNewNode(time.Minute, "", 7, 0)
- defer testFreeNodes([]INode{_node}, []context.CancelFunc{cancel}, 7)
+ _node, networkNode, cancel := testNewNode(time.Minute, "", 7, 0)
+ defer testFreeNodes([]INode{_node}, []network.INode{networkNode}, []context.CancelFunc{cancel}, 7)
node := _node.(*sNode)
- handler := node.networkHandler
+ handler := node.messageHandler
client := node.fQueue.GetClient()
privKey := client.GetPrivKey()
@@ -346,12 +346,12 @@ func TestHandleWrapper(t *testing.T) {
}
netMsg := node.testNewNetworkMessage(sett, msg)
- if err := handler(ctx, nil, nil, netMsg); err != nil {
+ if err := handler(ctx, netMsg); err != nil {
t.Error(err)
return
}
- if err := handler(ctx, nil, nil, netMsg); err != nil {
+ if err := handler(ctx, netMsg); err != nil {
t.Error("repeated message:", err.Error())
return
}
@@ -363,7 +363,7 @@ func TestHandleWrapper(t *testing.T) {
}
netMsgWithoutPld := node.testNewNetworkMessage(sett, msgWithoutPld)
- if err := handler(ctx, nil, nil, netMsgWithoutPld); err != nil {
+ if err := handler(ctx, netMsgWithoutPld); err != nil {
t.Error(err) // works only logger
return
}
@@ -388,7 +388,7 @@ func TestHandleWrapper(t *testing.T) {
}
netMsg2 := node.testNewNetworkMessage(sett, msg2)
- if err := handler(ctx, nil, nil, netMsg2); err != nil {
+ if err := handler(ctx, netMsg2); err != nil {
t.Error(err) // works only logger
return
}
@@ -406,7 +406,7 @@ func TestHandleWrapper(t *testing.T) {
}
netMsg3 := node.testNewNetworkMessage(sett, msg3)
- if err := handler(ctx, nil, nil, netMsg3); err != nil {
+ if err := handler(ctx, netMsg3); err != nil {
t.Error(err) // works only logger
return
}
@@ -424,20 +424,20 @@ func TestHandleWrapper(t *testing.T) {
}
netMsg4 := node.testNewNetworkMessage(sett, msg4)
- if err := handler(ctx, nil, nil, netMsg4); err != nil {
+ if err := handler(ctx, netMsg4); err != nil {
t.Error(err) // works only logger
return
}
netMsg5 := node.testNewNetworkMessage(sett, []byte{123})
- if err := handler(ctx, nil, nil, netMsg5); err == nil {
+ if err := handler(ctx, netMsg5); err == nil {
t.Error("got success code with invalid message body")
return
}
node.fKVDatavase.Close()
netMsg41 := node.testNewNetworkMessage(sett, msg4)
- if err := handler(ctx, nil, nil, netMsg41); err == nil {
+ if err := handler(ctx, netMsg41); err == nil {
t.Error("got success code with closed database")
return
}
@@ -446,8 +446,8 @@ func TestHandleWrapper(t *testing.T) {
func TestStoreHashWithBroadcastMessage(t *testing.T) {
t.Parallel()
- _node, cancel := testNewNode(time.Minute, "", 6, 0)
- defer testFreeNodes([]INode{_node}, []context.CancelFunc{cancel}, 6)
+ _node, networkNode, cancel := testNewNode(time.Minute, "", 6, 0)
+ defer testFreeNodes([]INode{_node}, []network.INode{networkNode}, []context.CancelFunc{cancel}, 6)
node := _node.(*sNode)
client := node.fQueue.GetClient()
@@ -472,12 +472,12 @@ func TestStoreHashWithBroadcastMessage(t *testing.T) {
logBuilder := anon_logger.NewLogBuilder("_")
ctx := context.Background()
- if ok, err := node.storeHashWithBroadcast(ctx, logBuilder, netMsg); !ok || err != nil {
+ if ok, err := node.storeHashWithProduce(ctx, logBuilder, netMsg); !ok || err != nil {
t.Error(err)
return
}
- if ok, err := node.storeHashWithBroadcast(ctx, logBuilder, netMsg); ok || err != nil {
+ if ok, err := node.storeHashWithProduce(ctx, logBuilder, netMsg); ok || err != nil {
switch {
case ok:
t.Error("success store one message again")
@@ -486,27 +486,13 @@ func TestStoreHashWithBroadcastMessage(t *testing.T) {
}
return
}
-
- // db := node.GetDBWrapper().Get()
- // node.GetDBWrapper().Set(nil)
- // if ok, err := node.storeHashWithBroadcast(ctx, logBuilder, netMsg); ok || err == nil {
- // t.Error("success use store function with null database")
- // return
- // }
-
- // node.GetDBWrapper().Set(db)
- // db.Close()
- // if ok, err := node.storeHashWithBroadcast(ctx, logBuilder, netMsg); ok || err == nil {
- // t.Error("success use store function with closed database")
- // return
- // }
}
func TestRecvSendMessage(t *testing.T) {
t.Parallel()
- _node, cancel := testNewNode(time.Minute, "", 5, 0)
- defer testFreeNodes([]INode{_node}, []context.CancelFunc{cancel}, 5)
+ _node, networkNode, cancel := testNewNode(time.Minute, "", 5, 0)
+ defer testFreeNodes([]INode{_node}, []network.INode{networkNode}, []context.CancelFunc{cancel}, 5)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
@@ -577,16 +563,17 @@ func TestRecvSendMessage(t *testing.T) {
// nodes[2], nodes[3], nodes[4] = routes
// nodes[2], nodes[4] are have open ports
// Scheme: (nodes[0]) -> nodes[2] -> nodes[3] -> nodes[4] -> (nodes[1])
-func testNewNodes(t *testing.T, timeWait time.Duration, addresses [2]string, typeDB int) ([5]INode, [5]context.CancelFunc) {
+func testNewNodes(t *testing.T, timeWait time.Duration, addresses [2]string, typeDB int) ([5]INode, [5]network.INode, [5]context.CancelFunc) {
nodes := [5]INode{}
+ networkNodes := [5]network.INode{}
cancels := [5]context.CancelFunc{}
addrs := [5]string{"", "", addresses[0], "", addresses[1]}
for i := 0; i < 5; i++ {
- nodes[i], cancels[i] = testNewNode(timeWait, addrs[i], typeDB, i)
+ nodes[i], networkNodes[i], cancels[i] = testNewNode(timeWait, addrs[i], typeDB, i)
if nodes[i] == nil {
t.Errorf("node (%d) is not running %d", i, typeDB)
- return [5]INode{}, [5]context.CancelFunc{}
+ return [5]INode{}, [5]network.INode{}, [5]context.CancelFunc{}
}
}
@@ -608,12 +595,12 @@ func testNewNodes(t *testing.T, timeWait time.Duration, addresses [2]string, typ
ctx := context.Background()
go func() {
- if err := nodes[2].GetNetworkNode().Listen(ctx); err != nil && !errors.Is(err, net.ErrClosed) {
+ if err := networkNodes[2].Listen(ctx); err != nil && !errors.Is(err, net.ErrClosed) {
t.Error(err)
}
}()
go func() {
- if err := nodes[4].GetNetworkNode().Listen(ctx); err != nil && !errors.Is(err, net.ErrClosed) {
+ if err := networkNodes[4].Listen(ctx); err != nil && !errors.Is(err, net.ErrClosed) {
t.Error(err)
}
}()
@@ -621,28 +608,28 @@ func testNewNodes(t *testing.T, timeWait time.Duration, addresses [2]string, typ
// try connect to new node listeners
// nodes to routes (nodes[0] -> nodes[2], nodes[1] -> nodes[4])
err1 := testutils.TryN(50, 10*time.Millisecond, func() error {
- return nodes[0].GetNetworkNode().AddConnection(ctx, addresses[0])
+ return networkNodes[0].AddConnection(ctx, addresses[0])
})
if err1 != nil {
t.Error(err1)
- return [5]INode{}, [5]context.CancelFunc{}
+ return [5]INode{}, [5]network.INode{}, [5]context.CancelFunc{}
}
err2 := testutils.TryN(50, 10*time.Millisecond, func() error {
- return nodes[1].GetNetworkNode().AddConnection(ctx, addresses[1])
+ return networkNodes[1].AddConnection(ctx, addresses[1])
})
if err2 != nil {
t.Error(err2)
- return [5]INode{}, [5]context.CancelFunc{}
+ return [5]INode{}, [5]network.INode{}, [5]context.CancelFunc{}
}
// routes to routes (nodes[3] -> nodes[2], nodes[3] -> nodes[4])
- if err := nodes[3].GetNetworkNode().AddConnection(ctx, addresses[0]); err != nil {
+ if err := networkNodes[3].AddConnection(ctx, addresses[0]); err != nil {
t.Error(err)
- return [5]INode{}, [5]context.CancelFunc{}
+ return [5]INode{}, [5]network.INode{}, [5]context.CancelFunc{}
}
- if err := nodes[3].GetNetworkNode().AddConnection(ctx, addresses[1]); err != nil {
+ if err := networkNodes[3].AddConnection(ctx, addresses[1]); err != nil {
t.Error(err)
- return [5]INode{}, [5]context.CancelFunc{}
+ return [5]INode{}, [5]network.INode{}, [5]context.CancelFunc{}
}
go func() {
@@ -652,7 +639,7 @@ func testNewNodes(t *testing.T, timeWait time.Duration, addresses [2]string, typ
}
}()
- return nodes, cancels
+ return nodes, networkNodes, cancels
}
/*
@@ -674,10 +661,33 @@ func (p *stLogging) HasErro() bool {
}
*/
-func testNewNodeWithDB(timeWait time.Duration, addr string, db database.IKVDatabase) (INode, context.CancelFunc) {
+func testNewNodeWithDB(timeWait time.Duration, addr string, db database.IKVDatabase) (INode, network.INode, context.CancelFunc) {
+ msgChan := make(chan net_message.IMessage)
parallel := uint64(1)
networkMask := uint32(1)
limitVoidSize := uint64(10_000)
+ networkNode := network.NewNode(
+ network.NewSettings(&network.SSettings{
+ FAddress: addr,
+ FMaxConnects: 16,
+ FReadTimeout: timeWait,
+ FWriteTimeout: timeWait,
+ FConnSettings: conn.NewSettings(&conn.SSettings{
+ FMessageSettings: net_message.NewSettings(&net_message.SSettings{
+ FWorkSizeBits: tcWorkSize,
+ }),
+ FLimitMessageSizeBytes: tcMsgSize + limitVoidSize,
+ FWaitReadTimeout: time.Hour,
+ FDialTimeout: time.Minute,
+ FReadTimeout: time.Minute,
+ FWriteTimeout: time.Minute,
+ }),
+ }),
+ cache.NewLRUCache(1024),
+ ).HandleFunc(networkMask, func(_ context.Context, _ network.INode, _ conn.IConn, msg net_message.IMessage) error {
+ msgChan <- msg
+ return nil
+ })
node := NewNode(
NewSettings(&SSettings{
FServiceName: "TEST",
@@ -688,26 +698,18 @@ func testNewNodeWithDB(timeWait time.Duration, addr string, db database.IKVDatab
logger.NewSettings(&logger.SSettings{}),
func(_ logger.ILogArg) string { return "" },
),
+ func(ctx context.Context, msg net_message.IMessage) error {
+ return networkNode.BroadcastMessage(ctx, msg)
+ },
+ func(ctx context.Context) (net_message.IMessage, error) {
+ select {
+ case <-ctx.Done():
+ return nil, ctx.Err()
+ case msg := <-msgChan:
+ return msg, nil
+ }
+ },
db,
- network.NewNode(
- network.NewSettings(&network.SSettings{
- FAddress: addr,
- FMaxConnects: 16,
- FReadTimeout: timeWait,
- FWriteTimeout: timeWait,
- FConnSettings: conn.NewSettings(&conn.SSettings{
- FMessageSettings: net_message.NewSettings(&net_message.SSettings{
- FWorkSizeBits: tcWorkSize,
- }),
- FLimitMessageSizeBytes: tcMsgSize + limitVoidSize,
- FWaitReadTimeout: time.Hour,
- FDialTimeout: time.Minute,
- FReadTimeout: time.Minute,
- FWriteTimeout: time.Minute,
- }),
- }),
- cache.NewLRUCache(1024),
- ),
queue.NewQBProblemProcessor(
queue.NewSettings(&queue.SSettings{
FMessageConstructSettings: net_message.NewConstructSettings(&net_message.SConstructSettings{
@@ -731,10 +733,10 @@ func testNewNodeWithDB(timeWait time.Duration, addr string, db database.IKVDatab
)
ctx, cancel := context.WithCancel(context.Background())
go func() { _ = node.Run(ctx) }()
- return node, cancel
+ return node, networkNode, cancel
}
-func testNewNode(timeWait time.Duration, addr string, typeDB, numDB int) (INode, context.CancelFunc) {
+func testNewNode(timeWait time.Duration, addr string, typeDB, numDB int) (INode, network.INode, context.CancelFunc) {
db, err := database.NewKVDatabase(fmt.Sprintf(tcPathDBTemplate, typeDB, numDB))
if err != nil {
panic(err)
@@ -742,10 +744,10 @@ func testNewNode(timeWait time.Duration, addr string, typeDB, numDB int) (INode,
return testNewNodeWithDB(timeWait, addr, db)
}
-func testFreeNodes(nodes []INode, cancels []context.CancelFunc, typeDB int) {
+func testFreeNodes(nodes []INode, networks []network.INode, cancels []context.CancelFunc, typeDB int) {
for i, node := range nodes {
node.GetKVDatabase().Close()
- node.GetNetworkNode().Close()
+ networks[i].Close()
cancels[i]()
}
testDeleteDB(typeDB)
diff --git a/pkg/network/anonymity/doc.go b/pkg/anonymity/doc.go
similarity index 100%
rename from pkg/network/anonymity/doc.go
rename to pkg/anonymity/doc.go
diff --git a/pkg/anonymity/errors.go b/pkg/anonymity/errors.go
new file mode 100644
index 000000000..a5f6695e7
--- /dev/null
+++ b/pkg/anonymity/errors.go
@@ -0,0 +1,32 @@
+package anonymity
+
+const (
+ errPrefix = "pkg/anonymity = "
+)
+
+type SAnonymityError struct {
+ str string
+}
+
+func (err *SAnonymityError) Error() string {
+ return errPrefix + err.str
+}
+
+var (
+ ErrSetHashIntoDB = &SAnonymityError{"set hash into database"}
+ ErrGetHashFromDB = &SAnonymityError{"get hash from database"}
+ ErrNilDB = &SAnonymityError{"database is nil"}
+ ErrRetryLimit = &SAnonymityError{"retry limit"}
+ ErrEnqueueMessage = &SAnonymityError{"enqueue message"}
+ ErrUnknownType = &SAnonymityError{"unknown type"}
+ ErrLoadMessage = &SAnonymityError{"load message"}
+ ErrStoreHashWithProduce = &SAnonymityError{"store hash with produce"}
+ ErrActionIsNotFound = &SAnonymityError{"action is not found"}
+ ErrActionIsClosed = &SAnonymityError{"action is closed"}
+ ErrActionTimeout = &SAnonymityError{"action timeout"}
+ ErrEnqueuePayload = &SAnonymityError{"enqueue payload"}
+ ErrFetchResponse = &SAnonymityError{"fetch response"}
+ ErrRunning = &SAnonymityError{"node running"}
+ ErrProcessRun = &SAnonymityError{"process run"}
+ ErrHashAlreadyExist = &SAnonymityError{"hash already exist"}
+)
diff --git a/pkg/network/anonymity/examples/echo/construct.go b/pkg/anonymity/examples/echo/construct.go
similarity index 57%
rename from pkg/network/anonymity/examples/echo/construct.go
rename to pkg/anonymity/examples/echo/construct.go
index 48f5f81df..fe5ab242b 100644
--- a/pkg/network/anonymity/examples/echo/construct.go
+++ b/pkg/anonymity/examples/echo/construct.go
@@ -1,17 +1,18 @@
package main
import (
+ "context"
"fmt"
"os"
"time"
+ "github.com/number571/go-peer/pkg/anonymity"
+ anon_logger "github.com/number571/go-peer/pkg/anonymity/logger"
+ "github.com/number571/go-peer/pkg/anonymity/queue"
"github.com/number571/go-peer/pkg/client"
"github.com/number571/go-peer/pkg/crypto/asymmetric"
"github.com/number571/go-peer/pkg/logger"
"github.com/number571/go-peer/pkg/network"
- "github.com/number571/go-peer/pkg/network/anonymity"
- anon_logger "github.com/number571/go-peer/pkg/network/anonymity/logger"
- "github.com/number571/go-peer/pkg/network/anonymity/queue"
"github.com/number571/go-peer/pkg/network/conn"
net_message "github.com/number571/go-peer/pkg/network/message"
"github.com/number571/go-peer/pkg/storage/cache"
@@ -26,8 +27,34 @@ const (
workSize = uint64(10)
)
-func newNode(serviceName, address string) anonymity.INode {
- return anonymity.NewNode(
+func newNode(serviceName, address string) (network.INode, anonymity.INode) {
+ msgChan := make(chan net_message.IMessage)
+ networkNode := network.NewNode(
+ network.NewSettings(&network.SSettings{
+ FAddress: address,
+ FMaxConnects: 256,
+ FReadTimeout: time.Minute,
+ FWriteTimeout: time.Minute,
+ FConnSettings: conn.NewSettings(&conn.SSettings{
+ FMessageSettings: net_message.NewSettings(&net_message.SSettings{
+ FWorkSizeBits: workSize,
+ }),
+ FLimitMessageSizeBytes: msgSize,
+ FWaitReadTimeout: time.Hour,
+ FDialTimeout: time.Minute,
+ FReadTimeout: time.Minute,
+ FWriteTimeout: time.Minute,
+ }),
+ }),
+ cache.NewLRUCache(1024),
+ ).HandleFunc(
+ networkMask,
+ func(ctx context.Context, _ network.INode, _ conn.IConn, msg net_message.IMessage) error {
+ msgChan <- msg
+ return nil
+ },
+ )
+ anonymityNode := anonymity.NewNode(
anonymity.NewSettings(&anonymity.SSettings{
FServiceName: serviceName,
FFetchTimeout: time.Minute,
@@ -53,6 +80,17 @@ func newNode(serviceName, address string) anonymity.INode {
)
},
),
+ func(ctx context.Context, msg net_message.IMessage) error {
+ return networkNode.BroadcastMessage(ctx, msg)
+ },
+ func(ctx context.Context) (net_message.IMessage, error) {
+ select {
+ case <-ctx.Done():
+ return nil, ctx.Err()
+ case msg := <-msgChan:
+ return msg, nil
+ }
+ },
func() database.IKVDatabase {
db, err := database.NewKVDatabase("./database_" + serviceName + ".db")
if err != nil {
@@ -60,25 +98,6 @@ func newNode(serviceName, address string) anonymity.INode {
}
return db
}(),
- network.NewNode(
- network.NewSettings(&network.SSettings{
- FAddress: address,
- FMaxConnects: 256,
- FReadTimeout: time.Minute,
- FWriteTimeout: time.Minute,
- FConnSettings: conn.NewSettings(&conn.SSettings{
- FMessageSettings: net_message.NewSettings(&net_message.SSettings{
- FWorkSizeBits: workSize,
- }),
- FLimitMessageSizeBytes: msgSize,
- FWaitReadTimeout: time.Hour,
- FDialTimeout: time.Minute,
- FReadTimeout: time.Minute,
- FWriteTimeout: time.Minute,
- }),
- }),
- cache.NewLRUCache(1024),
- ),
queue.NewQBProblemProcessor(
queue.NewSettings(&queue.SSettings{
FMessageConstructSettings: net_message.NewConstructSettings(&net_message.SConstructSettings{
@@ -88,7 +107,7 @@ func newNode(serviceName, address string) anonymity.INode {
}),
FNetworkMask: networkMask,
FQueuePeriod: 2 * time.Second,
- FConsumersCap: 5,
+ FConsumersCap: 1,
FQueuePoolCap: [2]uint64{32, 32},
}),
client.NewClient(
@@ -98,4 +117,5 @@ func newNode(serviceName, address string) anonymity.INode {
),
asymmetric.NewMapPubKeys(),
)
+ return networkNode, anonymityNode
}
diff --git a/pkg/network/anonymity/examples/echo/main.go b/pkg/anonymity/examples/echo/main.go
similarity index 84%
rename from pkg/network/anonymity/examples/echo/main.go
rename to pkg/anonymity/examples/echo/main.go
index 8dae4aa87..f9cb7d8f6 100644
--- a/pkg/network/anonymity/examples/echo/main.go
+++ b/pkg/anonymity/examples/echo/main.go
@@ -5,8 +5,8 @@ import (
"fmt"
"time"
+ "github.com/number571/go-peer/pkg/anonymity"
"github.com/number571/go-peer/pkg/crypto/asymmetric"
- "github.com/number571/go-peer/pkg/network/anonymity"
"github.com/number571/go-peer/pkg/payload"
)
@@ -32,17 +32,18 @@ func main() {
func runClientNode() anonymity.INode {
ctx := context.Background()
- node := newNode("cnode", "")
+ network, node := newNode("cnode", "")
go func() { _ = node.Run(ctx) }()
- node.GetNetworkNode().AddConnection(ctx, nodeAddress)
+ network.AddConnection(ctx, nodeAddress)
return node
}
func runServiceNode() anonymity.INode {
ctx := context.Background()
- node := newNode("snode", nodeAddress).HandleFunc(
+ network, node := newNode("snode", nodeAddress)
+ node.HandleFunc(
nodeRouter,
func(_ context.Context, _ anonymity.INode, _ asymmetric.IPubKey, b []byte) ([]byte, error) {
return []byte(fmt.Sprintf("echo: %s", string(b))), nil
@@ -50,7 +51,7 @@ func runServiceNode() anonymity.INode {
)
go func() { _ = node.Run(ctx) }()
- go func() { _ = node.GetNetworkNode().Listen(ctx) }()
+ go func() { _ = network.Listen(ctx) }()
time.Sleep(time.Second) // wait listener
return node
diff --git a/pkg/network/anonymity/examples/echo/main_test.go b/pkg/anonymity/examples/echo/main_test.go
similarity index 100%
rename from pkg/network/anonymity/examples/echo/main_test.go
rename to pkg/anonymity/examples/echo/main_test.go
diff --git a/pkg/network/anonymity/examples/ping-pong/construct.go b/pkg/anonymity/examples/ping-pong/construct.go
similarity index 57%
rename from pkg/network/anonymity/examples/ping-pong/construct.go
rename to pkg/anonymity/examples/ping-pong/construct.go
index 48f5f81df..fe5ab242b 100644
--- a/pkg/network/anonymity/examples/ping-pong/construct.go
+++ b/pkg/anonymity/examples/ping-pong/construct.go
@@ -1,17 +1,18 @@
package main
import (
+ "context"
"fmt"
"os"
"time"
+ "github.com/number571/go-peer/pkg/anonymity"
+ anon_logger "github.com/number571/go-peer/pkg/anonymity/logger"
+ "github.com/number571/go-peer/pkg/anonymity/queue"
"github.com/number571/go-peer/pkg/client"
"github.com/number571/go-peer/pkg/crypto/asymmetric"
"github.com/number571/go-peer/pkg/logger"
"github.com/number571/go-peer/pkg/network"
- "github.com/number571/go-peer/pkg/network/anonymity"
- anon_logger "github.com/number571/go-peer/pkg/network/anonymity/logger"
- "github.com/number571/go-peer/pkg/network/anonymity/queue"
"github.com/number571/go-peer/pkg/network/conn"
net_message "github.com/number571/go-peer/pkg/network/message"
"github.com/number571/go-peer/pkg/storage/cache"
@@ -26,8 +27,34 @@ const (
workSize = uint64(10)
)
-func newNode(serviceName, address string) anonymity.INode {
- return anonymity.NewNode(
+func newNode(serviceName, address string) (network.INode, anonymity.INode) {
+ msgChan := make(chan net_message.IMessage)
+ networkNode := network.NewNode(
+ network.NewSettings(&network.SSettings{
+ FAddress: address,
+ FMaxConnects: 256,
+ FReadTimeout: time.Minute,
+ FWriteTimeout: time.Minute,
+ FConnSettings: conn.NewSettings(&conn.SSettings{
+ FMessageSettings: net_message.NewSettings(&net_message.SSettings{
+ FWorkSizeBits: workSize,
+ }),
+ FLimitMessageSizeBytes: msgSize,
+ FWaitReadTimeout: time.Hour,
+ FDialTimeout: time.Minute,
+ FReadTimeout: time.Minute,
+ FWriteTimeout: time.Minute,
+ }),
+ }),
+ cache.NewLRUCache(1024),
+ ).HandleFunc(
+ networkMask,
+ func(ctx context.Context, _ network.INode, _ conn.IConn, msg net_message.IMessage) error {
+ msgChan <- msg
+ return nil
+ },
+ )
+ anonymityNode := anonymity.NewNode(
anonymity.NewSettings(&anonymity.SSettings{
FServiceName: serviceName,
FFetchTimeout: time.Minute,
@@ -53,6 +80,17 @@ func newNode(serviceName, address string) anonymity.INode {
)
},
),
+ func(ctx context.Context, msg net_message.IMessage) error {
+ return networkNode.BroadcastMessage(ctx, msg)
+ },
+ func(ctx context.Context) (net_message.IMessage, error) {
+ select {
+ case <-ctx.Done():
+ return nil, ctx.Err()
+ case msg := <-msgChan:
+ return msg, nil
+ }
+ },
func() database.IKVDatabase {
db, err := database.NewKVDatabase("./database_" + serviceName + ".db")
if err != nil {
@@ -60,25 +98,6 @@ func newNode(serviceName, address string) anonymity.INode {
}
return db
}(),
- network.NewNode(
- network.NewSettings(&network.SSettings{
- FAddress: address,
- FMaxConnects: 256,
- FReadTimeout: time.Minute,
- FWriteTimeout: time.Minute,
- FConnSettings: conn.NewSettings(&conn.SSettings{
- FMessageSettings: net_message.NewSettings(&net_message.SSettings{
- FWorkSizeBits: workSize,
- }),
- FLimitMessageSizeBytes: msgSize,
- FWaitReadTimeout: time.Hour,
- FDialTimeout: time.Minute,
- FReadTimeout: time.Minute,
- FWriteTimeout: time.Minute,
- }),
- }),
- cache.NewLRUCache(1024),
- ),
queue.NewQBProblemProcessor(
queue.NewSettings(&queue.SSettings{
FMessageConstructSettings: net_message.NewConstructSettings(&net_message.SConstructSettings{
@@ -88,7 +107,7 @@ func newNode(serviceName, address string) anonymity.INode {
}),
FNetworkMask: networkMask,
FQueuePeriod: 2 * time.Second,
- FConsumersCap: 5,
+ FConsumersCap: 1,
FQueuePoolCap: [2]uint64{32, 32},
}),
client.NewClient(
@@ -98,4 +117,5 @@ func newNode(serviceName, address string) anonymity.INode {
),
asymmetric.NewMapPubKeys(),
)
+ return networkNode, anonymityNode
}
diff --git a/pkg/network/anonymity/examples/ping-pong/main.go b/pkg/anonymity/examples/ping-pong/main.go
similarity index 85%
rename from pkg/network/anonymity/examples/ping-pong/main.go
rename to pkg/anonymity/examples/ping-pong/main.go
index d25017419..5b3421cde 100644
--- a/pkg/network/anonymity/examples/ping-pong/main.go
+++ b/pkg/anonymity/examples/ping-pong/main.go
@@ -5,9 +5,9 @@ import (
"fmt"
"time"
+ "github.com/number571/go-peer/pkg/anonymity"
"github.com/number571/go-peer/pkg/crypto/asymmetric"
"github.com/number571/go-peer/pkg/encoding"
- "github.com/number571/go-peer/pkg/network/anonymity"
"github.com/number571/go-peer/pkg/payload"
)
@@ -54,20 +54,22 @@ func main() {
func runClientNode() anonymity.INode {
ctx := context.Background()
- node := newNode("cnode", "").HandleFunc(nodeRouter, handler)
+ network, node := newNode("cnode", "")
+ node.HandleFunc(nodeRouter, handler)
go func() { _ = node.Run(ctx) }()
- node.GetNetworkNode().AddConnection(ctx, nodeAddress)
+ network.AddConnection(ctx, nodeAddress)
return node
}
func runServiceNode() anonymity.INode {
ctx := context.Background()
- node := newNode("snode", nodeAddress).HandleFunc(nodeRouter, handler)
+ network, node := newNode("snode", nodeAddress)
+ node.HandleFunc(nodeRouter, handler)
go func() { _ = node.Run(ctx) }()
- go func() { _ = node.GetNetworkNode().Listen(ctx) }()
+ go func() { _ = network.Listen(ctx) }()
time.Sleep(time.Second) // wait listener
return node
diff --git a/pkg/network/anonymity/examples/ping-pong/main_test.go b/pkg/anonymity/examples/ping-pong/main_test.go
similarity index 100%
rename from pkg/network/anonymity/examples/ping-pong/main_test.go
rename to pkg/anonymity/examples/ping-pong/main_test.go
diff --git a/pkg/network/anonymity/head.go b/pkg/anonymity/head.go
similarity index 100%
rename from pkg/network/anonymity/head.go
rename to pkg/anonymity/head.go
diff --git a/pkg/network/anonymity/logger/doc.go b/pkg/anonymity/logger/doc.go
similarity index 100%
rename from pkg/network/anonymity/logger/doc.go
rename to pkg/anonymity/logger/doc.go
diff --git a/pkg/network/anonymity/logger/log_builder.go b/pkg/anonymity/logger/log_builder.go
similarity index 100%
rename from pkg/network/anonymity/logger/log_builder.go
rename to pkg/anonymity/logger/log_builder.go
diff --git a/pkg/network/anonymity/logger/logger_test.go b/pkg/anonymity/logger/logger_test.go
similarity index 100%
rename from pkg/network/anonymity/logger/logger_test.go
rename to pkg/anonymity/logger/logger_test.go
diff --git a/pkg/network/anonymity/logger/types.go b/pkg/anonymity/logger/types.go
similarity index 100%
rename from pkg/network/anonymity/logger/types.go
rename to pkg/anonymity/logger/types.go
diff --git a/pkg/network/anonymity/queue/doc.go b/pkg/anonymity/queue/doc.go
similarity index 100%
rename from pkg/network/anonymity/queue/doc.go
rename to pkg/anonymity/queue/doc.go
diff --git a/pkg/network/anonymity/queue/errors.go b/pkg/anonymity/queue/errors.go
similarity index 86%
rename from pkg/network/anonymity/queue/errors.go
rename to pkg/anonymity/queue/errors.go
index 6e3a267a3..579997dbf 100644
--- a/pkg/network/anonymity/queue/errors.go
+++ b/pkg/anonymity/queue/errors.go
@@ -1,7 +1,7 @@
package queue
const (
- errPrefix = "pkg/network/anonymity/queue = "
+ errPrefix = "pkg/anonymity/queue = "
)
type SQueueError struct {
diff --git a/pkg/network/anonymity/queue/examples/queue/main.go b/pkg/anonymity/queue/examples/queue/main.go
similarity index 97%
rename from pkg/network/anonymity/queue/examples/queue/main.go
rename to pkg/anonymity/queue/examples/queue/main.go
index 65fcf5637..13aca4726 100644
--- a/pkg/network/anonymity/queue/examples/queue/main.go
+++ b/pkg/anonymity/queue/examples/queue/main.go
@@ -7,10 +7,10 @@ import (
"fmt"
"time"
+ "github.com/number571/go-peer/pkg/anonymity/queue"
"github.com/number571/go-peer/pkg/client"
"github.com/number571/go-peer/pkg/client/message"
"github.com/number571/go-peer/pkg/crypto/asymmetric"
- "github.com/number571/go-peer/pkg/network/anonymity/queue"
net_message "github.com/number571/go-peer/pkg/network/message"
"github.com/number571/go-peer/pkg/payload"
)
diff --git a/pkg/network/anonymity/queue/examples/queue/main_test.go b/pkg/anonymity/queue/examples/queue/main_test.go
similarity index 100%
rename from pkg/network/anonymity/queue/examples/queue/main_test.go
rename to pkg/anonymity/queue/examples/queue/main_test.go
diff --git a/pkg/network/anonymity/queue/queue.go b/pkg/anonymity/queue/queue.go
similarity index 100%
rename from pkg/network/anonymity/queue/queue.go
rename to pkg/anonymity/queue/queue.go
diff --git a/pkg/network/anonymity/queue/queue_test.go b/pkg/anonymity/queue/queue_test.go
similarity index 100%
rename from pkg/network/anonymity/queue/queue_test.go
rename to pkg/anonymity/queue/queue_test.go
diff --git a/pkg/network/anonymity/queue/settings.go b/pkg/anonymity/queue/settings.go
similarity index 100%
rename from pkg/network/anonymity/queue/settings.go
rename to pkg/anonymity/queue/settings.go
diff --git a/pkg/network/anonymity/queue/types.go b/pkg/anonymity/queue/types.go
similarity index 100%
rename from pkg/network/anonymity/queue/types.go
rename to pkg/anonymity/queue/types.go
diff --git a/pkg/network/anonymity/settings.go b/pkg/anonymity/settings.go
similarity index 100%
rename from pkg/network/anonymity/settings.go
rename to pkg/anonymity/settings.go
diff --git a/pkg/network/anonymity/types.go b/pkg/anonymity/types.go
similarity index 77%
rename from pkg/network/anonymity/types.go
rename to pkg/anonymity/types.go
index cde49998d..d97756468 100644
--- a/pkg/network/anonymity/types.go
+++ b/pkg/anonymity/types.go
@@ -4,15 +4,19 @@ import (
"context"
"time"
+ "github.com/number571/go-peer/pkg/anonymity/queue"
"github.com/number571/go-peer/pkg/crypto/asymmetric"
"github.com/number571/go-peer/pkg/logger"
- "github.com/number571/go-peer/pkg/network"
- "github.com/number571/go-peer/pkg/network/anonymity/queue"
"github.com/number571/go-peer/pkg/payload"
"github.com/number571/go-peer/pkg/storage/database"
"github.com/number571/go-peer/pkg/types"
+
+ net_message "github.com/number571/go-peer/pkg/network/message"
)
+type IProducerF func(context.Context, net_message.IMessage) error
+type IConsumerF func(context.Context) (net_message.IMessage, error)
+
type IHandlerF func(
context.Context,
INode,
@@ -27,7 +31,6 @@ type INode interface {
GetLogger() logger.ILogger
GetSettings() ISettings
GetKVDatabase() database.IKVDatabase
- GetNetworkNode() network.INode
GetMessageQueue() queue.IQBProblemProcessor
GetMapPubKeys() asymmetric.IMapPubKeys
diff --git a/pkg/network/anonymity/errors.go b/pkg/network/anonymity/errors.go
deleted file mode 100644
index 3a7b7e267..000000000
--- a/pkg/network/anonymity/errors.go
+++ /dev/null
@@ -1,32 +0,0 @@
-package anonymity
-
-const (
- errPrefix = "pkg/network/anonymity = "
-)
-
-type SAnonymityError struct {
- str string
-}
-
-func (err *SAnonymityError) Error() string {
- return errPrefix + err.str
-}
-
-var (
- ErrSetHashIntoDB = &SAnonymityError{"set hash into database"}
- ErrGetHashFromDB = &SAnonymityError{"get hash from database"}
- ErrNilDB = &SAnonymityError{"database is nil"}
- ErrRetryLimit = &SAnonymityError{"retry limit"}
- ErrEnqueueMessage = &SAnonymityError{"enqueue message"}
- ErrUnknownType = &SAnonymityError{"unknown type"}
- ErrLoadMessage = &SAnonymityError{"load message"}
- ErrStoreHashWithBroadcast = &SAnonymityError{"store hash with broadcast"}
- ErrActionIsNotFound = &SAnonymityError{"action is not found"}
- ErrActionIsClosed = &SAnonymityError{"action is closed"}
- ErrActionTimeout = &SAnonymityError{"action timeout"}
- ErrEnqueuePayload = &SAnonymityError{"enqueue payload"}
- ErrFetchResponse = &SAnonymityError{"fetch response"}
- ErrRunning = &SAnonymityError{"node running"}
- ErrProcessRun = &SAnonymityError{"process run"}
- ErrHashAlreadyExist = &SAnonymityError{"hash already exist"}
-)
diff --git a/test/result/badge_codelines.svg b/test/result/badge_codelines.svg
index 0079fd33f..fd419c153 100644
--- a/test/result/badge_codelines.svg
+++ b/test/result/badge_codelines.svg
@@ -1 +1 @@
-
\ No newline at end of file
+
\ No newline at end of file
diff --git a/test/result/badge_coverage.svg b/test/result/badge_coverage.svg
index 2eb8e6b26..9fd7bd14b 100644
--- a/test/result/badge_coverage.svg
+++ b/test/result/badge_coverage.svg
@@ -1 +1 @@
-
\ No newline at end of file
+
\ No newline at end of file
diff --git a/test/result/coverage.svg b/test/result/coverage.svg
index b02a6d16a..93cdc15e2 100644
--- a/test/result/coverage.svg
+++ b/test/result/coverage.svg
@@ -7,7 +7,7 @@
>
-
+
-
+
-
+
-
+
client
+ data-math="N">anonymity
-
+
crypto
+ data-math="N">client
-
+
encoding
+ data-math="N">crypto
-
+
logger
+ data-math="N">encoding
-
+
network
+ data-math="N">logger
-
+
payload
+ data-math="N">network
-
+
state
+ data-math="N">payload
-
+
storage
+ data-math="N">state
-
+
client.go
+ data-math="N">storage
-
+
message
+ data-math="N">action.go
-
+
asymmetric
+ data-math="N">anonymity.go
-
+
-hashing
-
-
+
-
+
puzzle/puzzle.go
+ data-math="N">logger/log_builder.go
-
+
random/random.go
+ data-math="N">queue
-
+
symmetric/symmetric.go
+ data-math="N">settings.go
-
+
bytes.go
+ data-math="N">client.go
-
+
+message
+
-
+
hex.go
+ data-math="N">asymmetric
-
+
serialize_json.go
+ data-math="N">hashing
-
+
-
+
logger.go
+ data-math="N">puzzle/puzzle.go
-
-
-
-
-
-
-
+
anonymity
+ data-math="N">random/random.go
-
+
conn
+ data-math="N">symmetric/symmetric.go
-
+
connkeeper
+ data-math="N">bytes.go
-
+
-
+
message
+ data-math="N">hex.go
-
+
network.go
+ data-math="N">serialize_json.go
-
+
-settings.go
-
-
+
joiner
+ data-math="N">logger.go
-
+
+
+
+
+
+
+
payload32.go
+ data-math="N">conn
-
+
payload64.go
+ data-math="N">connkeeper
-
+
+
+
+
+
+
+
state.go
+ data-math="N">message
-
+
cache/lru.go
+ data-math="N">network.go
-
+
database
+ data-math="N">settings.go
-
+
message.go
+ data-math="N">joiner
-
+
dsa.go
+ data-math="N">payload32.go
-
+
kem.go
+ data-math="N">payload64.go
-
+
+
+
+
+
+
+
+
+
+
+
+
+
key.go
+ data-math="N">cache/lru.go
-
+
map_pubkeys.go
+ data-math="N">database
-
+
-
+
hmac.go
+ data-math="N">queue.go
-
+
-
+
anonymity.go
+ data-math="N">message.go
-
+
+dsa.go
+
-
+
+kem.go
+
-
+
logger/log_builder.go
+ data-math="N">key.go
-
+
queue
+ data-math="N">map_pubkeys.go
-
+
-
+
conn.go
+ data-math="N">hmac.go
-
+
settings.go
+ data-math="N">conn.go
-
+
connkeeper.go
+ data-math="N">settings.go
-
-
-
-
-
-
-
+
settings.go
+ data-math="N">connkeeper.go
-
+
-
+
message.go
+ data-math="N">settings.go
-
+
-
+
joiner32.go
+ data-math="N">message.go
-
+
-database.go
-
-
+
-
+
queue.go
+ data-math="N">database.go
-
-
-
-
-
-
\ No newline at end of file
From c83af931851572f0d2dfbf38299d48ab92983f4c Mon Sep 17 00:00:00 2001
From: number571
Date: Thu, 5 Dec 2024 00:20:04 +0700
Subject: [PATCH 07/17] update
---
CHANGELOG.md | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index d5e1b4ca3..a31b3bcd9 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -14,7 +14,8 @@
### IMPROVEMENTS
-- Update `pkg/network/anonymity/queue`: add GetConsumersCap
+- `pkg/network/anonymity/queue`: add GetConsumersCap
+- `pkg/anonymity`: move from pkg/network/anonymity -> pkg/anonymity
### CHANGES
From fa2eab8b442186877515eeb22a6d68cb54ff3f08 Mon Sep 17 00:00:00 2001
From: number571
Date: Thu, 5 Dec 2024 17:02:06 +0700
Subject: [PATCH 08/17] update
---
pkg/anonymity/adapter.go | 27 +++++++++++++++++++
pkg/anonymity/anonymity.go | 13 ++++-----
pkg/anonymity/anonymity_test.go | 24 +++++++++--------
pkg/anonymity/examples/echo/construct.go | 24 +++++++++--------
pkg/anonymity/examples/ping-pong/construct.go | 24 +++++++++--------
pkg/anonymity/types.go | 11 ++++++--
6 files changed, 80 insertions(+), 43 deletions(-)
create mode 100644 pkg/anonymity/adapter.go
diff --git a/pkg/anonymity/adapter.go b/pkg/anonymity/adapter.go
new file mode 100644
index 000000000..247d1ff50
--- /dev/null
+++ b/pkg/anonymity/adapter.go
@@ -0,0 +1,27 @@
+package anonymity
+
+import (
+ "context"
+
+ net_message "github.com/number571/go-peer/pkg/network/message"
+)
+
+type sAdapter struct {
+ fProduce IProducerF
+ fConsume IConsumerF
+}
+
+func NewAdapterByFuncs(pProduce IProducerF, pConsume IConsumerF) IAdapter {
+ return &sAdapter{
+ fProduce: pProduce,
+ fConsume: pConsume,
+ }
+}
+
+func (p *sAdapter) Produce(pCtx context.Context, pMsg net_message.IMessage) error {
+ return p.fProduce(pCtx, pMsg)
+}
+
+func (p *sAdapter) Consume(pCtx context.Context) (net_message.IMessage, error) {
+ return p.fConsume(pCtx)
+}
diff --git a/pkg/anonymity/anonymity.go b/pkg/anonymity/anonymity.go
index 955771c86..74f1c1bb2 100644
--- a/pkg/anonymity/anonymity.go
+++ b/pkg/anonymity/anonymity.go
@@ -30,8 +30,7 @@ type sNode struct {
fState state.IState
fSettings ISettings
fLogger logger.ILogger
- fProducer IProducerF
- fConsumer IConsumerF
+ fAdapter IAdapter
fKVDatavase database.IKVDatabase
fQueue queue.IQBProblemProcessor
fMapPubKeys asymmetric.IMapPubKeys
@@ -42,8 +41,7 @@ type sNode struct {
func NewNode(
pSett ISettings,
pLogger logger.ILogger,
- pProducer IProducerF,
- pConsumer IConsumerF,
+ pAdapter IAdapter,
pKVDatavase database.IKVDatabase,
pQueue queue.IQBProblemProcessor,
pMapPubKeys asymmetric.IMapPubKeys,
@@ -52,8 +50,7 @@ func NewNode(
fState: state.NewBoolState(),
fSettings: pSett,
fLogger: pLogger,
- fProducer: pProducer,
- fConsumer: pConsumer,
+ fAdapter: pAdapter,
fKVDatavase: pKVDatavase,
fQueue: pQueue,
fMapPubKeys: pMapPubKeys,
@@ -135,7 +132,7 @@ func (p *sNode) runConsumer(pCtx context.Context) error {
case <-pCtx.Done():
return pCtx.Err()
default:
- netMsg, err := p.fConsumer(pCtx)
+ netMsg, err := p.fAdapter.Consume(pCtx)
if err != nil {
continue
}
@@ -414,7 +411,7 @@ func (p *sNode) storeHashWithProduce(
}
// redirect message to another nodes
- if err := p.fProducer(pCtx, pNetMsg); err != nil {
+ if err := p.fAdapter.Produce(pCtx, pNetMsg); err != nil {
// some connections can return errors
p.fLogger.PushWarn(pLogBuilder.WithType(anon_logger.CLogBaseBroadcast))
return true, nil
diff --git a/pkg/anonymity/anonymity_test.go b/pkg/anonymity/anonymity_test.go
index dd4fef331..6d3536bfc 100644
--- a/pkg/anonymity/anonymity_test.go
+++ b/pkg/anonymity/anonymity_test.go
@@ -698,17 +698,19 @@ func testNewNodeWithDB(timeWait time.Duration, addr string, db database.IKVDatab
logger.NewSettings(&logger.SSettings{}),
func(_ logger.ILogArg) string { return "" },
),
- func(ctx context.Context, msg net_message.IMessage) error {
- return networkNode.BroadcastMessage(ctx, msg)
- },
- func(ctx context.Context) (net_message.IMessage, error) {
- select {
- case <-ctx.Done():
- return nil, ctx.Err()
- case msg := <-msgChan:
- return msg, nil
- }
- },
+ NewAdapterByFuncs(
+ func(ctx context.Context, msg net_message.IMessage) error {
+ return networkNode.BroadcastMessage(ctx, msg)
+ },
+ func(ctx context.Context) (net_message.IMessage, error) {
+ select {
+ case <-ctx.Done():
+ return nil, ctx.Err()
+ case msg := <-msgChan:
+ return msg, nil
+ }
+ },
+ ),
db,
queue.NewQBProblemProcessor(
queue.NewSettings(&queue.SSettings{
diff --git a/pkg/anonymity/examples/echo/construct.go b/pkg/anonymity/examples/echo/construct.go
index fe5ab242b..0e7a046ee 100644
--- a/pkg/anonymity/examples/echo/construct.go
+++ b/pkg/anonymity/examples/echo/construct.go
@@ -80,17 +80,19 @@ func newNode(serviceName, address string) (network.INode, anonymity.INode) {
)
},
),
- func(ctx context.Context, msg net_message.IMessage) error {
- return networkNode.BroadcastMessage(ctx, msg)
- },
- func(ctx context.Context) (net_message.IMessage, error) {
- select {
- case <-ctx.Done():
- return nil, ctx.Err()
- case msg := <-msgChan:
- return msg, nil
- }
- },
+ anonymity.NewAdapterByFuncs(
+ func(ctx context.Context, msg net_message.IMessage) error {
+ return networkNode.BroadcastMessage(ctx, msg)
+ },
+ func(ctx context.Context) (net_message.IMessage, error) {
+ select {
+ case <-ctx.Done():
+ return nil, ctx.Err()
+ case msg := <-msgChan:
+ return msg, nil
+ }
+ },
+ ),
func() database.IKVDatabase {
db, err := database.NewKVDatabase("./database_" + serviceName + ".db")
if err != nil {
diff --git a/pkg/anonymity/examples/ping-pong/construct.go b/pkg/anonymity/examples/ping-pong/construct.go
index fe5ab242b..0e7a046ee 100644
--- a/pkg/anonymity/examples/ping-pong/construct.go
+++ b/pkg/anonymity/examples/ping-pong/construct.go
@@ -80,17 +80,19 @@ func newNode(serviceName, address string) (network.INode, anonymity.INode) {
)
},
),
- func(ctx context.Context, msg net_message.IMessage) error {
- return networkNode.BroadcastMessage(ctx, msg)
- },
- func(ctx context.Context) (net_message.IMessage, error) {
- select {
- case <-ctx.Done():
- return nil, ctx.Err()
- case msg := <-msgChan:
- return msg, nil
- }
- },
+ anonymity.NewAdapterByFuncs(
+ func(ctx context.Context, msg net_message.IMessage) error {
+ return networkNode.BroadcastMessage(ctx, msg)
+ },
+ func(ctx context.Context) (net_message.IMessage, error) {
+ select {
+ case <-ctx.Done():
+ return nil, ctx.Err()
+ case msg := <-msgChan:
+ return msg, nil
+ }
+ },
+ ),
func() database.IKVDatabase {
db, err := database.NewKVDatabase("./database_" + serviceName + ".db")
if err != nil {
diff --git a/pkg/anonymity/types.go b/pkg/anonymity/types.go
index d97756468..836e52f12 100644
--- a/pkg/anonymity/types.go
+++ b/pkg/anonymity/types.go
@@ -14,8 +14,15 @@ import (
net_message "github.com/number571/go-peer/pkg/network/message"
)
-type IProducerF func(context.Context, net_message.IMessage) error
-type IConsumerF func(context.Context) (net_message.IMessage, error)
+type (
+ IProducerF func(context.Context, net_message.IMessage) error
+ IConsumerF func(context.Context) (net_message.IMessage, error)
+)
+
+type IAdapter interface {
+ Produce(context.Context, net_message.IMessage) error
+ Consume(context.Context) (net_message.IMessage, error)
+}
type IHandlerF func(
context.Context,
From 2a7b08b56a3e00e4da36c330375ed6da783cb465 Mon Sep 17 00:00:00 2001
From: number571
Date: Thu, 5 Dec 2024 17:30:55 +0700
Subject: [PATCH 09/17] update
---
pkg/anonymity/anonymity.go | 3 +-
pkg/anonymity/anonymity_test.go | 1 -
pkg/anonymity/examples/echo/construct.go | 1 -
pkg/anonymity/examples/ping-pong/construct.go | 1 -
test/result/badge_codelines.svg | 2 +-
test/result/coverage.svg | 233 +++++++++---------
6 files changed, 125 insertions(+), 116 deletions(-)
diff --git a/pkg/anonymity/anonymity.go b/pkg/anonymity/anonymity.go
index 74f1c1bb2..4309f85ed 100644
--- a/pkg/anonymity/anonymity.go
+++ b/pkg/anonymity/anonymity.go
@@ -44,7 +44,6 @@ func NewNode(
pAdapter IAdapter,
pKVDatavase database.IKVDatabase,
pQueue queue.IQBProblemProcessor,
- pMapPubKeys asymmetric.IMapPubKeys,
) INode {
return &sNode{
fState: state.NewBoolState(),
@@ -53,7 +52,7 @@ func NewNode(
fAdapter: pAdapter,
fKVDatavase: pKVDatavase,
fQueue: pQueue,
- fMapPubKeys: pMapPubKeys,
+ fMapPubKeys: asymmetric.NewMapPubKeys(),
fHandleRoutes: make(map[uint32]IHandlerF, 64),
fHandleActions: make(map[string]chan []byte, 64),
}
diff --git a/pkg/anonymity/anonymity_test.go b/pkg/anonymity/anonymity_test.go
index 6d3536bfc..b66ac254c 100644
--- a/pkg/anonymity/anonymity_test.go
+++ b/pkg/anonymity/anonymity_test.go
@@ -731,7 +731,6 @@ func testNewNodeWithDB(timeWait time.Duration, addr string, db database.IKVDatab
tcMsgSize,
),
),
- asymmetric.NewMapPubKeys(),
)
ctx, cancel := context.WithCancel(context.Background())
go func() { _ = node.Run(ctx) }()
diff --git a/pkg/anonymity/examples/echo/construct.go b/pkg/anonymity/examples/echo/construct.go
index 0e7a046ee..efba07fca 100644
--- a/pkg/anonymity/examples/echo/construct.go
+++ b/pkg/anonymity/examples/echo/construct.go
@@ -117,7 +117,6 @@ func newNode(serviceName, address string) (network.INode, anonymity.INode) {
msgSize,
),
),
- asymmetric.NewMapPubKeys(),
)
return networkNode, anonymityNode
}
diff --git a/pkg/anonymity/examples/ping-pong/construct.go b/pkg/anonymity/examples/ping-pong/construct.go
index 0e7a046ee..efba07fca 100644
--- a/pkg/anonymity/examples/ping-pong/construct.go
+++ b/pkg/anonymity/examples/ping-pong/construct.go
@@ -117,7 +117,6 @@ func newNode(serviceName, address string) (network.INode, anonymity.INode) {
msgSize,
),
),
- asymmetric.NewMapPubKeys(),
)
return networkNode, anonymityNode
}
diff --git a/test/result/badge_codelines.svg b/test/result/badge_codelines.svg
index fd419c153..0a6127918 100644
--- a/test/result/badge_codelines.svg
+++ b/test/result/badge_codelines.svg
@@ -1 +1 @@
-
\ No newline at end of file
+
\ No newline at end of file
diff --git a/test/result/coverage.svg b/test/result/coverage.svg
index 93cdc15e2..75a740ff2 100644
--- a/test/result/coverage.svg
+++ b/test/result/coverage.svg
@@ -7,7 +7,7 @@
>
-
+
-
+
-
+
-
+
anonymity
@@ -52,12 +52,12 @@
-
+
client
@@ -65,12 +65,12 @@
-
+
crypto
@@ -78,12 +78,12 @@
-
+
encoding
@@ -91,12 +91,12 @@
-
+
logger
@@ -104,7 +104,7 @@
-
+
-
+
payload
@@ -130,12 +130,12 @@
-
+
state
@@ -143,12 +143,12 @@
-
+
storage
@@ -156,12 +156,12 @@
-
+
action.go
@@ -169,12 +169,18 @@
-
+
+
+
+
+
+
+
anonymity.go
@@ -182,24 +188,31 @@
-
+
-
+
+head.go
+
-
+
logger/log_builder.go
@@ -207,12 +220,12 @@
-
+
queue
@@ -220,12 +233,12 @@
-
+
settings.go
@@ -233,12 +246,12 @@
-
+
client.go
@@ -246,12 +259,12 @@
-
+
message
@@ -259,12 +272,12 @@
-
+
asymmetric
@@ -272,12 +285,12 @@
-
+
hashing
@@ -285,18 +298,18 @@
-
+
-
+
puzzle/puzzle.go
@@ -304,12 +317,12 @@
-
+
random/random.go
@@ -317,12 +330,12 @@
-
+
symmetric/symmetric.go
@@ -330,12 +343,12 @@
-
+
bytes.go
@@ -343,18 +356,18 @@
-
+
-
+
hex.go
@@ -362,12 +375,12 @@
-
+
serialize_json.go
@@ -375,18 +388,18 @@
-
+
-
+
logger.go
@@ -394,13 +407,13 @@
-
+
-
+
-
+
connkeeper
@@ -426,13 +439,13 @@
-
+
-
+
-
+
-
+
settings.go
@@ -471,12 +484,12 @@
-
+
joiner
@@ -484,12 +497,12 @@
-
+
payload32.go
@@ -497,12 +510,12 @@
-
+
payload64.go
@@ -510,24 +523,24 @@
-
+
-
+
-
+
cache/lru.go
@@ -535,12 +548,12 @@
-
+
database
@@ -548,18 +561,18 @@
-
+
-
+
queue.go
@@ -567,18 +580,18 @@
-
+
-
+
message.go
@@ -586,12 +599,12 @@
-
+
dsa.go
@@ -599,12 +612,12 @@
-
+
kem.go
@@ -612,12 +625,12 @@
-
+
key.go
@@ -625,12 +638,12 @@
-
+
map_pubkeys.go
@@ -638,18 +651,18 @@
-
+
-
+
hmac.go
@@ -657,7 +670,7 @@
-
+
-
+
settings.go
@@ -683,12 +696,12 @@
-
+
connkeeper.go
@@ -696,18 +709,18 @@
-
+
-
+
settings.go
@@ -715,18 +728,18 @@
-
+
-
+
message.go
@@ -734,24 +747,24 @@
-
+
-
+
-
+
database.go
From 29899935fbc4dbbfc684a4db9169d1281a8bd6f8 Mon Sep 17 00:00:00 2001
From: number571
Date: Thu, 5 Dec 2024 21:07:31 +0700
Subject: [PATCH 10/17] update
---
pkg/anonymity/anonymity.go | 22 ++++++-------
pkg/anonymity/anonymity_test.go | 40 ++++++++++++------------
pkg/anonymity/examples/echo/main.go | 4 +--
pkg/anonymity/examples/ping-pong/main.go | 4 +--
pkg/anonymity/types.go | 18 ++++++-----
test/result/badge_codelines.svg | 2 +-
test/result/coverage.svg | 8 ++---
7 files changed, 50 insertions(+), 48 deletions(-)
diff --git a/pkg/anonymity/anonymity.go b/pkg/anonymity/anonymity.go
index 4309f85ed..f97c46829 100644
--- a/pkg/anonymity/anonymity.go
+++ b/pkg/anonymity/anonymity.go
@@ -32,7 +32,7 @@ type sNode struct {
fLogger logger.ILogger
fAdapter IAdapter
fKVDatavase database.IKVDatabase
- fQueue queue.IQBProblemProcessor
+ fQBProcessor queue.IQBProblemProcessor
fMapPubKeys asymmetric.IMapPubKeys
fHandleRoutes map[uint32]IHandlerF
fHandleActions map[string]chan []byte
@@ -43,7 +43,7 @@ func NewNode(
pLogger logger.ILogger,
pAdapter IAdapter,
pKVDatavase database.IKVDatabase,
- pQueue queue.IQBProblemProcessor,
+ pQBProcessor queue.IQBProblemProcessor,
) INode {
return &sNode{
fState: state.NewBoolState(),
@@ -51,7 +51,7 @@ func NewNode(
fLogger: pLogger,
fAdapter: pAdapter,
fKVDatavase: pKVDatavase,
- fQueue: pQueue,
+ fQBProcessor: pQBProcessor,
fMapPubKeys: asymmetric.NewMapPubKeys(),
fHandleRoutes: make(map[uint32]IHandlerF, 64),
fHandleActions: make(map[string]chan []byte, 64),
@@ -75,7 +75,7 @@ func (p *sNode) Run(pCtx context.Context) error {
go func() {
defer func() { wg.Done(); cancel() }()
- errs[0] = p.fQueue.Run(chCtx)
+ errs[0] = p.fQBProcessor.Run(chCtx)
}()
go func() {
defer func() { wg.Done(); cancel() }()
@@ -109,7 +109,7 @@ func (p *sNode) runProducer(pCtx context.Context) error {
case <-pCtx.Done():
return pCtx.Err()
default:
- netMsg := p.fQueue.DequeueMessage(pCtx)
+ netMsg := p.fQBProcessor.DequeueMessage(pCtx)
if netMsg == nil {
// context done
continue
@@ -117,7 +117,7 @@ func (p *sNode) runProducer(pCtx context.Context) error {
// create logger state
logBuilder := p.enrichLogger(anon_logger.NewLogBuilder(serviceName), netMsg).
- WithPubKey(p.fQueue.GetClient().GetPrivKey().GetPubKey())
+ WithPubKey(p.fQBProcessor.GetClient().GetPrivKey().GetPubKey())
// internal logger
_, _ = p.storeHashWithProduce(pCtx, logBuilder, netMsg)
@@ -153,8 +153,8 @@ func (p *sNode) GetKVDatabase() database.IKVDatabase {
return p.fKVDatavase
}
-func (p *sNode) GetMessageQueue() queue.IQBProblemProcessor {
- return p.fQueue
+func (p *sNode) GetQBProcessor() queue.IQBProblemProcessor {
+ return p.fQBProcessor
}
// Return f2f structure.
@@ -237,7 +237,7 @@ func (p *sNode) messageHandler(pCtx context.Context, pNetMsg net_message.IMessag
// update logger state
p.enrichLogger(logBuilder, pNetMsg)
- client := p.fQueue.GetClient()
+ client := p.fQBProcessor.GetClient()
encMsg := pNetMsg.GetPayload().GetBody()
// load encrypted message without decryption try
@@ -367,14 +367,14 @@ func (p *sNode) enqueuePayload(
if loadHead(pPld.GetHead()).getAction().isRequest() {
logType = anon_logger.CLogBaseEnqueueRequest
- client := p.fQueue.GetClient()
+ client := p.fQBProcessor.GetClient()
// enrich logger
pLogBuilder.
WithPubKey(client.GetPrivKey().GetPubKey()).
WithSize(len(pldBytes))
}
- if err := p.fQueue.EnqueueMessage(pRecv, pldBytes); err != nil {
+ if err := p.fQBProcessor.EnqueueMessage(pRecv, pldBytes); err != nil {
p.fLogger.PushWarn(pLogBuilder.WithType(logType))
return errors.Join(ErrEnqueueMessage, err)
}
diff --git a/pkg/anonymity/anonymity_test.go b/pkg/anonymity/anonymity_test.go
index b66ac254c..8a992922c 100644
--- a/pkg/anonymity/anonymity_test.go
+++ b/pkg/anonymity/anonymity_test.go
@@ -119,7 +119,7 @@ func TestComplexFetchPayload(t *testing.T) {
// nodes[1] -> nodes[0] -> nodes[2]
resp, err := nodes[0].FetchPayload(
ctx,
- nodes[1].GetMessageQueue().GetClient().GetPrivKey().GetPubKey(),
+ nodes[1].GetQBProcessor().GetClient().GetPrivKey().GetPubKey(),
payload.NewPayload32(tcHead, []byte(reqBody)),
)
if err != nil {
@@ -149,15 +149,15 @@ func TestF2FWithoutFriends(t *testing.T) {
}
defer testFreeNodes(nodes[:], networkNodes[:], cancels[:], 1)
- nodes[0].GetMapPubKeys().DelPubKey(nodes[1].GetMessageQueue().GetClient().GetPrivKey().GetPubKey())
- nodes[1].GetMapPubKeys().DelPubKey(nodes[0].GetMessageQueue().GetClient().GetPrivKey().GetPubKey())
+ nodes[0].GetMapPubKeys().DelPubKey(nodes[1].GetQBProcessor().GetClient().GetPrivKey().GetPubKey())
+ nodes[1].GetMapPubKeys().DelPubKey(nodes[0].GetQBProcessor().GetClient().GetPrivKey().GetPubKey())
ctx := context.Background()
// nodes[1] -> nodes[0] -> nodes[2]
_, err := nodes[0].FetchPayload(
ctx,
- nodes[1].GetMessageQueue().GetClient().GetPrivKey().GetPubKey(),
+ nodes[1].GetQBProcessor().GetClient().GetPrivKey().GetPubKey(),
payload.NewPayload32(tcHead, []byte(tcMsgBody)),
)
if err != nil {
@@ -185,11 +185,11 @@ func TestFetchPayload(t *testing.T) {
},
)
- largeBodySize := nodes[0].GetMessageQueue().GetClient().GetPayloadLimit() - encoding.CSizeUint64 + 1
+ largeBodySize := nodes[0].GetQBProcessor().GetClient().GetPayloadLimit() - encoding.CSizeUint64 + 1
ctx := context.Background()
_, err := nodes[0].FetchPayload(
ctx,
- nodes[1].GetMessageQueue().GetClient().GetPrivKey().GetPubKey(),
+ nodes[1].GetQBProcessor().GetClient().GetPrivKey().GetPubKey(),
payload.NewPayload32(tcHead, random.NewRandom().GetBytes(largeBodySize)),
)
if err == nil {
@@ -199,7 +199,7 @@ func TestFetchPayload(t *testing.T) {
result, err1 := nodes[0].FetchPayload(
ctx,
- nodes[1].GetMessageQueue().GetClient().GetPrivKey().GetPubKey(),
+ nodes[1].GetQBProcessor().GetClient().GetPrivKey().GetPubKey(),
payload.NewPayload32(tcHead, []byte(tcMsgBody)),
)
if err1 != nil {
@@ -234,10 +234,10 @@ func TestBroadcastPayload(t *testing.T) {
},
)
- largeBodySize := nodes[0].GetMessageQueue().GetClient().GetPayloadLimit() - encoding.CSizeUint64 + 1
+ largeBodySize := nodes[0].GetQBProcessor().GetClient().GetPayloadLimit() - encoding.CSizeUint64 + 1
err := nodes[0].SendPayload(
context.Background(),
- nodes[1].GetMessageQueue().GetClient().GetPrivKey().GetPubKey(),
+ nodes[1].GetQBProcessor().GetClient().GetPrivKey().GetPubKey(),
payload.NewPayload64(uint64(tcHead), random.NewRandom().GetBytes(largeBodySize)),
)
if err == nil {
@@ -247,7 +247,7 @@ func TestBroadcastPayload(t *testing.T) {
err1 := nodes[0].SendPayload(
context.Background(),
- nodes[1].GetMessageQueue().GetClient().GetPrivKey().GetPubKey(),
+ nodes[1].GetQBProcessor().GetClient().GetPrivKey().GetPubKey(),
payload.NewPayload64(uint64(tcHead), []byte(tcMsgBody)),
)
if err1 != nil {
@@ -280,7 +280,7 @@ func TestEnqueuePayload(t *testing.T) {
defer testFreeNodes(nodes[:], networkNodes[:], cancels[:], 8)
node := nodes[0].(*sNode)
- pubKey := nodes[1].GetMessageQueue().GetClient().GetPrivKey().GetPubKey()
+ pubKey := nodes[1].GetQBProcessor().GetClient().GetPrivKey().GetPubKey()
logBuilder := anon_logger.NewLogBuilder("test")
pld := payload.NewPayload64(uint64(tcHead), []byte(tcMsgBody))
@@ -298,7 +298,7 @@ func TestEnqueuePayload(t *testing.T) {
).ToBytes()
for i := 0; i < tcQueueCap; i++ {
- if err := node.fQueue.EnqueueMessage(pubKey, pldBytes); err != nil {
+ if err := node.fQBProcessor.EnqueueMessage(pubKey, pldBytes); err != nil {
t.Error("failed send message (push to queue)")
return
}
@@ -322,7 +322,7 @@ func TestHandleWrapper(t *testing.T) {
node := _node.(*sNode)
handler := node.messageHandler
- client := node.fQueue.GetClient()
+ client := node.fQBProcessor.GetClient()
privKey := client.GetPrivKey()
pubKey := privKey.GetPubKey()
@@ -450,7 +450,7 @@ func TestStoreHashWithBroadcastMessage(t *testing.T) {
defer testFreeNodes([]INode{_node}, []network.INode{networkNode}, []context.CancelFunc{cancel}, 6)
node := _node.(*sNode)
- client := node.fQueue.GetClient()
+ client := node.fQBProcessor.GetClient()
msg, err := client.EncryptMessage(
client.GetPrivKey().GetPubKey(),
@@ -503,7 +503,7 @@ func TestRecvSendMessage(t *testing.T) {
return
}
- client := node.fQueue.GetClient()
+ client := node.fQBProcessor.GetClient()
pubKey := client.GetPrivKey().GetPubKey()
actionKey := newActionKey(pubKey, sAction(111).setType(true))
@@ -539,7 +539,7 @@ func TestRecvSendMessage(t *testing.T) {
).ToBytes()
for i := 0; i < tcQueueCap; i++ {
- if err := node.fQueue.EnqueueMessage(pubKey, pldBytes); err != nil {
+ if err := node.fQBProcessor.EnqueueMessage(pubKey, pldBytes); err != nil {
t.Error("failed send message (push to queue)")
return
}
@@ -548,7 +548,7 @@ func TestRecvSendMessage(t *testing.T) {
hasError := false
for i := 0; i < 10; i++ {
// message can be dequeued in the send's call time
- if err := node.fQueue.EnqueueMessage(pubKey, pldBytes); err != nil {
+ if err := node.fQBProcessor.EnqueueMessage(pubKey, pldBytes); err != nil {
hasError = true
break
}
@@ -577,8 +577,8 @@ func testNewNodes(t *testing.T, timeWait time.Duration, addresses [2]string, typ
}
}
- pubKey1 := nodes[1].GetMessageQueue().GetClient().GetPrivKey().GetPubKey()
- pubKey0 := nodes[0].GetMessageQueue().GetClient().GetPrivKey().GetPubKey()
+ pubKey1 := nodes[1].GetQBProcessor().GetClient().GetPrivKey().GetPubKey()
+ pubKey0 := nodes[0].GetQBProcessor().GetClient().GetPrivKey().GetPubKey()
nodes[0].GetMapPubKeys().SetPubKey(pubKey1)
nodes[1].GetMapPubKeys().SetPubKey(pubKey0)
@@ -764,7 +764,7 @@ func (p *sNode) testNewNetworkMessage(pSett net_message.IConstructSettings, pMsg
return net_message.NewMessage(
pSett,
payload.NewPayload32(
- p.fQueue.GetSettings().GetNetworkMask(),
+ p.fQBProcessor.GetSettings().GetNetworkMask(),
pMsgBytes,
),
)
diff --git a/pkg/anonymity/examples/echo/main.go b/pkg/anonymity/examples/echo/main.go
index f9cb7d8f6..212c57c94 100644
--- a/pkg/anonymity/examples/echo/main.go
+++ b/pkg/anonymity/examples/echo/main.go
@@ -58,8 +58,8 @@ func runServiceNode() anonymity.INode {
}
func exchangeKeys(node1, node2 anonymity.INode) (asymmetric.IPubKey, asymmetric.IPubKey) {
- pubKey1 := node1.GetMessageQueue().GetClient().GetPrivKey().GetPubKey()
- pubKey2 := node2.GetMessageQueue().GetClient().GetPrivKey().GetPubKey()
+ pubKey1 := node1.GetQBProcessor().GetClient().GetPrivKey().GetPubKey()
+ pubKey2 := node2.GetQBProcessor().GetClient().GetPrivKey().GetPubKey()
node1.GetMapPubKeys().SetPubKey(pubKey2)
node2.GetMapPubKeys().SetPubKey(pubKey1)
diff --git a/pkg/anonymity/examples/ping-pong/main.go b/pkg/anonymity/examples/ping-pong/main.go
index 5b3421cde..b0a5a89c8 100644
--- a/pkg/anonymity/examples/ping-pong/main.go
+++ b/pkg/anonymity/examples/ping-pong/main.go
@@ -76,8 +76,8 @@ func runServiceNode() anonymity.INode {
}
func exchangeKeys(node1, node2 anonymity.INode) (asymmetric.IPubKey, asymmetric.IPubKey) {
- pubKey1 := node1.GetMessageQueue().GetClient().GetPrivKey().GetPubKey()
- pubKey2 := node2.GetMessageQueue().GetClient().GetPrivKey().GetPubKey()
+ pubKey1 := node1.GetQBProcessor().GetClient().GetPrivKey().GetPubKey()
+ pubKey2 := node2.GetQBProcessor().GetClient().GetPrivKey().GetPubKey()
node1.GetMapPubKeys().SetPubKey(pubKey2)
node2.GetMapPubKeys().SetPubKey(pubKey1)
diff --git a/pkg/anonymity/types.go b/pkg/anonymity/types.go
index 836e52f12..8c5469e81 100644
--- a/pkg/anonymity/types.go
+++ b/pkg/anonymity/types.go
@@ -17,19 +17,21 @@ import (
type (
IProducerF func(context.Context, net_message.IMessage) error
IConsumerF func(context.Context) (net_message.IMessage, error)
+ IHandlerF func(context.Context, INode, asymmetric.IPubKey, []byte) ([]byte, error)
)
type IAdapter interface {
+ IProducer
+ IConsumer
+}
+
+type IProducer interface {
Produce(context.Context, net_message.IMessage) error
- Consume(context.Context) (net_message.IMessage, error)
}
-type IHandlerF func(
- context.Context,
- INode,
- asymmetric.IPubKey,
- []byte,
-) ([]byte, error)
+type IConsumer interface {
+ Consume(context.Context) (net_message.IMessage, error)
+}
type INode interface {
types.IRunner
@@ -38,8 +40,8 @@ type INode interface {
GetLogger() logger.ILogger
GetSettings() ISettings
GetKVDatabase() database.IKVDatabase
- GetMessageQueue() queue.IQBProblemProcessor
GetMapPubKeys() asymmetric.IMapPubKeys
+ GetQBProcessor() queue.IQBProblemProcessor
SendPayload(context.Context, asymmetric.IPubKey, payload.IPayload64) error
FetchPayload(context.Context, asymmetric.IPubKey, payload.IPayload32) ([]byte, error)
diff --git a/test/result/badge_codelines.svg b/test/result/badge_codelines.svg
index 0a6127918..6ce327f98 100644
--- a/test/result/badge_codelines.svg
+++ b/test/result/badge_codelines.svg
@@ -1 +1 @@
-
\ No newline at end of file
+
\ No newline at end of file
diff --git a/test/result/coverage.svg b/test/result/coverage.svg
index 75a740ff2..8d3f89ab4 100644
--- a/test/result/coverage.svg
+++ b/test/result/coverage.svg
@@ -7,7 +7,7 @@
>
-
+
-
+
-
+
-
+
Date: Thu, 5 Dec 2024 21:41:43 +0700
Subject: [PATCH 11/17] update
---
pkg/anonymity/{ => adapter}/adapter.go | 13 +-
pkg/anonymity/adapter/types.go | 20 ++
pkg/anonymity/anonymity.go | 9 +-
pkg/anonymity/anonymity_test.go | 3 +-
pkg/anonymity/examples/echo/construct.go | 3 +-
pkg/anonymity/examples/ping-pong/construct.go | 3 +-
pkg/anonymity/types.go | 21 +-
test/result/badge_codelines.svg | 2 +-
test/result/coverage.svg | 224 +++++++++---------
9 files changed, 158 insertions(+), 140 deletions(-)
rename pkg/anonymity/{ => adapter}/adapter.go (62%)
create mode 100644 pkg/anonymity/adapter/types.go
diff --git a/pkg/anonymity/adapter.go b/pkg/anonymity/adapter/adapter.go
similarity index 62%
rename from pkg/anonymity/adapter.go
rename to pkg/anonymity/adapter/adapter.go
index 247d1ff50..a40fad917 100644
--- a/pkg/anonymity/adapter.go
+++ b/pkg/anonymity/adapter/adapter.go
@@ -1,4 +1,4 @@
-package anonymity
+package adapter
import (
"context"
@@ -6,12 +6,17 @@ import (
net_message "github.com/number571/go-peer/pkg/network/message"
)
+type (
+ iProducerF func(context.Context, net_message.IMessage) error
+ iConsumerF func(context.Context) (net_message.IMessage, error)
+)
+
type sAdapter struct {
- fProduce IProducerF
- fConsume IConsumerF
+ fProduce iProducerF
+ fConsume iConsumerF
}
-func NewAdapterByFuncs(pProduce IProducerF, pConsume IConsumerF) IAdapter {
+func NewAdapterByFuncs(pProduce iProducerF, pConsume iConsumerF) IAdapter {
return &sAdapter{
fProduce: pProduce,
fConsume: pConsume,
diff --git a/pkg/anonymity/adapter/types.go b/pkg/anonymity/adapter/types.go
new file mode 100644
index 000000000..8e8b5ae98
--- /dev/null
+++ b/pkg/anonymity/adapter/types.go
@@ -0,0 +1,20 @@
+package adapter
+
+import (
+ "context"
+
+ net_message "github.com/number571/go-peer/pkg/network/message"
+)
+
+type IAdapter interface {
+ IProducer
+ IConsumer
+}
+
+type IProducer interface {
+ Produce(context.Context, net_message.IMessage) error
+}
+
+type IConsumer interface {
+ Consume(context.Context) (net_message.IMessage, error)
+}
diff --git a/pkg/anonymity/anonymity.go b/pkg/anonymity/anonymity.go
index f97c46829..f05468559 100644
--- a/pkg/anonymity/anonymity.go
+++ b/pkg/anonymity/anonymity.go
@@ -7,6 +7,7 @@ import (
"sync"
"time"
+ "github.com/number571/go-peer/pkg/anonymity/adapter"
"github.com/number571/go-peer/pkg/anonymity/queue"
"github.com/number571/go-peer/pkg/client/message"
"github.com/number571/go-peer/pkg/crypto/asymmetric"
@@ -30,7 +31,7 @@ type sNode struct {
fState state.IState
fSettings ISettings
fLogger logger.ILogger
- fAdapter IAdapter
+ fAdapter adapter.IAdapter
fKVDatavase database.IKVDatabase
fQBProcessor queue.IQBProblemProcessor
fMapPubKeys asymmetric.IMapPubKeys
@@ -41,7 +42,7 @@ type sNode struct {
func NewNode(
pSett ISettings,
pLogger logger.ILogger,
- pAdapter IAdapter,
+ pAdapter adapter.IAdapter,
pKVDatavase database.IKVDatabase,
pQBProcessor queue.IQBProblemProcessor,
) INode {
@@ -149,6 +150,10 @@ func (p *sNode) GetSettings() ISettings {
return p.fSettings
}
+func (p *sNode) GetAdapter() adapter.IAdapter {
+ return p.fAdapter
+}
+
func (p *sNode) GetKVDatabase() database.IKVDatabase {
return p.fKVDatavase
}
diff --git a/pkg/anonymity/anonymity_test.go b/pkg/anonymity/anonymity_test.go
index 8a992922c..b239f889f 100644
--- a/pkg/anonymity/anonymity_test.go
+++ b/pkg/anonymity/anonymity_test.go
@@ -11,6 +11,7 @@ import (
"testing"
"time"
+ "github.com/number571/go-peer/pkg/anonymity/adapter"
"github.com/number571/go-peer/pkg/anonymity/queue"
"github.com/number571/go-peer/pkg/client"
"github.com/number571/go-peer/pkg/crypto/asymmetric"
@@ -698,7 +699,7 @@ func testNewNodeWithDB(timeWait time.Duration, addr string, db database.IKVDatab
logger.NewSettings(&logger.SSettings{}),
func(_ logger.ILogArg) string { return "" },
),
- NewAdapterByFuncs(
+ adapter.NewAdapterByFuncs(
func(ctx context.Context, msg net_message.IMessage) error {
return networkNode.BroadcastMessage(ctx, msg)
},
diff --git a/pkg/anonymity/examples/echo/construct.go b/pkg/anonymity/examples/echo/construct.go
index efba07fca..50f8b8f1d 100644
--- a/pkg/anonymity/examples/echo/construct.go
+++ b/pkg/anonymity/examples/echo/construct.go
@@ -7,6 +7,7 @@ import (
"time"
"github.com/number571/go-peer/pkg/anonymity"
+ "github.com/number571/go-peer/pkg/anonymity/adapter"
anon_logger "github.com/number571/go-peer/pkg/anonymity/logger"
"github.com/number571/go-peer/pkg/anonymity/queue"
"github.com/number571/go-peer/pkg/client"
@@ -80,7 +81,7 @@ func newNode(serviceName, address string) (network.INode, anonymity.INode) {
)
},
),
- anonymity.NewAdapterByFuncs(
+ adapter.NewAdapterByFuncs(
func(ctx context.Context, msg net_message.IMessage) error {
return networkNode.BroadcastMessage(ctx, msg)
},
diff --git a/pkg/anonymity/examples/ping-pong/construct.go b/pkg/anonymity/examples/ping-pong/construct.go
index efba07fca..50f8b8f1d 100644
--- a/pkg/anonymity/examples/ping-pong/construct.go
+++ b/pkg/anonymity/examples/ping-pong/construct.go
@@ -7,6 +7,7 @@ import (
"time"
"github.com/number571/go-peer/pkg/anonymity"
+ "github.com/number571/go-peer/pkg/anonymity/adapter"
anon_logger "github.com/number571/go-peer/pkg/anonymity/logger"
"github.com/number571/go-peer/pkg/anonymity/queue"
"github.com/number571/go-peer/pkg/client"
@@ -80,7 +81,7 @@ func newNode(serviceName, address string) (network.INode, anonymity.INode) {
)
},
),
- anonymity.NewAdapterByFuncs(
+ adapter.NewAdapterByFuncs(
func(ctx context.Context, msg net_message.IMessage) error {
return networkNode.BroadcastMessage(ctx, msg)
},
diff --git a/pkg/anonymity/types.go b/pkg/anonymity/types.go
index 8c5469e81..3819d6b6d 100644
--- a/pkg/anonymity/types.go
+++ b/pkg/anonymity/types.go
@@ -4,41 +4,26 @@ import (
"context"
"time"
+ "github.com/number571/go-peer/pkg/anonymity/adapter"
"github.com/number571/go-peer/pkg/anonymity/queue"
"github.com/number571/go-peer/pkg/crypto/asymmetric"
"github.com/number571/go-peer/pkg/logger"
"github.com/number571/go-peer/pkg/payload"
"github.com/number571/go-peer/pkg/storage/database"
"github.com/number571/go-peer/pkg/types"
-
- net_message "github.com/number571/go-peer/pkg/network/message"
)
type (
- IProducerF func(context.Context, net_message.IMessage) error
- IConsumerF func(context.Context) (net_message.IMessage, error)
- IHandlerF func(context.Context, INode, asymmetric.IPubKey, []byte) ([]byte, error)
+ IHandlerF func(context.Context, INode, asymmetric.IPubKey, []byte) ([]byte, error)
)
-type IAdapter interface {
- IProducer
- IConsumer
-}
-
-type IProducer interface {
- Produce(context.Context, net_message.IMessage) error
-}
-
-type IConsumer interface {
- Consume(context.Context) (net_message.IMessage, error)
-}
-
type INode interface {
types.IRunner
HandleFunc(uint32, IHandlerF) INode
GetLogger() logger.ILogger
GetSettings() ISettings
+ GetAdapter() adapter.IAdapter
GetKVDatabase() database.IKVDatabase
GetMapPubKeys() asymmetric.IMapPubKeys
GetQBProcessor() queue.IQBProblemProcessor
diff --git a/test/result/badge_codelines.svg b/test/result/badge_codelines.svg
index 6ce327f98..12f1cc2e5 100644
--- a/test/result/badge_codelines.svg
+++ b/test/result/badge_codelines.svg
@@ -1 +1 @@
-
\ No newline at end of file
+
\ No newline at end of file
diff --git a/test/result/coverage.svg b/test/result/coverage.svg
index 8d3f89ab4..0b24b2541 100644
--- a/test/result/coverage.svg
+++ b/test/result/coverage.svg
@@ -7,7 +7,7 @@
>
-
+
-
+
-
+
-
+
anonymity
@@ -52,12 +52,12 @@
-
+
client
@@ -65,12 +65,12 @@
-
+
crypto
@@ -78,12 +78,12 @@
-
+
encoding
@@ -91,12 +91,12 @@
-
+
logger
@@ -104,7 +104,7 @@
-
+
-
+
payload
@@ -130,12 +130,12 @@
-
+
state
@@ -143,12 +143,12 @@
-
+
storage
@@ -156,12 +156,12 @@
-
+
action.go
@@ -169,18 +169,18 @@
-
+
-
+
anonymity.go
@@ -188,18 +188,18 @@
-
+
-
+
head.go
@@ -207,12 +207,12 @@
-
+
logger/log_builder.go
@@ -220,12 +220,12 @@
-
+
queue
@@ -233,12 +233,12 @@
-
+
settings.go
@@ -246,12 +246,12 @@
-
+
client.go
@@ -259,12 +259,12 @@
-
+
message
@@ -272,12 +272,12 @@
-
+
asymmetric
@@ -285,12 +285,12 @@
-
+
hashing
@@ -298,18 +298,18 @@
-
+
-
+
puzzle/puzzle.go
@@ -317,12 +317,12 @@
-
+
random/random.go
@@ -330,12 +330,12 @@
-
+
symmetric/symmetric.go
@@ -343,12 +343,12 @@
-
+
bytes.go
@@ -356,18 +356,18 @@
-
+
-
+
hex.go
@@ -375,12 +375,12 @@
-
+
serialize_json.go
@@ -388,18 +388,18 @@
-
+
-
+
logger.go
@@ -407,13 +407,13 @@
-
+
-
+
-
+
connkeeper
@@ -439,13 +439,13 @@
-
+
-
+
-
+
-
+
settings.go
@@ -484,12 +484,12 @@
-
+
joiner
@@ -497,12 +497,12 @@
-
+
payload32.go
@@ -510,12 +510,12 @@
-
+
payload64.go
@@ -523,24 +523,24 @@
-
+
-
+
-
+
cache/lru.go
@@ -548,12 +548,12 @@
-
+
database
@@ -561,18 +561,18 @@
-
+
-
+
queue.go
@@ -580,18 +580,18 @@
-
+
-
+
message.go
@@ -599,12 +599,12 @@
-
+
dsa.go
@@ -612,12 +612,12 @@
-
+
kem.go
@@ -625,12 +625,12 @@
-
+
key.go
@@ -638,12 +638,12 @@
-
+
map_pubkeys.go
@@ -651,18 +651,18 @@
-
+
-
+
hmac.go
@@ -670,7 +670,7 @@
-
+
-
+
settings.go
@@ -696,12 +696,12 @@
-
+
connkeeper.go
@@ -709,18 +709,18 @@
-
+
-
+
settings.go
@@ -728,18 +728,18 @@
-
+
-
+
message.go
@@ -747,24 +747,24 @@
-
+
-
+
-
+
database.go
From 21678709852b67d7c6d2da14c4001e7c5eacdaba Mon Sep 17 00:00:00 2001
From: number571
Date: Thu, 5 Dec 2024 23:11:42 +0700
Subject: [PATCH 12/17] update
---
.../{adapter => adapters}/adapter.go | 6 +-
pkg/anonymity/adapters/merge.go | 86 ++++++
pkg/anonymity/{adapter => adapters}/types.go | 2 +-
pkg/anonymity/anonymity.go | 8 +-
pkg/anonymity/anonymity_test.go | 4 +-
pkg/anonymity/examples/echo/construct.go | 4 +-
pkg/anonymity/examples/ping-pong/construct.go | 4 +-
pkg/anonymity/types.go | 4 +-
test/result/badge_coverage.svg | 2 +-
test/result/coverage.svg | 262 ++++++++++--------
10 files changed, 249 insertions(+), 133 deletions(-)
rename pkg/anonymity/{adapter => adapters}/adapter.go (92%)
create mode 100644 pkg/anonymity/adapters/merge.go
rename pkg/anonymity/{adapter => adapters}/types.go (94%)
diff --git a/pkg/anonymity/adapter/adapter.go b/pkg/anonymity/adapters/adapter.go
similarity index 92%
rename from pkg/anonymity/adapter/adapter.go
rename to pkg/anonymity/adapters/adapter.go
index a40fad917..dff0dfc02 100644
--- a/pkg/anonymity/adapter/adapter.go
+++ b/pkg/anonymity/adapters/adapter.go
@@ -1,4 +1,4 @@
-package adapter
+package adapters
import (
"context"
@@ -6,6 +6,10 @@ import (
net_message "github.com/number571/go-peer/pkg/network/message"
)
+var (
+ _ IAdapter = &sAdapter{}
+)
+
type (
iProducerF func(context.Context, net_message.IMessage) error
iConsumerF func(context.Context) (net_message.IMessage, error)
diff --git a/pkg/anonymity/adapters/merge.go b/pkg/anonymity/adapters/merge.go
new file mode 100644
index 000000000..13f3eaea5
--- /dev/null
+++ b/pkg/anonymity/adapters/merge.go
@@ -0,0 +1,86 @@
+package adapters
+
+import (
+ "context"
+ "errors"
+ "sync"
+
+ net_message "github.com/number571/go-peer/pkg/network/message"
+)
+
+var (
+ _ IAdapter = &sMergedAdapter{}
+)
+
+type sMergedAdapter struct {
+ fMsgChan chan net_message.IMessage
+ fAdapters []*sWaitAdapter
+}
+
+type sWaitAdapter struct {
+ fMutex sync.Mutex
+ fAdapter IAdapter
+}
+
+func NewMergedAdapter(pAdapters ...IAdapter) IAdapter {
+ return &sMergedAdapter{
+ fMsgChan: make(chan net_message.IMessage, len(pAdapters)),
+ fAdapters: toWaitAdapters(pAdapters),
+ }
+}
+
+func (p *sMergedAdapter) Produce(pCtx context.Context, pMsg net_message.IMessage) error {
+ N := len(p.fAdapters)
+ errs := make([]error, N)
+
+ wg := &sync.WaitGroup{}
+ wg.Add(N)
+ for i, a := range p.fAdapters {
+ go func(i int, a *sWaitAdapter) {
+ defer wg.Done()
+ errs[i] = a.fAdapter.Produce(pCtx, pMsg)
+ }(i, a)
+ }
+ wg.Wait()
+
+ return errors.Join(errs...)
+}
+
+func (p *sMergedAdapter) Consume(pCtx context.Context) (net_message.IMessage, error) {
+ select {
+ case <-pCtx.Done():
+ return nil, pCtx.Err()
+ case msg := <-p.fMsgChan:
+ return msg, nil
+ default:
+ }
+
+ for _, a := range p.fAdapters {
+ go func(a *sWaitAdapter) {
+ if ok := a.fMutex.TryLock(); !ok {
+ return
+ }
+ defer a.fMutex.Unlock()
+ msg, err := a.fAdapter.Consume(pCtx)
+ if err != nil {
+ return
+ }
+ p.fMsgChan <- msg
+ }(a)
+ }
+
+ select {
+ case <-pCtx.Done():
+ return nil, pCtx.Err()
+ case msg := <-p.fMsgChan:
+ return msg, nil
+ }
+}
+
+func toWaitAdapters(pAdapters []IAdapter) []*sWaitAdapter {
+ result := make([]*sWaitAdapter, 0, len(pAdapters))
+ for _, a := range pAdapters {
+ result = append(result, &sWaitAdapter{fAdapter: a})
+ }
+ return result
+}
diff --git a/pkg/anonymity/adapter/types.go b/pkg/anonymity/adapters/types.go
similarity index 94%
rename from pkg/anonymity/adapter/types.go
rename to pkg/anonymity/adapters/types.go
index 8e8b5ae98..5ebe9ff03 100644
--- a/pkg/anonymity/adapter/types.go
+++ b/pkg/anonymity/adapters/types.go
@@ -1,4 +1,4 @@
-package adapter
+package adapters
import (
"context"
diff --git a/pkg/anonymity/anonymity.go b/pkg/anonymity/anonymity.go
index f05468559..c9320c3d5 100644
--- a/pkg/anonymity/anonymity.go
+++ b/pkg/anonymity/anonymity.go
@@ -7,7 +7,7 @@ import (
"sync"
"time"
- "github.com/number571/go-peer/pkg/anonymity/adapter"
+ "github.com/number571/go-peer/pkg/anonymity/adapters"
"github.com/number571/go-peer/pkg/anonymity/queue"
"github.com/number571/go-peer/pkg/client/message"
"github.com/number571/go-peer/pkg/crypto/asymmetric"
@@ -31,7 +31,7 @@ type sNode struct {
fState state.IState
fSettings ISettings
fLogger logger.ILogger
- fAdapter adapter.IAdapter
+ fAdapter adapters.IAdapter
fKVDatavase database.IKVDatabase
fQBProcessor queue.IQBProblemProcessor
fMapPubKeys asymmetric.IMapPubKeys
@@ -42,7 +42,7 @@ type sNode struct {
func NewNode(
pSett ISettings,
pLogger logger.ILogger,
- pAdapter adapter.IAdapter,
+ pAdapter adapters.IAdapter,
pKVDatavase database.IKVDatabase,
pQBProcessor queue.IQBProblemProcessor,
) INode {
@@ -150,7 +150,7 @@ func (p *sNode) GetSettings() ISettings {
return p.fSettings
}
-func (p *sNode) GetAdapter() adapter.IAdapter {
+func (p *sNode) GetAdapter() adapters.IAdapter {
return p.fAdapter
}
diff --git a/pkg/anonymity/anonymity_test.go b/pkg/anonymity/anonymity_test.go
index b239f889f..babb9094e 100644
--- a/pkg/anonymity/anonymity_test.go
+++ b/pkg/anonymity/anonymity_test.go
@@ -11,7 +11,7 @@ import (
"testing"
"time"
- "github.com/number571/go-peer/pkg/anonymity/adapter"
+ "github.com/number571/go-peer/pkg/anonymity/adapters"
"github.com/number571/go-peer/pkg/anonymity/queue"
"github.com/number571/go-peer/pkg/client"
"github.com/number571/go-peer/pkg/crypto/asymmetric"
@@ -699,7 +699,7 @@ func testNewNodeWithDB(timeWait time.Duration, addr string, db database.IKVDatab
logger.NewSettings(&logger.SSettings{}),
func(_ logger.ILogArg) string { return "" },
),
- adapter.NewAdapterByFuncs(
+ adapters.NewAdapterByFuncs(
func(ctx context.Context, msg net_message.IMessage) error {
return networkNode.BroadcastMessage(ctx, msg)
},
diff --git a/pkg/anonymity/examples/echo/construct.go b/pkg/anonymity/examples/echo/construct.go
index 50f8b8f1d..76edfb644 100644
--- a/pkg/anonymity/examples/echo/construct.go
+++ b/pkg/anonymity/examples/echo/construct.go
@@ -7,7 +7,7 @@ import (
"time"
"github.com/number571/go-peer/pkg/anonymity"
- "github.com/number571/go-peer/pkg/anonymity/adapter"
+ "github.com/number571/go-peer/pkg/anonymity/adapters"
anon_logger "github.com/number571/go-peer/pkg/anonymity/logger"
"github.com/number571/go-peer/pkg/anonymity/queue"
"github.com/number571/go-peer/pkg/client"
@@ -81,7 +81,7 @@ func newNode(serviceName, address string) (network.INode, anonymity.INode) {
)
},
),
- adapter.NewAdapterByFuncs(
+ adapters.NewAdapterByFuncs(
func(ctx context.Context, msg net_message.IMessage) error {
return networkNode.BroadcastMessage(ctx, msg)
},
diff --git a/pkg/anonymity/examples/ping-pong/construct.go b/pkg/anonymity/examples/ping-pong/construct.go
index 50f8b8f1d..76edfb644 100644
--- a/pkg/anonymity/examples/ping-pong/construct.go
+++ b/pkg/anonymity/examples/ping-pong/construct.go
@@ -7,7 +7,7 @@ import (
"time"
"github.com/number571/go-peer/pkg/anonymity"
- "github.com/number571/go-peer/pkg/anonymity/adapter"
+ "github.com/number571/go-peer/pkg/anonymity/adapters"
anon_logger "github.com/number571/go-peer/pkg/anonymity/logger"
"github.com/number571/go-peer/pkg/anonymity/queue"
"github.com/number571/go-peer/pkg/client"
@@ -81,7 +81,7 @@ func newNode(serviceName, address string) (network.INode, anonymity.INode) {
)
},
),
- adapter.NewAdapterByFuncs(
+ adapters.NewAdapterByFuncs(
func(ctx context.Context, msg net_message.IMessage) error {
return networkNode.BroadcastMessage(ctx, msg)
},
diff --git a/pkg/anonymity/types.go b/pkg/anonymity/types.go
index 3819d6b6d..d8580a9a8 100644
--- a/pkg/anonymity/types.go
+++ b/pkg/anonymity/types.go
@@ -4,7 +4,7 @@ import (
"context"
"time"
- "github.com/number571/go-peer/pkg/anonymity/adapter"
+ "github.com/number571/go-peer/pkg/anonymity/adapters"
"github.com/number571/go-peer/pkg/anonymity/queue"
"github.com/number571/go-peer/pkg/crypto/asymmetric"
"github.com/number571/go-peer/pkg/logger"
@@ -23,7 +23,7 @@ type INode interface {
GetLogger() logger.ILogger
GetSettings() ISettings
- GetAdapter() adapter.IAdapter
+ GetAdapter() adapters.IAdapter
GetKVDatabase() database.IKVDatabase
GetMapPubKeys() asymmetric.IMapPubKeys
GetQBProcessor() queue.IQBProblemProcessor
diff --git a/test/result/badge_coverage.svg b/test/result/badge_coverage.svg
index 9fd7bd14b..09558fd26 100644
--- a/test/result/badge_coverage.svg
+++ b/test/result/badge_coverage.svg
@@ -1 +1 @@
-
\ No newline at end of file
+
\ No newline at end of file
diff --git a/test/result/coverage.svg b/test/result/coverage.svg
index 0b24b2541..f5b175d89 100644
--- a/test/result/coverage.svg
+++ b/test/result/coverage.svg
@@ -7,7 +7,7 @@
>
-
+
-
+
-
+
-
+
anonymity
-
+
client
@@ -65,12 +65,12 @@
-
+
crypto
@@ -78,12 +78,12 @@
-
+
encoding
@@ -91,12 +91,12 @@
-
+
logger
@@ -104,12 +104,12 @@
-
+
network
@@ -117,12 +117,12 @@
-
+
payload
@@ -130,12 +130,12 @@
-
+
state
@@ -143,12 +143,12 @@
-
+
storage
@@ -156,12 +156,12 @@
-
+
action.go
@@ -169,18 +169,25 @@
-
+
+adapters
+
-
+
anonymity.go
@@ -188,18 +195,18 @@
-
+
-
+
head.go
@@ -207,12 +214,12 @@
-
+
logger/log_builder.go
@@ -220,12 +227,12 @@
-
+
queue
@@ -233,12 +240,12 @@
-
+
settings.go
@@ -246,12 +253,12 @@
-
+
client.go
@@ -259,12 +266,12 @@
-
+
message
@@ -272,12 +279,12 @@
-
+
asymmetric
@@ -285,12 +292,12 @@
-
+
hashing
@@ -298,18 +305,18 @@
-
+
-
+
puzzle/puzzle.go
@@ -317,12 +324,12 @@
-
+
random/random.go
@@ -330,12 +337,12 @@
-
+
symmetric/symmetric.go
@@ -343,12 +350,12 @@
-
+
bytes.go
@@ -356,18 +363,18 @@
-
+
-
+
hex.go
@@ -375,12 +382,12 @@
-
+
serialize_json.go
@@ -388,18 +395,18 @@
-
+
-
+
logger.go
@@ -407,18 +414,18 @@
-
+
-
+
conn
@@ -426,12 +433,12 @@
-
+
connkeeper
@@ -439,18 +446,18 @@
-
+
-
+
message
@@ -458,12 +465,12 @@
-
+
network.go
@@ -471,12 +478,12 @@
-
+
settings.go
@@ -484,12 +491,12 @@
-
+
joiner
@@ -497,12 +504,12 @@
-
+
payload32.go
@@ -510,12 +517,12 @@
-
+
payload64.go
@@ -523,24 +530,24 @@
-
+
-
+
-
+
cache/lru.go
@@ -548,12 +555,12 @@
-
+
database
@@ -561,18 +568,37 @@
-
+
+
+
+
+
+
+
+
+merge.go
+
+
+
+
+
+
-
+
queue.go
@@ -580,18 +606,18 @@
-
+
-
+
message.go
@@ -599,12 +625,12 @@
-
+
dsa.go
@@ -612,12 +638,12 @@
-
+
kem.go
@@ -625,12 +651,12 @@
-
+
key.go
@@ -638,12 +664,12 @@
-
+
map_pubkeys.go
@@ -651,18 +677,18 @@
-
+
-
+
hmac.go
@@ -670,12 +696,12 @@
-
+
conn.go
@@ -683,12 +709,12 @@
-
+
settings.go
@@ -696,12 +722,12 @@
-
+
connkeeper.go
@@ -709,18 +735,18 @@
-
+
-
+
settings.go
@@ -728,18 +754,18 @@
-
+
-
+
message.go
@@ -747,24 +773,24 @@
-
+
-
+
-
+
database.go
From 88c7d2a37397866d4ebf14e0cd4e1d06718c77b1 Mon Sep 17 00:00:00 2001
From: number571
Date: Fri, 6 Dec 2024 20:56:32 +0700
Subject: [PATCH 13/17] update
---
pkg/anonymity/anonymity.go | 9 +-
test/result/badge_codelines.svg | 2 +-
test/result/coverage.svg | 236 ++++++++++++++++----------------
3 files changed, 121 insertions(+), 126 deletions(-)
diff --git a/pkg/anonymity/anonymity.go b/pkg/anonymity/anonymity.go
index c9320c3d5..6ac305285 100644
--- a/pkg/anonymity/anonymity.go
+++ b/pkg/anonymity/anonymity.go
@@ -70,7 +70,7 @@ func (p *sNode) Run(pCtx context.Context) error {
const N = 3
- errs := [N]error{}
+ errs := make([]error, N)
wg := &sync.WaitGroup{}
wg.Add(N)
@@ -93,12 +93,7 @@ func (p *sNode) Run(pCtx context.Context) error {
case <-pCtx.Done():
return pCtx.Err()
default:
- for _, err := range errs {
- if err != nil {
- return err
- }
- }
- panic("closed without errors")
+ return errors.Join(errs...)
}
}
diff --git a/test/result/badge_codelines.svg b/test/result/badge_codelines.svg
index 12f1cc2e5..82acdd075 100644
--- a/test/result/badge_codelines.svg
+++ b/test/result/badge_codelines.svg
@@ -1 +1 @@
-
\ No newline at end of file
+
\ No newline at end of file
diff --git a/test/result/coverage.svg b/test/result/coverage.svg
index f5b175d89..201a8de65 100644
--- a/test/result/coverage.svg
+++ b/test/result/coverage.svg
@@ -20,7 +20,7 @@
-
+
-
+
-
+
-
+
client
@@ -65,12 +65,12 @@
-
+
crypto
@@ -78,12 +78,12 @@
-
+
encoding
@@ -91,12 +91,12 @@
-
+
logger
@@ -104,12 +104,12 @@
-
+
network
@@ -117,12 +117,12 @@
-
+
payload
@@ -130,12 +130,12 @@
-
+
state
@@ -143,12 +143,12 @@
-
+
storage
@@ -156,12 +156,12 @@
-
+
action.go
@@ -169,12 +169,12 @@
-
+
adapters
@@ -182,7 +182,7 @@
-
+
-
+
-
+
head.go
@@ -214,12 +214,12 @@
-
+
logger/log_builder.go
@@ -227,12 +227,12 @@
-
+
queue
@@ -240,12 +240,12 @@
-
+
settings.go
@@ -253,12 +253,12 @@
-
+
client.go
@@ -266,12 +266,12 @@
-
+
message
@@ -279,12 +279,12 @@
-
+
asymmetric
@@ -292,12 +292,12 @@
-
+
hashing
@@ -305,18 +305,18 @@
-
+
-
+
puzzle/puzzle.go
@@ -324,12 +324,12 @@
-
+
random/random.go
@@ -337,12 +337,12 @@
-
+
symmetric/symmetric.go
@@ -350,12 +350,12 @@
-
+
bytes.go
@@ -363,18 +363,18 @@
-
+
-
+
hex.go
@@ -382,12 +382,12 @@
-
+
serialize_json.go
@@ -395,18 +395,18 @@
-
+
-
+
logger.go
@@ -414,18 +414,18 @@
-
+
-
+
conn
@@ -433,12 +433,12 @@
-
+
connkeeper
@@ -446,18 +446,18 @@
-
+
-
+
message
@@ -465,12 +465,12 @@
-
+
network.go
@@ -478,12 +478,12 @@
-
+
settings.go
@@ -491,12 +491,12 @@
-
+
joiner
@@ -504,12 +504,12 @@
-
+
payload32.go
@@ -517,12 +517,12 @@
-
+
payload64.go
@@ -530,24 +530,24 @@
-
+
-
+
-
+
cache/lru.go
@@ -555,12 +555,12 @@
-
+
database
@@ -568,18 +568,18 @@
-
+
-
+
merge.go
@@ -587,18 +587,18 @@
-
+
-
+
queue.go
@@ -606,18 +606,18 @@
-
+
-
+
message.go
@@ -625,12 +625,12 @@
-
+
dsa.go
@@ -638,12 +638,12 @@
-
+
kem.go
@@ -651,12 +651,12 @@
-
+
key.go
@@ -664,12 +664,12 @@
-
+
map_pubkeys.go
@@ -677,18 +677,18 @@
-
+
-
+
hmac.go
@@ -696,12 +696,12 @@
-
+
conn.go
@@ -709,12 +709,12 @@
-
+
settings.go
@@ -722,12 +722,12 @@
-
+
connkeeper.go
@@ -735,18 +735,18 @@
-
+
-
+
settings.go
@@ -754,18 +754,18 @@
-
+
-
+
message.go
@@ -773,24 +773,24 @@
-
+
-
+
-
+
database.go
From 4117521cb48d3a8c6bbd90c6a1e1acab4cc0f9c4 Mon Sep 17 00:00:00 2001
From: number571
Date: Fri, 6 Dec 2024 22:35:21 +0700
Subject: [PATCH 14/17] update
---
pkg/anonymity/adapters/adapters_test.go | 5 +
pkg/anonymity/anonymity_test.go | 106 +++++-----
pkg/anonymity/examples/echo/main.go | 2 +-
pkg/anonymity/examples/ping-pong/main.go | 2 +-
pkg/network/examples/broadcast/main.go | 11 +-
pkg/network/examples/echo/main.go | 12 +-
pkg/network/examples/ping-pong/main.go | 12 +-
pkg/network/network.go | 30 ++-
pkg/network/network_test.go | 42 ++--
pkg/network/types.go | 6 +-
test/result/badge_codelines.svg | 2 +-
test/result/coverage.svg | 236 +++++++++++------------
12 files changed, 230 insertions(+), 236 deletions(-)
create mode 100644 pkg/anonymity/adapters/adapters_test.go
diff --git a/pkg/anonymity/adapters/adapters_test.go b/pkg/anonymity/adapters/adapters_test.go
new file mode 100644
index 000000000..5f36924db
--- /dev/null
+++ b/pkg/anonymity/adapters/adapters_test.go
@@ -0,0 +1,5 @@
+package adapters
+
+import "testing"
+
+func TestTODO(_ *testing.T) {}
diff --git a/pkg/anonymity/anonymity_test.go b/pkg/anonymity/anonymity_test.go
index babb9094e..4041df6c4 100644
--- a/pkg/anonymity/anonymity_test.go
+++ b/pkg/anonymity/anonymity_test.go
@@ -54,8 +54,11 @@ func TestError(t *testing.T) {
func TestNodeSettings(t *testing.T) {
t.Parallel()
- node, networkNode, cancels := testNewNodeWithDB(time.Minute, "", &tsDatabase{})
- defer testFreeNodes([]INode{node}, []network.INode{networkNode}, []context.CancelFunc{cancels}, 9)
+ ctx, cancel := context.WithCancel(context.Background())
+ defer cancel()
+
+ node, _ := testRunNodeWithDB(ctx, time.Minute, "", &tsDatabase{})
+ defer testFreeNodes([]INode{node}, 9)
sett := node.GetSettings()
if sett.GetFetchTimeout() != time.Minute {
@@ -99,19 +102,20 @@ func testSettings(t *testing.T, n int) {
func TestComplexFetchPayload(t *testing.T) {
t.Parallel()
+ ctx, cancel := context.WithCancel(context.Background())
+ defer cancel()
+
addresses := [2]string{testutils.TgAddrs[2], testutils.TgAddrs[3]}
- nodes, networkNodes, cancels := testNewNodes(t, time.Minute, addresses, 0)
+ nodes := testRunNodes(ctx, t, time.Minute, addresses, 0)
if nodes[0] == nil {
t.Error("nodes is null")
return
}
- defer testFreeNodes(nodes[:], networkNodes[:], cancels[:], 0)
+ defer testFreeNodes(nodes[:], 0)
wg := sync.WaitGroup{}
wg.Add(tcIter)
- ctx := context.Background()
-
for i := 0; i < tcIter; i++ {
go func(i int) {
defer wg.Done()
@@ -141,20 +145,21 @@ func TestComplexFetchPayload(t *testing.T) {
func TestF2FWithoutFriends(t *testing.T) {
t.Parallel()
+ ctx, cancel := context.WithCancel(context.Background())
+ defer cancel()
+
// 3 seconds for wait
addresses := [2]string{testutils.TgAddrs[10], testutils.TgAddrs[11]}
- nodes, networkNodes, cancels := testNewNodes(t, 3*time.Second, addresses, 1)
+ nodes := testRunNodes(ctx, t, 3*time.Second, addresses, 1)
if nodes[0] == nil {
t.Error("nodes is null")
return
}
- defer testFreeNodes(nodes[:], networkNodes[:], cancels[:], 1)
+ defer testFreeNodes(nodes[:], 1)
nodes[0].GetMapPubKeys().DelPubKey(nodes[1].GetQBProcessor().GetClient().GetPrivKey().GetPubKey())
nodes[1].GetMapPubKeys().DelPubKey(nodes[0].GetQBProcessor().GetClient().GetPrivKey().GetPubKey())
- ctx := context.Background()
-
// nodes[1] -> nodes[0] -> nodes[2]
_, err := nodes[0].FetchPayload(
ctx,
@@ -171,13 +176,16 @@ func TestF2FWithoutFriends(t *testing.T) {
func TestFetchPayload(t *testing.T) {
t.Parallel()
+ ctx, cancel := context.WithCancel(context.Background())
+ defer cancel()
+
addresses := [2]string{testutils.TgAddrs[12], testutils.TgAddrs[13]}
- nodes, networkNodes, cancels := testNewNodes(t, time.Minute, addresses, 4)
+ nodes := testRunNodes(ctx, t, time.Minute, addresses, 4)
if nodes[0] == nil {
t.Error("nodes is null")
return
}
- defer testFreeNodes(nodes[:], networkNodes[:], cancels[:], 4)
+ defer testFreeNodes(nodes[:], 4)
nodes[1].HandleFunc(
tcHead,
@@ -187,7 +195,6 @@ func TestFetchPayload(t *testing.T) {
)
largeBodySize := nodes[0].GetQBProcessor().GetClient().GetPayloadLimit() - encoding.CSizeUint64 + 1
- ctx := context.Background()
_, err := nodes[0].FetchPayload(
ctx,
nodes[1].GetQBProcessor().GetClient().GetPrivKey().GetPubKey(),
@@ -217,13 +224,16 @@ func TestFetchPayload(t *testing.T) {
func TestBroadcastPayload(t *testing.T) {
t.Parallel()
+ ctx, cancel := context.WithCancel(context.Background())
+ defer cancel()
+
addresses := [2]string{testutils.TgAddrs[14], testutils.TgAddrs[15]}
- nodes, networkNodes, cancels := testNewNodes(t, time.Minute, addresses, 3)
+ nodes := testRunNodes(ctx, t, time.Minute, addresses, 3)
if nodes[0] == nil {
t.Error("nodes is null")
return
}
- defer testFreeNodes(nodes[:], networkNodes[:], cancels[:], 3)
+ defer testFreeNodes(nodes[:], 3)
chResult := make(chan string)
nodes[1].HandleFunc(
@@ -272,13 +282,16 @@ func TestBroadcastPayload(t *testing.T) {
func TestEnqueuePayload(t *testing.T) {
t.Parallel()
+ ctx, cancel := context.WithCancel(context.Background())
+ defer cancel()
+
addresses := [2]string{testutils.TgAddrs[16], testutils.TgAddrs[17]}
- nodes, networkNodes, cancels := testNewNodes(t, time.Minute, addresses, 8)
+ nodes := testRunNodes(ctx, t, time.Minute, addresses, 8)
if nodes[0] == nil {
t.Error("nodes is null")
return
}
- defer testFreeNodes(nodes[:], networkNodes[:], cancels[:], 8)
+ defer testFreeNodes(nodes[:], 8)
node := nodes[0].(*sNode)
pubKey := nodes[1].GetQBProcessor().GetClient().GetPrivKey().GetPubKey()
@@ -318,8 +331,11 @@ func TestEnqueuePayload(t *testing.T) {
func TestHandleWrapper(t *testing.T) {
t.Parallel()
- _node, networkNode, cancel := testNewNode(time.Minute, "", 7, 0)
- defer testFreeNodes([]INode{_node}, []network.INode{networkNode}, []context.CancelFunc{cancel}, 7)
+ ctx, cancel := context.WithCancel(context.Background())
+ defer cancel()
+
+ _node, _ := testRunNode(ctx, time.Minute, "", 7, 0)
+ defer testFreeNodes([]INode{_node}, 7)
node := _node.(*sNode)
handler := node.messageHandler
@@ -329,7 +345,6 @@ func TestHandleWrapper(t *testing.T) {
pubKey := privKey.GetPubKey()
node.GetMapPubKeys().SetPubKey(privKey.GetPubKey())
- ctx := context.Background()
sett := net_message.NewConstructSettings(&net_message.SConstructSettings{
FSettings: net_message.NewSettings(&net_message.SSettings{}),
})
@@ -447,8 +462,11 @@ func TestHandleWrapper(t *testing.T) {
func TestStoreHashWithBroadcastMessage(t *testing.T) {
t.Parallel()
- _node, networkNode, cancel := testNewNode(time.Minute, "", 6, 0)
- defer testFreeNodes([]INode{_node}, []network.INode{networkNode}, []context.CancelFunc{cancel}, 6)
+ ctx, cancel := context.WithCancel(context.Background())
+ defer cancel()
+
+ _node, _ := testRunNode(ctx, time.Minute, "", 6, 0)
+ defer testFreeNodes([]INode{_node}, 6)
node := _node.(*sNode)
client := node.fQBProcessor.GetClient()
@@ -472,7 +490,6 @@ func TestStoreHashWithBroadcastMessage(t *testing.T) {
netMsg := node.testNewNetworkMessage(sett, msg)
logBuilder := anon_logger.NewLogBuilder("_")
- ctx := context.Background()
if ok, err := node.storeHashWithProduce(ctx, logBuilder, netMsg); !ok || err != nil {
t.Error(err)
return
@@ -492,12 +509,12 @@ func TestStoreHashWithBroadcastMessage(t *testing.T) {
func TestRecvSendMessage(t *testing.T) {
t.Parallel()
- _node, networkNode, cancel := testNewNode(time.Minute, "", 5, 0)
- defer testFreeNodes([]INode{_node}, []network.INode{networkNode}, []context.CancelFunc{cancel}, 5)
-
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
+ _node, _ := testRunNode(ctx, time.Minute, "", 5, 0)
+ defer testFreeNodes([]INode{_node}, 5)
+
node := _node.(*sNode)
if _, err := node.recvResponse(ctx, "not_exist"); err == nil {
t.Error("success got action by undefined key")
@@ -564,17 +581,16 @@ func TestRecvSendMessage(t *testing.T) {
// nodes[2], nodes[3], nodes[4] = routes
// nodes[2], nodes[4] are have open ports
// Scheme: (nodes[0]) -> nodes[2] -> nodes[3] -> nodes[4] -> (nodes[1])
-func testNewNodes(t *testing.T, timeWait time.Duration, addresses [2]string, typeDB int) ([5]INode, [5]network.INode, [5]context.CancelFunc) {
+func testRunNodes(ctx context.Context, t *testing.T, timeWait time.Duration, addresses [2]string, typeDB int) [5]INode {
nodes := [5]INode{}
networkNodes := [5]network.INode{}
- cancels := [5]context.CancelFunc{}
addrs := [5]string{"", "", addresses[0], "", addresses[1]}
for i := 0; i < 5; i++ {
- nodes[i], networkNodes[i], cancels[i] = testNewNode(timeWait, addrs[i], typeDB, i)
+ nodes[i], networkNodes[i] = testRunNode(ctx, timeWait, addrs[i], typeDB, i)
if nodes[i] == nil {
t.Errorf("node (%d) is not running %d", i, typeDB)
- return [5]INode{}, [5]network.INode{}, [5]context.CancelFunc{}
+ return [5]INode{}
}
}
@@ -594,14 +610,13 @@ func testNewNodes(t *testing.T, timeWait time.Duration, addresses [2]string, typ
)
}
- ctx := context.Background()
go func() {
- if err := networkNodes[2].Listen(ctx); err != nil && !errors.Is(err, net.ErrClosed) {
+ if err := networkNodes[2].Run(ctx); err != nil && !errors.Is(err, net.ErrClosed) {
t.Error(err)
}
}()
go func() {
- if err := networkNodes[4].Listen(ctx); err != nil && !errors.Is(err, net.ErrClosed) {
+ if err := networkNodes[4].Run(ctx); err != nil && !errors.Is(err, net.ErrClosed) {
t.Error(err)
}
}()
@@ -613,24 +628,24 @@ func testNewNodes(t *testing.T, timeWait time.Duration, addresses [2]string, typ
})
if err1 != nil {
t.Error(err1)
- return [5]INode{}, [5]network.INode{}, [5]context.CancelFunc{}
+ return [5]INode{}
}
err2 := testutils.TryN(50, 10*time.Millisecond, func() error {
return networkNodes[1].AddConnection(ctx, addresses[1])
})
if err2 != nil {
t.Error(err2)
- return [5]INode{}, [5]network.INode{}, [5]context.CancelFunc{}
+ return [5]INode{}
}
// routes to routes (nodes[3] -> nodes[2], nodes[3] -> nodes[4])
if err := networkNodes[3].AddConnection(ctx, addresses[0]); err != nil {
t.Error(err)
- return [5]INode{}, [5]network.INode{}, [5]context.CancelFunc{}
+ return [5]INode{}
}
if err := networkNodes[3].AddConnection(ctx, addresses[1]); err != nil {
t.Error(err)
- return [5]INode{}, [5]network.INode{}, [5]context.CancelFunc{}
+ return [5]INode{}
}
go func() {
@@ -640,7 +655,7 @@ func testNewNodes(t *testing.T, timeWait time.Duration, addresses [2]string, typ
}
}()
- return nodes, networkNodes, cancels
+ return nodes
}
/*
@@ -662,7 +677,7 @@ func (p *stLogging) HasErro() bool {
}
*/
-func testNewNodeWithDB(timeWait time.Duration, addr string, db database.IKVDatabase) (INode, network.INode, context.CancelFunc) {
+func testRunNodeWithDB(ctx context.Context, timeWait time.Duration, addr string, db database.IKVDatabase) (INode, network.INode) {
msgChan := make(chan net_message.IMessage)
parallel := uint64(1)
networkMask := uint32(1)
@@ -733,24 +748,21 @@ func testNewNodeWithDB(timeWait time.Duration, addr string, db database.IKVDatab
),
),
)
- ctx, cancel := context.WithCancel(context.Background())
go func() { _ = node.Run(ctx) }()
- return node, networkNode, cancel
+ return node, networkNode
}
-func testNewNode(timeWait time.Duration, addr string, typeDB, numDB int) (INode, network.INode, context.CancelFunc) {
+func testRunNode(ctx context.Context, timeWait time.Duration, addr string, typeDB, numDB int) (INode, network.INode) {
db, err := database.NewKVDatabase(fmt.Sprintf(tcPathDBTemplate, typeDB, numDB))
if err != nil {
panic(err)
}
- return testNewNodeWithDB(timeWait, addr, db)
+ return testRunNodeWithDB(ctx, timeWait, addr, db)
}
-func testFreeNodes(nodes []INode, networks []network.INode, cancels []context.CancelFunc, typeDB int) {
- for i, node := range nodes {
+func testFreeNodes(nodes []INode, typeDB int) {
+ for _, node := range nodes {
node.GetKVDatabase().Close()
- networks[i].Close()
- cancels[i]()
}
testDeleteDB(typeDB)
}
diff --git a/pkg/anonymity/examples/echo/main.go b/pkg/anonymity/examples/echo/main.go
index 212c57c94..3e67e7f4e 100644
--- a/pkg/anonymity/examples/echo/main.go
+++ b/pkg/anonymity/examples/echo/main.go
@@ -51,7 +51,7 @@ func runServiceNode() anonymity.INode {
)
go func() { _ = node.Run(ctx) }()
- go func() { _ = network.Listen(ctx) }()
+ go func() { _ = network.Run(ctx) }()
time.Sleep(time.Second) // wait listener
return node
diff --git a/pkg/anonymity/examples/ping-pong/main.go b/pkg/anonymity/examples/ping-pong/main.go
index b0a5a89c8..e942da268 100644
--- a/pkg/anonymity/examples/ping-pong/main.go
+++ b/pkg/anonymity/examples/ping-pong/main.go
@@ -69,7 +69,7 @@ func runServiceNode() anonymity.INode {
node.HandleFunc(nodeRouter, handler)
go func() { _ = node.Run(ctx) }()
- go func() { _ = network.Listen(ctx) }()
+ go func() { _ = network.Run(ctx) }()
time.Sleep(time.Second) // wait listener
return node
diff --git a/pkg/network/examples/broadcast/main.go b/pkg/network/examples/broadcast/main.go
index 9923cd65b..645cb6fd8 100644
--- a/pkg/network/examples/broadcast/main.go
+++ b/pkg/network/examples/broadcast/main.go
@@ -28,8 +28,11 @@ var handler = func(serviceName string) network.IHandlerF {
}
func main() {
+ ctx, cancel := context.WithCancel(context.Background())
+ defer cancel()
+
var (
- _ = runServiceNode("node1")
+ _ = runServiceNode(ctx, "node1")
_ = runClientNode("node2")
_ = runClientNode("node3")
node4 = runClientNode("node4")
@@ -59,11 +62,9 @@ func runClientNode(id string) network.INode {
return node
}
-func runServiceNode(id string) network.INode {
- ctx := context.Background()
+func runServiceNode(ctx context.Context, id string) network.INode {
node := newNode(serviceAddress).HandleFunc(serviceHeader, handler(id))
-
- go func() { _ = node.Listen(ctx) }()
+ go func() { _ = node.Run(ctx) }()
time.Sleep(time.Second) // wait listener
return node
diff --git a/pkg/network/examples/echo/main.go b/pkg/network/examples/echo/main.go
index 8114cfc1b..60b86a02b 100644
--- a/pkg/network/examples/echo/main.go
+++ b/pkg/network/examples/echo/main.go
@@ -33,9 +33,11 @@ var handler = func(ctx context.Context, node network.INode, c conn.IConn, msg me
}
func main() {
+ ctx, cancel := context.WithCancel(context.Background())
+ defer cancel()
+
var (
- _ = runServiceNode()
- ctx = context.Background()
+ _ = runServiceNode(ctx)
conn, _ = conn.Connect(ctx, connSettings(), serviceAddress)
)
@@ -56,11 +58,9 @@ func main() {
fmt.Println(string(recvMsg.GetPayload().GetBody()))
}
-func runServiceNode() network.INode {
- ctx := context.Background()
+func runServiceNode(ctx context.Context) network.INode {
node := newNode(serviceAddress).HandleFunc(serviceHeader, handler)
-
- go func() { _ = node.Listen(ctx) }()
+ go func() { _ = node.Run(ctx) }()
time.Sleep(time.Second) // wait listener
return node
diff --git a/pkg/network/examples/ping-pong/main.go b/pkg/network/examples/ping-pong/main.go
index b59140672..9a65c4ce9 100644
--- a/pkg/network/examples/ping-pong/main.go
+++ b/pkg/network/examples/ping-pong/main.go
@@ -47,12 +47,14 @@ var handler = func(id string) network.IHandlerF {
}
func main() {
+ ctx, cancel := context.WithCancel(context.Background())
+ defer cancel()
+
var (
- _ = runServiceNode("node1")
+ _ = runServiceNode(ctx, "node1")
node1 = runClientNode("node2")
)
- ctx := context.Background()
msg := message.NewMessage(
message.NewConstructSettings(&message.SConstructSettings{
FSettings: node1.GetSettings().GetConnSettings().GetMessageSettings(),
@@ -72,11 +74,9 @@ func runClientNode(id string) network.INode {
return node
}
-func runServiceNode(id string) network.INode {
- ctx := context.Background()
+func runServiceNode(ctx context.Context, id string) network.INode {
node := newNode(serviceAddress).HandleFunc(serviceHeader, handler(id))
-
- go func() { _ = node.Listen(ctx) }()
+ go func() { _ = node.Run(ctx) }()
time.Sleep(time.Second) // wait listener
return node
diff --git a/pkg/network/network.go b/pkg/network/network.go
index ab7dee7c3..9006e7575 100644
--- a/pkg/network/network.go
+++ b/pkg/network/network.go
@@ -108,13 +108,23 @@ func (p *sNode) BroadcastMessage(pCtx context.Context, pMsg net_message.IMessage
// Opens a tcp connection to receive data from outside.
// Checks the number of valid connections.
// Redirects connections to the handle router.
-func (p *sNode) Listen(pCtx context.Context) error {
+func (p *sNode) Run(pCtx context.Context) error {
+ if p.fSettings.GetAddress() == "" {
+ <-pCtx.Done()
+ return pCtx.Err()
+ }
+
listener, err := net.Listen("tcp", p.fSettings.GetAddress())
if err != nil {
return errors.Join(ErrCreateListener, err)
}
defer listener.Close()
+ go func() {
+ <-pCtx.Done()
+ listener.Close()
+ }()
+
p.setListener(listener)
for {
select {
@@ -140,24 +150,6 @@ func (p *sNode) Listen(pCtx context.Context) error {
}
}
-// Closes the listener and all connections.
-func (p *sNode) Close() error {
- p.fMutex.Lock()
- defer p.fMutex.Unlock()
-
- listErr := make([]error, 0, len(p.fConnections)+1)
- if p.fListener != nil {
- listErr = append(listErr, p.fListener.Close())
- }
-
- for id, conn := range p.fConnections {
- delete(p.fConnections, id)
- listErr = append(listErr, conn.Close())
- }
-
- return errors.Join(listErr...)
-}
-
// Saves the function to the map by key for subsequent redirection.
func (p *sNode) HandleFunc(pHead uint32, pHandle IHandlerF) INode {
p.fMutex.Lock()
diff --git a/pkg/network/network_test.go b/pkg/network/network_test.go
index c2bda8597..6b2cb9e4a 100644
--- a/pkg/network/network_test.go
+++ b/pkg/network/network_test.go
@@ -105,12 +105,14 @@ func testSettings(t *testing.T, n int) {
func TestBroadcast(t *testing.T) {
t.Parallel()
- nodes, mapp, err := testNodes()
+ ctx, cancel := context.WithCancel(context.Background())
+ defer cancel()
+
+ nodes, mapp, err := testNodes(ctx)
if err != nil {
t.Error(err)
return
}
- defer testFreeNodes(nodes[:])
// four receivers, sender not receive his messages
tcMutex := sync.Mutex{}
@@ -150,7 +152,6 @@ func TestBroadcast(t *testing.T) {
}
// nodes[0] -> nodes[1:]
- ctx := context.Background()
for i := 0; i < tcIter; i++ {
go func(i int) {
pld := payload.NewPayload32(
@@ -205,28 +206,27 @@ func TestNodeConnection(t *testing.T) {
node2 = newTestNode(testutils.TgAddrs[4], 1)
node3 = newTestNode(testutils.TgAddrs[5], 16)
)
- defer testFreeNodes([]INode{node1, node2, node3})
- ctx := context.Background()
+ ctx, cancel := context.WithCancel(context.Background())
+ defer cancel()
+
go func() {
- if err := node2.Listen(ctx); err != nil && !errors.Is(err, net.ErrClosed) {
+ if err := node2.Run(ctx); err != nil && !errors.Is(err, net.ErrClosed) {
t.Error(err)
return
}
}()
- defer node2.Close()
go func() {
- if err := node3.Listen(ctx); err != nil && !errors.Is(err, net.ErrClosed) {
+ if err := node3.Run(ctx); err != nil && !errors.Is(err, net.ErrClosed) {
t.Error(err)
return
}
}()
- defer node3.Close()
time.Sleep(200 * time.Millisecond)
go func() {
- if err := node2.Listen(ctx); err == nil {
+ if err := node2.Run(ctx); err == nil {
t.Error("success second run node")
return
}
@@ -278,18 +278,12 @@ func TestNodeConnection(t *testing.T) {
t.Error(err2)
return
}
-
- if err := node2.Close(); err != nil {
- t.Error(err)
- return
- }
}
func TestHandleMessage(t *testing.T) {
t.Parallel()
node := newTestNode("", 16).(*sNode)
- defer testFreeNodes([]INode{node})
ctx := context.Background()
sett := message.NewConstructSettings(&message.SConstructSettings{
@@ -340,7 +334,7 @@ func TestContextCancel(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
- go func() { _ = node1.Listen(ctx) }()
+ go func() { _ = node1.Run(ctx) }()
err1 := testutils.TryN(50, 10*time.Millisecond, func() error {
return node2.AddConnection(ctx, testutils.TgAddrs[6])
@@ -371,7 +365,7 @@ func TestContextCancel(t *testing.T) {
cancel()
}
-func testNodes() ([5]INode, map[INode]map[string]bool, error) {
+func testNodes(ctx context.Context) ([5]INode, map[INode]map[string]bool, error) {
nodes := [5]INode{}
addrs := [5]string{"", "", testutils.TgAddrs[0], "", testutils.TgAddrs[1]}
@@ -379,10 +373,8 @@ func testNodes() ([5]INode, map[INode]map[string]bool, error) {
nodes[i] = newTestNode(addrs[i], 16)
}
- ctx := context.Background()
-
- go func() { _ = nodes[2].Listen(ctx) }()
- go func() { _ = nodes[4].Listen(ctx) }()
+ go func() { _ = nodes[2].Run(ctx) }()
+ go func() { _ = nodes[4].Run(ctx) }()
err1 := testutils.TryN(50, 10*time.Millisecond, func() error {
return nodes[0].AddConnection(ctx, testutils.TgAddrs[0])
@@ -437,9 +429,3 @@ func newTestNode(pAddr string, pMaxConns uint64) INode {
cache.NewLRUCache(1024),
)
}
-
-func testFreeNodes(nodes []INode) {
- for _, node := range nodes {
- node.Close()
- }
-}
diff --git a/pkg/network/types.go b/pkg/network/types.go
index 576648a6f..06dbf87ad 100644
--- a/pkg/network/types.go
+++ b/pkg/network/types.go
@@ -2,11 +2,11 @@ package network
import (
"context"
- "io"
"time"
"github.com/number571/go-peer/pkg/network/conn"
"github.com/number571/go-peer/pkg/storage/cache"
+ "github.com/number571/go-peer/pkg/types"
net_message "github.com/number571/go-peer/pkg/network/message"
)
@@ -19,9 +19,7 @@ type IHandlerF func(
) error
type INode interface {
- io.Closer
-
- Listen(context.Context) error
+ types.IRunner
HandleFunc(uint32, IHandlerF) INode
GetSettings() ISettings
diff --git a/test/result/badge_codelines.svg b/test/result/badge_codelines.svg
index 82acdd075..ef78a20ef 100644
--- a/test/result/badge_codelines.svg
+++ b/test/result/badge_codelines.svg
@@ -1 +1 @@
-
\ No newline at end of file
+
\ No newline at end of file
diff --git a/test/result/coverage.svg b/test/result/coverage.svg
index 201a8de65..4e23008e8 100644
--- a/test/result/coverage.svg
+++ b/test/result/coverage.svg
@@ -7,7 +7,7 @@
>
-
+
-
+
-
+
-
+
-
+
client
@@ -65,12 +65,12 @@
-
+
crypto
@@ -78,12 +78,12 @@
-
+
encoding
@@ -91,12 +91,12 @@
-
+
logger
@@ -104,12 +104,12 @@
-
+
network
@@ -117,12 +117,12 @@
-
+
payload
@@ -130,12 +130,12 @@
-
+
state
@@ -143,12 +143,12 @@
-
+
storage
@@ -156,12 +156,12 @@
-
+
action.go
@@ -169,12 +169,12 @@
-
+
adapters
@@ -182,7 +182,7 @@
-
+
-
+
-
+
head.go
@@ -214,12 +214,12 @@
-
+
logger/log_builder.go
@@ -227,7 +227,7 @@
-
+
-
+
settings.go
@@ -253,12 +253,12 @@
-
+
client.go
@@ -266,12 +266,12 @@
-
+
message
@@ -279,12 +279,12 @@
-
+
asymmetric
@@ -292,12 +292,12 @@
-
+
hashing
@@ -305,18 +305,18 @@
-
+
-
+
puzzle/puzzle.go
@@ -324,12 +324,12 @@
-
+
random/random.go
@@ -337,12 +337,12 @@
-
+
symmetric/symmetric.go
@@ -350,12 +350,12 @@
-
+
bytes.go
@@ -363,18 +363,18 @@
-
+
-
+
hex.go
@@ -382,12 +382,12 @@
-
+
serialize_json.go
@@ -395,18 +395,18 @@
-
+
-
+
logger.go
@@ -414,18 +414,18 @@
-
+
-
+
conn
@@ -433,12 +433,12 @@
-
+
connkeeper
@@ -446,18 +446,18 @@
-
+
-
+
message
@@ -465,25 +465,25 @@
-
+
network.go
-
+
settings.go
@@ -491,12 +491,12 @@
-
+
joiner
@@ -504,12 +504,12 @@
-
+
payload32.go
@@ -517,12 +517,12 @@
-
+
payload64.go
@@ -530,24 +530,24 @@
-
+
-
+
-
+
cache/lru.go
@@ -555,12 +555,12 @@
-
+
database
@@ -568,18 +568,18 @@
-
+
-
+
merge.go
@@ -587,13 +587,13 @@
-
+
-
+
-
+
-
+
message.go
@@ -625,12 +625,12 @@
-
+
dsa.go
@@ -638,12 +638,12 @@
-
+
kem.go
@@ -651,12 +651,12 @@
-
+
key.go
@@ -664,12 +664,12 @@
-
+
map_pubkeys.go
@@ -677,18 +677,18 @@
-
+
-
+
hmac.go
@@ -696,12 +696,12 @@
-
+
conn.go
@@ -709,12 +709,12 @@
-
+
settings.go
@@ -722,12 +722,12 @@
-