This commit is contained in:
number571 2026-03-29 22:43:39 +07:00
parent 2f16d96d7f
commit c4142f535f
4 changed files with 25 additions and 25 deletions

View File

@ -7,6 +7,7 @@ import (
"sync"
"time"
"github.com/number571/go-peer/pkg/encoding"
"github.com/number571/go-peer/pkg/message/layer1"
"github.com/number571/go-peer/pkg/network/conn"
"github.com/number571/go-peer/pkg/storage/cache"
@ -60,7 +61,8 @@ func (p *sNode) BroadcastMessage(pCtx context.Context, pMsg layer1.IMessage) err
}
// node can redirect received message
_ = p.fCacheSetter.Set(pMsg.GetHash(), []byte{})
hash := encoding.HexEncode(pMsg.GetHash())
_ = p.fCacheSetter.Set(hash, []byte{})
wg := sync.WaitGroup{}
wg.Add(lenConnections)
@ -277,7 +279,8 @@ func (p *sNode) messageReader(
// Returns true if the message was successfully redirected to the handler function
// > or if the message already existed in the hash value store.
func (p *sNode) handleMessage(pCtx context.Context, pConn conn.IConn, pMsg layer1.IMessage) bool {
if !p.fCacheSetter.Set(pMsg.GetHash(), []byte{}) {
hash := encoding.HexEncode(pMsg.GetHash())
if !p.fCacheSetter.Set(hash, []byte{}) {
return true // hash of message already in queue
}

View File

@ -13,14 +13,14 @@ func TestLRUCache(t *testing.T) {
lruCache := NewLRUCache(3)
if _, ok := lruCache.Get([]byte("unknown-key")); ok {
if _, ok := lruCache.Get("unknown-key"); ok {
t.Error("success load unknown key")
return
}
for i := 0; i < 3; i++ {
key := encoding.Uint64ToBytes(uint64(i)) //nolint:gosec
if ok := lruCache.Set(key[:], []byte(fmt.Sprintf("_%d_", i))); !ok {
if ok := lruCache.Set(string(key[:]), []byte(fmt.Sprintf("_%d_", i))); !ok {
t.Errorf("failed push %d", i)
return
}
@ -33,7 +33,7 @@ func TestLRUCache(t *testing.T) {
for i := 0; i < 3; i++ {
key := encoding.Uint64ToBytes(uint64(i)) //nolint:gosec
val, ok := lruCache.Get(
key[:],
string(key[:]),
)
if !ok {
t.Errorf("failed load %d", i)
@ -51,14 +51,14 @@ func TestLRUCache(t *testing.T) {
break
}
key := encoding.Uint64ToBytes(i)
if !bytes.Equal(k, key[:]) {
if !bytes.Equal([]byte(k), key[:]) {
t.Error("got incorrect key")
return
}
}
key1 := encoding.Uint64ToBytes(1)
if ok := lruCache.Set(key1[:], []byte(fmt.Sprintf("_%d_", 1))); ok {
if ok := lruCache.Set(string(key1[:]), []byte(fmt.Sprintf("_%d_", 1))); ok {
t.Error("success push already exist value")
return
}
@ -66,7 +66,7 @@ func TestLRUCache(t *testing.T) {
// start cycle of queue
i := uint64(4)
key2 := encoding.Uint64ToBytes(i)
if ok := lruCache.Set(key2[:], []byte(fmt.Sprintf("_%d_", i))); !ok {
if ok := lruCache.Set(string(key2[:]), []byte(fmt.Sprintf("_%d_", i))); !ok {
t.Errorf("failed push %d", i)
return
}
@ -74,7 +74,7 @@ func TestLRUCache(t *testing.T) {
// try load init value
i = 0
key3 := encoding.Uint64ToBytes(i)
if _, ok := lruCache.Get(key3[:]); ok {
if _, ok := lruCache.Get(string(key3[:])); ok {
t.Errorf("success load rewrited value %d", i)
return
}

View File

@ -2,8 +2,6 @@ package cache
import (
"sync"
"github.com/number571/go-peer/pkg/encoding"
)
var (
@ -31,33 +29,32 @@ func (p *sLRUCache) GetIndex() uint64 {
return p.fIndex
}
func (p *sLRUCache) GetKey(i uint64) ([]byte, bool) {
func (p *sLRUCache) GetKey(i uint64) (string, bool) {
p.fMutex.RLock()
defer p.fMutex.RUnlock()
if uint64(len(p.fQueue)) <= i {
return nil, false
return "", false
}
hash := encoding.HexDecode(p.fQueue[i])
return hash, len(hash) != 0
key := p.fQueue[i]
return key, len(key) != 0
}
func (p *sLRUCache) Get(pKey []byte) (interface{}, bool) {
func (p *sLRUCache) Get(pKey string) (interface{}, bool) {
p.fMutex.RLock()
defer p.fMutex.RUnlock()
val, ok := p.fMap[encoding.HexEncode(pKey)]
val, ok := p.fMap[pKey]
return val, ok
}
func (p *sLRUCache) Set(pKey []byte, pValue interface{}) bool {
func (p *sLRUCache) Set(pKey string, pValue interface{}) bool {
p.fMutex.Lock()
defer p.fMutex.Unlock()
// hash already exists in queue
key := encoding.HexEncode(pKey)
if _, ok := p.fMap[key]; ok {
if _, ok := p.fMap[pKey]; ok {
return false
}
@ -65,8 +62,8 @@ func (p *sLRUCache) Set(pKey []byte, pValue interface{}) bool {
delete(p.fMap, p.fQueue[p.fIndex])
// push hash to queue
p.fQueue[p.fIndex] = key
p.fMap[key] = pValue
p.fQueue[p.fIndex] = pKey
p.fMap[pKey] = pValue
// increment queue index
p.fIndex = (p.fIndex + 1) % uint64(len(p.fQueue))

View File

@ -4,7 +4,7 @@ type ILRUCache interface {
ICache
GetIndex() uint64
GetKey(i uint64) ([]byte, bool)
GetKey(i uint64) (string, bool)
}
type ICache interface {
@ -13,9 +13,9 @@ type ICache interface {
}
type ICacheSetter interface {
Set([]byte, interface{}) bool
Set(string, interface{}) bool
}
type ICacheGetter interface {
Get([]byte) (interface{}, bool)
Get(string) (interface{}, bool)
}