Skip to content

Commit

Permalink
nits
Browse files Browse the repository at this point in the history
  • Loading branch information
ceyonur committed Dec 14, 2023
1 parent 7049cf5 commit 5ba8195
Show file tree
Hide file tree
Showing 8 changed files with 237 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func init() {
}
// MakeConfig returns a new precompile config instance.
// This is required for Marshal/Unmarshal the precompile config.
// This is required to Marshal/Unmarshal the precompile config.
func (*configurator) MakeConfig() precompileconfig.Config {
return new(Config)
}
Expand Down
2 changes: 1 addition & 1 deletion precompile/contracts/deployerallowlist/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func init() {
}

// MakeConfig returns a new precompile config instance.
// This is required for Marshal/Unmarshal the precompile config.
// This is required to Marshal/Unmarshal the precompile config.
func (*configurator) MakeConfig() precompileconfig.Config {
return new(Config)
}
Expand Down
2 changes: 1 addition & 1 deletion precompile/contracts/feemanager/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func init() {
}

// MakeConfig returns a new precompile config instance.
// This is required for Marshal/Unmarshal the precompile config.
// This is required to Marshal/Unmarshal the precompile config.
func (*configurator) MakeConfig() precompileconfig.Config {
return new(Config)
}
Expand Down
206 changes: 154 additions & 52 deletions precompile/contracts/feemanager/unpack_pack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,42 +52,91 @@ func FuzzPackGetFeeConfigOutputEqualTest(f *testing.F) {
})
}

func TestPackUnpackGetFeeConfigOutputEdgeCases(t *testing.T) {
func TestOldPackGetFeeConfigOutputEqual(t *testing.T) {
testOldPackGetFeeConfigOutputEqual(t, testFeeConfig, true)
// These should panic
}
func TestPackGetFeeConfigOutputPanic(t *testing.T) {
require.Panics(t, func() {
_, _ = OldPackFeeConfig(commontype.FeeConfig{})
})
require.Panics(t, func() {
_, _ = PackGetFeeConfigOutput(commontype.FeeConfig{})
})
}

unpacked, err := OldUnpackFeeConfig([]byte{})
require.ErrorIs(t, err, ErrInvalidLen)
unpacked2, err := UnpackGetFeeConfigOutput([]byte{}, false)
require.ErrorIs(t, err, ErrInvalidLen)
require.Equal(t, unpacked, unpacked2)

_, err = UnpackGetFeeConfigOutput([]byte{}, true)
require.Error(t, err)

// Test for extra padded bytes
input, err := PackGetFeeConfigOutput(testFeeConfig)
func TestPackGetFeeConfigOutput(t *testing.T) {
testInputBytes, err := PackGetFeeConfigOutput(testFeeConfig)
require.NoError(t, err)
// add extra padded bytes
input = append(input, make([]byte, 32)...)
_, err = OldUnpackFeeConfig(input)
require.ErrorIs(t, err, ErrInvalidLen)
_, err = UnpackGetFeeConfigOutput([]byte{}, false)
require.ErrorIs(t, err, ErrInvalidLen)

_, err = UnpackGetFeeConfigOutput(input, true)
require.NoError(t, err)

// now it's now divisible by 32
input = append(input, make([]byte, 1)...)
_, err = UnpackGetFeeConfigOutput(input, true)
require.Error(t, err)
tests := []struct {
name string
input []byte
skipLenCheck bool
expectedErr string
expectedOldErr string
expectedOutput commontype.FeeConfig
}{
{
name: "empty input",
input: []byte{},
skipLenCheck: false,
expectedErr: ErrInvalidLen.Error(),
expectedOldErr: ErrInvalidLen.Error(),
},
{
name: "empty input skip len check",
input: []byte{},
skipLenCheck: true,
expectedErr: "attempting to unmarshall an empty string",
expectedOldErr: ErrInvalidLen.Error(),
},
{
name: "input with extra bytes",
input: append(testInputBytes, make([]byte, 32)...),
skipLenCheck: false,
expectedErr: ErrInvalidLen.Error(),
expectedOldErr: ErrInvalidLen.Error(),
},
{
name: "input with extra bytes skip len check",
input: append(testInputBytes, make([]byte, 32)...),
skipLenCheck: true,
expectedErr: "",
expectedOldErr: ErrInvalidLen.Error(),
expectedOutput: testFeeConfig,
},
{
name: "input with extra bytes (not divisible by 32)",
input: append(testInputBytes, make([]byte, 33)...),
skipLenCheck: false,
expectedErr: ErrInvalidLen.Error(),
expectedOldErr: ErrInvalidLen.Error(),
},
{
name: "input with extra bytes (not divisible by 32) skip len check",
input: append(testInputBytes, make([]byte, 33)...),
skipLenCheck: true,
expectedErr: "improperly formatted output",
expectedOldErr: ErrInvalidLen.Error(),
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
unpacked, err := UnpackGetFeeConfigOutput(test.input, test.skipLenCheck)
if test.expectedErr != "" {
require.ErrorContains(t, err, test.expectedErr)
} else {
require.NoError(t, err)
require.True(t, test.expectedOutput.Equal(&unpacked), "not equal: expectedOutput %v, unpacked %v", test.expectedOutput, unpacked)
}
oldUnpacked, oldErr := OldUnpackFeeConfig(test.input)
if test.expectedOldErr != "" {
require.ErrorContains(t, oldErr, test.expectedOldErr)
} else {
require.NoError(t, oldErr)
require.True(t, test.expectedOutput.Equal(&oldUnpacked), "not equal: expectedOutput %v, oldUnpacked %v", test.expectedOutput, oldUnpacked)
}
})
}
}

