[rpc] delegations by block & error checks (#2903)

* [rpc] Move all error definitions to error.go

* [core] Add function to read delegations from offchain data for a specific block
[rpc] Add GetDelegationsByDelegatorAt

* [rpc] Return error when calling all staking blockchain rpcs on auxillary shards

* [rpc] Rename to GetDelegationsByDelegatorByBlockNumber to follow naming conventions

* [rpc] Return error when requested block is greater than exists on chain

* [rpc] Fix lint errors

* [rpc] Fix check to not break on current block

* [rpc] Fix broken getBalance RPC
[rpc] Change getValidators to give balance at the end of the selected epoch or current block

* [rpc] Address PR comments

* [README] Update go version

* [rpc] Revert changes to Call

* [rpc] Move error checks to blockchain

* [rpc] Rename to ByBlock, because it takes a block as input

* [rpc] Small performance optimization for getting delegator information
pull/2949/head
Janet Liang 5 years ago committed by GitHub
parent b3e1d61a26
commit 60ec94b9fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      README.md
  2. 27
      core/blockchain.go
  3. 21
      hmy/api_backend.go
  4. 1
      internal/hmyapi/apiv1/backend.go
  5. 208
      internal/hmyapi/apiv1/blockchain.go
  6. 3
      internal/hmyapi/apiv1/debug.go
  7. 16
      internal/hmyapi/apiv1/error.go
  8. 9
      internal/hmyapi/apiv1/transactionpool.go
  9. 1
      internal/hmyapi/apiv2/backend.go
  10. 213
      internal/hmyapi/apiv2/blockchain.go
  11. 3
      internal/hmyapi/apiv2/debug.go
  12. 16
      internal/hmyapi/apiv2/error.go
  13. 9
      internal/hmyapi/apiv2/transactionpool.go
  14. 1
      internal/hmyapi/backend.go

@ -112,7 +112,7 @@ based library dependencies (`libbls` and `mcl`) setup correctly for you. You can
versions easily, an example:
```
$ eval $(gimme 1.13.6)
$ eval $(gimme 1.14.1)
```
Note that changing the go version might mean that dependencies won't work out right when trying to
@ -129,7 +129,7 @@ brew install openssl
## Dev Environment Setup
The required go version is: **go1.13.6**
The required go version is: **go1.14.1**
```bash
export GOPATH=$HOME/<path_of_your_choice>

@ -2500,6 +2500,33 @@ func (bc *BlockChain) ReadDelegationsByDelegator(
return m, nil
}
// ReadDelegationsByDelegatorAt reads the addresses of validators delegated by a delegator at a given block
func (bc *BlockChain) ReadDelegationsByDelegatorAt(
delegator common.Address, blockNum *big.Int,
) (m staking.DelegationIndexes, err error) {
rawResult := staking.DelegationIndexes{}
if cached, ok := bc.validatorListByDelegatorCache.Get(string(delegator.Bytes())); ok {
by := cached.([]byte)
if err := rlp.DecodeBytes(by, &rawResult); err != nil {
return nil, err
}
} else {
if rawResult, err = rawdb.ReadDelegationsByDelegator(bc.db, delegator); err != nil {
return nil, err
}
}
for _, index := range rawResult {
if index.BlockNum.Cmp(blockNum) <= 0 {
m = append(m, index)
} else {
// Filter out index that's created beyond current height of chain.
// This only happens when there is a chain rollback.
utils.Logger().Warn().Msgf("Future delegation index encountered. Skip: %+v", index)
}
}
return m, nil
}
// writeDelegationsByDelegator writes the list of validator addresses to database
func (bc *BlockChain) writeDelegationsByDelegator(
batch rawdb.DatabaseWriter,

@ -574,20 +574,21 @@ func (b *APIBackend) GetDelegationsByValidator(validator common.Address) []*stak
return delegations
}
// GetDelegationsByDelegator returns all delegation information of a delegator
func (b *APIBackend) GetDelegationsByDelegator(
delegator common.Address,
// GetDelegationsByDelegatorByBlock returns all delegation information of a delegator
func (b *APIBackend) GetDelegationsByDelegatorByBlock(
delegator common.Address, block *types.Block,
) ([]common.Address, []*staking.Delegation) {
addresses := []common.Address{}
delegations := []*staking.Delegation{}
delegationIndexes, err := b.hmy.BlockChain().ReadDelegationsByDelegator(delegator)
delegationIndexes, err := b.hmy.BlockChain().
ReadDelegationsByDelegatorAt(delegator, block.Number())
if err != nil {
return nil, nil
}
for i := range delegationIndexes {
wrapper, err := b.hmy.BlockChain().ReadValidatorInformation(
delegationIndexes[i].ValidatorAddress,
wrapper, err := b.hmy.BlockChain().ReadValidatorInformationAt(
delegationIndexes[i].ValidatorAddress, block.Root(),
)
if err != nil || wrapper == nil {
return nil, nil
@ -603,6 +604,14 @@ func (b *APIBackend) GetDelegationsByDelegator(
return addresses, delegations
}
// GetDelegationsByDelegator returns all delegation information of a delegator
func (b *APIBackend) GetDelegationsByDelegator(
delegator common.Address,
) ([]common.Address, []*staking.Delegation) {
block := b.hmy.BlockChain().CurrentBlock()
return b.GetDelegationsByDelegatorByBlock(delegator, block)
}
// GetValidatorSelfDelegation returns the amount of staking after applying all delegated stakes
func (b *APIBackend) GetValidatorSelfDelegation(addr common.Address) *big.Int {
wrapper, err := b.hmy.BlockChain().ReadValidatorInformation(addr)

@ -77,6 +77,7 @@ type Backend interface {
GetValidatorInformation(addr common.Address, block *types.Block) (*staking.ValidatorRPCEnchanced, error)
GetDelegationsByValidator(validator common.Address) []*staking.Delegation
GetDelegationsByDelegator(delegator common.Address) ([]common.Address, []*staking.Delegation)
GetDelegationsByDelegatorByBlock(delegator common.Address, block *types.Block) ([]common.Address, []*staking.Delegation)
GetValidatorSelfDelegation(addr common.Address) *big.Int
GetShardState() (*shard.State, error)
GetCurrentStakingErrorSink() types.TransactionErrorReports

@ -58,9 +58,26 @@ type BlockArgs struct {
InclStaking bool `json:"inclStaking"`
}
func (s *PublicBlockChainAPI) isBeaconShard() error {
if s.b.GetShardID() != shard.BeaconChainShardID {
return ErrNotBeaconShard
}
return nil
}
func (s *PublicBlockChainAPI) isBlockGreaterThanLatest(blockNum rpc.BlockNumber) error {
if uint64(blockNum) > s.b.CurrentBlock().NumberU64() {
return ErrRequestedBlockTooHigh
}
return nil
}
// GetBlockByNumber returns the requested block. When blockNr is -1 the chain head is returned. When fullTx is true all
// transactions in the block are returned in full detail, otherwise only the transaction hash is returned.
func (s *PublicBlockChainAPI) GetBlockByNumber(ctx context.Context, blockNr rpc.BlockNumber, fullTx bool) (map[string]interface{}, error) {
if err := s.isBlockGreaterThanLatest(blockNr); err != nil {
return nil, err
}
block, err := s.b.BlockByNumber(ctx, blockNr)
if block != nil {
blockArgs := BlockArgs{WithSigners: false, InclTx: true, FullTx: fullTx, InclStaking: true}
@ -91,6 +108,9 @@ func (s *PublicBlockChainAPI) GetBlockByHash(ctx context.Context, blockHash comm
// transactions in the block are returned in full detail, otherwise only the transaction hash is returned. When withSigners in BlocksArgs is true
// it shows block signers for this block in list of one addresses.
func (s *PublicBlockChainAPI) GetBlockByNumberNew(ctx context.Context, blockNr rpc.BlockNumber, blockArgs BlockArgs) (map[string]interface{}, error) {
if err := s.isBlockGreaterThanLatest(blockNr); err != nil {
return nil, err
}
block, err := s.b.BlockByNumber(ctx, blockNr)
blockArgs.InclTx = true
if blockArgs.WithSigners {
@ -158,17 +178,24 @@ func (s *PublicBlockChainAPI) GetBlocks(ctx context.Context, blockStart rpc.Bloc
// GetValidators returns validators list for a particular epoch.
func (s *PublicBlockChainAPI) GetValidators(ctx context.Context, epoch int64) (map[string]interface{}, error) {
if err := s.isBeaconShard(); err != nil {
return nil, err
}
committee, err := s.b.GetValidators(big.NewInt(epoch))
if err != nil {
return nil, err
}
balanceQueryBlock := shard.Schedule.EpochLastBlock(uint64(epoch))
if balanceQueryBlock > s.b.CurrentBlock().NumberU64() {
balanceQueryBlock = s.b.CurrentBlock().NumberU64()
}
validators := make([]map[string]interface{}, 0)
for _, validator := range committee.Slots {
oneAddress, err := internal_common.AddressToBech32(validator.EcdsaAddress)
if err != nil {
return nil, err
}
validatorBalance, err := s.GetBalance(ctx, oneAddress, rpc.LatestBlockNumber)
validatorBalance, err := s.getBalanceByBlockNumber(ctx, oneAddress, rpc.BlockNumber(balanceQueryBlock))
if err != nil {
return nil, err
}
@ -187,24 +214,27 @@ func (s *PublicBlockChainAPI) GetValidators(ctx context.Context, epoch int64) (m
// IsLastBlock checks if block is last epoch block.
func (s *PublicBlockChainAPI) IsLastBlock(blockNum uint64) (bool, error) {
if s.b.GetShardID() == shard.BeaconChainShardID {
return shard.Schedule.IsLastBlock(blockNum), nil
if err := s.isBeaconShard(); err != nil {
return false, err
}
return false, errNotBeaconChainShard
return shard.Schedule.IsLastBlock(blockNum), nil
}
// EpochLastBlock returns epoch last block.
func (s *PublicBlockChainAPI) EpochLastBlock(epoch uint64) (uint64, error) {
if s.b.GetShardID() == shard.BeaconChainShardID {
return shard.Schedule.EpochLastBlock(epoch), nil
if err := s.isBeaconShard(); err != nil {
return 0, err
}
return 0, errNotBeaconChainShard
return shard.Schedule.EpochLastBlock(epoch), nil
}
// GetBlockSigners returns signers for a particular block.
func (s *PublicBlockChainAPI) GetBlockSigners(ctx context.Context, blockNr rpc.BlockNumber) ([]string, error) {
if uint64(blockNr) == 0 || uint64(blockNr) >= uint64(s.BlockNumber()) {
return make([]string, 0), nil
if uint64(blockNr) == 0 {
return []string{}, nil
}
if err := s.isBlockGreaterThanLatest(blockNr); err != nil {
return nil, err
}
block, err := s.b.BlockByNumber(ctx, blockNr)
if err != nil {
@ -223,22 +253,19 @@ func (s *PublicBlockChainAPI) GetBlockSigners(ctx context.Context, blockNr rpc.B
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
}
if err != nil {
return result, err
return nil, err
}
err = mask.SetMask(blockWithSigners.Header().LastCommitBitmap())
if err != nil {
return result, err
return nil, err
}
result := []string{}
for _, validator := range committee.Slots {
oneAddress, err := internal_common.AddressToBech32(validator.EcdsaAddress)
if err != nil {
return result, err
return nil, err
}
blsPublicKey := new(bls.PublicKey)
validator.BLSPublicKey.ToLibBLSPublicKey(blsPublicKey)
@ -251,9 +278,12 @@ 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) {
if uint64(blockNr) == 0 || uint64(blockNr) >= uint64(s.BlockNumber()) {
if uint64(blockNr) == 0 {
return false, nil
}
if err := s.isBlockGreaterThanLatest(blockNr); err != nil {
return false, err
}
block, err := s.b.BlockByNumber(ctx, blockNr)
if err != nil {
return false, err
@ -324,19 +354,25 @@ func (s *PublicBlockChainAPI) GetLeader(ctx context.Context) string {
}
// GetValidatorSelfDelegation returns validator stake.
func (s *PublicBlockChainAPI) GetValidatorSelfDelegation(ctx context.Context, address string) hexutil.Uint64 {
return hexutil.Uint64(s.b.GetValidatorSelfDelegation(internal_common.ParseAddr(address)).Uint64())
func (s *PublicBlockChainAPI) GetValidatorSelfDelegation(ctx context.Context, address string) (hexutil.Uint64, error) {
if err := s.isBeaconShard(); err != nil {
return hexutil.Uint64(0), err
}
return hexutil.Uint64(s.b.GetValidatorSelfDelegation(internal_common.ParseAddr(address)).Uint64()), nil
}
// GetValidatorTotalDelegation returns total balace stacking for validator with delegation.
func (s *PublicBlockChainAPI) GetValidatorTotalDelegation(ctx context.Context, address string) hexutil.Uint64 {
func (s *PublicBlockChainAPI) GetValidatorTotalDelegation(ctx context.Context, address string) (hexutil.Uint64, error) {
if err := s.isBeaconShard(); err != nil {
return hexutil.Uint64(0), err
}
delegations := s.b.GetDelegationsByValidator(internal_common.ParseAddr(address))
totalStake := big.NewInt(0)
for _, delegation := range delegations {
totalStake.Add(totalStake, delegation.Amount)
}
// TODO: return more than uint64
return hexutil.Uint64(totalStake.Uint64())
return hexutil.Uint64(totalStake.Uint64()), nil
}
// GetShardingStructure returns an array of sharding structures.
@ -378,8 +414,7 @@ func (s *PublicBlockChainAPI) GetStorageAt(ctx context.Context, addr string, key
return res[:], state.Error()
}
// GetBalanceByBlockNumber returns balance by block number.
func (s *PublicBlockChainAPI) GetBalanceByBlockNumber(ctx context.Context, address string, blockNr rpc.BlockNumber) (*hexutil.Big, error) {
func (s *PublicBlockChainAPI) getBalanceByBlockNumber(ctx context.Context, address string, blockNr rpc.BlockNumber) (*hexutil.Big, error) {
addr := internal_common.ParseAddr(address)
balance, err := s.b.GetBalance(ctx, addr, blockNr)
if balance == nil {
@ -388,6 +423,14 @@ func (s *PublicBlockChainAPI) GetBalanceByBlockNumber(ctx context.Context, addre
return (*hexutil.Big)(balance), err
}
// GetBalanceByBlockNumber returns balance by block number.
func (s *PublicBlockChainAPI) GetBalanceByBlockNumber(ctx context.Context, address string, blockNr rpc.BlockNumber) (*hexutil.Big, error) {
if err := s.isBlockGreaterThanLatest(blockNr); err != nil {
return nil, err
}
return s.getBalanceByBlockNumber(ctx, address, blockNr)
}
// GetAccountNonce returns the nonce value of the given address for the given block number
func (s *PublicBlockChainAPI) GetAccountNonce(ctx context.Context, address string, blockNr rpc.BlockNumber) (uint64, error) {
addr := internal_common.ParseAddr(address)
@ -398,7 +441,7 @@ func (s *PublicBlockChainAPI) GetAccountNonce(ctx context.Context, address strin
// given block number. The rpc.LatestBlockNumber and rpc.PendingBlockNumber meta
// block numbers are also allowed.
func (s *PublicBlockChainAPI) GetBalance(ctx context.Context, address string, blockNr rpc.BlockNumber) (*hexutil.Big, error) {
return s.GetBalanceByBlockNumber(ctx, address, rpc.LatestBlockNumber)
return s.getBalanceByBlockNumber(ctx, address, rpc.BlockNumber(blockNr))
}
// BlockNumber returns the block number of the chain head.
@ -527,17 +570,13 @@ func (s *PublicBlockChainAPI) LatestHeader(ctx context.Context) *HeaderInformati
return newHeaderInformation(header)
}
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
if err := s.isBeaconShard(); err != nil {
return nil, err
}
return nil, errNotBeaconChainShard
return s.b.GetTotalStakingSnapshot(), nil
}
// GetMedianRawStakeSnapshot returns the raw median stake, only meant to be called on beaconchain
@ -545,10 +584,10 @@ func (s *PublicBlockChainAPI) GetTotalStaking() (*big.Int, error) {
func (s *PublicBlockChainAPI) GetMedianRawStakeSnapshot() (
*committee.CompletedEPoSRound, error,
) {
if s.b.GetShardID() == shard.BeaconChainShardID {
return s.b.GetMedianRawStakeSnapshot()
if err := s.isBeaconShard(); err != nil {
return nil, err
}
return nil, errNotBeaconChainShard
return s.b.GetMedianRawStakeSnapshot()
}
// GetLatestChainHeaders ..
@ -558,6 +597,9 @@ func (s *PublicBlockChainAPI) GetLatestChainHeaders() *block.HeaderPair {
// GetAllValidatorAddresses returns all validator addresses.
func (s *PublicBlockChainAPI) GetAllValidatorAddresses() ([]string, error) {
if err := s.isBeaconShard(); err != nil {
return nil, err
}
addresses := []string{}
for _, addr := range s.b.GetAllValidatorAddresses() {
oneAddr, _ := internal_common.AddressToBech32(addr)
@ -568,6 +610,9 @@ func (s *PublicBlockChainAPI) GetAllValidatorAddresses() ([]string, error) {
// GetElectedValidatorAddresses returns elected validator addresses.
func (s *PublicBlockChainAPI) GetElectedValidatorAddresses() ([]string, error) {
if err := s.isBeaconShard(); err != nil {
return nil, err
}
addresses := []string{}
for _, addr := range s.b.GetElectedValidatorAddresses() {
oneAddr, _ := internal_common.AddressToBech32(addr)
@ -580,6 +625,9 @@ func (s *PublicBlockChainAPI) GetElectedValidatorAddresses() ([]string, error) {
func (s *PublicBlockChainAPI) GetValidatorInformation(
ctx context.Context, address string,
) (*staking.ValidatorRPCEnchanced, error) {
if err := s.isBeaconShard(); err != nil {
return nil, err
}
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")
@ -593,6 +641,12 @@ func (s *PublicBlockChainAPI) GetValidatorInformation(
func (s *PublicBlockChainAPI) GetValidatorInformationByBlockNumber(
ctx context.Context, address string, blockNr rpc.BlockNumber,
) (*staking.ValidatorRPCEnchanced, error) {
if err := s.isBeaconShard(); err != nil {
return nil, err
}
if err := s.isBlockGreaterThanLatest(blockNr); err != nil {
return nil, err
}
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)
@ -641,6 +695,9 @@ func (s *PublicBlockChainAPI) getAllValidatorInformation(
func (s *PublicBlockChainAPI) GetAllValidatorInformation(
ctx context.Context, page int,
) ([]*staking.ValidatorRPCEnchanced, error) {
if err := s.isBeaconShard(); err != nil {
return nil, err
}
blockNr := s.b.CurrentBlock().NumberU64()
// delete cache for previous block
@ -665,6 +722,12 @@ func (s *PublicBlockChainAPI) GetAllValidatorInformation(
func (s *PublicBlockChainAPI) GetAllValidatorInformationByBlockNumber(
ctx context.Context, page int, blockNr rpc.BlockNumber,
) ([]*staking.ValidatorRPCEnchanced, error) {
if err := s.isBeaconShard(); err != nil {
return nil, err
}
if err := s.isBlockGreaterThanLatest(blockNr); err != nil {
return nil, err
}
return s.getAllValidatorInformation(ctx, page, blockNr)
}
@ -672,6 +735,9 @@ func (s *PublicBlockChainAPI) GetAllValidatorInformationByBlockNumber(
// starting at `page*validatorsPageSize`.
// If page is -1, return all instead of `validatorsPageSize` elements.
func (s *PublicBlockChainAPI) GetAllDelegationInformation(ctx context.Context, page int) ([][]*RPCDelegation, error) {
if err := s.isBeaconShard(); err != nil {
return nil, err
}
if page < -1 {
return make([][]*RPCDelegation, 0), nil
}
@ -701,13 +767,16 @@ func (s *PublicBlockChainAPI) GetAllDelegationInformation(ctx context.Context, p
// GetDelegationsByDelegator returns list of delegations for a delegator address.
func (s *PublicBlockChainAPI) GetDelegationsByDelegator(ctx context.Context, address string) ([]*RPCDelegation, error) {
if err := s.isBeaconShard(); err != nil {
return nil, err
}
delegatorAddress := internal_common.ParseAddr(address)
validators, delegations := s.b.GetDelegationsByDelegator(delegatorAddress)
result := []*RPCDelegation{}
for i := range delegations {
delegation := delegations[i]
undelegations := []RPCUndelegation{}
undelegations := make([]RPCUndelegation, len(delegation.Undelegations))
for j := range delegation.Undelegations {
undelegations = append(undelegations, RPCUndelegation{
@ -728,8 +797,52 @@ func (s *PublicBlockChainAPI) GetDelegationsByDelegator(ctx context.Context, add
return result, nil
}
// GetDelegationsByDelegatorByBlockNumber returns list of delegations for a delegator address at given block number
func (s *PublicBlockChainAPI) GetDelegationsByDelegatorByBlockNumber(
ctx context.Context, address string, blockNum rpc.BlockNumber,
) ([]*RPCDelegation, error) {
if err := s.isBeaconShard(); err != nil {
return nil, err
}
if err := s.isBlockGreaterThanLatest(blockNum); err != nil {
return nil, err
}
delegatorAddress := internal_common.ParseAddr(address)
block, err := s.b.BlockByNumber(ctx, rpc.BlockNumber(blockNum))
if err != nil {
return nil, errors.Wrapf(err, "could not retrieve the block information for block number: %d", blockNum)
}
validators, delegations := s.b.GetDelegationsByDelegatorByBlock(delegatorAddress, block)
result := make([]*RPCDelegation, len(delegations))
for i := range delegations {
delegation := delegations[i]
undelegations := make([]RPCUndelegation, len(delegation.Undelegations))
for j := range delegation.Undelegations {
undelegations[j] = RPCUndelegation{
delegation.Undelegations[j].Amount,
delegation.Undelegations[j].Epoch,
}
}
valAddr, _ := internal_common.AddressToBech32(validators[i])
delAddr, _ := internal_common.AddressToBech32(delegatorAddress)
result[i] = &RPCDelegation{
valAddr,
delAddr,
delegation.Amount,
delegation.Reward,
undelegations,
}
}
return result, nil
}
// GetDelegationsByValidator returns list of delegations for a validator address.
func (s *PublicBlockChainAPI) GetDelegationsByValidator(ctx context.Context, address string) ([]*RPCDelegation, error) {
if err := s.isBeaconShard(); err != nil {
return nil, err
}
validatorAddress := internal_common.ParseAddr(address)
delegations := s.b.GetDelegationsByValidator(validatorAddress)
result := make([]*RPCDelegation, 0)
@ -758,6 +871,9 @@ func (s *PublicBlockChainAPI) GetDelegationsByValidator(ctx context.Context, add
// GetDelegationByDelegatorAndValidator returns a delegation for delegator and validator.
func (s *PublicBlockChainAPI) GetDelegationByDelegatorAndValidator(ctx context.Context, address string, validator string) (*RPCDelegation, error) {
if err := s.isBeaconShard(); err != nil {
return nil, err
}
delegatorAddress := internal_common.ParseAddr(address)
validatorAddress := internal_common.ParseAddr(validator)
validators, delegations := s.b.GetDelegationsByDelegator(delegatorAddress)
@ -853,18 +969,18 @@ func (s *PublicBlockChainAPI) EstimateGas(ctx context.Context, args CallArgs) (h
// GetCurrentUtilityMetrics ..
func (s *PublicBlockChainAPI) GetCurrentUtilityMetrics() (*network.UtilityMetric, error) {
if s.b.GetShardID() == shard.BeaconChainShardID {
return s.b.GetCurrentUtilityMetrics()
if err := s.isBeaconShard(); err != nil {
return nil, err
}
return nil, errNotBeaconChainShard
return s.b.GetCurrentUtilityMetrics()
}
// GetSuperCommittees ..
func (s *PublicBlockChainAPI) GetSuperCommittees() (*quorum.Transition, error) {
if s.b.GetShardID() == shard.BeaconChainShardID {
return s.b.GetSuperCommittees()
if err := s.isBeaconShard(); err != nil {
return nil, err
}
return nil, errNotBeaconChainShard
return s.b.GetSuperCommittees()
}
// GetCurrentBadBlocks ..
@ -887,8 +1003,8 @@ func (s *PublicBlockChainAPI) GetCirculatingSupply() (numeric.Dec, error) {
func (s *PublicBlockChainAPI) GetStakingNetworkInfo(
ctx context.Context,
) (*StakingNetworkInfo, error) {
if s.b.GetShardID() != shard.BeaconChainShardID {
return nil, errNotBeaconChainShard
if err := s.isBeaconShard(); err != nil {
return nil, err
}
totalStaking, _ := s.GetTotalStaking()
round, _ := s.GetMedianRawStakeSnapshot()
@ -907,8 +1023,8 @@ func (s *PublicBlockChainAPI) GetStakingNetworkInfo(
// GetLastCrossLinks ..
func (s *PublicBlockChainAPI) GetLastCrossLinks() ([]*types.CrossLink, error) {
if s.b.GetShardID() == shard.BeaconChainShardID {
return s.b.GetLastCrossLinks()
if err := s.isBeaconShard(); err != nil {
return nil, err
}
return nil, errNotBeaconChainShard
return s.b.GetLastCrossLinks()
}

@ -2,7 +2,6 @@ package apiv1
import (
"context"
"errors"
"github.com/ethereum/go-ethereum/log"
"github.com/harmony-one/harmony/internal/utils"
@ -23,7 +22,7 @@ func NewDebugAPI(b Backend) *DebugAPI {
// curl -H "Content-Type: application/json" -d '{"method":"debug_setLogVerbosity","params":[0],"id":1}' http://localhost:9123
func (*DebugAPI) SetLogVerbosity(ctx context.Context, level int) (map[string]interface{}, error) {
if level < int(log.LvlCrit) || level > int(log.LvlTrace) {
return nil, errors.New("invalid log level")
return nil, ErrInvalidLogLevel
}
verbosity := log.Lvl(level)

@ -1,8 +1,18 @@
package apiv1
import "errors"
import (
"errors"
)
var (
// ErrIncorrectChainID is an incorrect chain ID.
ErrIncorrectChainID = errors.New("incorrect chain ID")
// ErrInvalidLogLevel when invalid log level is provided
ErrInvalidLogLevel = errors.New("invalid log level")
// ErrIncorrectChainID when ChainID does not match running node
ErrIncorrectChainID = errors.New("incorrect chain id")
// ErrInvalidChainID when ChainID of signer does not match that of running node
ErrInvalidChainID = errors.New("invalid chain id for signer")
// ErrNotBeaconShard when rpc is called on not beacon chain node
ErrNotBeaconShard = errors.New("cannot call this rpc on non beaconchain node")
// ErrRequestedBlockTooHigh when given block is greater than latest block number
ErrRequestedBlockTooHigh = errors.New("requested block number greater than current block number")
)

@ -16,11 +16,6 @@ import (
"github.com/pkg/errors"
)
var (
// ErrInvalidChainID when ChainID of signer does not match that of running node
errInvalidChainID = errors.New("invalid chain id for signer")
)
// TxHistoryArgs is struct to make GetTransactionsHistory request
type TxHistoryArgs struct {
Address string `json:"address"`
@ -216,7 +211,7 @@ func (s *PublicTransactionPoolAPI) SendRawStakingTransaction(
c := s.b.ChainConfig().ChainID
if id := tx.ChainID(); id.Cmp(c) != 0 {
return common.Hash{}, errors.Wrapf(
errInvalidChainID, "blockchain chain id:%s, given %s", c.String(), id.String(),
ErrInvalidChainID, "blockchain chain id:%s, given %s", c.String(), id.String(),
)
}
return SubmitStakingTransaction(ctx, s.b, tx)
@ -238,7 +233,7 @@ func (s *PublicTransactionPoolAPI) SendRawTransaction(
c := s.b.ChainConfig().ChainID
if id := tx.ChainID(); id.Cmp(c) != 0 {
return common.Hash{}, errors.Wrapf(
errInvalidChainID, "blockchain chain id:%s, given %s", c.String(), id.String(),
ErrInvalidChainID, "blockchain chain id:%s, given %s", c.String(), id.String(),
)
}
return SubmitTransaction(ctx, s.b, tx)

@ -73,6 +73,7 @@ type Backend interface {
GetValidatorInformation(addr common.Address, block *types.Block) (*staking.ValidatorRPCEnchanced, error)
GetDelegationsByValidator(validator common.Address) []*staking.Delegation
GetDelegationsByDelegator(delegator common.Address) ([]common.Address, []*staking.Delegation)
GetDelegationsByDelegatorByBlock(delegator common.Address, block *types.Block) ([]common.Address, []*staking.Delegation)
GetValidatorSelfDelegation(addr common.Address) *big.Int
GetShardState() (*shard.State, error)
GetCurrentStakingErrorSink() types.TransactionErrorReports

@ -57,9 +57,26 @@ type BlockArgs struct {
InclStaking bool `json:"inclStaking"`
}
func (s *PublicBlockChainAPI) isBeaconShard() error {
if s.b.GetShardID() != shard.BeaconChainShardID {
return ErrNotBeaconShard
}
return nil
}
func (s *PublicBlockChainAPI) isBlockGreaterThanLatest(blockNum uint64) error {
if blockNum > s.b.CurrentBlock().NumberU64() {
return ErrRequestedBlockTooHigh
}
return nil
}
// GetBlockByNumber returns the requested block. When fullTx in blockArgs is true all transactions in the block are returned in full detail,
// otherwise only the transaction hash is returned. When withSigners in BlocksArgs is true it shows block signers for this block in list of one addresses.
func (s *PublicBlockChainAPI) GetBlockByNumber(ctx context.Context, blockNr uint64, blockArgs BlockArgs) (map[string]interface{}, error) {
if err := s.isBlockGreaterThanLatest(blockNr); err != nil {
return nil, err
}
block, err := s.b.BlockByNumber(ctx, rpc.BlockNumber(blockNr))
blockArgs.InclTx = true
if block != nil {
@ -127,17 +144,25 @@ func (s *PublicBlockChainAPI) GetBlocks(ctx context.Context, blockStart, blockEn
// GetValidators returns validators list for a particular epoch.
func (s *PublicBlockChainAPI) GetValidators(ctx context.Context, epoch int64) (map[string]interface{}, error) {
if err := s.isBeaconShard(); err != nil {
return nil, err
}
committee, err := s.b.GetValidators(big.NewInt(epoch))
if err != nil {
return nil, err
}
balanceQueryBlock := shard.Schedule.EpochLastBlock(uint64(epoch))
if balanceQueryBlock > s.b.CurrentBlock().NumberU64() {
balanceQueryBlock = s.b.CurrentBlock().NumberU64()
}
validators := make([]map[string]interface{}, 0)
for _, validator := range committee.Slots {
oneAddress, err := internal_common.AddressToBech32(validator.EcdsaAddress)
if err != nil {
return nil, err
}
validatorBalance, err := s.GetBalance(ctx, oneAddress)
addr := internal_common.ParseAddr(oneAddress)
validatorBalance, err := s.b.GetBalance(ctx, addr, rpc.BlockNumber(balanceQueryBlock))
if err != nil {
return nil, err
}
@ -156,24 +181,27 @@ func (s *PublicBlockChainAPI) GetValidators(ctx context.Context, epoch int64) (m
// IsLastBlock checks if block is last epoch block.
func (s *PublicBlockChainAPI) IsLastBlock(blockNum uint64) (bool, error) {
if s.b.GetShardID() == shard.BeaconChainShardID {
return shard.Schedule.IsLastBlock(blockNum), nil
if err := s.isBeaconShard(); err != nil {
return false, err
}
return false, errNotBeaconChainShard
return shard.Schedule.IsLastBlock(blockNum), nil
}
// EpochLastBlock returns epoch last block.
func (s *PublicBlockChainAPI) EpochLastBlock(epoch uint64) (uint64, error) {
if s.b.GetShardID() == shard.BeaconChainShardID {
return shard.Schedule.EpochLastBlock(epoch), nil
if err := s.isBeaconShard(); err != nil {
return 0, err
}
return 0, errNotBeaconChainShard
return shard.Schedule.EpochLastBlock(epoch), nil
}
// GetBlockSigners returns signers for a particular block.
func (s *PublicBlockChainAPI) GetBlockSigners(ctx context.Context, blockNr uint64) ([]string, error) {
if blockNr == 0 || blockNr >= uint64(s.BlockNumber()) {
return make([]string, 0), nil
return []string{}, nil
}
if err := s.isBlockGreaterThanLatest(blockNr); err != nil {
return nil, err
}
block, err := s.b.BlockByNumber(ctx, rpc.BlockNumber(blockNr))
if err != nil {
@ -192,22 +220,19 @@ func (s *PublicBlockChainAPI) GetBlockSigners(ctx context.Context, blockNr uint6
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
}
if err != nil {
return result, err
return nil, err
}
err = mask.SetMask(blockWithSigners.Header().LastCommitBitmap())
if err != nil {
return result, err
return nil, err
}
result := []string{}
for _, validator := range committee.Slots {
oneAddress, err := internal_common.AddressToBech32(validator.EcdsaAddress)
if err != nil {
return result, err
return nil, err
}
blsPublicKey := new(bls.PublicKey)
validator.BLSPublicKey.ToLibBLSPublicKey(blsPublicKey)
@ -220,9 +245,12 @@ func (s *PublicBlockChainAPI) GetBlockSigners(ctx context.Context, blockNr uint6
// IsBlockSigner returns true if validator with address signed blockNr block.
func (s *PublicBlockChainAPI) IsBlockSigner(ctx context.Context, blockNr uint64, address string) (bool, error) {
if blockNr == 0 || blockNr >= uint64(s.BlockNumber()) {
if blockNr == 0 {
return false, nil
}
if err := s.isBlockGreaterThanLatest(blockNr); err != nil {
return false, err
}
block, err := s.b.BlockByNumber(ctx, rpc.BlockNumber(blockNr))
if err != nil {
return false, err
@ -293,18 +321,24 @@ func (s *PublicBlockChainAPI) GetLeader(ctx context.Context) string {
}
// GetValidatorSelfDelegation returns validator stake.
func (s *PublicBlockChainAPI) GetValidatorSelfDelegation(ctx context.Context, address string) *big.Int {
return s.b.GetValidatorSelfDelegation(internal_common.ParseAddr(address))
func (s *PublicBlockChainAPI) GetValidatorSelfDelegation(ctx context.Context, address string) (*big.Int, error) {
if err := s.isBeaconShard(); err != nil {
return nil, err
}
return s.b.GetValidatorSelfDelegation(internal_common.ParseAddr(address)), nil
}
// GetValidatorTotalDelegation returns total balace stacking for validator with delegation.
func (s *PublicBlockChainAPI) GetValidatorTotalDelegation(ctx context.Context, address string) *big.Int {
func (s *PublicBlockChainAPI) GetValidatorTotalDelegation(ctx context.Context, address string) (*big.Int, error) {
if err := s.isBeaconShard(); err != nil {
return nil, err
}
delegations := s.b.GetDelegationsByValidator(internal_common.ParseAddr(address))
totalStake := big.NewInt(0)
for _, delegation := range delegations {
totalStake.Add(totalStake, delegation.Amount)
}
return totalStake
return totalStake, nil
}
// GetShardingStructure returns an array of sharding structures.
@ -347,7 +381,10 @@ func (s *PublicBlockChainAPI) GetStorageAt(ctx context.Context, addr string, key
}
// GetBalanceByBlockNumber returns balance by block number.
func (s *PublicBlockChainAPI) GetBalanceByBlockNumber(ctx context.Context, address string, blockNr int64) (*big.Int, error) {
func (s *PublicBlockChainAPI) GetBalanceByBlockNumber(ctx context.Context, address string, blockNr uint64) (*big.Int, error) {
if err := s.isBlockGreaterThanLatest(blockNr); err != nil {
return nil, err
}
addr := internal_common.ParseAddr(address)
return s.b.GetBalance(ctx, addr, rpc.BlockNumber(blockNr))
}
@ -361,8 +398,9 @@ func (s *PublicBlockChainAPI) GetAccountNonce(ctx context.Context, address strin
// GetBalance returns the amount of Nano for the given address in the state of the
// given block number. The rpc.LatestBlockNumber and rpc.PendingBlockNumber meta
// block numbers are also allowed.
func (s *PublicBlockChainAPI) GetBalance(ctx context.Context, address string) (*big.Int, error) {
return s.GetBalanceByBlockNumber(ctx, address, -1)
func (s *PublicBlockChainAPI) GetBalance(ctx context.Context, address string, blockNr rpc.BlockNumber) (*big.Int, error) {
addr := internal_common.ParseAddr(address)
return s.b.GetBalance(ctx, addr, rpc.BlockNumber(blockNr))
}
// BlockNumber returns the block number of the chain head.
@ -383,8 +421,8 @@ func (s *PublicBlockChainAPI) ResendCx(ctx context.Context, txID common.Hash) (b
// Call executes the given transaction on the state for the given block number.
// It doesn't make and changes in the state/blockchain and is useful to execute and retrieve values.
func (s *PublicBlockChainAPI) Call(ctx context.Context, args CallArgs, blockNr rpc.BlockNumber) (hexutil.Bytes, error) {
result, _, _, err := doCall(ctx, s.b, args, blockNr, vm.Config{}, 5*time.Second, s.b.RPCGasCap())
func (s *PublicBlockChainAPI) Call(ctx context.Context, args CallArgs, blockNr uint64) (hexutil.Bytes, error) {
result, _, _, err := doCall(ctx, s.b, args, rpc.BlockNumber(blockNr), vm.Config{}, 5*time.Second, s.b.RPCGasCap())
return (hexutil.Bytes)(result), err
}
@ -491,17 +529,13 @@ func (s *PublicBlockChainAPI) LatestHeader(ctx context.Context) *HeaderInformati
return newHeaderInformation(header)
}
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
if err := s.isBeaconShard(); err != nil {
return nil, err
}
return nil, errNotBeaconChainShard
return s.b.GetTotalStakingSnapshot(), nil
}
// GetMedianRawStakeSnapshot returns the raw median stake, only meant to be called on beaconchain
@ -509,14 +543,17 @@ func (s *PublicBlockChainAPI) GetTotalStaking() (*big.Int, error) {
func (s *PublicBlockChainAPI) GetMedianRawStakeSnapshot() (
*committee.CompletedEPoSRound, error,
) {
if s.b.GetShardID() == shard.BeaconChainShardID {
return s.b.GetMedianRawStakeSnapshot()
if err := s.isBeaconShard(); err != nil {
return nil, err
}
return nil, errNotBeaconChainShard
return s.b.GetMedianRawStakeSnapshot()
}
// GetAllValidatorAddresses returns all validator addresses.
func (s *PublicBlockChainAPI) GetAllValidatorAddresses() ([]string, error) {
if err := s.isBeaconShard(); err != nil {
return nil, err
}
addresses := []string{}
for _, addr := range s.b.GetAllValidatorAddresses() {
oneAddr, _ := internal_common.AddressToBech32(addr)
@ -527,6 +564,9 @@ func (s *PublicBlockChainAPI) GetAllValidatorAddresses() ([]string, error) {
// GetElectedValidatorAddresses returns elected validator addresses.
func (s *PublicBlockChainAPI) GetElectedValidatorAddresses() ([]string, error) {
if err := s.isBeaconShard(); err != nil {
return nil, err
}
addresses := []string{}
for _, addr := range s.b.GetElectedValidatorAddresses() {
oneAddr, _ := internal_common.AddressToBech32(addr)
@ -539,6 +579,9 @@ func (s *PublicBlockChainAPI) GetElectedValidatorAddresses() ([]string, error) {
func (s *PublicBlockChainAPI) GetValidatorInformation(
ctx context.Context, address string,
) (*staking.ValidatorRPCEnchanced, error) {
if err := s.isBeaconShard(); err != nil {
return nil, err
}
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")
@ -550,8 +593,14 @@ func (s *PublicBlockChainAPI) GetValidatorInformation(
// GetValidatorInformationByBlockNumber ..
func (s *PublicBlockChainAPI) GetValidatorInformationByBlockNumber(
ctx context.Context, address string, blockNr rpc.BlockNumber,
ctx context.Context, address string, blockNr uint64,
) (*staking.ValidatorRPCEnchanced, error) {
if err := s.isBeaconShard(); err != nil {
return nil, err
}
if err := s.isBlockGreaterThanLatest(blockNr); err != nil {
return nil, err
}
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)
@ -600,6 +649,10 @@ func (s *PublicBlockChainAPI) getAllValidatorInformation(
func (s *PublicBlockChainAPI) GetAllValidatorInformation(
ctx context.Context, page int,
) ([]*staking.ValidatorRPCEnchanced, error) {
if err := s.isBeaconShard(); err != nil {
return nil, err
}
blockNr := s.b.CurrentBlock().NumberU64()
// delete cache for previous block
@ -623,15 +676,25 @@ func (s *PublicBlockChainAPI) GetAllValidatorInformation(
// 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,
ctx context.Context, page int, blockNr uint64,
) ([]*staking.ValidatorRPCEnchanced, error) {
return s.getAllValidatorInformation(ctx, page, blockNr)
if err := s.isBeaconShard(); err != nil {
return nil, err
}
if err := s.isBlockGreaterThanLatest(blockNr); err != nil {
return nil, err
}
return s.getAllValidatorInformation(ctx, page, rpc.BlockNumber(blockNr))
}
// GetAllDelegationInformation returns delegation information about `validatorsPageSize` validators,
// starting at `page*validatorsPageSize`.
// If page is -1, return all instead of `validatorsPageSize` elements.
func (s *PublicBlockChainAPI) GetAllDelegationInformation(ctx context.Context, page int) ([][]*RPCDelegation, error) {
if err := s.isBeaconShard(); err != nil {
return nil, err
}
if page < -1 {
return make([][]*RPCDelegation, 0), nil
}
@ -661,13 +724,16 @@ func (s *PublicBlockChainAPI) GetAllDelegationInformation(ctx context.Context, p
// GetDelegationsByDelegator returns list of delegations for a delegator address.
func (s *PublicBlockChainAPI) GetDelegationsByDelegator(ctx context.Context, address string) ([]*RPCDelegation, error) {
if err := s.isBeaconShard(); err != nil {
return nil, err
}
delegatorAddress := internal_common.ParseAddr(address)
validators, delegations := s.b.GetDelegationsByDelegator(delegatorAddress)
result := []*RPCDelegation{}
for i := range delegations {
delegation := delegations[i]
undelegations := []RPCUndelegation{}
undelegations := make([]RPCUndelegation, len(delegation.Undelegations))
for j := range delegation.Undelegations {
undelegations = append(undelegations, RPCUndelegation{
@ -688,8 +754,52 @@ func (s *PublicBlockChainAPI) GetDelegationsByDelegator(ctx context.Context, add
return result, nil
}
// GetDelegationsByDelegatorByBlockNumber returns list of delegations for a delegator address at given block number
func (s *PublicBlockChainAPI) GetDelegationsByDelegatorByBlockNumber(
ctx context.Context, address string, blockNum uint64,
) ([]*RPCDelegation, error) {
if err := s.isBeaconShard(); err != nil {
return nil, err
}
if err := s.isBlockGreaterThanLatest(blockNum); err != nil {
return nil, err
}
delegatorAddress := internal_common.ParseAddr(address)
block, err := s.b.BlockByNumber(ctx, rpc.BlockNumber(blockNum))
if err != nil {
return nil, errors.Wrapf(err, "could not retrieve the block information for block number: %d", blockNum)
}
validators, delegations := s.b.GetDelegationsByDelegatorByBlock(delegatorAddress, block)
result := make([]*RPCDelegation, len(delegations))
for i := range delegations {
delegation := delegations[i]
undelegations := make([]RPCUndelegation, len(delegation.Undelegations))
for j := range delegation.Undelegations {
undelegations[j] = RPCUndelegation{
delegation.Undelegations[j].Amount,
delegation.Undelegations[j].Epoch,
}
}
valAddr, _ := internal_common.AddressToBech32(validators[i])
delAddr, _ := internal_common.AddressToBech32(delegatorAddress)
result[i] = &RPCDelegation{
valAddr,
delAddr,
delegation.Amount,
delegation.Reward,
undelegations,
}
}
return result, nil
}
// GetDelegationsByValidator returns list of delegations for a validator address.
func (s *PublicBlockChainAPI) GetDelegationsByValidator(ctx context.Context, address string) ([]*RPCDelegation, error) {
if err := s.isBeaconShard(); err != nil {
return nil, err
}
validatorAddress := internal_common.ParseAddr(address)
delegations := s.b.GetDelegationsByValidator(validatorAddress)
result := make([]*RPCDelegation, 0)
@ -718,6 +828,9 @@ func (s *PublicBlockChainAPI) GetDelegationsByValidator(ctx context.Context, add
// GetDelegationByDelegatorAndValidator returns a delegation for delegator and validator.
func (s *PublicBlockChainAPI) GetDelegationByDelegatorAndValidator(ctx context.Context, address string, validator string) (*RPCDelegation, error) {
if err := s.isBeaconShard(); err != nil {
return nil, err
}
delegatorAddress := internal_common.ParseAddr(address)
validatorAddress := internal_common.ParseAddr(validator)
validators, delegations := s.b.GetDelegationsByDelegator(delegatorAddress)
@ -813,18 +926,18 @@ func (s *PublicBlockChainAPI) EstimateGas(ctx context.Context, args CallArgs) (h
// GetCurrentUtilityMetrics ..
func (s *PublicBlockChainAPI) GetCurrentUtilityMetrics() (*network.UtilityMetric, error) {
if s.b.GetShardID() == shard.BeaconChainShardID {
return s.b.GetCurrentUtilityMetrics()
if err := s.isBeaconShard(); err != nil {
return nil, err
}
return nil, errNotBeaconChainShard
return s.b.GetCurrentUtilityMetrics()
}
// GetSuperCommittees ..
func (s *PublicBlockChainAPI) GetSuperCommittees() (*quorum.Transition, error) {
if s.b.GetShardID() == shard.BeaconChainShardID {
return s.b.GetSuperCommittees()
if err := s.isBeaconShard(); err != nil {
return nil, err
}
return nil, errNotBeaconChainShard
return s.b.GetSuperCommittees()
}
// GetCurrentBadBlocks ..
@ -847,8 +960,8 @@ func (s *PublicBlockChainAPI) GetCirculatingSupply() (numeric.Dec, error) {
func (s *PublicBlockChainAPI) GetStakingNetworkInfo(
ctx context.Context,
) (*StakingNetworkInfo, error) {
if s.b.GetShardID() != shard.BeaconChainShardID {
return nil, errNotBeaconChainShard
if err := s.isBeaconShard(); err != nil {
return nil, err
}
totalStaking, _ := s.GetTotalStaking()
round, _ := s.GetMedianRawStakeSnapshot()
@ -867,8 +980,8 @@ func (s *PublicBlockChainAPI) GetStakingNetworkInfo(
// GetLastCrossLinks ..
func (s *PublicBlockChainAPI) GetLastCrossLinks() ([]*types.CrossLink, error) {
if s.b.GetShardID() == shard.BeaconChainShardID {
return s.b.GetLastCrossLinks()
if err := s.isBeaconShard(); err != nil {
return nil, err
}
return nil, errNotBeaconChainShard
return s.b.GetLastCrossLinks()
}

@ -2,7 +2,6 @@ package apiv2
import (
"context"
"errors"
"github.com/ethereum/go-ethereum/log"
"github.com/harmony-one/harmony/internal/utils"
@ -23,7 +22,7 @@ func NewDebugAPI(b Backend) *DebugAPI {
// curl -H "Content-Type: application/json" -d '{"method":"debug_setLogVerbosity","params":[0],"id":1}' http://localhost:9123
func (*DebugAPI) SetLogVerbosity(ctx context.Context, level int) (map[string]interface{}, error) {
if level < int(log.LvlCrit) || level > int(log.LvlTrace) {
return nil, errors.New("invalid log level")
return nil, ErrInvalidLogLevel
}
verbosity := log.Lvl(level)

@ -1,8 +1,18 @@
package apiv2
import "errors"
import (
"errors"
)
var (
// ErrIncorrectChainID is an incorrect chain ID.
ErrIncorrectChainID = errors.New("Incorrect chain ID")
// ErrInvalidLogLevel when invalid log level is provided
ErrInvalidLogLevel = errors.New("invalid log level")
// ErrIncorrectChainID when ChainID does not match running node
ErrIncorrectChainID = errors.New("incorrect chain id")
// ErrInvalidChainID when ChainID of signer does not match that of running node
ErrInvalidChainID = errors.New("invalid chain id for signer")
// ErrNotBeaconShard when rpc is called on not beacon chain node
ErrNotBeaconShard = errors.New("cannot call this rpc on non beaconchain node")
// ErrRequestedBlockTooHigh when given block is greater than latest block number
ErrRequestedBlockTooHigh = errors.New("requested block number greater than current block number")
)

@ -16,11 +16,6 @@ import (
"github.com/pkg/errors"
)
var (
// ErrInvalidChainID when ChainID of signer does not match that of running node
errInvalidChainID = errors.New("invalid chain id for signer")
)
// TxHistoryArgs is struct to make GetTransactionsHistory request
type TxHistoryArgs struct {
Address string `json:"address"`
@ -242,7 +237,7 @@ func (s *PublicTransactionPoolAPI) SendRawStakingTransaction(
c := s.b.ChainConfig().ChainID
if id := tx.ChainID(); id.Cmp(c) != 0 {
return common.Hash{}, errors.Wrapf(
errInvalidChainID, "blockchain chain id:%s, given %s", c.String(), id.String(),
ErrInvalidChainID, "blockchain chain id:%s, given %s", c.String(), id.String(),
)
}
return SubmitStakingTransaction(ctx, s.b, tx)
@ -262,7 +257,7 @@ func (s *PublicTransactionPoolAPI) SendRawTransaction(ctx context.Context, encod
c := s.b.ChainConfig().ChainID
if id := tx.ChainID(); id.Cmp(c) != 0 {
return common.Hash{}, errors.Wrapf(
errInvalidChainID, "blockchain chain id:%s, given %s", c.String(), id.String(),
ErrInvalidChainID, "blockchain chain id:%s, given %s", c.String(), id.String(),
)
}
return SubmitTransaction(ctx, s.b, tx)

@ -67,6 +67,7 @@ type Backend interface {
GetValidatorInformation(addr common.Address, block *types.Block) (*staking.ValidatorRPCEnchanced, error)
GetDelegationsByValidator(validator common.Address) []*staking.Delegation
GetDelegationsByDelegator(delegator common.Address) ([]common.Address, []*staking.Delegation)
GetDelegationsByDelegatorByBlock(delegator common.Address, block *types.Block) ([]common.Address, []*staking.Delegation)
GetValidatorSelfDelegation(addr common.Address) *big.Int
GetShardState() (*shard.State, error)
GetCurrentStakingErrorSink() types.TransactionErrorReports

Loading…
Cancel
Save