Skip to content

Commit

Permalink
go/staking: add ConensusParameters query method
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrus committed Mar 24, 2020
1 parent ea2c74d commit 28edc2e
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
5 changes: 5 additions & 0 deletions go/consensus/tendermint/apps/staking/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type Query interface {
Delegations(context.Context, signature.PublicKey) (map[signature.PublicKey]*staking.Delegation, error)
DebondingDelegations(context.Context, signature.PublicKey) (map[signature.PublicKey][]*staking.DebondingDelegation, error)
Genesis(context.Context) (*staking.Genesis, error)
ConsensusParameters(context.Context) (*staking.ConsensusParameters, error)
}

// QueryFactory is the staking query factory.
Expand Down Expand Up @@ -105,6 +106,10 @@ func (sq *stakingQuerier) DebondingDelegations(ctx context.Context, id signature
return sq.state.DebondingDelegationsFor(id)
}

func (sq *stakingQuerier) ConsensusParameters(ctx context.Context) (*staking.ConsensusParameters, error) {
return sq.state.ConsensusParameters()
}

func (app *stakingApplication) QueryFactory() interface{} {
return &QueryFactory{app}
}
8 changes: 8 additions & 0 deletions go/consensus/tendermint/staking/staking.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,14 @@ func (tb *tendermintBackend) StateToGenesis(ctx context.Context, height int64) (
return q.Genesis(ctx)
}

func (tb *tendermintBackend) ConsensusParameters(ctx context.Context, height int64) (*api.ConsensusParameters, error) {
q, err := tb.querier.QueryAt(ctx, height)
if err != nil {
return nil, err
}

return q.ConsensusParameters(ctx)
}
func (tb *tendermintBackend) Cleanup() {
<-tb.closedCh
}
Expand Down
3 changes: 3 additions & 0 deletions go/staking/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ type Backend interface {
// StateToGenesis returns the genesis state at specified block height.
StateToGenesis(ctx context.Context, height int64) (*Genesis, error)

// Paremeters returns the staking consensus parameters.
ConsensusParameters(ctx context.Context, height int64) (*ConsensusParameters, error)

// WatchTransfers returns a channel that produces a stream of TranserEvent
// on all balance transfers.
WatchTransfers(ctx context.Context) (<-chan *TransferEvent, pubsub.ClosableSubscription, error)
Expand Down
37 changes: 37 additions & 0 deletions go/staking/api/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ var (
methodDebondingDelegations = serviceName.NewMethod("DebondingDelegations", OwnerQuery{})
// methodStateToGenesis is the StateToGenesis method.
methodStateToGenesis = serviceName.NewMethod("StateToGenesis", int64(0))
// methodConsensusParameters is the ConsensusParameters method.
methodConsensusParameters = serviceName.NewMethod("ConsensusParameters", int64(0))

// methodWatchTransfers is the WatchTransfers method.
methodWatchTransfers = serviceName.NewMethod("WatchTransfers", nil)
Expand Down Expand Up @@ -82,6 +84,10 @@ var (
MethodName: methodStateToGenesis.ShortName(),
Handler: handlerStateToGenesis,
},
{
MethodName: methodConsensusParameters.ShortName(),
Handler: handlerConsensusParameters,
},
},
Streams: []grpc.StreamDesc{
{
Expand Down Expand Up @@ -310,6 +316,29 @@ func handlerStateToGenesis( // nolint: golint
return interceptor(ctx, height, info, handler)
}

func handlerConsensusParameters( // nolint: golint
srv interface{},
ctx context.Context,
dec func(interface{}) error,
interceptor grpc.UnaryServerInterceptor,
) (interface{}, error) {
var height int64
if err := dec(&height); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(Backend).ConsensusParameters(ctx, height)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: methodConsensusParameters.FullName(),
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(Backend).ConsensusParameters(ctx, req.(int64))
}
return interceptor(ctx, height, info, handler)
}

func handlerWatchTransfers(srv interface{}, stream grpc.ServerStream) error {
if err := stream.RecvMsg(nil); err != nil {
return err
Expand Down Expand Up @@ -475,6 +504,14 @@ func (c *stakingClient) StateToGenesis(ctx context.Context, height int64) (*Gene
return &rsp, nil
}

func (c *stakingClient) ConsensusParameters(ctx context.Context, height int64) (*ConsensusParameters, error) {
var rsp ConsensusParameters
if err := c.conn.Invoke(ctx, methodConsensusParameters.FullName(), height, &rsp); err != nil {
return nil, err
}
return &rsp, nil
}

func (c *stakingClient) WatchTransfers(ctx context.Context) (<-chan *TransferEvent, pubsub.ClosableSubscription, error) {
ctx, sub := pubsub.NewContextSubscription(ctx)

Expand Down

0 comments on commit 28edc2e

Please sign in to comment.