Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zero-pad string representation of addresses #1214

Merged
merged 1 commit into from Nov 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions runtime/common/address.go
Expand Up @@ -87,6 +87,10 @@ func (a Address) ShortHexWithPrefix() string {
return fmt.Sprintf("0x%s", strings.TrimLeft(hexString, "0"))
}

func (a Address) HexWithPrefix() string {
return fmt.Sprintf("0x%x", [AddressLength]byte(a))
}

// HexToAddress converts a hex string to an Address.
func HexToAddress(h string) (Address, error) {
trimmed := strings.TrimPrefix(h, "0x")
Expand Down
20 changes: 20 additions & 0 deletions runtime/common/address_test.go
Expand Up @@ -144,6 +144,26 @@ func TestAddress_ShortHexWithPrefix(t *testing.T) {
)
}

func TestAddress_HexWithPrefix(t *testing.T) {

t.Parallel()

assert.Equal(t,
"0x1234567890abcdef",
Address{0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef}.HexWithPrefix(),
)

assert.Equal(t,
"0x0100000000000000",
Address{0x1}.HexWithPrefix(),
)

assert.Equal(t,
"0x0000000000000001",
Address{0, 0, 0, 0, 0, 0, 0, 0x1}.HexWithPrefix(),
)
}

func TestAddress_HexToAddress(t *testing.T) {

t.Parallel()
Expand Down
2 changes: 1 addition & 1 deletion runtime/common/addresslocation.go
Expand Up @@ -88,7 +88,7 @@ func (l AddressLocation) MarshalJSON() ([]byte, error) {
Name string
}{
Type: "AddressLocation",
Address: l.Address.ShortHexWithPrefix(),
Address: l.Address.HexWithPrefix(),
Name: l.Name,
})
}
Expand Down
2 changes: 1 addition & 1 deletion runtime/common/addresslocation_test.go
Expand Up @@ -42,7 +42,7 @@ func TestAddressLocation_MarshalJSON(t *testing.T) {
`
{
"Type": "AddressLocation",
"Address": "0x1",
"Address": "0x0000000000000001",
"Name": "A"
}
`,
Expand Down
2 changes: 1 addition & 1 deletion runtime/format/address.go
Expand Up @@ -23,5 +23,5 @@ import (
)

func Address(address common.Address) string {
return address.ShortHexWithPrefix()
return address.HexWithPrefix()
}
2 changes: 1 addition & 1 deletion runtime/interpreter/errors_test.go
Expand Up @@ -37,6 +37,6 @@ func TestOverwriteError_Error(t *testing.T) {
Identifier: "test",
},
},
"failed to save object: path /storage/test in account 0x1 already stores an object",
"failed to save object: path /storage/test in account 0x0000000000000001 already stores an object",
)
}
6 changes: 3 additions & 3 deletions runtime/interpreter/value_test.go
Expand Up @@ -913,7 +913,7 @@ func TestStringer(t *testing.T) {
},
"Address": {
value: NewAddressValue(common.Address{0, 0, 0, 0, 0, 0, 0, 1}),
expected: "0x1",
expected: "0x0000000000000001",
},
"composite": {
value: func() Value {
Expand Down Expand Up @@ -995,7 +995,7 @@ func TestStringer(t *testing.T) {
Address: NewAddressValueFromBytes([]byte{1, 2, 3, 4, 5}),
BorrowType: PrimitiveStaticTypeInt,
},
expected: "Capability<Int>(address: 0x102030405, path: /storage/foo)",
expected: "Capability<Int>(address: 0x0000000102030405, path: /storage/foo)",
},
"Capability without borrow type": {
value: &CapabilityValue{
Expand All @@ -1005,7 +1005,7 @@ func TestStringer(t *testing.T) {
},
Address: NewAddressValueFromBytes([]byte{1, 2, 3, 4, 5}),
},
expected: "Capability(address: 0x102030405, path: /storage/foo)",
expected: "Capability(address: 0x0000000102030405, path: /storage/foo)",
},
"Recursive ephemeral reference (array)": {
value: func() Value {
Expand Down
76 changes: 38 additions & 38 deletions runtime/runtime_test.go
Expand Up @@ -809,7 +809,7 @@ func TestRuntimeTransactionWithAccount(t *testing.T) {
)
require.NoError(t, err)

assert.Equal(t, "0x2a", loggedMessage)
assert.Equal(t, "0x000000000000002a", loggedMessage)
}

func TestRuntimeTransactionWithArguments(t *testing.T) {
Expand Down Expand Up @@ -857,7 +857,7 @@ func TestRuntimeTransactionWithArguments(t *testing.T) {
jsoncdc.MustEncode(cadence.NewInt(42)),
},
authorizers: []Address{common.BytesToAddress([]byte{42})},
expectedLogs: []string{"0x2a", "42"},
expectedLogs: []string{"0x000000000000002a", "42"},
},
{
label: "Multiple arguments",
Expand Down Expand Up @@ -926,7 +926,7 @@ func TestRuntimeTransactionWithArguments(t *testing.T) {
),
),
},
expectedLogs: []string{"0x1"},
expectedLogs: []string{"0x0000000000000001"},
},
{
label: "Array",
Expand Down Expand Up @@ -1222,7 +1222,7 @@ func TestRuntimeScriptArguments(t *testing.T) {
),
),
},
expectedLogs: []string{"0x1"},
expectedLogs: []string{"0x0000000000000001"},
},
{
name: "Array",
Expand Down Expand Up @@ -2751,7 +2751,7 @@ func TestRuntimeAccountAddress(t *testing.T) {
)
require.NoError(t, err)

assert.Equal(t, []string{"0x2a"}, loggedMessages)
assert.Equal(t, []string{"0x000000000000002a"}, loggedMessages)
}

