From 6e2481873badd9a2ccfa1e6b37cee6d6d18c18be Mon Sep 17 00:00:00 2001 From: fearlseefe <505380967@qq.com> Date: Tue, 12 Dec 2023 16:56:43 +0800 Subject: [PATCH] faat: change the content_storage package --- p2p/discover/portal_protocol.go | 19 +++++++++++++++---- .../storage}/content_storage.go | 12 +++--------- .../storage}/content_storage_test.go | 18 ++++++++++++------ 3 files changed, 30 insertions(+), 19 deletions(-) rename {p2p/discover => portalnetwork/storage}/content_storage.go (97%) rename {p2p/discover => portalnetwork/storage}/content_storage_test.go (95%) diff --git a/p2p/discover/portal_protocol.go b/p2p/discover/portal_protocol.go index 981a3bfcc0..ea9f04cb8c 100644 --- a/p2p/discover/portal_protocol.go +++ b/p2p/discover/portal_protocol.go @@ -24,6 +24,7 @@ import ( "github.com/ethereum/go-ethereum/p2p/enode" "github.com/ethereum/go-ethereum/p2p/enr" "github.com/ethereum/go-ethereum/p2p/netutil" + "github.com/ethereum/go-ethereum/portalnetwork/storage" "github.com/ethereum/go-ethereum/rlp" ssz "github.com/ferranbt/fastssz" "github.com/holiman/uint256" @@ -63,6 +64,16 @@ const ( PersistOfferRequestKind byte = 0x02 ) +var ErrNilContentKey = errors.New("content key cannot be nil") + +var ContentNotFound = storage.ErrContentNotFound + +type ContentStorage interface { + Get(contentId []byte) ([]byte, error) + + Put(contentId []byte, content []byte) error +} + type ContentElement struct { Node enode.ID ContentKeys [][]byte @@ -133,7 +144,7 @@ type PortalProtocol struct { radiusCache *fastcache.Cache closeCtx context.Context cancelCloseCtx context.CancelFunc - storage Storage + storage ContentStorage toContentId func(contentKey []byte) []byte contentQueue chan *ContentElement @@ -144,7 +155,7 @@ func defaultContentIdFunc(contentKey []byte) []byte { return digest[:] } -func NewPortalProtocol(config *PortalProtocolConfig, protocolId string, privateKey *ecdsa.PrivateKey, storage Storage, contentQueue chan *ContentElement, opts ...PortalProtocolOption) (*PortalProtocol, error) { +func NewPortalProtocol(config *PortalProtocolConfig, protocolId string, privateKey *ecdsa.PrivateKey, storage ContentStorage, contentQueue chan *ContentElement, opts ...PortalProtocolOption) (*PortalProtocol, error) { nodeDB, err := enode.OpenDB(config.NodeDBPath) if err != nil { return nil, err @@ -850,7 +861,7 @@ func (p *PortalProtocol) handleFindContent(id enode.ID, addr *net.UDPAddr, reque contentId := p.toContentId(request.ContentKey) if contentId == nil { - return nil, ContentNotFound + return nil, ErrNilContentKey } var content []byte @@ -1027,7 +1038,7 @@ func (p *PortalProtocol) handleOffer(id enode.ID, addr *net.UDPAddr, request *po } } } else { - return nil, nil + return nil, ErrNilContentKey } } diff --git a/p2p/discover/content_storage.go b/portalnetwork/storage/content_storage.go similarity index 97% rename from p2p/discover/content_storage.go rename to portalnetwork/storage/content_storage.go index c8e5b5c022..d0579a1bed 100644 --- a/p2p/discover/content_storage.go +++ b/portalnetwork/storage/content_storage.go @@ -1,4 +1,4 @@ -package discover +package storage import ( "bytes" @@ -16,7 +16,7 @@ import ( ) var ( - ContentNotFound = fmt.Errorf("content not found") + ErrContentNotFound = fmt.Errorf("content not found") maxDistance = uint256.MustFromHex("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff") ) @@ -41,12 +41,6 @@ const ( ORDER BY distance DESC` ) -type Storage interface { - Get(contentId []byte) ([]byte, error) - - Put(contentId []byte, content []byte) error -} - type ContentStorage struct { nodeId enode.ID nodeDataDir string @@ -133,7 +127,7 @@ func (p *ContentStorage) Get(contentId []byte) ([]byte, error) { var res []byte err := p.getStmt.QueryRow(contentId).Scan(&res) if err == sql.ErrNoRows { - return nil, ContentNotFound + return nil, ErrContentNotFound } return res, err } diff --git a/p2p/discover/content_storage_test.go b/portalnetwork/storage/content_storage_test.go similarity index 95% rename from p2p/discover/content_storage_test.go rename to portalnetwork/storage/content_storage_test.go index f175043bf3..9437cae88f 100644 --- a/p2p/discover/content_storage_test.go +++ b/portalnetwork/storage/content_storage_test.go @@ -1,6 +1,7 @@ -package discover +package storage import ( + "crypto/sha256" "fmt" "math" "os" @@ -25,6 +26,11 @@ func genBytes(length int) []byte { return res } +func defaultContentIdFunc(contentKey []byte) []byte { + digest := sha256.Sum256(contentKey) + return digest[:] +} + func TestBasicStorage(t *testing.T) { zeroNodeId := uint256.NewInt(0).Bytes32() storage, err := NewContentStorage(math.MaxUint32, enode.ID(zeroNodeId), nodeDataDir) @@ -37,7 +43,7 @@ func TestBasicStorage(t *testing.T) { content := []byte("value") _, err = storage.Get(contentId) - assert.Equal(t, ContentNotFound, err) + assert.Equal(t, ErrContentNotFound, err) pt := storage.Put(contentId, content) assert.NoError(t, pt.Err()) @@ -178,10 +184,10 @@ func TestDBPruning(t *testing.T) { assert.True(t, usedSize < storage.storageCapacityInBytes) _, err = storage.Get(furthestElement.Bytes()) - assert.Equal(t, ContentNotFound, err) + assert.Equal(t, ErrContentNotFound, err) _, err = storage.Get(secondFurthest.Bytes()) - assert.Equal(t, ContentNotFound, err) + assert.Equal(t, ErrContentNotFound, err) val, err := storage.Get(thirdFurthest.Bytes()) assert.NoError(t, err) @@ -242,10 +248,10 @@ func TestSimpleForcePruning(t *testing.T) { assert.NoError(t, err) _, err = storage.Get(furthestElement.Bytes()) - assert.Equal(t, ContentNotFound, err) + assert.Equal(t, ErrContentNotFound, err) _, err = storage.Get(secondFurthest.Bytes()) - assert.Equal(t, ContentNotFound, err) + assert.Equal(t, ErrContentNotFound, err) _, err = storage.Get(third.Bytes()) assert.NoError(t, err)