From 28edc2e8c43bd80e0ebb304f06d4d413ec0197a4 Mon Sep 17 00:00:00 2001 From: ptrus Date: Wed, 18 Mar 2020 16:18:38 +0100 Subject: [PATCH] go/staking: add ConensusParameters query method --- go/consensus/tendermint/apps/staking/query.go | 5 +++ go/consensus/tendermint/staking/staking.go | 8 ++++ go/staking/api/api.go | 3 ++ go/staking/api/grpc.go | 37 +++++++++++++++++++ 4 files changed, 53 insertions(+) diff --git a/go/consensus/tendermint/apps/staking/query.go b/go/consensus/tendermint/apps/staking/query.go index c6481cf9403..f8e5c58af21 100644 --- a/go/consensus/tendermint/apps/staking/query.go +++ b/go/consensus/tendermint/apps/staking/query.go @@ -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. @@ -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} } diff --git a/go/consensus/tendermint/staking/staking.go b/go/consensus/tendermint/staking/staking.go index b2f8430170b..a247c30b16d 100644 --- a/go/consensus/tendermint/staking/staking.go +++ b/go/consensus/tendermint/staking/staking.go @@ -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 } diff --git a/go/staking/api/api.go b/go/staking/api/api.go index da380ac1c47..835f0319171 100644 --- a/go/staking/api/api.go +++ b/go/staking/api/api.go @@ -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) diff --git a/go/staking/api/grpc.go b/go/staking/api/grpc.go index 9137e42e281..600ff7017cf 100644 --- a/go/staking/api/grpc.go +++ b/go/staking/api/grpc.go @@ -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) @@ -82,6 +84,10 @@ var ( MethodName: methodStateToGenesis.ShortName(), Handler: handlerStateToGenesis, }, + { + MethodName: methodConsensusParameters.ShortName(), + Handler: handlerConsensusParameters, + }, }, Streams: []grpc.StreamDesc{ { @@ -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 @@ -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)