forked from ethereum/go-ethereum
-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Content storage #14
Merged
Merged
Content storage #14
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ import ( | |
"context" | ||
"crypto/ecdsa" | ||
crand "crypto/rand" | ||
"crypto/sha256" | ||
"encoding/binary" | ||
"errors" | ||
"fmt" | ||
|
@@ -23,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" | ||
|
@@ -62,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 | ||
|
@@ -86,6 +98,8 @@ type OfferRequest struct { | |
Request interface{} | ||
} | ||
|
||
type PortalProtocolOption func(p *PortalProtocol) | ||
|
||
type PortalProtocolConfig struct { | ||
BootstrapNodes []*enode.Node | ||
|
||
|
@@ -130,12 +144,18 @@ type PortalProtocol struct { | |
radiusCache *fastcache.Cache | ||
closeCtx context.Context | ||
cancelCloseCtx context.CancelFunc | ||
storage Storage | ||
storage ContentStorage | ||
toContentId func(contentKey []byte) []byte | ||
|
||
contentQueue chan *ContentElement | ||
} | ||
|
||
func NewPortalProtocol(config *PortalProtocolConfig, protocolId string, privateKey *ecdsa.PrivateKey, storage Storage, contentQueue chan *ContentElement) (*PortalProtocol, error) { | ||
func defaultContentIdFunc(contentKey []byte) []byte { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should be in |
||
digest := sha256.Sum256(contentKey) | ||
return digest[:] | ||
} | ||
|
||
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 | ||
|
@@ -159,9 +179,14 @@ func NewPortalProtocol(config *PortalProtocolConfig, protocolId string, privateK | |
localNode: localNode, | ||
validSchemes: enode.ValidSchemes, | ||
storage: storage, | ||
toContentId: defaultContentIdFunc, | ||
contentQueue: contentQueue, | ||
} | ||
|
||
for _, opt := range opts { | ||
opt(protocol) | ||
} | ||
|
||
return protocol, nil | ||
} | ||
|
||
|
@@ -451,9 +476,9 @@ func (p *PortalProtocol) processOffer(target *enode.Node, resp []byte, request * | |
} else { | ||
for _, index := range contentKeyBitlist.BitIndices() { | ||
contentKey := request.Request.(*PersistOfferRequest).ContentKeys[index] | ||
contentId := p.storage.ContentId(contentKey) | ||
contentId := p.toContentId(contentKey) | ||
if contentId != nil { | ||
content, err = p.storage.Get(contentKey, contentId) | ||
content, err = p.storage.Get(contentId) | ||
if err != nil { | ||
p.log.Error("failed to get content from storage", "err", err) | ||
contents = append(contents, []byte{}) | ||
|
@@ -834,13 +859,13 @@ func (p *PortalProtocol) handleFindContent(id enode.ID, addr *net.UDPAddr, reque | |
enrOverhead := 4 //per added ENR, 4 bytes offset overhead | ||
var err error | ||
|
||
contentId := p.storage.ContentId(request.ContentKey) | ||
contentId := p.toContentId(request.ContentKey) | ||
if contentId == nil { | ||
return nil, ContentNotFound | ||
return nil, ErrNilContentKey | ||
} | ||
|
||
var content []byte | ||
content, err = p.storage.Get(request.ContentKey, contentId) | ||
content, err = p.storage.Get(contentId) | ||
if err != nil && !errors.Is(err, ContentNotFound) { | ||
return nil, err | ||
} | ||
|
@@ -1004,16 +1029,16 @@ func (p *PortalProtocol) handleOffer(id enode.ID, addr *net.UDPAddr, request *po | |
|
||
contentKeys := make([][]byte, 0) | ||
for i, contentKey := range request.ContentKeys { | ||
contentId := p.storage.ContentId(contentKey) | ||
contentId := p.toContentId(contentKey) | ||
if contentId != nil { | ||
if inRange(p.Self().ID(), p.nodeRadius, contentId) { | ||
if _, err = p.storage.Get(contentKey, contentId); err != nil { | ||
if _, err = p.storage.Get(contentId); err != nil { | ||
contentKeyBitlist.SetBitAt(uint64(i), true) | ||
contentKeys = append(contentKeys, contentKey) | ||
} | ||
} | ||
} else { | ||
return nil, nil | ||
return nil, ErrNilContentKey | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't these all be moved to content_storage.go?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this err is return from the
PortalProtocol
before call thecontent_storage
, so this should be inPortalProtocol