Merge pull request #2580 from gupadhyaya/get-validator-info-by-block

Get validator info by block
pull/2585/head
Leo Chen 5 years ago committed by GitHub
commit a0dad48abf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 11
      hmy/api_backend.go
  2. 2
      internal/hmyapi/apiv1/backend.go
  3. 47
      internal/hmyapi/apiv1/blockchain.go
  4. 2
      internal/hmyapi/apiv2/backend.go
  5. 47
      internal/hmyapi/apiv2/blockchain.go
  6. 2
      internal/hmyapi/backend.go

@ -332,15 +332,16 @@ var (
// GetValidatorInformation returns the information of validator
func (b *APIBackend) GetValidatorInformation(
addr common.Address,
addr common.Address, block *types.Block,
) (*staking.ValidatorRPCEnchanced, error) {
wrapper, err := b.hmy.BlockChain().ReadValidatorInformation(addr)
bc := b.hmy.BlockChain()
wrapper, err := bc.ReadValidatorInformationAt(addr, block.Root())
if err != nil {
s, _ := internal_common.AddressToBech32(addr)
return nil, errors.Wrapf(err, "not found address in current state %s", s)
}
now := b.hmy.BlockChain().CurrentHeader().Epoch()
now := block.Epoch()
inCommittee := now.Cmp(wrapper.LastEpochInCommittee) == 0
defaultReply := &staking.ValidatorRPCEnchanced{
CurrentlyInCommittee: inCommittee,
@ -358,7 +359,7 @@ func (b *APIBackend) GetValidatorInformation(
},
}
snapshot, err := b.hmy.BlockChain().ReadValidatorSnapshotAtEpoch(
snapshot, err := bc.ReadValidatorSnapshotAtEpoch(
now, addr,
)
@ -371,7 +372,7 @@ func (b *APIBackend) GetValidatorInformation(
)
computed.BlocksLeftInEpoch = shard.Schedule.BlocksPerEpoch() - computed.ToSign.Uint64()
stats, err := b.hmy.BlockChain().ReadValidatorStats(addr)
stats, err := bc.ReadValidatorStats(addr)
if err != nil {
return defaultReply, nil
}

@ -74,7 +74,7 @@ type Backend interface {
SendStakingTx(ctx context.Context, newStakingTx *staking.StakingTransaction) error
GetElectedValidatorAddresses() []common.Address
GetAllValidatorAddresses() []common.Address
GetValidatorInformation(addr common.Address) (*staking.ValidatorRPCEnchanced, error)
GetValidatorInformation(addr common.Address, block *types.Block) (*staking.ValidatorRPCEnchanced, error)
GetDelegationsByValidator(validator common.Address) []*staking.Delegation
GetDelegationsByDelegator(delegator common.Address) ([]common.Address, []*staking.Delegation)
GetValidatorSelfDelegation(addr common.Address) *big.Int

@ -568,15 +568,30 @@ func (s *PublicBlockChainAPI) GetElectedValidatorAddresses() ([]string, error) {
func (s *PublicBlockChainAPI) GetValidatorInformation(
ctx context.Context, address string,
) (*staking.ValidatorRPCEnchanced, error) {
block, err := s.b.BlockByNumber(ctx, rpc.BlockNumber(rpc.LatestBlockNumber))
if err != nil {
return nil, errors.Wrapf(err, "could not retrieve the latest block information")
}
return s.b.GetValidatorInformation(
internal_common.ParseAddr(address),
internal_common.ParseAddr(address), block,
)
}
// GetAllValidatorInformation returns information about all validators.
// If page is -1, return all instead of `validatorsPageSize` elements.
func (s *PublicBlockChainAPI) GetAllValidatorInformation(
ctx context.Context, page int,
// GetValidatorInformationByBlockNumber returns information about a validator.
func (s *PublicBlockChainAPI) GetValidatorInformationByBlockNumber(
ctx context.Context, address string, blockNr rpc.BlockNumber,
) (*staking.ValidatorRPCEnchanced, error) {
block, err := s.b.BlockByNumber(ctx, rpc.BlockNumber(blockNr))
if err != nil {
return nil, errors.Wrapf(err, "could not retrieve the block information for block number: %d", blockNr)
}
return s.b.GetValidatorInformation(
internal_common.ParseAddr(address), block,
)
}
func (s *PublicBlockChainAPI) getAllValidatorInformation(
ctx context.Context, page int, blockNr rpc.BlockNumber,
) ([]*staking.ValidatorRPCEnchanced, error) {
if page < -1 {
return nil, errors.Errorf("page given %d cannot be less than -1", page)
@ -595,8 +610,12 @@ func (s *PublicBlockChainAPI) GetAllValidatorInformation(
}
}
validators := make([]*staking.ValidatorRPCEnchanced, validatorsNum)
block, err := s.b.BlockByNumber(ctx, rpc.BlockNumber(blockNr))
if err != nil {
return nil, errors.Wrapf(err, "could not retrieve the block information for block number: %d", blockNr)
}
for i := start; i < start+validatorsNum; i++ {
information, err := s.b.GetValidatorInformation(addresses[i])
information, err := s.b.GetValidatorInformation(addresses[i], block)
if err != nil {
return nil, err
}
@ -605,6 +624,22 @@ func (s *PublicBlockChainAPI) GetAllValidatorInformation(
return validators, nil
}
// GetAllValidatorInformation returns information about all validators.
// If page is -1, return all instead of `validatorsPageSize` elements.
func (s *PublicBlockChainAPI) GetAllValidatorInformation(
ctx context.Context, page int,
) ([]*staking.ValidatorRPCEnchanced, error) {
return s.getAllValidatorInformation(ctx, page, rpc.LatestBlockNumber)
}
// GetAllValidatorInformationByBlockNumber returns information about all validators.
// If page is -1, return all instead of `validatorsPageSize` elements.
func (s *PublicBlockChainAPI) GetAllValidatorInformationByBlockNumber(
ctx context.Context, page int, blockNr rpc.BlockNumber,
) ([]*staking.ValidatorRPCEnchanced, error) {
return s.getAllValidatorInformation(ctx, page, blockNr)
}
// GetAllDelegationInformation returns delegation information about `validatorsPageSize` validators,
// starting at `page*validatorsPageSize`.
// If page is -1, return all instead of `validatorsPageSize` elements.

@ -74,7 +74,7 @@ type Backend interface {
SendStakingTx(ctx context.Context, newStakingTx *staking.StakingTransaction) error
GetElectedValidatorAddresses() []common.Address
GetAllValidatorAddresses() []common.Address
GetValidatorInformation(addr common.Address) (*staking.ValidatorRPCEnchanced, error)
GetValidatorInformation(addr common.Address, block *types.Block) (*staking.ValidatorRPCEnchanced, error)
GetDelegationsByValidator(validator common.Address) []*staking.Delegation
GetDelegationsByDelegator(delegator common.Address) ([]common.Address, []*staking.Delegation)
GetValidatorSelfDelegation(addr common.Address) *big.Int

@ -533,15 +533,30 @@ func (s *PublicBlockChainAPI) GetElectedValidatorAddresses() ([]string, error) {
func (s *PublicBlockChainAPI) GetValidatorInformation(
ctx context.Context, address string,
) (*staking.ValidatorRPCEnchanced, error) {
block, err := s.b.BlockByNumber(ctx, rpc.BlockNumber(rpc.LatestBlockNumber))
if err != nil {
return nil, errors.Wrapf(err, "could not retrieve the latest block information")
}
return s.b.GetValidatorInformation(
internal_common.ParseAddr(address),
internal_common.ParseAddr(address), block,
)
}
// GetAllValidatorInformation returns information about all validators.
// If page is -1, return all else return the pagination.
func (s *PublicBlockChainAPI) GetAllValidatorInformation(
ctx context.Context, page int,
// GetValidatorInformationByBlockNumber ..
func (s *PublicBlockChainAPI) GetValidatorInformationByBlockNumber(
ctx context.Context, address string, blockNr rpc.BlockNumber,
) (*staking.ValidatorRPCEnchanced, error) {
block, err := s.b.BlockByNumber(ctx, rpc.BlockNumber(blockNr))
if err != nil {
return nil, errors.Wrapf(err, "could not retrieve the block information for block number: %d", blockNr)
}
return s.b.GetValidatorInformation(
internal_common.ParseAddr(address), block,
)
}
func (s *PublicBlockChainAPI) getAllValidatorInformation(
ctx context.Context, page int, blockNr rpc.BlockNumber,
) ([]*staking.ValidatorRPCEnchanced, error) {
if page < -1 {
return nil, errors.Errorf("page given %d cannot be less than -1", page)
@ -560,8 +575,12 @@ func (s *PublicBlockChainAPI) GetAllValidatorInformation(
}
}
validators := make([]*staking.ValidatorRPCEnchanced, validatorsNum)
block, err := s.b.BlockByNumber(ctx, rpc.BlockNumber(blockNr))
if err != nil {
return nil, errors.Wrapf(err, "could not retrieve the block information for block number: %d", blockNr)
}
for i := start; i < start+validatorsNum; i++ {
information, err := s.b.GetValidatorInformation(addresses[i])
information, err := s.b.GetValidatorInformation(addresses[i], block)
if err != nil {
return nil, err
}
@ -570,6 +589,22 @@ func (s *PublicBlockChainAPI) GetAllValidatorInformation(
return validators, nil
}
// GetAllValidatorInformation returns information about all validators.
// If page is -1, return all else return the pagination.
func (s *PublicBlockChainAPI) GetAllValidatorInformation(
ctx context.Context, page int,
) ([]*staking.ValidatorRPCEnchanced, error) {
return s.getAllValidatorInformation(ctx, page, rpc.LatestBlockNumber)
}
// GetAllValidatorInformationByBlockNumber returns information about all validators.
// If page is -1, return all else return the pagination.
func (s *PublicBlockChainAPI) GetAllValidatorInformationByBlockNumber(
ctx context.Context, page int, blockNr rpc.BlockNumber,
) ([]*staking.ValidatorRPCEnchanced, error) {
return s.getAllValidatorInformation(ctx, page, blockNr)
}
// GetAllDelegationInformation returns delegation information about `validatorsPageSize` validators,
// starting at `page*validatorsPageSize`.
// If page is -1, return all instead of `validatorsPageSize` elements.

@ -76,7 +76,7 @@ type Backend interface {
SendStakingTx(ctx context.Context, newStakingTx *staking.StakingTransaction) error
GetElectedValidatorAddresses() []common.Address
GetAllValidatorAddresses() []common.Address
GetValidatorInformation(addr common.Address) (*staking.ValidatorRPCEnchanced, error)
GetValidatorInformation(addr common.Address, block *types.Block) (*staking.ValidatorRPCEnchanced, error)
GetDelegationsByValidator(validator common.Address) []*staking.Delegation
GetDelegationsByDelegator(delegator common.Address) ([]common.Address, []*staking.Delegation)
GetValidatorSelfDelegation(addr common.Address) *big.Int

Loading…
Cancel
Save