Skip to content

Commit

Permalink
Balance api update (#2222)
Browse files Browse the repository at this point in the history
  • Loading branch information
flicker-harmony authored Feb 8, 2020
1 parent 0f7aee4 commit 95d9e4d
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 18 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ export GO111MODULE=on
```
Note : Some of our scripts require bash 4.x support, please [install bash 4.x](http://tldrdevnotes.com/bash-upgrade-3-4-macos) on MacOS X.

## Harmony docs and guides

https://docs.harmony.one

## API guides

https://docs.harmony.one/home/developers/api

### Build all executables

You can run the script `./scripts/go_executable_build.sh` to build all the executables.
Expand Down
8 changes: 6 additions & 2 deletions hmy/api_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,12 @@ func (b *APIBackend) GetPoolTransactions() (types.Transactions, error) {
}

// GetBalance returns balance of an given address.
func (b *APIBackend) GetBalance(address common.Address) (*big.Int, error) {
return b.hmy.nodeAPI.GetBalanceOfAddress(address)
func (b *APIBackend) GetBalance(ctx context.Context, address common.Address, blockNr rpc.BlockNumber) (*big.Int, error) {
state, _, err := b.StateAndHeaderByNumber(ctx, blockNr)
if state == nil || err != nil {
return nil, err
}
return state.GetBalance(address), state.Error()
}

// GetTransactionsHistory returns list of transactions hashes of address.
Expand Down
2 changes: 1 addition & 1 deletion internal/hmyapi/apiv1/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ type Backend interface {
ChainConfig() *params.ChainConfig
CurrentBlock() *types.Block
// Get balance
GetBalance(address common.Address) (*big.Int, error)
GetBalance(ctx context.Context, address common.Address, blockNr rpc.BlockNumber) (*big.Int, error)
// Get committee for a particular epoch
GetCommittee(epoch *big.Int) (*shard.Committee, error)
GetShardID() uint32
Expand Down
20 changes: 12 additions & 8 deletions internal/hmyapi/apiv1/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ func (s *PublicBlockChainAPI) GetCommittee(ctx context.Context, epoch int64) (ma
}
validators := make([]map[string]interface{}, 0)
for _, validator := range committee.NodeList {
validatorBalance, err := s.b.GetBalance(validator.EcdsaAddress)
oneAddress, err := internal_common.AddressToBech32(validator.EcdsaAddress)
if err != nil {
return nil, err
}
oneAddress, err := internal_common.AddressToBech32(validator.EcdsaAddress)
validatorBalance, err := s.GetBalance(ctx, oneAddress, rpc.LatestBlockNumber)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -132,19 +132,23 @@ func (s *PublicBlockChainAPI) GetStorageAt(ctx context.Context, addr string, key
return res[:], state.Error()
}

// 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, blockNr rpc.BlockNumber) (*hexutil.Big, error) {
// TODO: currently only get latest balance. Will add complete logic later.
// GetBalanceByBlockNumber returns balance by block number.
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(addr)
balance, err := s.b.GetBalance(ctx, addr, blockNr)
if balance == nil {
return nil, err
}
return (*hexutil.Big)(balance), err
}

// 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, blockNr rpc.BlockNumber) (*hexutil.Big, error) {
return s.GetBalanceByBlockNumber(ctx, address, rpc.LatestBlockNumber)
}

// BlockNumber returns the block number of the chain head.
func (s *PublicBlockChainAPI) BlockNumber() hexutil.Uint64 {
header, _ := s.b.HeaderByNumber(context.Background(), rpc.LatestBlockNumber) // latest header should always be available
Expand Down
2 changes: 1 addition & 1 deletion internal/hmyapi/apiv2/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ type Backend interface {
ChainConfig() *params.ChainConfig
CurrentBlock() *types.Block
// Get balance
GetBalance(address common.Address) (*big.Int, error)
GetBalance(ctx context.Context, address common.Address, blockNr rpc.BlockNumber) (*big.Int, error)
// Get committee for a particular epoch
GetCommittee(epoch *big.Int) (*shard.Committee, error)
GetShardID() uint32
Expand Down
17 changes: 12 additions & 5 deletions internal/hmyapi/apiv2/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ func (s *PublicBlockChainAPI) GetCommittee(ctx context.Context, epoch int64) (ma
}
validators := make([]map[string]interface{}, 0)
for _, validator := range committee.NodeList {
validatorBalance, err := s.b.GetBalance(validator.EcdsaAddress)
oneAddress, err := internal_common.AddressToBech32(validator.EcdsaAddress)
if err != nil {
return nil, err
}
oneAddress, err := internal_common.AddressToBech32(validator.EcdsaAddress)
validatorBalance, err := s.GetBalance(ctx, oneAddress)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -144,10 +144,17 @@ func (s *PublicBlockChainAPI) GetStorageAt(ctx context.Context, addr string, key
return res[:], state.Error()
}

// GetBalance returns the amount of Nano for the given address in the state.
func (s *PublicBlockChainAPI) GetBalance(ctx context.Context, address string) (*big.Int, error) {
// GetBalanceByBlockNumber returns balance by block number.
func (s *PublicBlockChainAPI) GetBalanceByBlockNumber(ctx context.Context, address string, blockNr int64) (*big.Int, error) {
addr := internal_common.ParseAddr(address)
return s.b.GetBalance(addr)
return s.b.GetBalance(ctx, addr, rpc.BlockNumber(blockNr))
}

// 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)
}

// BlockNumber returns the block number of the chain head.
Expand Down
2 changes: 1 addition & 1 deletion internal/hmyapi/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ type Backend interface {
ChainConfig() *params.ChainConfig
CurrentBlock() *types.Block
// Get balance
GetBalance(address common.Address) (*big.Int, error)
GetBalance(ctx context.Context, address common.Address, blockNr rpc.BlockNumber) (*big.Int, error)
// Get validators for a particular epoch
GetCommittee(epoch *big.Int) (*shard.Committee, error)
GetShardID() uint32
Expand Down

0 comments on commit 95d9e4d

Please sign in to comment.