@@ -21,6 +21,7 @@ import (
2121 "errors"
2222 "fmt"
2323 "math"
24+ "reflect"
2425 "sync"
2526 "time"
2627
@@ -87,6 +88,9 @@ type Registry struct {
8788 intervalsStore state.Store
8889 autoRetrieval bool //automatically subscribe to retrieve request stream
8990 maxPeerServers int
91+ spec * protocols.Spec //this protocol's spec
92+ balance protocols.Balance //implements protocols.Balance, for accounting
93+ prices protocols.Prices //implements protocols.Prices, provides prices to accounting
9094}
9195
9296// RegistryOptions holds optional values for NewRegistry constructor.
@@ -99,7 +103,7 @@ type RegistryOptions struct {
99103}
100104
101105// NewRegistry is Streamer constructor
102- func NewRegistry (localID enode.ID , delivery * Delivery , syncChunkStore storage.SyncChunkStore , intervalsStore state.Store , options * RegistryOptions ) * Registry {
106+ func NewRegistry (localID enode.ID , delivery * Delivery , syncChunkStore storage.SyncChunkStore , intervalsStore state.Store , options * RegistryOptions , balance protocols. Balance ) * Registry {
103107 if options == nil {
104108 options = & RegistryOptions {}
105109 }
@@ -119,7 +123,10 @@ func NewRegistry(localID enode.ID, delivery *Delivery, syncChunkStore storage.Sy
119123 intervalsStore : intervalsStore ,
120124 autoRetrieval : retrieval ,
121125 maxPeerServers : options .MaxPeerServers ,
126+ balance : balance ,
122127 }
128+ streamer .setupSpec ()
129+
123130 streamer .api = NewAPI (streamer )
124131 delivery .getPeer = streamer .getPeer
125132
@@ -228,6 +235,17 @@ func NewRegistry(localID enode.ID, delivery *Delivery, syncChunkStore storage.Sy
228235 return streamer
229236}
230237
238+ //we need to construct a spec instance per node instance
239+ func (r * Registry ) setupSpec () {
240+ //first create the "bare" spec
241+ r .createSpec ()
242+ //if balance is nil, this node has been started without swap support (swapEnabled flag is false)
243+ if r .balance != nil && ! reflect .ValueOf (r .balance ).IsNil () {
244+ //swap is enabled, so setup the hook
245+ r .spec .Hook = protocols .NewAccounting (r .balance , r .prices )
246+ }
247+ }
248+
231249// RegisterClient registers an incoming streamer constructor
232250func (r * Registry ) RegisterClientFunc (stream string , f func (* Peer , string , bool ) (Client , error )) {
233251 r .clientMu .Lock ()
@@ -492,7 +510,7 @@ func (r *Registry) updateSyncing() {
492510}
493511
494512func (r * Registry ) runProtocol (p * p2p.Peer , rw p2p.MsgReadWriter ) error {
495- peer := protocols .NewPeer (p , rw , Spec )
513+ peer := protocols .NewPeer (p , rw , r . spec )
496514 bp := network .NewBzzPeer (peer )
497515 np := network .NewPeer (bp , r .delivery .kad )
498516 r .delivery .kad .On (np )
@@ -716,35 +734,43 @@ func (c *clientParams) clientCreated() {
716734 close (c .clientCreatedC )
717735}
718736
719- // Spec is the spec of the streamer protocol
720- var Spec = & protocols.Spec {
721- Name : "stream" ,
722- Version : 8 ,
723- MaxMsgSize : 10 * 1024 * 1024 ,
724- Messages : []interface {}{
725- UnsubscribeMsg {},
726- OfferedHashesMsg {},
727- WantedHashesMsg {},
728- TakeoverProofMsg {},
729- SubscribeMsg {},
730- RetrieveRequestMsg {},
731- ChunkDeliveryMsgRetrieval {},
732- SubscribeErrorMsg {},
733- RequestSubscriptionMsg {},
734- QuitMsg {},
735- ChunkDeliveryMsgSyncing {},
736- },
737+ //GetSpec returns the streamer spec to callers
738+ //This used to be a global variable but for simulations with
739+ //multiple nodes its fields (notably the Hook) would be overwritten
740+ func (r * Registry ) GetSpec () * protocols.Spec {
741+ return r .spec
742+ }
743+
744+ func (r * Registry ) createSpec () {
745+ // Spec is the spec of the streamer protocol
746+ var spec = & protocols.Spec {
747+ Name : "stream" ,
748+ Version : 8 ,
749+ MaxMsgSize : 10 * 1024 * 1024 ,
750+ Messages : []interface {}{
751+ UnsubscribeMsg {},
752+ OfferedHashesMsg {},
753+ WantedHashesMsg {},
754+ TakeoverProofMsg {},
755+ SubscribeMsg {},
756+ RetrieveRequestMsg {},
757+ ChunkDeliveryMsgRetrieval {},
758+ SubscribeErrorMsg {},
759+ RequestSubscriptionMsg {},
760+ QuitMsg {},
761+ ChunkDeliveryMsgSyncing {},
762+ },
763+ }
764+ r .spec = spec
737765}
738766
739767func (r * Registry ) Protocols () []p2p.Protocol {
740768 return []p2p.Protocol {
741769 {
742- Name : Spec .Name ,
743- Version : Spec .Version ,
744- Length : Spec .Length (),
770+ Name : r . spec .Name ,
771+ Version : r . spec .Version ,
772+ Length : r . spec .Length (),
745773 Run : r .runProtocol ,
746- // NodeInfo: ,
747- // PeerInfo: ,
748774 },
749775 }
750776}
0 commit comments