func TestRuntimePublicAccountAddress(t *testing.T) {
Expand Down Expand Up @@ -3226,7 +3226,7 @@ func TestRuntimeInvokeContractFunction(t *testing.T) {
)
require.NoError(tt, err)

assert.Equal(tt, `"Hello number 42 from 0x1"`, loggedMessage)
assert.Equal(tt, `"Hello number 42 from 0x0000000000000001"`, loggedMessage)
})

t.Run("function with not enough arguments panics", func(tt *testing.T) {
Expand Down Expand Up @@ -3294,7 +3294,7 @@ func TestRuntimeInvokeContractFunction(t *testing.T) {
)
require.NoError(tt, err)

assert.Equal(tt, `"Hello 0x1"`, loggedMessage)
assert.Equal(tt, `"Hello 0x0000000000000001"`, loggedMessage)
})
t.Run("function with public account works", func(tt *testing.T) {
_, err = runtime.InvokeContractFunction(
Expand All @@ -3316,7 +3316,7 @@ func TestRuntimeInvokeContractFunction(t *testing.T) {
)
require.NoError(tt, err)

assert.Equal(tt, `"Hello pub 0x1"`, loggedMessage)
assert.Equal(tt, `"Hello pub 0x0000000000000001"`, loggedMessage)
})
}

