|
1 | 1 | package oasis |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
4 | 7 | "google.golang.org/grpc" |
5 | 8 | "google.golang.org/grpc/credentials/insecure" |
6 | 9 |
|
7 | 10 | beacon "github.com/oasisprotocol/oasis-core/go/beacon/api" |
| 11 | + "github.com/oasisprotocol/oasis-core/go/common" |
8 | 12 | cmnGrpc "github.com/oasisprotocol/oasis-core/go/common/grpc" |
9 | 13 | consensus "github.com/oasisprotocol/oasis-core/go/consensus/api" |
10 | 14 | control "github.com/oasisprotocol/oasis-core/go/control/api" |
@@ -46,6 +50,52 @@ func (c *Controller) Close() { |
46 | 50 | c.conn.Close() |
47 | 51 | } |
48 | 52 |
|
| 53 | +// WaitConsensusHeight waits until the controller observes at least specified consensus height. |
| 54 | +func (c *Controller) WaitConsensusHeight(ctx context.Context, height int64) error { |
| 55 | + blkCh, sub, err := c.Consensus.WatchBlocks(ctx) |
| 56 | + if err != nil { |
| 57 | + return fmt.Errorf("failed to watch consensus blocks: %w", err) |
| 58 | + } |
| 59 | + defer sub.Close() |
| 60 | + |
| 61 | + for { |
| 62 | + select { |
| 63 | + case blk, ok := <-blkCh: |
| 64 | + if !ok { |
| 65 | + return fmt.Errorf("consensus block channel closed") |
| 66 | + } |
| 67 | + if blk.Height >= height { |
| 68 | + return nil |
| 69 | + } |
| 70 | + case <-ctx.Done(): |
| 71 | + return ctx.Err() |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +// WaitRuntimeRound waits until the controller observes at least specified runtime round. |
| 77 | +func (c *Controller) WaitRuntimeRound(ctx context.Context, runtimeID common.Namespace, round uint64) error { |
| 78 | + blkCh, sub, err := c.RuntimeClient.WatchBlocks(ctx, runtimeID) |
| 79 | + if err != nil { |
| 80 | + return fmt.Errorf("failed to watch runtime blocks: %w", err) |
| 81 | + } |
| 82 | + defer sub.Close() |
| 83 | + |
| 84 | + for { |
| 85 | + select { |
| 86 | + case annBlk, ok := <-blkCh: |
| 87 | + if !ok { |
| 88 | + return fmt.Errorf("runtime block channel closed") |
| 89 | + } |
| 90 | + if annBlk.Block.Header.Round >= round { |
| 91 | + return nil |
| 92 | + } |
| 93 | + case <-ctx.Done(): |
| 94 | + return ctx.Err() |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | + |
49 | 99 | // NewController creates a new node controller given the path to |
50 | 100 | // a node's internal socket. |
51 | 101 | func NewController(socketPath string) (*Controller, error) { |
|
0 commit comments