mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-15 00:13:46 +00:00
faat: change the content_storage package
This commit is contained in:
parent
4fc88f8dbe
commit
6e2481873b
3 changed files with 30 additions and 19 deletions
|
|
@ -24,6 +24,7 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/p2p/enode"
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
||||||
"github.com/ethereum/go-ethereum/p2p/enr"
|
"github.com/ethereum/go-ethereum/p2p/enr"
|
||||||
"github.com/ethereum/go-ethereum/p2p/netutil"
|
"github.com/ethereum/go-ethereum/p2p/netutil"
|
||||||
|
"github.com/ethereum/go-ethereum/portalnetwork/storage"
|
||||||
"github.com/ethereum/go-ethereum/rlp"
|
"github.com/ethereum/go-ethereum/rlp"
|
||||||
ssz "github.com/ferranbt/fastssz"
|
ssz "github.com/ferranbt/fastssz"
|
||||||
"github.com/holiman/uint256"
|
"github.com/holiman/uint256"
|
||||||
|
|
@ -63,6 +64,16 @@ const (
|
||||||
PersistOfferRequestKind byte = 0x02
|
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 {
|
type ContentElement struct {
|
||||||
Node enode.ID
|
Node enode.ID
|
||||||
ContentKeys [][]byte
|
ContentKeys [][]byte
|
||||||
|
|
@ -133,7 +144,7 @@ type PortalProtocol struct {
|
||||||
radiusCache *fastcache.Cache
|
radiusCache *fastcache.Cache
|
||||||
closeCtx context.Context
|
closeCtx context.Context
|
||||||
cancelCloseCtx context.CancelFunc
|
cancelCloseCtx context.CancelFunc
|
||||||
storage Storage
|
storage ContentStorage
|
||||||
toContentId func(contentKey []byte) []byte
|
toContentId func(contentKey []byte) []byte
|
||||||
|
|
||||||
contentQueue chan *ContentElement
|
contentQueue chan *ContentElement
|
||||||
|
|
@ -144,7 +155,7 @@ func defaultContentIdFunc(contentKey []byte) []byte {
|
||||||
return digest[:]
|
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)
|
nodeDB, err := enode.OpenDB(config.NodeDBPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
@ -850,7 +861,7 @@ func (p *PortalProtocol) handleFindContent(id enode.ID, addr *net.UDPAddr, reque
|
||||||
|
|
||||||
contentId := p.toContentId(request.ContentKey)
|
contentId := p.toContentId(request.ContentKey)
|
||||||
if contentId == nil {
|
if contentId == nil {
|
||||||
return nil, ContentNotFound
|
return nil, ErrNilContentKey
|
||||||
}
|
}
|
||||||
|
|
||||||
var content []byte
|
var content []byte
|
||||||
|
|
@ -1027,7 +1038,7 @@ func (p *PortalProtocol) handleOffer(id enode.ID, addr *net.UDPAddr, request *po
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return nil, nil
|
return nil, ErrNilContentKey
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package discover
|
package storage
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
|
@ -16,7 +16,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ContentNotFound = fmt.Errorf("content not found")
|
ErrContentNotFound = fmt.Errorf("content not found")
|
||||||
|
|
||||||
maxDistance = uint256.MustFromHex("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")
|
maxDistance = uint256.MustFromHex("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")
|
||||||
)
|
)
|
||||||
|
|
@ -41,12 +41,6 @@ const (
|
||||||
ORDER BY distance DESC`
|
ORDER BY distance DESC`
|
||||||
)
|
)
|
||||||
|
|
||||||
type Storage interface {
|
|
||||||
Get(contentId []byte) ([]byte, error)
|
|
||||||
|
|
||||||
Put(contentId []byte, content []byte) error
|
|
||||||
}
|
|
||||||
|
|
||||||
type ContentStorage struct {
|
type ContentStorage struct {
|
||||||
nodeId enode.ID
|
nodeId enode.ID
|
||||||
nodeDataDir string
|
nodeDataDir string
|
||||||
|
|
@ -133,7 +127,7 @@ func (p *ContentStorage) Get(contentId []byte) ([]byte, error) {
|
||||||
var res []byte
|
var res []byte
|
||||||
err := p.getStmt.QueryRow(contentId).Scan(&res)
|
err := p.getStmt.QueryRow(contentId).Scan(&res)
|
||||||
if err == sql.ErrNoRows {
|
if err == sql.ErrNoRows {
|
||||||
return nil, ContentNotFound
|
return nil, ErrContentNotFound
|
||||||
}
|
}
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package discover
|
package storage
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/sha256"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
"os"
|
"os"
|
||||||
|
|
@ -25,6 +26,11 @@ func genBytes(length int) []byte {
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func defaultContentIdFunc(contentKey []byte) []byte {
|
||||||
|
digest := sha256.Sum256(contentKey)
|
||||||
|
return digest[:]
|
||||||
|
}
|
||||||
|
|
||||||
func TestBasicStorage(t *testing.T) {
|
func TestBasicStorage(t *testing.T) {
|
||||||
zeroNodeId := uint256.NewInt(0).Bytes32()
|
zeroNodeId := uint256.NewInt(0).Bytes32()
|
||||||
storage, err := NewContentStorage(math.MaxUint32, enode.ID(zeroNodeId), nodeDataDir)
|
storage, err := NewContentStorage(math.MaxUint32, enode.ID(zeroNodeId), nodeDataDir)
|
||||||
|
|
@ -37,7 +43,7 @@ func TestBasicStorage(t *testing.T) {
|
||||||
content := []byte("value")
|
content := []byte("value")
|
||||||
|
|
||||||
_, err = storage.Get(contentId)
|
_, err = storage.Get(contentId)
|
||||||
assert.Equal(t, ContentNotFound, err)
|
assert.Equal(t, ErrContentNotFound, err)
|
||||||
|
|
||||||
pt := storage.Put(contentId, content)
|
pt := storage.Put(contentId, content)
|
||||||
assert.NoError(t, pt.Err())
|
assert.NoError(t, pt.Err())
|
||||||
|
|
@ -178,10 +184,10 @@ func TestDBPruning(t *testing.T) {
|
||||||
assert.True(t, usedSize < storage.storageCapacityInBytes)
|
assert.True(t, usedSize < storage.storageCapacityInBytes)
|
||||||
|
|
||||||
_, err = storage.Get(furthestElement.Bytes())
|
_, err = storage.Get(furthestElement.Bytes())
|
||||||
assert.Equal(t, ContentNotFound, err)
|
assert.Equal(t, ErrContentNotFound, err)
|
||||||
|
|
||||||
_, err = storage.Get(secondFurthest.Bytes())
|
_, err = storage.Get(secondFurthest.Bytes())
|
||||||
assert.Equal(t, ContentNotFound, err)
|
assert.Equal(t, ErrContentNotFound, err)
|
||||||
|
|
||||||
val, err := storage.Get(thirdFurthest.Bytes())
|
val, err := storage.Get(thirdFurthest.Bytes())
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
@ -242,10 +248,10 @@ func TestSimpleForcePruning(t *testing.T) {
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
_, err = storage.Get(furthestElement.Bytes())
|
_, err = storage.Get(furthestElement.Bytes())
|
||||||
assert.Equal(t, ContentNotFound, err)
|
assert.Equal(t, ErrContentNotFound, err)
|
||||||
|
|
||||||
_, err = storage.Get(secondFurthest.Bytes())
|
_, err = storage.Get(secondFurthest.Bytes())
|
||||||
assert.Equal(t, ContentNotFound, err)
|
assert.Equal(t, ErrContentNotFound, err)
|
||||||
|
|
||||||
_, err = storage.Get(third.Bytes())
|
_, err = storage.Get(third.Bytes())
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
Loading…
Reference in a new issue