Replace non-constant HexToAddress with ParseAddr

Leave constant HexToAddress calls as is for now; they are numerous and
converting them have no real benefit.

ParseAddr parses both base16 and bech32 addresses.
pull/992/head
Eugene Kim 6 years ago
parent 28976708a6
commit b841559e28
  1. 4
      accounts/keystore/account_cache.go
  2. 6
      api/service/explorer/service.go
  3. 4
      api/service/staking/service.go
  4. 15
      cmd/client/wallet/main.go
  5. 8
      core/resharding.go
  6. 6
      core/vm/contracts_test.go
  7. 3
      node/node_genesis.go
  8. 5
      test/txgen/main.go

@ -30,7 +30,9 @@ import (
mapset "github.com/deckarep/golang-set"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log"
"github.com/harmony-one/harmony/accounts"
common2 "github.com/harmony-one/harmony/internal/common"
)
// Minimum amount of time between cache reloads. This limit applies if the platform does
@ -258,7 +260,7 @@ func (ac *accountCache) scanAccounts() error {
// Parse the address.
key.Address = ""
err = json.NewDecoder(buf).Decode(&key)
addr := common.HexToAddress(key.Address)
addr := common2.ParseAddr(key.Address)
switch {
case err != nil:
log.Debug("Failed to decode keystore key", "path", path, "err", err)

@ -14,12 +14,14 @@ import (
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/rpc"
"github.com/gorilla/mux"
libp2p_peer "github.com/libp2p/go-libp2p-peer"
msg_pb "github.com/harmony-one/harmony/api/proto/message"
"github.com/harmony-one/harmony/core/types"
common2 "github.com/harmony-one/harmony/internal/common"
"github.com/harmony-one/harmony/internal/ctxerror"
"github.com/harmony-one/harmony/internal/utils"
"github.com/harmony-one/harmony/p2p"
libp2p_peer "github.com/libp2p/go-libp2p-peer"
)
// Constants for explorer service.
@ -291,7 +293,7 @@ func (s *Service) GetExplorerAddress(w http.ResponseWriter, r *http.Request) {
// Check the balance from blockchain rather than local DB dump
if s.GetAccountBalance != nil {
address := common.HexToAddress(id)
address := common2.ParseAddr(id)
balance, err := s.GetAccountBalance(address)
if err == nil {
data.Address.Balance = balance

@ -13,6 +13,7 @@ import (
"github.com/ethereum/go-ethereum/rpc"
protobuf "github.com/golang/protobuf/proto"
"github.com/harmony-one/bls/ffi/go/bls"
"github.com/harmony-one/harmony/accounts"
"github.com/harmony-one/harmony/accounts/abi"
proto "github.com/harmony-one/harmony/api/client/service/proto"
@ -23,6 +24,7 @@ import (
"github.com/harmony-one/harmony/contracts"
"github.com/harmony-one/harmony/core"
"github.com/harmony-one/harmony/core/types"
common2 "github.com/harmony-one/harmony/internal/common"
"github.com/harmony-one/harmony/internal/ctxerror"
"github.com/harmony-one/harmony/internal/genesis"
hmykey "github.com/harmony-one/harmony/internal/keystore"
@ -187,7 +189,7 @@ func constructStakingMessage(ts types.Transactions) []byte {
func (s *Service) createRawStakingMessage() []byte {
// TODO(minhdoan): Enable getStakingInfo back after testing.
stakingInfo := s.getFakeStakingInfo()
toAddress := common.HexToAddress(stakingInfo.ContractAddress)
toAddress := common2.ParseAddr(stakingInfo.ContractAddress)
abi, err := abi.JSON(strings.NewReader(contracts.StakeLockContractABI))
if err != nil {

@ -12,15 +12,17 @@ import (
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log"
"github.com/harmony-one/harmony/accounts"
"github.com/harmony-one/harmony/accounts/keystore"
"github.com/harmony-one/harmony/api/client"
clientService "github.com/harmony-one/harmony/api/client/service"
proto_node "github.com/harmony-one/harmony/api/proto/node"
"github.com/harmony-one/harmony/common/denominations"
"github.com/harmony-one/harmony/core"
"github.com/harmony-one/harmony/core/types"
common2 "github.com/harmony-one/harmony/internal/common"
nodeconfig "github.com/harmony-one/harmony/internal/configs/node"
"github.com/harmony-one/harmony/internal/ctxerror"
"github.com/harmony-one/harmony/internal/shardchain"
@ -29,9 +31,6 @@ import (
"github.com/harmony-one/harmony/p2p"
p2p_host "github.com/harmony-one/harmony/p2p/host"
"github.com/harmony-one/harmony/p2p/p2pimpl"
"github.com/harmony-one/harmony/accounts"
"github.com/harmony-one/harmony/accounts/keystore"
)
var (
@ -380,7 +379,7 @@ func processBalancesCommand() {
}
}
} else {
address := common.HexToAddress(*balanceAddressPtr)
address := common2.ParseAddr(*balanceAddressPtr)
fmt.Printf("Account: %s:\n", address.Hex())
for shardID, balanceNonce := range FetchBalance(address) {
fmt.Printf(" Balance in Shard %d: %s, nonce: %v \n", shardID, convertBalanceIntoReadableFormat(balanceNonce.balance), balanceNonce.nonce)
@ -397,7 +396,7 @@ func processGetFreeToken() {
if *freeTokenAddressPtr == "" {
fmt.Println("Error: --address is required")
} else {
address := common.HexToAddress(*freeTokenAddressPtr)
address := common2.ParseAddr(*freeTokenAddressPtr)
GetFreeToken(address)
}
}
@ -430,13 +429,13 @@ func processTransferCommand() {
return
}
receiverAddress := common.HexToAddress(receiver)
receiverAddress := common2.ParseAddr(receiver)
if len(receiverAddress) != 20 {
fmt.Println("The receiver address is not valid.")
return
}
senderAddress := common.HexToAddress(sender)
senderAddress := common2.ParseAddr(sender)
if len(senderAddress) != 20 {
fmt.Println("The sender address is not valid.")
return

@ -10,11 +10,11 @@ import (
"github.com/harmony-one/bls/ffi/go/bls"
"github.com/harmony-one/harmony/contracts/structs"
"github.com/harmony-one/harmony/core/types"
common2 "github.com/harmony-one/harmony/internal/common"
"github.com/harmony-one/harmony/internal/ctxerror"
"github.com/harmony-one/harmony/internal/genesis"
"github.com/harmony-one/harmony/internal/utils"
"github.com/harmony-one/harmony/core/types"
)
const (
@ -231,7 +231,7 @@ func GetInitShardState() types.ShardState {
pubKey := types.BlsPublicKey{}
pubKey.FromLibBLSPublicKey(priKey.GetPublicKey())
// TODO: directly read address for bls too
curNodeID := types.NodeID{common.HexToAddress(genesis.GenesisAccounts[index].Address), pubKey}
curNodeID := types.NodeID{common2.ParseAddr(genesis.GenesisAccounts[index].Address), pubKey}
com.NodeList = append(com.NodeList, curNodeID)
}
@ -243,7 +243,7 @@ func GetInitShardState() types.ShardState {
pubKey := types.BlsPublicKey{}
pubKey.FromLibBLSPublicKey(priKey.GetPublicKey())
// TODO: directly read address for bls too
curNodeID := types.NodeID{common.HexToAddress(genesis.GenesisFNAccounts[index].Address), pubKey}
curNodeID := types.NodeID{common2.ParseAddr(genesis.GenesisFNAccounts[index].Address), pubKey}
com.NodeList = append(com.NodeList, curNodeID)
}
shardState = append(shardState, com)

@ -22,6 +22,8 @@ import (
"testing"
"github.com/ethereum/go-ethereum/common"
common2 "github.com/harmony-one/harmony/internal/common"
)
// precompiledTest defines the input/output pairs for precompiled contract tests.
@ -337,7 +339,7 @@ var bn256PairingTests = []precompiledTest{
}
func testPrecompiled(addr string, test precompiledTest, t *testing.T) {
p := PrecompiledContractsByzantium[common.HexToAddress(addr)]
p := PrecompiledContractsByzantium[common2.ParseAddr(addr)]
in := common.Hex2Bytes(test.input)
contract := NewContract(AccountRef(common.HexToAddress("1337")),
nil, new(big.Int), p.RequiredGas(in))
@ -354,7 +356,7 @@ func benchmarkPrecompiled(addr string, test precompiledTest, bench *testing.B) {
if test.noBenchmark {
return
}
p := PrecompiledContractsByzantium[common.HexToAddress(addr)]
p := PrecompiledContractsByzantium[common2.ParseAddr(addr)]
in := common.Hex2Bytes(test.input)
reqGas := p.RequiredGas(in)
contract := NewContract(AccountRef(common.HexToAddress("1337")),

@ -16,6 +16,7 @@ import (
"github.com/harmony-one/harmony/core"
"github.com/harmony-one/harmony/core/rawdb"
"github.com/harmony-one/harmony/core/types"
common2 "github.com/harmony-one/harmony/internal/common"
"github.com/harmony-one/harmony/internal/ctxerror"
"github.com/harmony-one/harmony/internal/genesis"
"github.com/harmony-one/harmony/internal/utils"
@ -134,7 +135,7 @@ func AddNodeAddressesToGenesisAlloc(genesisAlloc core.GenesisAlloc) {
for _, account := range genesis.GenesisAccounts {
testBankFunds := big.NewInt(InitFreeFundInEther)
testBankFunds = testBankFunds.Mul(testBankFunds, big.NewInt(denominations.One))
address := common.HexToAddress(account.Address)
address := common2.ParseAddr(account.Address)
genesisAlloc[address] = core.GenesisAccount{Balance: testBankFunds}
}
}

@ -6,9 +6,10 @@ import (
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/harmony-one/harmony/core/types"
"github.com/harmony-one/harmony/internal/common"
)
// {Address: "0xd2Cb501B40D3a9a013A38267a4d2A4Cf6bD2CAa8", Private: "3c8642f7188e05acc4467d9e2aa7fd539e82aa90a5497257cf0ecbb98ed3b88f", Public: "0xd2Cb501B40D3a9a013A38267a4d2A4Cf6bD2CAa8"},
@ -52,7 +53,7 @@ func generateTxnHarmony(PrivateKeyFrom string, ToAddress string, shardID uint32,
nonce := uint64(0)
value := big.NewInt(1000000000000000000 * amount)
gasLimit := uint64(21000)
toAddress := common.HexToAddress(ToAddress)
toAddress := common.ParseAddr(ToAddress)
var data []byte
unsignedTx := types.NewTransaction(nonce,

Loading…
Cancel
Save