faat: change the content_storage package

This commit is contained in:
fearlseefe 2023-12-12 16:56:43 +08:00 committed by Chen Kai
parent 4fc88f8dbe
commit 6e2481873b
3 changed files with 30 additions and 19 deletions

View file

@ -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
}
}

View file

@ -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
}

View file

@ -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)