Expand Down Expand Up @@ -4716,8 +4716,8 @@ func TestRuntimeResourceOwnerFieldUseComposite(t *testing.T) {
assert.Equal(t,
[]string{
"nil", "nil",
"0x1", "0x1",
"0x1", "0x1",
"0x0000000000000001", "0x0000000000000001",
"0x0000000000000001", "0x0000000000000001",
},
loggedMessages,
)
Expand All @@ -4736,21 +4736,21 @@ func TestRuntimeResourceOwnerFieldUseComposite(t *testing.T) {

assert.Equal(t,
[]string{
"0x1", // ref1.owner?.address
"123.00000000", // ref2.owner?.balance
"1523.00000000", // ref2.owner?.availableBalance
"120", // ref1.owner?.storageUsed
"1245", // ref1.owner?.storageCapacity
"0x0000000000000001", // ref1.owner?.address
"123.00000000", // ref2.owner?.balance
"1523.00000000", // ref2.owner?.availableBalance
"120", // ref1.owner?.storageUsed
"1245", // ref1.owner?.storageCapacity

"0x1",
"0x0000000000000001",

"0x1", // ref2.owner?.address
"123.00000000", // ref2.owner?.balance
"1523.00000000", // ref2.owner?.availableBalance
"120", // ref2.owner?.storageUsed
"1245", // ref2.owner?.storageCapacity
"0x0000000000000001", // ref2.owner?.address
"123.00000000", // ref2.owner?.balance
"1523.00000000", // ref2.owner?.availableBalance
"120", // ref2.owner?.storageUsed
"1245", // ref2.owner?.storageCapacity

"0x1",
"0x0000000000000001",
},
loggedMessages,
)
Expand Down Expand Up @@ -4906,10 +4906,10 @@ func TestRuntimeResourceOwnerFieldUseArray(t *testing.T) {
[]string{
"nil", "nil",
"nil", "nil",
"0x1", "0x1",
"0x1", "0x1",
"0x1", "0x1",
"0x1", "0x1",
"0x0000000000000001", "0x0000000000000001",
"0x0000000000000001", "0x0000000000000001",
"0x0000000000000001", "0x0000000000000001",
"0x0000000000000001", "0x0000000000000001",
},
loggedMessages,
)
Expand All @@ -4928,10 +4928,10 @@ func TestRuntimeResourceOwnerFieldUseArray(t *testing.T) {

assert.Equal(t,
[]string{
"0x1", "0x1",
"0x1", "0x1",
"0x1", "0x1",
"0x1", "0x1",
"0x0000000000000001", "0x0000000000000001",
"0x0000000000000001", "0x0000000000000001",
"0x0000000000000001", "0x0000000000000001",
"0x0000000000000001", "0x0000000000000001",
},
loggedMessages,
)
Expand Down Expand Up @@ -5087,10 +5087,10 @@ func TestRuntimeResourceOwnerFieldUseDictionary(t *testing.T) {
[]string{
"nil", "nil",
"nil", "nil",
"0x1", "0x1",
"0x1", "0x1",
"0x1", "0x1",
"0x1", "0x1",
"0x0000000000000001", "0x0000000000000001",
"0x0000000000000001", "0x0000000000000001",
"0x0000000000000001", "0x0000000000000001",
"0x0000000000000001", "0x0000000000000001",
},
loggedMessages,
)
Expand All @@ -5109,10 +5109,10 @@ func TestRuntimeResourceOwnerFieldUseDictionary(t *testing.T) {

assert.Equal(t,
[]string{
"0x1", "0x1",
"0x1", "0x1",
"0x1", "0x1",
"0x1", "0x1",
"0x0000000000000001", "0x0000000000000001",
"0x0000000000000001", "0x0000000000000001",
"0x0000000000000001", "0x0000000000000001",
"0x0000000000000001", "0x0000000000000001",
},
loggedMessages,
)
Expand Down
2 changes: 1 addition & 1 deletion runtime/tests/interpreter/builtinfunctions_test.go
Expand Up @@ -67,7 +67,7 @@ func TestInterpretToString(t *testing.T) {
AssertValuesEqual(
t,
inter,
interpreter.NewStringValue("0x42"),
interpreter.NewStringValue("0x0000000000000042"),
inter.Globals["y"].GetValue(),
)
})
Expand Down
4 changes: 2 additions & 2 deletions values_test.go
Expand Up @@ -170,7 +170,7 @@ func TestStringer(t *testing.T) {
},
"Address": {
value: NewAddress([8]byte{0, 0, 0, 0, 0, 0, 0, 1}),
expected: "0x1",
expected: "0x0000000000000001",
},
"struct": {
value: NewStruct([]Value{String("bar")}).WithType(&StructType{
Expand Down Expand Up @@ -260,7 +260,7 @@ func TestStringer(t *testing.T) {
Address: BytesToAddress([]byte{1, 2, 3, 4, 5}),
BorrowType: "Int",
},
expected: "Capability<Int>(address: 0x102030405, path: /storage/foo)",
expected: "Capability<Int>(address: 0x0000000102030405, path: /storage/foo)",
},
}

Expand Down