This repository has been archived by the owner on May 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 346
/
query_server.go
188 lines (165 loc) · 5.3 KB
/
query_server.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package rpcquery
import (
"context"
"fmt"
"github.com/hyperledger/burrow/acm"
"github.com/hyperledger/burrow/acm/acmstate"
"github.com/hyperledger/burrow/acm/validator"
"github.com/hyperledger/burrow/bcm"
"github.com/hyperledger/burrow/consensus/tendermint"
"github.com/hyperledger/burrow/event/query"
"github.com/hyperledger/burrow/execution/names"
"github.com/hyperledger/burrow/execution/proposal"
"github.com/hyperledger/burrow/execution/state"
"github.com/hyperledger/burrow/logging"
"github.com/hyperledger/burrow/rpc"
"github.com/hyperledger/burrow/txs/payload"
"github.com/tendermint/tendermint/abci/types"
tmtypes "github.com/tendermint/tendermint/types"
)
type queryServer struct {
accounts acmstate.IterableStatsReader
nameReg names.IterableReader
proposalReg proposal.IterableReader
blockchain bcm.BlockchainInfo
validators validator.History
nodeView *tendermint.NodeView
logger *logging.Logger
}
var _ QueryServer = &queryServer{}
func NewQueryServer(state acmstate.IterableStatsReader, nameReg names.IterableReader, proposalReg proposal.IterableReader,
blockchain bcm.BlockchainInfo, validators validator.History, nodeView *tendermint.NodeView, logger *logging.Logger) *queryServer {
return &queryServer{
accounts: state,
nameReg: nameReg,
proposalReg: proposalReg,
blockchain: blockchain,
validators: validators,
nodeView: nodeView,
logger: logger,
}
}
func (qs *queryServer) Status(ctx context.Context, param *StatusParam) (*rpc.ResultStatus, error) {
return rpc.Status(qs.blockchain, qs.validators, qs.nodeView, param.BlockTimeWithin, param.BlockSeenTimeWithin)
}
// Account state
func (qs *queryServer) GetAccount(ctx context.Context, param *GetAccountParam) (*acm.Account, error) {
acc, err := qs.accounts.GetAccount(param.Address)
if acc == nil {
acc = &acm.Account{}
}
return acc, err
}
func (qs *queryServer) GetStorage(ctx context.Context, param *GetStorageParam) (*StorageValue, error) {
val, err := qs.accounts.GetStorage(param.Address, param.Key)
return &StorageValue{Value: val}, err
}
func (qs *queryServer) ListAccounts(param *ListAccountsParam, stream Query_ListAccountsServer) error {
qry, err := query.NewOrEmpty(param.Query)
var streamErr error
err = qs.accounts.IterateAccounts(func(acc *acm.Account) error {
if qry.Matches(acc.Tagged()) {
return stream.Send(acc)
} else {
return nil
}
})
if err != nil {
return err
}
return streamErr
}
// Names
func (qs *queryServer) GetName(ctx context.Context, param *GetNameParam) (entry *names.Entry, err error) {
entry, err = qs.nameReg.GetName(param.Name)
if entry == nil && err == nil {
err = fmt.Errorf("name %s not found", param.Name)
}
return
}
func (qs *queryServer) ListNames(param *ListNamesParam, stream Query_ListNamesServer) error {
qry, err := query.NewOrEmpty(param.Query)
if err != nil {
return err
}
var streamErr error
err = qs.nameReg.IterateNames(func(entry *names.Entry) error {
if qry.Matches(entry.Tagged()) {
return stream.Send(entry)
} else {
return nil
}
})
if err != nil {
return err
}
return streamErr
}
// Validators
func (qs *queryServer) GetValidatorSet(ctx context.Context, param *GetValidatorSetParam) (*ValidatorSet, error) {
set := validator.Copy(qs.validators.Validators(0))
return &ValidatorSet{
Set: set.Validators(),
}, nil
}
func (qs *queryServer) GetValidatorSetHistory(ctx context.Context, param *GetValidatorSetHistoryParam) (*ValidatorSetHistory, error) {
lookback := int(param.IncludePrevious)
switch {
case lookback == 0:
lookback = 1
case lookback < 0 || lookback > state.DefaultValidatorsWindowSize:
lookback = state.DefaultValidatorsWindowSize
}
height := qs.blockchain.LastBlockHeight()
if height < uint64(lookback) {
lookback = int(height)
}
history := &ValidatorSetHistory{}
for i := 0; i < lookback; i++ {
set := validator.Copy(qs.validators.Validators(i))
vs := &ValidatorSet{
Height: height - uint64(i),
Set: set.Validators(),
}
history.History = append(history.History, vs)
}
return history, nil
}
// proposals
func (qs *queryServer) GetProposal(ctx context.Context, param *GetProposalParam) (proposal *payload.Ballot, err error) {
proposal, err = qs.proposalReg.GetProposal(param.Hash)
if proposal == nil && err == nil {
err = fmt.Errorf("proposal %x not found", param.Hash)
}
return
}
func (qs *queryServer) ListProposals(param *ListProposalsParam, stream Query_ListProposalsServer) error {
var streamErr error
err := qs.proposalReg.IterateProposals(func(hash []byte, ballot *payload.Ballot) error {
if param.GetProposed() == false || ballot.ProposalState == payload.Ballot_PROPOSED {
return stream.Send(&ProposalResult{Hash: hash, Ballot: ballot})
} else {
return nil
}
})
if err != nil {
return err
}
return streamErr
}
func (qs *queryServer) GetStats(ctx context.Context, param *GetStatsParam) (*Stats, error) {
stats := qs.accounts.GetAccountStats()
return &Stats{
AccountsWithCode: stats.AccountsWithCode,
AccountsWithoutCode: stats.AccountsWithoutCode,
}, nil
}
// Tendermint and blocks
func (qs *queryServer) GetBlockHeader(ctx context.Context, param *GetBlockParam) (*types.Header, error) {
header, err := qs.blockchain.GetBlockHeader(param.Height)
if err != nil {
return nil, err
}
abciHeader := tmtypes.TM2PB.Header(header)
return &abciHeader, nil
}