Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
number571 committed Oct 3, 2024
1 parent 574a791 commit 53a284c
Show file tree
Hide file tree
Showing 32 changed files with 12,479 additions and 13,029 deletions.
17,813 changes: 8,724 additions & 9,089 deletions cmd/hidden_lake/_test/result/coverage.out

Large diffs are not rendered by default.

625 changes: 312 additions & 313 deletions cmd/hidden_lake/_test/result/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,14 @@ func TestHandleTransferAPI(t *testing.T) {
}),
})

cancel1, hltClient1, err := testCreateHLT(netMsgSettings, tcNameHLT1, tgProducer)
cancel1, hltClient1, err := testCreateHLT(netMsgSettings.GetSettings(), tcNameHLT1, tgProducer)
if err != nil {
t.Error(err)
return
}
defer cancel1()

cancel2, hltClient2, err := testCreateHLT(netMsgSettings, tcNameHLT2, tgConsumer)
cancel2, hltClient2, err := testCreateHLT(netMsgSettings.GetSettings(), tcNameHLT2, tgConsumer)
if err != nil {
t.Error(err)
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func testAllRun(addr string) (*http.Server, context.CancelFunc, database.IDataba
hlt_client.NewRequester(
"http://"+addr,
&http.Client{Timeout: time.Minute},
testNetworkMessageSettings(),
testNetworkMessageSettings().GetSettings(),
),
)

Expand Down Expand Up @@ -170,7 +170,7 @@ func testNewNetworkNode(addr string) network.INode {
FReadTimeout: time.Minute,
FWriteTimeout: time.Minute,
FConnSettings: conn.NewSettings(&conn.SSettings{
FMessageSettings: testNetworkMessageSettings(),
FMessageSettings: testNetworkMessageSettings().GetSettings(),
FLimitMessageSizeBytes: testutils.TCMessageSize,
FWaitReadTimeout: time.Hour,
FDialTimeout: time.Minute,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ func TestErrorsAPI(t *testing.T) {
hlt_client.NewRequester(
"http://"+testutils.TcUnknownHost,
&http.Client{Timeout: time.Second},
testNetworkMessageSettings(),
testNetworkMessageSettings().GetSettings(),
),
)

pld := payload.NewPayload32(testutils.TcHead, []byte(testutils.TcBody))
sett := message.NewConstructSettings(&message.SConstructSettings{
FSettings: testNetworkMessageSettings(),
FSettings: testNetworkMessageSettings().GetSettings(),
})
if err := client.PutMessage(context.Background(), message.NewMessage(sett, pld)); err == nil {
t.Error("success put message with unknown host")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func HandleMessageAPI(pCtx context.Context, pCfg config.IConfig, pDatabase datab
}

netMsg, err := net_message.LoadMessage(
pNode.GetSettings().GetConnSettings(),
pNode.GetSettings().GetConnSettings().GetMessageSettings(),
string(msgStringAsBytes),
)
if err != nil {
Expand Down
5 changes: 0 additions & 5 deletions cmd/hidden_lake/service/internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ var (
)

type SConfigSettings struct {
fMutex sync.RWMutex

FMessageSizeBytes uint64 `json:"message_size_bytes" yaml:"message_size_bytes"`
FKeySizeBits uint64 `json:"key_size_bits" yaml:"key_size_bits"`
FFetchTimeoutMS uint64 `json:"fetch_timeout_ms" yaml:"fetch_timeout_ms"`
Expand Down Expand Up @@ -122,9 +120,6 @@ func (p *SConfigSettings) GetRandMessageSizeBytes() uint64 {
}

func (p *SConfigSettings) GetNetworkKey() string {
p.fMutex.RLock()
defer p.fMutex.RUnlock()

return p.FNetworkKey
}

Expand Down
23 changes: 0 additions & 23 deletions cmd/hidden_lake/service/internal/config/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,6 @@ func newEditor(pCfg IConfig) IEditor {
}
}

func (p *sEditor) UpdateNetworkKey(pNetworkKey string) error {
p.fMutex.Lock()
defer p.fMutex.Unlock()

filepath := p.fConfig.fFilepath
icfg, err := LoadConfig(filepath)
if err != nil {
return utils.MergeErrors(ErrLoadConfig, err)
}

cfg := icfg.(*SConfig)
cfg.FSettings.FNetworkKey = pNetworkKey
if err := os.WriteFile(filepath, encoding.SerializeYAML(cfg), 0o600); err != nil {
return utils.MergeErrors(ErrWriteConfig, err)
}

p.fConfig.FSettings.fMutex.Lock()
defer p.fConfig.FSettings.fMutex.Unlock()

p.fConfig.FSettings.FNetworkKey = cfg.FSettings.FNetworkKey
return nil
}

func (p *sEditor) UpdateConnections(pConns []string) error {
p.fMutex.Lock()
defer p.fMutex.Unlock()
Expand Down
24 changes: 0 additions & 24 deletions cmd/hidden_lake/service/internal/config/editor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ import (
testutils "github.com/number571/go-peer/test/utils"
)

const (
tcNewNetworkKey = "abc_network_key"
)

var (
tgNewConnections = []string{"a", "b", "c", "b"}
tgNewFriends = map[string]asymmetric.IPubKey{
Expand Down Expand Up @@ -92,24 +88,9 @@ func TestEditor(t *testing.T) {
config := wrapper.GetConfig()
editor := wrapper.GetEditor()

beforeNetworkKey := config.GetSettings().GetNetworkKey()
beforeConnections := config.GetConnections()
beforeFriends := config.GetFriends()

if err := editor.UpdateNetworkKey(tcNewNetworkKey); err != nil {
t.Error(err)
return
}
afterNetworkKey := config.GetSettings().GetNetworkKey()
if beforeNetworkKey == afterNetworkKey {
t.Error("beforeNetworkKey == afterNetworkKey")
return
}
if afterNetworkKey != tcNewNetworkKey {
t.Error("afterNetworkKey != tcNewNetworkKey")
return
}

if err := editor.UpdateConnections(tgNewConnections); err != nil {
t.Error(err)
return
Expand Down Expand Up @@ -185,11 +166,6 @@ func TestIncorrectFilepathEditor(t *testing.T) {

config.fFilepath = random.NewCSPRNG().GetString(32)

if err := editor.UpdateNetworkKey(tcNewNetworkKey); err == nil {
t.Error("success update network key with incorrect filepath")
return
}

if err := editor.UpdateConnections(tgNewConnections); err == nil {
t.Error("success update connections with incorrect filepath")
return
Expand Down
1 change: 0 additions & 1 deletion cmd/hidden_lake/service/internal/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ type IWrapper interface {
}

type IEditor interface {
UpdateNetworkKey(string) error
UpdateConnections([]string) error
UpdateFriends(map[string]asymmetric.IPubKey) error
}
Expand Down
2 changes: 1 addition & 1 deletion examples/traffic_actions/keeper/client_hlt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func main() {
hlt_client.NewRequester(
"http://"+addr,
&http.Client{Timeout: time.Minute},
netSett,
netSett.GetSettings(),
),
)

Expand Down
2 changes: 1 addition & 1 deletion examples/traffic_actions/keeper/prod_test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func main() {
hlt_client.NewRequester(
"http://"+addrHLT[0],
&http.Client{Timeout: 5 * time.Second},
netSett,
netSett.GetSettings(),
),
)

Expand Down
4 changes: 2 additions & 2 deletions examples/traffic_actions/loader/client_hll/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func main() {

time.Sleep(time.Second)

if err := checkMessages(ctx, netMsgSettings, msgSettings); err != nil {
if err := checkMessages(ctx, netMsgSettings.GetSettings(), msgSettings); err != nil {
panic(err)
}

Expand All @@ -93,7 +93,7 @@ func pushMessages(ctx context.Context, netMsgSettings net_message.IConstructSett
hlt_client.NewRequester(
"http://"+hltHost1,
&http.Client{Timeout: time.Minute / 2},
netMsgSettings,
netMsgSettings.GetSettings(),
),
)

Expand Down
4 changes: 2 additions & 2 deletions pkg/network/anonymity/queue/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func (p *sQBProblemProcessor) fillMainPool(pCtx context.Context, pMsg []byte) er
chNetMsg := make(chan net_message.IMessage)
go func() {
chNetMsg <- net_message.NewMessage(
p.fSettings,
p.fSettings.GetMessageConstructSettings(),
payload.NewPayload32(p.fSettings.GetNetworkMask(), pMsg),
)
}()
Expand Down Expand Up @@ -219,7 +219,7 @@ func (p *sQBProblemProcessor) fillRandPool(pCtx context.Context) error {
chNetMsg := make(chan net_message.IMessage)
go func() {
chNetMsg <- net_message.NewMessage(
p.fSettings,
p.fSettings.GetMessageConstructSettings(),
payload.NewPayload32(p.fSettings.GetNetworkMask(), msg),
)
}()
Expand Down
8 changes: 2 additions & 6 deletions pkg/network/anonymity/queue/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ func (p *sSettings) mustNotNull() ISettings {
return p
}

func (p *sSettings) GetWorkSizeBits() uint64 {
return p.FMessageConstructSettings.GetWorkSizeBits()
func (p *sSettings) GetMessageConstructSettings() net_message.IConstructSettings {
return p.FMessageConstructSettings
}

func (p *sSettings) GetParallel() uint64 {
Expand All @@ -65,10 +65,6 @@ func (p *sSettings) GetRandMessageSizeBytes() uint64 {
return p.FMessageConstructSettings.GetRandMessageSizeBytes()
}

func (p *sSettings) GetNetworkKey() string {
return p.FMessageConstructSettings.GetNetworkKey()
}

func (p *sSettings) GetNetworkMask() uint32 {
return p.FNetworkMask
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/network/anonymity/queue/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type IQBProblemProcessor interface {
}

type ISettings interface {
net_message.IConstructSettings
GetMessageConstructSettings() net_message.IConstructSettings
GetNetworkMask() uint32
GetMainPoolCapacity() uint64
GetRandPoolCapacity() uint64
Expand Down
2 changes: 1 addition & 1 deletion pkg/network/conn/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (p *sConn) ReadMessage(pCtx context.Context, pChRead chan<- struct{}) (net_
}

// try unpack message from bytes
msg, err := net_message.LoadMessage(p.fSettings, dataBytes)
msg, err := net_message.LoadMessage(p.fSettings.GetMessageSettings(), dataBytes)
if err != nil {
return nil, utils.MergeErrors(ErrInvalidMessageBytes, err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/network/conn/conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func TestClosedConn(t *testing.T) {
}

sett := message.NewConstructSettings(&message.SConstructSettings{
FSettings: conn.GetSettings(),
FSettings: conn.GetSettings().GetMessageSettings(),
})

pld := payload.NewPayload32(1, []byte("aaa"))
Expand Down Expand Up @@ -437,7 +437,7 @@ func testConn(t *testing.T, pAddr, pNetworkKey string) {
}

msgSett := message.NewConstructSettings(&message.SConstructSettings{
FSettings: conn.GetSettings(),
FSettings: conn.GetSettings().GetMessageSettings(),
})

pld := payload.NewPayload32(tcHead, []byte(tcBody))
Expand Down
8 changes: 2 additions & 6 deletions pkg/network/conn/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,8 @@ func (p *sSettings) mustNotNull() ISettings {
return p
}

func (p *sSettings) GetNetworkKey() string {
return p.FMessageSettings.GetNetworkKey()
}

func (p *sSettings) GetWorkSizeBits() uint64 {
return p.FMessageSettings.GetWorkSizeBits()
func (p *sSettings) GetMessageSettings() net_message.ISettings {
return p.FMessageSettings
}

func (p *sSettings) GetLimitMessageSizeBytes() uint64 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/network/conn/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type IConn interface {
}

type ISettings interface {
net_message.ISettings
GetMessageSettings() net_message.ISettings
GetLimitMessageSizeBytes() uint64
GetDialTimeout() time.Duration
GetReadTimeout() time.Duration
Expand Down
2 changes: 1 addition & 1 deletion pkg/network/examples/broadcast/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func main() {
context.Background(),
message.NewMessage(
message.NewConstructSettings(&message.SConstructSettings{
FSettings: node4.GetSettings().GetConnSettings(),
FSettings: node4.GetSettings().GetConnSettings().GetMessageSettings(),
}),
payload.NewPayload32(
serviceHeader,
Expand Down
4 changes: 2 additions & 2 deletions pkg/network/examples/echo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var handler = func(ctx context.Context, node network.INode, c conn.IConn, msg me
ctx,
message.NewMessage(
message.NewConstructSettings(&message.SConstructSettings{
FSettings: node.GetSettings().GetConnSettings(),
FSettings: node.GetSettings().GetConnSettings().GetMessageSettings(),
}),
payload.NewPayload32(serviceHeader, []byte(resp)),
),
Expand All @@ -43,7 +43,7 @@ func main() {
ctx,
message.NewMessage(
message.NewConstructSettings(&message.SConstructSettings{
FSettings: conn.GetSettings(),
FSettings: conn.GetSettings().GetMessageSettings(),
}),
payload.NewPayload32(serviceHeader, []byte("hello, world!")),
),
Expand Down
4 changes: 2 additions & 2 deletions pkg/network/examples/ping-pong/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var handler = func(id string) network.IHandlerF {
ctx,
message.NewMessage(
message.NewConstructSettings(&message.SConstructSettings{
FSettings: n.GetSettings().GetConnSettings(),
FSettings: n.GetSettings().GetConnSettings().GetMessageSettings(),
}),
payload.NewPayload32(serviceHeader, []byte(fmt.Sprintf("%d", num+1))),
),
Expand All @@ -55,7 +55,7 @@ func main() {
ctx := context.Background()
msg := message.NewMessage(
message.NewConstructSettings(&message.SConstructSettings{
FSettings: node1.GetSettings().GetConnSettings(),
FSettings: node1.GetSettings().GetConnSettings().GetMessageSettings(),
}),
payload.NewPayload32(serviceHeader, []byte("0")),
)
Expand Down
5 changes: 3 additions & 2 deletions pkg/network/message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,15 @@ type sMessage struct {

func NewMessage(pSett IConstructSettings, pPld payload.IPayload32) IMessage {
prng := random.NewCSPRNG()
sett := pSett.GetSettings()

randBytes := prng.GetBytes(prng.GetUint64() % (pSett.GetRandMessageSizeBytes() + 1))
bytesJoiner := joiner.NewBytesJoiner32([][]byte{pPld.ToBytes(), randBytes})

key := hashing.NewSHA256Hasher([]byte(pSett.GetNetworkKey())).ToBytes()
key := hashing.NewSHA256Hasher([]byte(sett.GetNetworkKey())).ToBytes()
hash := hashing.NewHMACSHA256Hasher(key, bytesJoiner).ToBytes()

proof := puzzle.NewPoWPuzzle(pSett.GetWorkSizeBits()).ProofBytes(hash, pSett.GetParallel())
proof := puzzle.NewPoWPuzzle(sett.GetWorkSizeBits()).ProofBytes(hash, pSett.GetParallel())
proofBytes := encoding.Uint64ToBytes(proof)

cipher := symmetric.NewAESCipher(key)
Expand Down
4 changes: 2 additions & 2 deletions pkg/network/message/message_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func BenchmarkMessageSequence(b *testing.B) {

nowLoad := time.Now()
for i := 0; i < b.N; i++ {
_, err := LoadMessage(t.sett, messages[i].ToBytes())
_, err := LoadMessage(t.sett.GetSettings(), messages[i].ToBytes())
if err != nil {
b.Error(err)
return
Expand Down Expand Up @@ -226,7 +226,7 @@ func BenchmarkMessageParallel(b *testing.B) {

nowLoad := time.Now()
for i := 0; i < b.N; i++ {
_, err := LoadMessage(t.sett, messages[i].ToBytes())
_, err := LoadMessage(t.sett.GetSettings(), messages[i].ToBytes())
if err != nil {
b.Error(err)
return
Expand Down
Loading

0 comments on commit 53a284c

Please sign in to comment.