-
Notifications
You must be signed in to change notification settings - Fork 107
/
query.go
97 lines (80 loc) · 3.02 KB
/
query.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package scheduler
import (
"context"
abciAPI "github.com/oasisprotocol/oasis-core/go/consensus/tendermint/api"
registryState "github.com/oasisprotocol/oasis-core/go/consensus/tendermint/apps/registry/state"
schedulerState "github.com/oasisprotocol/oasis-core/go/consensus/tendermint/apps/scheduler/state"
scheduler "github.com/oasisprotocol/oasis-core/go/scheduler/api"
)
// Query is the scheduler query interface.
type Query interface {
Validators(context.Context) ([]*scheduler.Validator, error)
AllCommittees(context.Context) ([]*scheduler.Committee, error)
KindsCommittees(context.Context, []scheduler.CommitteeKind) ([]*scheduler.Committee, error)
Genesis(context.Context) (*scheduler.Genesis, error)
ConsensusParameters(context.Context) (*scheduler.ConsensusParameters, error)
}
// QueryFactory is the scheduler query factory.
type QueryFactory struct {
state abciAPI.ApplicationQueryState
}
// QueryAt returns the scheduler query interface for a specific height.
func (sf *QueryFactory) QueryAt(ctx context.Context, height int64) (Query, error) {
state, err := schedulerState.NewImmutableState(ctx, sf.state, height)
if err != nil {
return nil, err
}
// Some queries need access to the registry to give useful responses.
regState, err := registryState.NewImmutableState(ctx, sf.state, height)
if err != nil {
return nil, err
}
return &schedulerQuerier{state, regState}, nil
}
type schedulerQuerier struct {
state *schedulerState.ImmutableState
regState *registryState.ImmutableState
}
func (sq *schedulerQuerier) Validators(ctx context.Context) ([]*scheduler.Validator, error) {
vals, err := sq.state.CurrentValidators(ctx)
if err != nil {
return nil, err
}
ret := make([]*scheduler.Validator, 0, len(vals))
for v, power := range vals {
// The validator list uses consensus addresses, so convert them
// to node identifiers.
//
// This is probably better than switching the scheduler to use
// node identifiers for validators, because user queries are
// likely more infrequent than all the business of actually
// scheduling...
node, err := sq.regState.NodeBySubKey(ctx, v)
if err != nil {
// Should NEVER happen.
return nil, err
}
ret = append(ret, &scheduler.Validator{
ID: node.ID,
VotingPower: power,
})
}
return ret, nil
}
func (sq *schedulerQuerier) AllCommittees(ctx context.Context) ([]*scheduler.Committee, error) {
return sq.state.AllCommittees(ctx)
}
func (sq *schedulerQuerier) KindsCommittees(ctx context.Context, kinds []scheduler.CommitteeKind) ([]*scheduler.Committee, error) {
return sq.state.KindsCommittees(ctx, kinds)
}
func (sq *schedulerQuerier) ConsensusParameters(ctx context.Context) (*scheduler.ConsensusParameters, error) {
return sq.state.ConsensusParameters(ctx)
}
func (app *schedulerApplication) QueryFactory() interface{} {
return &QueryFactory{app.state}
}
// NewQueryFactory returns a new QueryFactory backed by the given state
// instance.
func NewQueryFactory(state abciAPI.ApplicationQueryState) *QueryFactory {
return &QueryFactory{state}
}