|
| 1 | +// Go Substrate RPC Client (GSRPC) provides APIs and types around Polkadot and any Substrate-based chain RPC calls |
| 2 | +// |
| 3 | +// Copyright 2021 Snowfork |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +// you may not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, software |
| 12 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +// See the License for the specific language governing permissions and |
| 15 | +// limitations under the License. |
| 16 | + |
| 17 | +package beefy |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "sync" |
| 22 | + |
| 23 | + "github.com/centrifuge/go-substrate-rpc-client/v4/config" |
| 24 | + gethrpc "github.com/centrifuge/go-substrate-rpc-client/v4/gethrpc" |
| 25 | + "github.com/centrifuge/go-substrate-rpc-client/v4/types" |
| 26 | +) |
| 27 | + |
| 28 | +// JustificationsSubscription is a subscription established through one of the Client's subscribe methods. |
| 29 | +type JustificationsSubscription struct { |
| 30 | + sub *gethrpc.ClientSubscription |
| 31 | + channel chan types.SignedCommitment |
| 32 | + quitOnce sync.Once // ensures quit is closed once |
| 33 | +} |
| 34 | + |
| 35 | +// Chan returns the subscription channel. |
| 36 | +// |
| 37 | +// The channel is closed when Unsubscribe is called on the subscription. |
| 38 | +func (s *JustificationsSubscription) Chan() <-chan types.SignedCommitment { |
| 39 | + return s.channel |
| 40 | +} |
| 41 | + |
| 42 | +// Err returns the subscription error channel. The intended use of Err is to schedule |
| 43 | +// resubscription when the client connection is closed unexpectedly. |
| 44 | +// |
| 45 | +// The error channel receives a value when the subscription has ended due |
| 46 | +// to an error. The received error is nil if Close has been called |
| 47 | +// on the underlying client and no other error has occurred. |
| 48 | +// |
| 49 | +// The error channel is closed when Unsubscribe is called on the subscription. |
| 50 | +func (s *JustificationsSubscription) Err() <-chan error { |
| 51 | + return s.sub.Err() |
| 52 | +} |
| 53 | + |
| 54 | +// Unsubscribe unsubscribes the notification and closes the error channel. |
| 55 | +// It can safely be called more than once. |
| 56 | +func (s *JustificationsSubscription) Unsubscribe() { |
| 57 | + s.sub.Unsubscribe() |
| 58 | + s.quitOnce.Do(func() { |
| 59 | + close(s.channel) |
| 60 | + }) |
| 61 | +} |
| 62 | + |
| 63 | +// SubscribeJustifications subscribes beefy justifications, returning a subscription that will |
| 64 | +// receive server notifications containing the Header. |
| 65 | +func (b *Beefy) SubscribeJustifications() (*JustificationsSubscription, error) { |
| 66 | + ctx, cancel := context.WithTimeout(context.Background(), config.Default().SubscribeTimeout) |
| 67 | + defer cancel() |
| 68 | + |
| 69 | + ch := make(chan types.SignedCommitment) |
| 70 | + |
| 71 | + sub, err := b.client.Subscribe(ctx, "beefy", "subscribeJustifications", "unsubscribeJustifications", |
| 72 | + "justifications", ch) |
| 73 | + if err != nil { |
| 74 | + return nil, err |
| 75 | + } |
| 76 | + |
| 77 | + return &JustificationsSubscription{sub: sub, channel: ch}, nil |
| 78 | +} |
0 commit comments