diff --git a/blockchain/options.go b/blockchain/options.go index 2ca4af97..7e102e6b 100644 --- a/blockchain/options.go +++ b/blockchain/options.go @@ -8,6 +8,7 @@ type ( Option func(*options) error options struct { authorizer peer.ID + authorizedPeers []peer.ID allowTransientConnection bool blockchainEndPoint string timeout int @@ -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 diff --git a/blox/blox.go b/blox/blox.go index 0ff3e9d0..6fec41fd 100644 --- a/blox/blox.go +++ b/blox/blox.go @@ -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 diff --git a/exchange/fx_exchange.go b/exchange/fx_exchange.go index 92e4211b..fc5fcf0e 100644 --- a/exchange/fx_exchange.go +++ b/exchange/fx_exchange.go @@ -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) @@ -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 {