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" mapset "github.com/deckarep/golang-set"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/harmony-one/harmony/accounts" "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 // 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. // Parse the address.
key.Address = "" key.Address = ""
err = json.NewDecoder(buf).Decode(&key) err = json.NewDecoder(buf).Decode(&key)
addr := common.HexToAddress(key.Address) addr := common2.ParseAddr(key.Address)
switch { switch {
case err != nil: case err != nil:
log.Debug("Failed to decode keystore key", "path", path, "err", err) 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/rlp"
"github.com/ethereum/go-ethereum/rpc" "github.com/ethereum/go-ethereum/rpc"
"github.com/gorilla/mux" "github.com/gorilla/mux"
libp2p_peer "github.com/libp2p/go-libp2p-peer"
msg_pb "github.com/harmony-one/harmony/api/proto/message" msg_pb "github.com/harmony-one/harmony/api/proto/message"
"github.com/harmony-one/harmony/core/types" "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/ctxerror"
"github.com/harmony-one/harmony/internal/utils" "github.com/harmony-one/harmony/internal/utils"
"github.com/harmony-one/harmony/p2p" "github.com/harmony-one/harmony/p2p"
libp2p_peer "github.com/libp2p/go-libp2p-peer"
) )
// Constants for explorer service. // 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 // Check the balance from blockchain rather than local DB dump
if s.GetAccountBalance != nil { if s.GetAccountBalance != nil {
address := common.HexToAddress(id) address := common2.ParseAddr(id)
balance, err := s.GetAccountBalance(address) balance, err := s.GetAccountBalance(address)
if err == nil { if err == nil {
data.Address.Balance = balance data.Address.Balance = balance

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

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

@ -10,11 +10,11 @@ import (
"github.com/harmony-one/bls/ffi/go/bls" "github.com/harmony-one/bls/ffi/go/bls"
"github.com/harmony-one/harmony/contracts/structs" "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/ctxerror"
"github.com/harmony-one/harmony/internal/genesis" "github.com/harmony-one/harmony/internal/genesis"
"github.com/harmony-one/harmony/internal/utils" "github.com/harmony-one/harmony/internal/utils"
"github.com/harmony-one/harmony/core/types"
) )
const ( const (
@ -231,7 +231,7 @@ func GetInitShardState() types.ShardState {
pubKey := types.BlsPublicKey{} pubKey := types.BlsPublicKey{}
pubKey.FromLibBLSPublicKey(priKey.GetPublicKey()) pubKey.FromLibBLSPublicKey(priKey.GetPublicKey())
// TODO: directly read address for bls too // 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) com.NodeList = append(com.NodeList, curNodeID)
} }
@ -243,7 +243,7 @@ func GetInitShardState() types.ShardState {
pubKey := types.BlsPublicKey{} pubKey := types.BlsPublicKey{}
pubKey.FromLibBLSPublicKey(priKey.GetPublicKey()) pubKey.FromLibBLSPublicKey(priKey.GetPublicKey())
// TODO: directly read address for bls too // 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) com.NodeList = append(com.NodeList, curNodeID)
} }
shardState = append(shardState, com) shardState = append(shardState, com)

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

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

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

Loading…
Cancel
Save