Add new stacking api methods

pull/1745/head
flicker-harmony 5 years ago
parent b797a16742
commit 5dc5217021
  1. 33
      hmy/api_backend.go
  2. 2
      hmy/backend.go
  3. 9
      internal/hmyapi/backend.go
  4. 117
      internal/hmyapi/blockchain.go

@ -209,8 +209,8 @@ func (b *APIBackend) GetBalance(address common.Address) (*hexutil.Big, error) {
}
// GetTransactionsHistory returns list of transactions hashes of address.
func (b *APIBackend) GetTransactionsHistory(address string) ([]common.Hash, error) {
hashes, err := b.hmy.nodeAPI.GetTransactionsHistory(address)
func (b *APIBackend) GetTransactionsHistory(address, txType, order string) ([]common.Hash, error) {
hashes, err := b.hmy.nodeAPI.GetTransactionsHistory(address, txType, order)
return hashes, err
}
@ -241,8 +241,8 @@ func (b *APIBackend) GetShardID() uint32 {
return b.hmy.shardID
}
// GetCommittee returns committee for a particular epoch.
func (b *APIBackend) GetCommittee(epoch *big.Int) (*shard.Committee, error) {
// GetValidators returns validators for a particular epoch.
func (b *APIBackend) GetValidators(epoch *big.Int) (*shard.Committee, error) {
state, err := b.hmy.BlockChain().ReadShardState(epoch)
if err != nil {
return nil, err
@ -289,3 +289,28 @@ func (b *APIBackend) SendStakingTx(
b.hmy.nodeAPI.AddPendingStakingTransaction(newStakingTx)
return nil
}
// GetCurrentValidatorAddresses returns the address of active validators for current epoch
func (b *APIBackend) GetCurrentValidatorAddresses() []common.Address {
return b.hmy.BlockChain().CurrentValidatorAddresses()
}
// GetValidatorCandidates returns the up to date validator candidates for next epoch
func (b *APIBackend) GetValidatorCandidates() []common.Address {
return b.hmy.BlockChain().ValidatorCandidates()
}
// GetValidatorInformation returns the information of validator
func (b *APIBackend) GetValidatorInformation(addr common.Address) *staking.Validator {
return b.hmy.BlockChain().ValidatorInformation(addr)
}
// GetDelegatorsInformation returns up to date information of delegators of a given validator address
func (b *APIBackend) GetDelegatorsInformation(addr common.Address) []*staking.Delegation {
return b.hmy.BlockChain().DelegatorsInformation(addr)
}
// GetValidatorStakingWithDelegation returns the amount of staking after applying all delegated stakes
func (b *APIBackend) GetValidatorStakingWithDelegation(addr common.Address) *big.Int {
return b.hmy.BlockChain().ValidatorStakingWithDelegation(addr)
}

@ -50,7 +50,7 @@ type NodeAPI interface {
AccountManager() *accounts.Manager
GetBalanceOfAddress(address common.Address) (*big.Int, error)
GetNonceOfAddress(address common.Address) uint64
GetTransactionsHistory(address string) ([]common.Hash, error)
GetTransactionsHistory(address, txType, order string) ([]common.Hash, error)
IsCurrentlyLeader() bool
}

@ -64,8 +64,8 @@ type Backend interface {
CurrentBlock() *types.Block
// Get balance
GetBalance(address common.Address) (*hexutil.Big, error)
// Get committee for a particular epoch
GetCommittee(epoch *big.Int) (*shard.Committee, error)
// Get validators for a particular epoch
GetValidators(epoch *big.Int) (*shard.Committee, error)
GetShardID() uint32
// Get transactions history for an address
GetTransactionsHistory(address, txType, order string) ([]common.Hash, error)
@ -73,6 +73,11 @@ type Backend interface {
ResendCx(ctx context.Context, txID common.Hash) (uint64, bool)
IsLeader() bool
SendStakingTx(ctx context.Context, newStakingTx *staking.StakingTransaction) error
GetCurrentValidatorAddresses() []common.Address
GetValidatorCandidates() []common.Address
GetValidatorInformation(addr common.Address) *staking.Validator
GetDelegatorsInformation(addr common.Address) []*staking.Delegation
GetValidatorStakingWithDelegation(addr common.Address) *big.Int
}
// GetAPIs returns all the APIs.

@ -92,11 +92,11 @@ func (s *PublicBlockChainAPI) GetBlockByHash(ctx context.Context, blockHash comm
// GetBlocks method returns blocks in range blockStart, blockEnd just like GetBlockByNumber but all at once.
func (s *PublicBlockChainAPI) GetBlocks(ctx context.Context, blockStart rpc.BlockNumber, blockEnd rpc.BlockNumber, blockArgs BlockArgs) ([]map[string]interface{}, error) {
result := make([]map[string]interface{}, 0)
for i := blockStart; i < blockEnd; i++ {
for i := blockStart; i <= blockEnd; i++ {
block, err := s.b.BlockByNumber(ctx, i)
blockArgs.InclTx = true
if blockArgs.WithSigners {
blockArgs.Signers, err = s.GetBlockSigners(ctx, i)
blockArgs.Signers, err = s.GetBlockSigners(ctx, rpc.BlockNumber(i))
if err != nil {
return nil, err
}
@ -108,16 +108,16 @@ func (s *PublicBlockChainAPI) GetBlocks(ctx context.Context, blockStart rpc.Bloc
for _, field := range []string{"hash", "nonce", "miner"} {
rpcBlock[field] = nil
}
result = append(result, rpcBlock)
}
result = append(result, rpcBlock)
}
}
return result, nil
}
// GetCommittee returns committee for a particular epoch.
func (s *PublicBlockChainAPI) GetCommittee(ctx context.Context, epoch int64) (map[string]interface{}, error) {
committee, err := s.b.GetCommittee(big.NewInt(epoch))
// GetValidators returns validators list for a particular epoch.
func (s *PublicBlockChainAPI) GetValidators(ctx context.Context, epoch int64) (map[string]interface{}, error) {
committee, err := s.b.GetValidators(big.NewInt(epoch))
if err != nil {
return nil, err
}
@ -147,23 +147,35 @@ func (s *PublicBlockChainAPI) GetCommittee(ctx context.Context, epoch int64) (ma
// GetBlockSigners returns signers for a particular block.
func (s *PublicBlockChainAPI) GetBlockSigners(ctx context.Context, blockNr rpc.BlockNumber) ([]string, error) {
epoch := s.GetEpoch(ctx)
committee, err := s.b.GetCommittee(big.NewInt(int64(epoch)))
if uint64(blockNr) == 0 {
return make([]string, 0), nil
}
block, err := s.b.BlockByNumber(ctx, blockNr)
if err != nil {
return nil, err
}
blockWithSigners, err := s.b.BlockByNumber(ctx, blockNr+1)
if err != nil {
return nil, err
}
committee, err := s.b.GetValidators(block.Epoch())
if err != nil {
return nil, err
}
pubkeys := make([]*bls.PublicKey, len(committee.NodeList))
result := make([]string, 0)
for i, validator := range committee.NodeList {
pubkeys[i] = new(bls.PublicKey)
validator.BlsPublicKey.ToLibBLSPublicKey(pubkeys[i])
}
result := make([]string, 0)
mask, err := internal_bls.NewMask(pubkeys, nil)
if err != nil {
return result, err
}
block, err := s.b.BlockByNumber(ctx, blockNr+1)
if err != nil {
return result, err
}
err = mask.SetMask(block.Header().LastCommitBitmap())
err = mask.SetMask(blockWithSigners.Header().LastCommitBitmap())
if err != nil {
return result, err
}
@ -183,8 +195,18 @@ func (s *PublicBlockChainAPI) GetBlockSigners(ctx context.Context, blockNr rpc.B
// IsBlockSigner returns true if validator with address signed blockNr block.
func (s *PublicBlockChainAPI) IsBlockSigner(ctx context.Context, blockNr rpc.BlockNumber, address string) (bool, error) {
epoch := s.GetEpoch(ctx)
committee, err := s.b.GetCommittee(big.NewInt(int64(epoch)))
block, err := s.b.BlockByNumber(ctx, blockNr)
if err != nil {
return false, err
}
blockWithSigners, err := s.b.BlockByNumber(ctx, blockNr+1)
if err != nil {
return false, err
}
committee, err := s.b.GetValidators(block.Epoch())
if err != nil {
return false, err
}
pubkeys := make([]*bls.PublicKey, len(committee.NodeList))
for i, validator := range committee.NodeList {
pubkeys[i] = new(bls.PublicKey)
@ -194,11 +216,7 @@ func (s *PublicBlockChainAPI) IsBlockSigner(ctx context.Context, blockNr rpc.Blo
if err != nil {
return false, err
}
block, err := s.b.BlockByNumber(ctx, blockNr+1)
if err != nil {
return false, err
}
err = mask.SetMask(block.Header().LastCommitBitmap())
err = mask.SetMask(blockWithSigners.Header().LastCommitBitmap())
if err != nil {
return false, err
}
@ -220,7 +238,7 @@ func (s *PublicBlockChainAPI) IsBlockSigner(ctx context.Context, blockNr rpc.Blo
}
// GetSignedBlocks returns how many blocks a particular validator signed for last defaultBlocksPeriod (3 hours ~ 1500 blocks).
func (s *PublicBlockChainAPI) GetSignedBlocks(ctx context.Context, address string) uint64 {
func (s *PublicBlockChainAPI) GetSignedBlocks(ctx context.Context, address string) hexutil.Uint64 {
header := s.LatestHeader(ctx)
totalSigned := uint64(0)
lastBlock := uint64(0)
@ -233,12 +251,12 @@ func (s *PublicBlockChainAPI) GetSignedBlocks(ctx context.Context, address strin
totalSigned++
}
}
return totalSigned
return hexutil.Uint64(totalSigned)
}
// GetEpoch returns current epoch.
func (s *PublicBlockChainAPI) GetEpoch(ctx context.Context) uint64 {
return s.LatestHeader(ctx).Epoch
func (s *PublicBlockChainAPI) GetEpoch(ctx context.Context) hexutil.Uint64 {
return hexutil.Uint64(s.LatestHeader(ctx).Epoch)
}
// GetLeader returns current shard leader.
@ -246,6 +264,61 @@ func (s *PublicBlockChainAPI) GetLeader(ctx context.Context) string {
return s.LatestHeader(ctx).Leader
}
// GetValidatorInformation returns full validator info.
func (s *PublicBlockChainAPI) GetValidatorInformation(ctx context.Context, address string) (map[string]interface{}, error) {
validator := s.b.GetValidatorInformation(internal_common.ParseAddr(address))
fields := map[string]interface{}{
"address": validator.Address.String(),
"stake": hexutil.Uint64(validator.Stake.Uint64()),
"name": validator.Description.Name,
"validatingPublicKey": validator.ValidatingPubKey.SerializeToHexStr(),
"unbondingHeight": hexutil.Uint64(validator.UnbondingHeight.Uint64()),
"minSelfDelegation": hexutil.Uint64(validator.MinSelfDelegation.Uint64()),
"active": validator.Active,
"identity": validator.Description.Identity,
"commissionRate": hexutil.Uint64(validator.Commission.CommissionRates.Rate.Int.Uint64()),
"commissionUpdateHeight": hexutil.Uint64(validator.Commission.UpdateHeight.Uint64()),
"commissionMaxRate": hexutil.Uint64(validator.Commission.CommissionRates.MaxRate.Uint64()),
"commissionMaxChangeRate": hexutil.Uint64(validator.Commission.CommissionRates.MaxChangeRate.Uint64()),
"website": validator.Description.Website,
"securityContact": validator.Description.SecurityContact,
"details": validator.Description.Details,
}
return fields, nil
}
// GetStake returns validator stake.
func (s *PublicBlockChainAPI) GetStake(ctx context.Context, address string) hexutil.Uint64 {
validator := s.b.GetValidatorInformation(internal_common.ParseAddr(address))
return hexutil.Uint64(validator.Stake.Uint64())
}
// GetValidatorStakingAddress stacking address returns validator stacking address.
func (s *PublicBlockChainAPI) GetValidatorStakingAddress(ctx context.Context, address string) string {
validator := s.b.GetValidatorInformation(internal_common.ParseAddr(address))
return validator.Address.String()
}
// GetValidatorStakingWithDelegation returns total balace stacking for validator with delegation.
func (s *PublicBlockChainAPI) GetValidatorStakingWithDelegation(ctx context.Context, address string) hexutil.Uint64 {
return hexutil.Uint64(s.b.GetValidatorStakingWithDelegation(internal_common.ParseAddr(address)).Uint64())
}
// GetDelegatorsInformation returns list of delegators for a validator address.
func (s *PublicBlockChainAPI) GetDelegatorsInformation(ctx context.Context, address string) ([]map[string]interface{}, error) {
delegators := s.b.GetDelegatorsInformation(internal_common.ParseAddr(address))
delegatorsFields := make([]map[string]interface{}, 0)
for _, delegator := range delegators {
fields := map[string]interface{}{
"delegator": delegator.DelegatorAddress.String(),
"validator": delegator.ValidatorAddress.String(),
"amount": hexutil.Uint64(delegator.Amount.Uint64()),
}
delegatorsFields = append(delegatorsFields, fields)
}
return delegatorsFields, nil
}
// GetShardingStructure returns an array of sharding structures.
func (s *PublicBlockChainAPI) GetShardingStructure(ctx context.Context) ([]map[string]interface{}, error) {
// Get header and number of shards.

Loading…
Cancel
Save