From 922ceecd981cbae753b88a59c37e325b1432bb31 Mon Sep 17 00:00:00 2001 From: Gary Rong Date: Tue, 28 Mar 2023 16:02:30 +0800 Subject: [PATCH 1/2] common: fix the json marshaller of MixedcaseAddress --- common/types.go | 2 +- common/types_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/common/types.go b/common/types.go index 218ca0be4c44..0b68a19dd319 100644 --- a/common/types.go +++ b/common/types.go @@ -400,7 +400,7 @@ func (ma *MixedcaseAddress) UnmarshalJSON(input []byte) error { } // MarshalJSON marshals the original value -func (ma *MixedcaseAddress) MarshalJSON() ([]byte, error) { +func (ma MixedcaseAddress) MarshalJSON() ([]byte, error) { if strings.HasPrefix(ma.original, "0x") || strings.HasPrefix(ma.original, "0X") { return json.Marshal(fmt.Sprintf("0x%s", ma.original[2:])) } diff --git a/common/types_test.go b/common/types_test.go index 94492278d84a..f18335f63017 100644 --- a/common/types_test.go +++ b/common/types_test.go @@ -177,7 +177,7 @@ func TestMixedcaseAccount_Address(t *testing.T) { } } - //These should throw exceptions: + // These should throw exceptions: var r2 []MixedcaseAddress for _, r := range []string{ `["0x11111111111111111111122222222222233333"]`, // Too short From 89d1ddf386a227660b31293503ba83b13331fded Mon Sep 17 00:00:00 2001 From: Gary Rong Date: Tue, 28 Mar 2023 19:39:06 +0800 Subject: [PATCH 2/2] common: add test --- common/types_test.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/common/types_test.go b/common/types_test.go index f18335f63017..88c642522d80 100644 --- a/common/types_test.go +++ b/common/types_test.go @@ -154,6 +154,31 @@ func BenchmarkAddressHex(b *testing.B) { } } +// Test checks if the customized json marshaller of MixedcaseAddress object +// is invoked correctly. In golang the struct pointer will inherit the +// non-pointer receiver methods, the reverse is not true. In the case of +// MixedcaseAddress, it must define the MarshalJSON method in the object +// but not the pointer level, so that this customized marshalled can be used +// for both MixedcaseAddress object and pointer. +func TestMixedcaseAddressMarshal(t *testing.T) { + var ( + output string + input = "0xae967917c465db8578ca9024c205720b1a3651A9" + ) + addr, err := NewMixedcaseAddressFromString(input) + if err != nil { + t.Fatal(err) + } + blob, err := json.Marshal(*addr) + if err != nil { + t.Fatal(err) + } + json.Unmarshal(blob, &output) + if output != input { + t.Fatal("Failed to marshal/unmarshal MixedcaseAddress object") + } +} + func TestMixedcaseAccount_Address(t *testing.T) { // https://github.com/ethereum/EIPs/blob/master/EIPS/eip-55.md // Note: 0X{checksum_addr} is not valid according to spec above