Skip to content

Commit 4cf2ffa

Browse files
authored
Merge pull request #248 from optimism-java/content
add PutContent
2 parents 97e4e25 + 77b4860 commit 4cf2ffa

File tree

5 files changed

+57
-0
lines changed

5 files changed

+57
-0
lines changed

p2p/discover/api.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ func NewDiscV5API(discV5 *UDPv5) *DiscV5API {
1919
return &DiscV5API{discV5}
2020
}
2121

22+
type PutContentResult struct {
23+
PeerCount int `json:"peerCount"`
24+
StoredLocally bool `json:"storedLocally"`
25+
}
26+
2227
type NodeInfo struct {
2328
NodeId string `json:"nodeId"`
2429
Enr string `json:"enr"`
@@ -540,6 +545,30 @@ func (p *PortalProtocolAPI) Gossip(contentKeyHex, contentHex string) (int, error
540545
return p.portalProtocol.Gossip(&id, [][]byte{contentKey}, [][]byte{content})
541546
}
542547

548+
func (p *PortalProtocolAPI) PutContent(contentKeyHex, contentHex string) (*PutContentResult, error) {
549+
contentKey, err := hexutil.Decode(contentKeyHex)
550+
if err != nil {
551+
return nil, err
552+
}
553+
content, err := hexutil.Decode(contentHex)
554+
if err != nil {
555+
return nil, err
556+
}
557+
shouldStore, err := p.portalProtocol.ShouldStore(contentKey, content)
558+
if err != nil {
559+
return nil, err
560+
}
561+
id := p.portalProtocol.Self().ID()
562+
num, err := p.portalProtocol.Gossip(&id, [][]byte{contentKey}, [][]byte{content})
563+
if err != nil {
564+
return nil, err
565+
}
566+
return &PutContentResult{
567+
PeerCount: num,
568+
StoredLocally: shouldStore,
569+
}, nil
570+
}
571+
543572
func (p *PortalProtocolAPI) TraceRecursiveFindContent(contentKeyHex string) (*TraceContentResult, error) {
544573
contentKey, err := hexutil.Decode(contentKeyHex)
545574
if err != nil {

p2p/discover/portal_protocol.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1861,6 +1861,19 @@ func (p *PortalProtocol) Gossip(srcNodeId *enode.ID, contentKeys [][]byte, conte
18611861
return len(finalGossipNodes), nil
18621862
}
18631863

1864+
// if the content is not in range, return false; else store the content and return true
1865+
func (p *PortalProtocol) ShouldStore(contentKey []byte, content []byte) (bool, error) {
1866+
if !p.InRange(p.toContentId(contentKey)) {
1867+
return false, nil
1868+
}
1869+
1870+
err := p.storage.Put(contentKey, p.toContentId(contentKey), content)
1871+
if err != nil {
1872+
return false, err
1873+
}
1874+
return true, nil
1875+
}
1876+
18641877
func (p *PortalProtocol) Distance(a, b enode.ID) enode.ID {
18651878
res := [32]byte{}
18661879
for i := range a {

portalnetwork/beacon/api.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,15 @@ func (p *API) BeaconStore(contentKeyHex string, contextHex string) (bool, error)
6060
return p.Store(contentKeyHex, contextHex)
6161
}
6262

63+
// deprecated, use BeaconPutContent instead
6364
func (p *API) BeaconGossip(contentKeyHex, contentHex string) (int, error) {
6465
return p.Gossip(contentKeyHex, contentHex)
6566
}
6667

68+
func (p *API) BeaconPutContent(contentKeyHex, contentHex string) (*discover.PutContentResult, error) {
69+
return p.PutContent(contentKeyHex, contentHex)
70+
}
71+
6772
func (p *API) BeaconTraceGetContent(contentKeyHex string) (*discover.TraceContentResult, error) {
6873
return p.TraceRecursiveFindContent(contentKeyHex)
6974
}

portalnetwork/history/api.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,15 @@ func (p *API) HistoryStore(contentKeyHex string, contextHex string) (bool, error
6060
return p.Store(contentKeyHex, contextHex)
6161
}
6262

63+
// deprecated, use HistoryPutContent instead
6364
func (p *API) HistoryGossip(contentKeyHex, contentHex string) (int, error) {
6465
return p.Gossip(contentKeyHex, contentHex)
6566
}
6667

68+
func (p *API) HistoryPutContent(contentKeyHex, contentHex string) (*discover.PutContentResult, error) {
69+
return p.PutContent(contentKeyHex, contentHex)
70+
}
71+
6772
func (p *API) HistoryTraceGetContent(contentKeyHex string) (*discover.TraceContentResult, error) {
6873
return p.TraceRecursiveFindContent(contentKeyHex)
6974
}

portalnetwork/state/api.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,15 @@ func (p *API) StateStore(contentKeyHex string, contextHex string) (bool, error)
6060
return p.Store(contentKeyHex, contextHex)
6161
}
6262

63+
// deprecated, use StatePutContent instead
6364
func (p *API) StateGossip(contentKeyHex, contentHex string) (int, error) {
6465
return p.Gossip(contentKeyHex, contentHex)
6566
}
6667

68+
func (p *API) StatePutContent(contentKeyHex, contentHex string) (*discover.PutContentResult, error) {
69+
return p.PutContent(contentKeyHex, contentHex)
70+
}
71+
6772
func (p *API) StateTraceGetContent(contentKeyHex string) (*discover.TraceContentResult, error) {
6873
return p.TraceRecursiveFindContent(contentKeyHex)
6974
}

0 commit comments

Comments
 (0)