Skip to content

Commit c7ab3e5

Browse files
fjlkaralabe
authored andcommitted
common: delete StringToAddress, StringToHash (#16436)
* common: delete StringToAddress, StringToHash These functions are confusing because they don't parse hex, but use the bytes of the string. This change removes them, replacing all uses of StringToAddress(s) by BytesToAddress([]byte(s)). * eth/filters: remove incorrect use of common.BytesToAddress
1 parent 149f706 commit c7ab3e5

File tree

4 files changed

+10
-12
lines changed

4 files changed

+10
-12
lines changed

cmd/evm/runner.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ func runCmd(ctx *cli.Context) error {
8484
debugLogger *vm.StructLogger
8585
statedb *state.StateDB
8686
chainConfig *params.ChainConfig
87-
sender = common.StringToAddress("sender")
88-
receiver = common.StringToAddress("receiver")
87+
sender = common.BytesToAddress([]byte("sender"))
88+
receiver = common.BytesToAddress([]byte("receiver"))
8989
)
9090
if ctx.GlobalBool(MachineFlag.Name) {
9191
tracer = NewJSONLogger(logconfig, os.Stdout)

common/types.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,8 @@ func BytesToHash(b []byte) Hash {
4545
h.SetBytes(b)
4646
return h
4747
}
48-
func StringToHash(s string) Hash { return BytesToHash([]byte(s)) }
49-
func BigToHash(b *big.Int) Hash { return BytesToHash(b.Bytes()) }
50-
func HexToHash(s string) Hash { return BytesToHash(FromHex(s)) }
48+
func BigToHash(b *big.Int) Hash { return BytesToHash(b.Bytes()) }
49+
func HexToHash(s string) Hash { return BytesToHash(FromHex(s)) }
5150

5251
// Get the string representation of the underlying hash
5352
func (h Hash) Str() string { return string(h[:]) }
@@ -143,9 +142,8 @@ func BytesToAddress(b []byte) Address {
143142
a.SetBytes(b)
144143
return a
145144
}
146-
func StringToAddress(s string) Address { return BytesToAddress([]byte(s)) }
147-
func BigToAddress(b *big.Int) Address { return BytesToAddress(b.Bytes()) }
148-
func HexToAddress(s string) Address { return BytesToAddress(FromHex(s)) }
145+
func BigToAddress(b *big.Int) Address { return BytesToAddress(b.Bytes()) }
146+
func HexToAddress(s string) Address { return BytesToAddress(FromHex(s)) }
149147

150148
// IsHexAddress verifies whether a string can represent a valid hex-encoded
151149
// Ethereum address or not.

core/vm/runtime/runtime.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func Execute(code, input []byte, cfg *Config) ([]byte, *state.StateDB, error) {
103103
cfg.State, _ = state.New(common.Hash{}, state.NewDatabase(db))
104104
}
105105
var (
106-
address = common.StringToAddress("contract")
106+
address = common.BytesToAddress([]byte("contract"))
107107
vmenv = NewEnv(cfg)
108108
sender = vm.AccountRef(cfg.Origin)
109109
)
@@ -113,7 +113,7 @@ func Execute(code, input []byte, cfg *Config) ([]byte, *state.StateDB, error) {
113113
// Call the code with the given configuration.
114114
ret, _, err := vmenv.Call(
115115
sender,
116-
common.StringToAddress("contract"),
116+
common.BytesToAddress([]byte("contract")),
117117
input,
118118
cfg.GasLimit,
119119
cfg.Value,

eth/filters/api_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ func TestUnmarshalJSONNewFilterArgs(t *testing.T) {
2929
var (
3030
fromBlock rpc.BlockNumber = 0x123435
3131
toBlock rpc.BlockNumber = 0xabcdef
32-
address0 = common.StringToAddress("70c87d191324e6712a591f304b4eedef6ad9bb9d")
33-
address1 = common.StringToAddress("9b2055d370f73ec7d8a03e965129118dc8f5bf83")
32+
address0 = common.HexToAddress("70c87d191324e6712a591f304b4eedef6ad9bb9d")
33+
address1 = common.HexToAddress("9b2055d370f73ec7d8a03e965129118dc8f5bf83")
3434
topic0 = common.HexToHash("3ac225168df54212a25c1c01fd35bebfea408fdac2e31ddd6f80a4bbf9a5f1ca")
3535
topic1 = common.HexToHash("9084a792d2f8b16a62b882fd56f7860c07bf5fa91dd8a2ae7e809e5180fef0b3")
3636
topic2 = common.HexToHash("6ccae1c4af4152f460ff510e573399795dfab5dcf1fa60d1f33ac8fdc1e480ce")

0 commit comments

Comments
 (0)