diff --git a/hmy/api_backend.go b/hmy/api_backend.go index 639543169..7e51a54d7 100644 --- a/hmy/api_backend.go +++ b/hmy/api_backend.go @@ -20,6 +20,7 @@ import ( "github.com/harmony-one/harmony/core/types" "github.com/harmony-one/harmony/core/vm" "github.com/harmony-one/harmony/internal/params" + "github.com/harmony-one/harmony/shard" ) // APIBackend An implementation of internal/hmyapi/Backend. Full client. @@ -233,6 +234,20 @@ 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) { + state, err := b.hmy.BlockChain().ReadShardState(epoch) + if err != nil { + return nil, err + } + for _, committee := range state { + if committee.ShardID == b.GetShardID() { + return &committee, nil + } + } + return nil, nil +} + // ResendCx retrieve blockHash from txID and add blockHash to CxPool for resending func (b *APIBackend) ResendCx(ctx context.Context, txID common.Hash) (uint64, bool) { blockHash, blockNum, index := b.hmy.BlockChain().ReadTxLookupEntry(txID) diff --git a/internal/hmyapi/backend.go b/internal/hmyapi/backend.go index 360defc49..f940d6922 100644 --- a/internal/hmyapi/backend.go +++ b/internal/hmyapi/backend.go @@ -17,6 +17,7 @@ import ( "github.com/harmony-one/harmony/core/types" "github.com/harmony-one/harmony/core/vm" "github.com/harmony-one/harmony/internal/params" + "github.com/harmony-one/harmony/shard" ) // Backend interface provides the common API services (that are provided by @@ -63,6 +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) GetShardID() uint32 // retrieve the blockHash using txID and add blockHash to CxPool for resending diff --git a/internal/hmyapi/blockchain.go b/internal/hmyapi/blockchain.go index 33b9532ef..c36a8b374 100644 --- a/internal/hmyapi/blockchain.go +++ b/internal/hmyapi/blockchain.go @@ -63,6 +63,36 @@ func (s *PublicBlockChainAPI) GetBlockByHash(ctx context.Context, blockHash comm return nil, err } +// 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)) + if err != nil { + return nil, err + } + validators := make([]map[string]interface{}, 0) + for _, validator := range committee.NodeList { + validatorBalance := new(hexutil.Big) + validatorBalance, err = s.b.GetBalance(validator.EcdsaAddress) + if err != nil { + return nil, err + } + oneAddress, err := internal_common.AddressToBech32(validator.EcdsaAddress) + if err != nil { + return nil, err + } + validatorsFields := map[string]interface{}{ + "address": oneAddress, + "balance": validatorBalance, + } + validators = append(validators, validatorsFields) + } + result := map[string]interface{}{ + "shardID": committee.ShardID, + "validators": validators, + } + return result, 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.