Skip to content

Commit

Permalink
updated blockchain with peerlist as well
Browse files Browse the repository at this point in the history
  • Loading branch information
ehsan6sha committed Jul 19, 2023
1 parent 31c8b4d commit 538cac8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
8 changes: 8 additions & 0 deletions blockchain/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type (
Option func(*options) error
options struct {
authorizer peer.ID
authorizedPeers []peer.ID
allowTransientConnection bool
blockchainEndPoint string
timeout int
Expand All @@ -31,6 +32,13 @@ func WithAuthorizer(a peer.ID) Option {
}
}

func WithAuthorizedPeers(l []peer.ID) Option {
return func(o *options) error {
o.authorizedPeers = l
return nil
}
}

func WithAllowTransientConnection(t bool) Option {
return func(o *options) error {
o.allowTransientConnection = t
Expand Down
5 changes: 5 additions & 0 deletions blox/blox.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,14 @@ func New(o ...Option) (*Blox, error) {
if err != nil {
authorizer = opts.authorizer
}
authorizedPeers, err := p.ex.GetAuthorizedPeers(p.ctx)
if err != nil {
authorizedPeers = opts.authorizedPeers
}
p.bl, _ = blockchain.NewFxBlockchain(p.h,
blockchain.NewSimpleKeyStorer(),
blockchain.WithAuthorizer(authorizer),
blockchain.WithAuthorizedPeers(authorizedPeers),
blockchain.WithBlockchainEndPoint("127.0.0.1:4000"),
blockchain.WithTimeout(30))
return &p, nil
Expand Down
14 changes: 11 additions & 3 deletions exchange/fx_exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@ func (e *FxExchange) GetAuth(ctx context.Context) (peer.ID, error) {
return e.authorizer, nil
}

func (e *FxExchange) GetAuthorizedPeers(ctx context.Context) ([]peer.ID, error) {
var peerList []peer.ID
for peerId := range e.authorizedPeers {
peerList = append(peerList, peerId)
}
e.options.authorizedPeers = peerList
return peerList, nil
}

func (e *FxExchange) Start(ctx context.Context) error {
gsn := gsnet.NewFromLibp2pHost(e.h)
e.gx = gs.New(ctx, gsn, e.ls)
Expand Down Expand Up @@ -278,9 +287,8 @@ func (e *FxExchange) handleAuthorization(from peer.ID, w http.ResponseWriter, r

func (e *FxExchange) updateAuthorizePeers() error {
var peerList []peer.ID
for peerId := range e.authorizedPeers {
peerList = append(peerList, peerId)
}
ctx := context.TODO()
peerList, _ = e.GetAuthorizedPeers(ctx)
e.options.authorizedPeers = peerList
err := e.updateConfig(e.options.authorizedPeers)
if err != nil {
Expand Down

0 comments on commit 538cac8

Please sign in to comment.