forked from ethereum/go-ethereum
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8dc76c1
commit bf40a75
Showing
6 changed files
with
202 additions
and
6 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package state | ||
|
||
import ( | ||
"github.com/ethereum/go-ethereum/p2p/discover" | ||
) | ||
|
||
type API struct { | ||
*discover.PortalProtocolAPI | ||
} | ||
|
||
func (p *API) StateRoutingTableInfo() *discover.RoutingTableInfo { | ||
return p.RoutingTableInfo() | ||
} | ||
|
||
func (p *API) StateAddEnr(enr string) (bool, error) { | ||
return p.AddEnr(enr) | ||
} | ||
|
||
func (p *API) StateGetEnr(nodeId string) (string, error) { | ||
return p.GetEnr(nodeId) | ||
} | ||
|
||
func (p *API) StateDeleteEnr(nodeId string) (bool, error) { | ||
return p.DeleteEnr(nodeId) | ||
} | ||
|
||
func (p *API) StateLookupEnr(nodeId string) (string, error) { | ||
return p.LookupEnr(nodeId) | ||
} | ||
|
||
func (p *API) StatePing(enr string) (*discover.PortalPongResp, error) { | ||
return p.Ping(enr) | ||
} | ||
|
||
func (p *API) StateFindNodes(enr string, distances []uint) ([]string, error) { | ||
return p.FindNodes(enr, distances) | ||
} | ||
|
||
func (p *API) StateFindContent(enr string, contentKey string) (interface{}, error) { | ||
return p.FindContent(enr, contentKey) | ||
} | ||
|
||
func (p *API) StateOffer(enr string, contentKey string, contentValue string) (string, error) { | ||
return p.Offer(enr, contentKey, contentValue) | ||
} | ||
|
||
func (p *API) StateRecursiveFindNodes(nodeId string) ([]string, error) { | ||
return p.RecursiveFindNodes(nodeId) | ||
} | ||
|
||
func (p *API) StateRecursiveFindContent(contentKeyHex string) (*discover.ContentInfo, error) { | ||
return p.RecursiveFindContent(contentKeyHex) | ||
} | ||
|
||
func (p *API) StateLocalContent(contentKeyHex string) (string, error) { | ||
return p.LocalContent(contentKeyHex) | ||
} | ||
|
||
func (p *API) StateStore(contentKeyHex string, contextHex string) (bool, error) { | ||
return p.Store(contentKeyHex, contextHex) | ||
} | ||
|
||
func (p *API) StateGossip(contentKeyHex, contentHex string) (int, error) { | ||
return p.Gossip(contentKeyHex, contentHex) | ||
} | ||
|
||
func (p *API) StateTraceRecursiveFindContent(contentKeyHex string) (*discover.TraceContentResult, error) { | ||
return p.TraceRecursiveFindContent(contentKeyHex) | ||
} | ||
|
||
func NewStateNetworkAPI(portalProtocolAPI *discover.PortalProtocolAPI) *API { | ||
return &API{ | ||
portalProtocolAPI, | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package state | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/ethereum/go-ethereum/log" | ||
"github.com/ethereum/go-ethereum/p2p/discover" | ||
) | ||
|
||
type StateNetwork struct { | ||
portalProtocol *discover.PortalProtocol | ||
closeCtx context.Context | ||
closeFunc context.CancelFunc | ||
log log.Logger | ||
} | ||
|
||
func NewStateNetwork(portalProtocol *discover.PortalProtocol) *StateNetwork { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
|
||
return &StateNetwork{ | ||
portalProtocol: portalProtocol, | ||
closeCtx: ctx, | ||
closeFunc: cancel, | ||
log: log.New("sub-protocol", "state"), | ||
} | ||
} | ||
|
||
func (h *StateNetwork) Start() error { | ||
err := h.portalProtocol.Start() | ||
if err != nil { | ||
return err | ||
} | ||
go h.processContentLoop(h.closeCtx) | ||
h.log.Debug("state network start successfully") | ||
return nil | ||
} | ||
|
||
func (h *StateNetwork) Stop() { | ||
h.closeFunc() | ||
h.portalProtocol.Stop() | ||
} | ||
|
||
func (h *StateNetwork) processContentLoop(ctx context.Context) { | ||
contentChan := h.portalProtocol.GetContent() | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case contentElement := <-contentChan: | ||
err := h.validateContents(contentElement.ContentKeys, contentElement.Contents) | ||
if err != nil { | ||
h.log.Error("validate content failed", "err", err) | ||
continue | ||
} | ||
|
||
go func(ctx context.Context) { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
default: | ||
var gossippedNum int | ||
gossippedNum, err = h.portalProtocol.Gossip(&contentElement.Node, contentElement.ContentKeys, contentElement.Contents) | ||
h.log.Trace("gossippedNum", "gossippedNum", gossippedNum) | ||
if err != nil { | ||
h.log.Error("gossip failed", "err", err) | ||
return | ||
} | ||
} | ||
}(ctx) | ||
} | ||
} | ||
} | ||
|
||
func (h *StateNetwork) validateContents(contentKeys [][]byte, contents [][]byte) error { | ||
// TODO | ||
panic("implement me") | ||
} |
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 |
---|---|---|
|
@@ -12,4 +12,5 @@ type PortalStorageConfig struct { | |
DB *sql.DB | ||
NodeId enode.ID | ||
Spec *common.Spec | ||
NetworkName string | ||
} |