func TestGetFeeConfig(t *testing.T) {
Expand Down Expand Up @@ -163,41 +212,94 @@ func FuzzPackSetFeeConfigEqualTest(f *testing.F) {
})
}

func TestPackSetFeeConfigInputEdgeCases(t *testing.T) {
// Some edge cases
func TestOldPackSetFeeConfigInputEqual(t *testing.T) {
testOldPackSetFeeConfigInputEqual(t, testFeeConfig, true)
// These should panic
}

func TestPackSetFeeConfigInputPanic(t *testing.T) {
require.Panics(t, func() {
_, _ = OldPackSetFeeConfig(commontype.FeeConfig{})
})
require.Panics(t, func() {
_, _ = PackSetFeeConfig(commontype.FeeConfig{})
})
// These should err
_, err := UnpackSetFeeConfigInput([]byte{123}, false)
require.ErrorIs(t, err, ErrInvalidLen)

_, err = UnpackSetFeeConfigInput([]byte{123}, true)
require.ErrorContains(t, err, "abi: improperly formatted input")

_, err = OldUnpackFeeConfig([]byte{123})
require.ErrorIs(t, err, ErrInvalidLen)
}

// Test for extra padded bytes
input, err := PackSetFeeConfig(testFeeConfig)
func TestPackSetFeeConfigInput(t *testing.T) {
testInputBytes, err := PackSetFeeConfig(testFeeConfig)
require.NoError(t, err)
// exclude 4 bytes for function selector
input = input[4:]
// add extra padded bytes
input = append(input, make([]byte, 32)...)
_, err = OldUnpackFeeConfig(input)
require.ErrorIs(t, err, ErrInvalidLen)
_, err = UnpackSetFeeConfigInput(input, false)
require.ErrorIs(t, err, ErrInvalidLen)

unpacked, err := UnpackSetFeeConfigInput(input, true)
require.NoError(t, err)
require.True(t, testFeeConfig.Equal(&unpacked))
testInputBytes = testInputBytes[4:]
tests := []struct {
name string
input []byte
skipLenCheck bool
expectedErr string
expectedOldErr string
expectedOutput commontype.FeeConfig
}{
{
name: "empty input",
input: []byte{},
skipLenCheck: false,
expectedErr: ErrInvalidLen.Error(),
expectedOldErr: ErrInvalidLen.Error(),
},
{
name: "empty input skip len check",
input: []byte{},
skipLenCheck: true,
expectedErr: "attempting to unmarshall an empty string",
expectedOldErr: ErrInvalidLen.Error(),
},
{
name: "input with extra bytes",
input: append(testInputBytes, make([]byte, 32)...),
skipLenCheck: false,
expectedErr: ErrInvalidLen.Error(),
expectedOldErr: ErrInvalidLen.Error(),
},
{
name: "input with extra bytes skip len check",
input: append(testInputBytes, make([]byte, 32)...),
skipLenCheck: true,
expectedErr: "",
expectedOldErr: ErrInvalidLen.Error(),
expectedOutput: testFeeConfig,
},
{
name: "input with extra bytes (not divisible by 32)",
input: append(testInputBytes, make([]byte, 33)...),
skipLenCheck: false,
expectedErr: ErrInvalidLen.Error(),
expectedOldErr: ErrInvalidLen.Error(),
},
{
name: "input with extra bytes (not divisible by 32) skip len check",
input: append(testInputBytes, make([]byte, 33)...),
skipLenCheck: true,
expectedErr: "improperly formatted input",
expectedOldErr: ErrInvalidLen.Error(),
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
unpacked, err := UnpackSetFeeConfigInput(test.input, test.skipLenCheck)
if test.expectedErr != "" {
require.ErrorContains(t, err, test.expectedErr)
} else {
require.NoError(t, err)
require.True(t, test.expectedOutput.Equal(&unpacked), "not equal: expectedOutput %v, unpacked %v", test.expectedOutput, unpacked)
}
oldUnpacked, oldErr := OldUnpackFeeConfig(test.input)
if test.expectedOldErr != "" {
require.ErrorContains(t, oldErr, test.expectedOldErr)
} else {
require.NoError(t, oldErr)
require.True(t, test.expectedOutput.Equal(&oldUnpacked), "not equal: expectedOutput %v, oldUnpacked %v", test.expectedOutput, oldUnpacked)
}
})
}
}

