Skip to content

Commit

Permalink
Merge pull request #248 from optimism-java/content
Browse files Browse the repository at this point in the history
add PutContent
  • Loading branch information
fearlessfe authored Jan 5, 2025
2 parents 97e4e25 + 77b4860 commit 4cf2ffa
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 0 deletions.
29 changes: 29 additions & 0 deletions p2p/discover/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ func NewDiscV5API(discV5 *UDPv5) *DiscV5API {
return &DiscV5API{discV5}
}

type PutContentResult struct {
PeerCount int `json:"peerCount"`
StoredLocally bool `json:"storedLocally"`
}

type NodeInfo struct {
NodeId string `json:"nodeId"`
Enr string `json:"enr"`
Expand Down Expand Up @@ -540,6 +545,30 @@ func (p *PortalProtocolAPI) Gossip(contentKeyHex, contentHex string) (int, error
return p.portalProtocol.Gossip(&id, [][]byte{contentKey}, [][]byte{content})
}

func (p *PortalProtocolAPI) PutContent(contentKeyHex, contentHex string) (*PutContentResult, error) {
contentKey, err := hexutil.Decode(contentKeyHex)
if err != nil {
return nil, err
}
content, err := hexutil.Decode(contentHex)
if err != nil {
return nil, err
}
shouldStore, err := p.portalProtocol.ShouldStore(contentKey, content)
if err != nil {
return nil, err
}
id := p.portalProtocol.Self().ID()
num, err := p.portalProtocol.Gossip(&id, [][]byte{contentKey}, [][]byte{content})
if err != nil {
return nil, err
}
return &PutContentResult{
PeerCount: num,
StoredLocally: shouldStore,
}, nil
}

func (p *PortalProtocolAPI) TraceRecursiveFindContent(contentKeyHex string) (*TraceContentResult, error) {
contentKey, err := hexutil.Decode(contentKeyHex)
if err != nil {
Expand Down
13 changes: 13 additions & 0 deletions p2p/discover/portal_protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -1861,6 +1861,19 @@ func (p *PortalProtocol) Gossip(srcNodeId *enode.ID, contentKeys [][]byte, conte
return len(finalGossipNodes), nil
}

// if the content is not in range, return false; else store the content and return true
func (p *PortalProtocol) ShouldStore(contentKey []byte, content []byte) (bool, error) {
if !p.InRange(p.toContentId(contentKey)) {
return false, nil
}

err := p.storage.Put(contentKey, p.toContentId(contentKey), content)
if err != nil {
return false, err
}
return true, nil
}

func (p *PortalProtocol) Distance(a, b enode.ID) enode.ID {
res := [32]byte{}
for i := range a {
Expand Down
5 changes: 5 additions & 0 deletions portalnetwork/beacon/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,15 @@ func (p *API) BeaconStore(contentKeyHex string, contextHex string) (bool, error)
return p.Store(contentKeyHex, contextHex)
}

// deprecated, use BeaconPutContent instead
func (p *API) BeaconGossip(contentKeyHex, contentHex string) (int, error) {
return p.Gossip(contentKeyHex, contentHex)
}

func (p *API) BeaconPutContent(contentKeyHex, contentHex string) (*discover.PutContentResult, error) {
return p.PutContent(contentKeyHex, contentHex)
}

func (p *API) BeaconTraceGetContent(contentKeyHex string) (*discover.TraceContentResult, error) {
return p.TraceRecursiveFindContent(contentKeyHex)
}
Expand Down
5 changes: 5 additions & 0 deletions portalnetwork/history/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,15 @@ func (p *API) HistoryStore(contentKeyHex string, contextHex string) (bool, error
return p.Store(contentKeyHex, contextHex)
}

// deprecated, use HistoryPutContent instead
func (p *API) HistoryGossip(contentKeyHex, contentHex string) (int, error) {
return p.Gossip(contentKeyHex, contentHex)
}

func (p *API) HistoryPutContent(contentKeyHex, contentHex string) (*discover.PutContentResult, error) {
return p.PutContent(contentKeyHex, contentHex)
}

func (p *API) HistoryTraceGetContent(contentKeyHex string) (*discover.TraceContentResult, error) {
return p.TraceRecursiveFindContent(contentKeyHex)
}
Expand Down
5 changes: 5 additions & 0 deletions portalnetwork/state/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,15 @@ func (p *API) StateStore(contentKeyHex string, contextHex string) (bool, error)
return p.Store(contentKeyHex, contextHex)
}

// deprecated, use StatePutContent instead
func (p *API) StateGossip(contentKeyHex, contentHex string) (int, error) {
return p.Gossip(contentKeyHex, contentHex)
}

func (p *API) StatePutContent(contentKeyHex, contentHex string) (*discover.PutContentResult, error) {
return p.PutContent(contentKeyHex, contentHex)
}

func (p *API) StateTraceGetContent(contentKeyHex string) (*discover.TraceContentResult, error) {
return p.TraceRecursiveFindContent(contentKeyHex)
}
Expand Down

0 comments on commit 4cf2ffa

Please sign in to comment.