Total staking

pull/2290/head
flicker-harmony 5 years ago
parent a653dabe1e
commit f4e3af9bf7
  1. 29
      hmy/api_backend.go
  2. 10
      hmy/backend.go
  3. 1
      internal/hmyapi/apiv1/backend.go
  4. 9
      internal/hmyapi/apiv1/blockchain.go
  5. 1
      internal/hmyapi/apiv2/backend.go
  6. 9
      internal/hmyapi/apiv2/blockchain.go
  7. 1
      internal/hmyapi/backend.go

@ -35,6 +35,11 @@ type APIBackend struct {
BlockHeight int64
MedianRawStake *big.Int
}
TotalStakingCache struct {
sync.Mutex
BlockHeight int64
TotalStaking *big.Int
}
}
// ChainDb ...
@ -377,6 +382,30 @@ func (b *APIBackend) GetMedianRawStakeSnapshot() *big.Int {
return b.MedianStakeCache.MedianRawStake
}
// GetTotalStakingSnapshot ..
func (b *APIBackend) GetTotalStakingSnapshot() *big.Int {
b.TotalStakingCache.Lock()
defer b.TotalStakingCache.Unlock()
if b.TotalStakingCache.BlockHeight != -1 && b.TotalStakingCache.BlockHeight > int64(rpc.LatestBlockNumber)-20 {
return b.TotalStakingCache.TotalStaking
}
b.TotalStakingCache.BlockHeight = int64(rpc.LatestBlockNumber)
candidates := b.hmy.BlockChain().ValidatorCandidates()
if len(candidates) == 0 {
b.TotalStakingCache.TotalStaking = big.NewInt(0)
return b.TotalStakingCache.TotalStaking
}
stakes := big.NewInt(0)
for i := range candidates {
validator, _ := b.hmy.BlockChain().ReadValidatorInformation(candidates[i])
for i := range validator.Delegations {
stakes.Add(stakes, validator.Delegations[i].Amount)
}
}
b.TotalStakingCache.TotalStaking = stakes
return b.TotalStakingCache.TotalStaking
}
// GetValidatorStats returns the stats of validator
func (b *APIBackend) GetValidatorStats(addr common.Address) *staking.ValidatorStats {
val, _ := b.hmy.BlockChain().ReadValidatorStats(addr)

@ -85,6 +85,16 @@ func New(
MedianRawStake: big.NewInt(0),
},
}
hmy.APIBackend = &APIBackend{hmy: hmy,
TotalStakingCache: struct {
sync.Mutex
BlockHeight int64
TotalStaking *big.Int
}{
BlockHeight: -1,
TotalStaking: big.NewInt(0),
},
}
return hmy, nil
}

@ -85,4 +85,5 @@ type Backend interface {
GetPendingCXReceipts() []*types.CXReceiptsProof
GetCurrentUtilityMetrics() (*network.UtilityMetric, error)
GetSuperCommittees() (*quorum.Transition, error)
GetTotalStakingSnapshot() *big.Int
}

@ -520,6 +520,15 @@ var (
errNotBeaconChainShard = errors.New("cannot call this rpc on non beaconchain node")
)
// GetTotalStaking returns total staking by validators, only meant to be called on beaconchain
// explorer node
func (s *PublicBlockChainAPI) GetTotalStaking() (*big.Int, error) {
if s.b.GetShardID() == shard.BeaconChainShardID {
return s.b.GetTotalStakingSnapshot(), nil
}
return nil, errNotBeaconChainShard
}
// GetMedianRawStakeSnapshot returns the raw median stake, only meant to be called on beaconchain
// explorer node
func (s *PublicBlockChainAPI) GetMedianRawStakeSnapshot() (*big.Int, error) {

@ -85,4 +85,5 @@ type Backend interface {
GetPendingCXReceipts() []*types.CXReceiptsProof
GetCurrentUtilityMetrics() (*network.UtilityMetric, error)
GetSuperCommittees() (*quorum.Transition, error)
GetTotalStakingSnapshot() *big.Int
}

@ -485,6 +485,15 @@ var (
errNotBeaconChainShard = errors.New("cannot call this rpc on non beaconchain node")
)
// GetTotalStaking returns total staking by validators, only meant to be called on beaconchain
// explorer node
func (s *PublicBlockChainAPI) GetTotalStaking() (*big.Int, error) {
if s.b.GetShardID() == shard.BeaconChainShardID {
return s.b.GetTotalStakingSnapshot(), nil
}
return nil, errNotBeaconChainShard
}
// GetMedianRawStakeSnapshot returns the raw median stake, only meant to be called on beaconchain
// explorer node
func (s *PublicBlockChainAPI) GetMedianRawStakeSnapshot() (*big.Int, error) {

@ -87,6 +87,7 @@ type Backend interface {
GetPendingCXReceipts() []*types.CXReceiptsProof
GetCurrentUtilityMetrics() (*network.UtilityMetric, error)
GetSuperCommittees() (*quorum.Transition, error)
GetTotalStakingSnapshot() *big.Int
}
// GetAPIs returns all the APIs.

Loading…
Cancel
Save