func TestFunctionSignatures(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion precompile/contracts/nativeminter/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func init() {
}

// MakeConfig returns a new precompile config instance.
// This is required for Marshal/Unmarshal the precompile config.
// This is required to Marshal/Unmarshal the precompile config.
func (*configurator) MakeConfig() precompileconfig.Config {
return new(Config)
}
Expand Down
98 changes: 77 additions & 21 deletions precompile/contracts/nativeminter/unpack_pack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,29 +44,85 @@ func FuzzPackMintNativeCoinEqualTest(f *testing.F) {
})
}

func TestUnpackMintNativeCoinInputEdgeCases(t *testing.T) {
input, err := PackMintNativeCoin(constants.BlackholeAddr, common.Big2)
func TestUnpackMintNativeCoinInput(t *testing.T) {
testInputBytes, err := PackMintNativeCoin(constants.BlackholeAddr, common.Big2)
require.NoError(t, err)
// exclude 4 bytes for function selector
input = input[4:]
// add extra padded bytes
input = append(input, make([]byte, 32)...)

_, _, err = OldUnpackMintNativeCoinInput(input)
require.ErrorIs(t, err, ErrInvalidLen)

_, _, err = UnpackMintNativeCoinInput(input, false)
require.ErrorIs(t, err, ErrInvalidLen)

addr, value, err := UnpackMintNativeCoinInput(input, true)
require.NoError(t, err)
require.Equal(t, constants.BlackholeAddr, addr)
require.Equal(t, common.Big2.Bytes(), value.Bytes())

input = append(input, make([]byte, 1)...)
// now it is not divisible by 32
_, _, err = UnpackMintNativeCoinInput(input, true)
require.Error(t, err)
testInputBytes = testInputBytes[4:]
tests := []struct {
name string
input []byte
skipLenCheck bool
expectedErr string
expectedOldErr string
expectedAddr common.Address
expectedAmount *big.Int
}{
{
name: "empty input",
input: []byte{},
skipLenCheck: false,
expectedErr: ErrInvalidLen.Error(),
expectedOldErr: ErrInvalidLen.Error(),
},
{
name: "empty input skip len check",
input: []byte{},
skipLenCheck: true,
expectedErr: "attempting to unmarshall an empty string",
expectedOldErr: ErrInvalidLen.Error(),
},
{
name: "input with extra bytes",
input: append(testInputBytes, make([]byte, 32)...),
skipLenCheck: false,
expectedErr: ErrInvalidLen.Error(),
expectedOldErr: ErrInvalidLen.Error(),
},
{
name: "input with extra bytes skip len check",
input: append(testInputBytes, make([]byte, 32)...),
skipLenCheck: true,
expectedErr: "",
expectedOldErr: ErrInvalidLen.Error(),
expectedAddr: constants.BlackholeAddr,
expectedAmount: common.Big2,
},
{
name: "input with extra bytes (not divisible by 32)",
input: append(testInputBytes, make([]byte, 33)...),
skipLenCheck: false,
expectedErr: ErrInvalidLen.Error(),
expectedOldErr: ErrInvalidLen.Error(),
},
{
name: "input with extra bytes (not divisible by 32) skip len check",
input: append(testInputBytes, make([]byte, 33)...),
skipLenCheck: true,
expectedErr: "improperly formatted input",
expectedOldErr: ErrInvalidLen.Error(),
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
unpackedAddress, unpackedAmount, err := UnpackMintNativeCoinInput(test.input, test.skipLenCheck)
if test.expectedErr != "" {
require.ErrorContains(t, err, test.expectedErr)
} else {
require.NoError(t, err)
require.Equal(t, test.expectedAddr, unpackedAddress)
require.True(t, test.expectedAmount.Cmp(unpackedAmount) == 0, "expected %s, got %s", test.expectedAmount.String(), unpackedAmount.String())
}
oldUnpackedAddress, oldUnpackedAmount, oldErr := OldUnpackMintNativeCoinInput(test.input)
if test.expectedOldErr != "" {
require.ErrorContains(t, oldErr, test.expectedOldErr)
} else {
require.NoError(t, oldErr)
require.Equal(t, test.expectedAddr, oldUnpackedAddress)
require.True(t, test.expectedAmount.Cmp(oldUnpackedAmount) == 0, "expected %s, got %s", test.expectedAmount.String(), oldUnpackedAmount.String())
}
})
}
}

func TestFunctionSignatures(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion precompile/contracts/txallowlist/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func init() {
}

// MakeConfig returns a new precompile config instance.
// This is required for Marshal/Unmarshal the precompile config.
// This is required to Marshal/Unmarshal the precompile config.
func (*configurator) MakeConfig() precompileconfig.Config {
return new(Config)
}
Expand Down
2 changes: 1 addition & 1 deletion x/warp/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func init() {
}

// MakeConfig returns a new precompile config instance.
// This is required for Marshal/Unmarshal the precompile config.
// This is required to Marshal/Unmarshal the precompile config.
func (*configurator) MakeConfig() precompileconfig.Config {
return new(Config)
}
Expand Down

0 comments on commit 5ba8195

Please sign in to comment.