From b44df9c939ad103e23e4c4f2a5b8cbfeb06acf42 Mon Sep 17 00:00:00 2001 From: Jonathan Oppenheimer Date: Thu, 4 Dec 2025 12:26:43 -0500 Subject: [PATCH 1/2] chore: remove old pack/unpack code --- precompile/allowlist/unpack_pack_test.go | 124 ------ .../contracts/feemanager/unpack_pack_test.go | 373 +++++------------- .../nativeminter/unpack_pack_test.go | 120 ++---- 3 files changed, 138 insertions(+), 479 deletions(-) diff --git a/precompile/allowlist/unpack_pack_test.go b/precompile/allowlist/unpack_pack_test.go index 727e345dd7..3f97111532 100644 --- a/precompile/allowlist/unpack_pack_test.go +++ b/precompile/allowlist/unpack_pack_test.go @@ -51,20 +51,6 @@ func FuzzPackReadAllowlistTest(f *testing.F) { }) } -func FuzzPackReadAllowlistTestSkipCheck(f *testing.F) { - f.Fuzz(func(t *testing.T, b []byte) { - require := require.New(t) - res, err := UnpackReadAllowListInput(b, true) - oldRes, oldErr := OldUnpackReadAllowList(b) - if oldErr != nil { - require.ErrorContains(err, oldErr.Error()) - } else { - require.NoError(err) - } - require.Equal(oldRes, res) - }) -} - func TestPackReadAllowlistTest(f *testing.T) { testPackReadAllowlistTest(f, common.Address{}) } @@ -73,7 +59,6 @@ func testPackReadAllowlistTest(t *testing.T, address common.Address) { t.Helper() t.Run(fmt.Sprintf("TestPackReadAllowlistTest, address %v", address), func(t *testing.T) { require := require.New(t) - // use new Pack/Unpack methods input, err := PackReadAllowList(address) require.NoError(err) // exclude 4 bytes for function selector @@ -81,30 +66,6 @@ func testPackReadAllowlistTest(t *testing.T, address common.Address) { unpacked, err := UnpackReadAllowListInput(input, true) require.NoError(err) require.Equal(address, unpacked) - - // use old Pack/Unpack methods - input = OldPackReadAllowList(address) - // exclude 4 bytes for function selector - input = input[4:] - require.NoError(err) - unpacked, err = OldUnpackReadAllowList(input) - require.NoError(err) - require.Equal(address, unpacked) - - // now mix and match old and new methods - input, err = PackReadAllowList(address) - require.NoError(err) - // exclude 4 bytes for function selector - input = input[4:] - input2 := OldPackReadAllowList(address) - // exclude 4 bytes for function selector - input2 = input2[4:] - require.Equal(input, input2) - unpacked, err = UnpackReadAllowListInput(input2, true) - require.NoError(err) - unpacked2, err := OldUnpackReadAllowList(input) - require.NoError(err) - require.Equal(unpacked, unpacked2) }) } @@ -119,25 +80,10 @@ func FuzzPackModifyAllowListTest(f *testing.F) { }) } -func FuzzPackModifyAllowlistTestSkipCheck(f *testing.F) { - f.Fuzz(func(t *testing.T, b []byte) { - require := require.New(t) - res, err := UnpackModifyAllowListInput(b, AdminRole, true) - oldRes, oldErr := OldUnpackModifyAllowList(b, AdminRole) - if oldErr != nil { - require.ErrorContains(err, oldErr.Error()) - } else { - require.NoError(err) - } - require.Equal(oldRes, res) - }) -} - func testPackModifyAllowListTest(t *testing.T, address common.Address, role Role) { t.Helper() t.Run(fmt.Sprintf("TestPackModifyAllowlistTest, address %v, role %s", address, role.String()), func(t *testing.T) { require := require.New(t) - // use new Pack/Unpack methods input, err := PackModifyAllowList(address, role) require.NoError(err) // exclude 4 bytes for function selector @@ -145,34 +91,6 @@ func testPackModifyAllowListTest(t *testing.T, address common.Address, role Role unpacked, err := UnpackModifyAllowListInput(input, role, true) require.NoError(err) require.Equal(address, unpacked) - - // use old Pack/Unpack methods - input, err = OldPackModifyAllowList(address, role) - require.NoError(err) - // exclude 4 bytes for function selector - input = input[4:] - require.NoError(err) - - unpacked, err = OldUnpackModifyAllowList(input, role) - require.NoError(err) - - require.Equal(address, unpacked) - - // now mix and match new and old methods - input, err = PackModifyAllowList(address, role) - require.NoError(err) - // exclude 4 bytes for function selector - input = input[4:] - input2, err := OldPackModifyAllowList(address, role) - require.NoError(err) - // exclude 4 bytes for function selector - input2 = input2[4:] - require.Equal(input, input2) - unpacked, err = UnpackModifyAllowListInput(input2, role, true) - require.NoError(err) - unpacked2, err := OldUnpackModifyAllowList(input, role) - require.NoError(err) - require.Equal(unpacked, unpacked2) }) } @@ -185,48 +103,6 @@ func FuzzPackReadAllowListOutputTest(f *testing.F) { }) } -func OldPackReadAllowList(address common.Address) []byte { - input := make([]byte, 0, contract.SelectorLen+common.HashLength) - input = append(input, readAllowListSignature...) - input = append(input, common.BytesToHash(address[:]).Bytes()...) - return input -} - -func OldUnpackReadAllowList(input []byte) (common.Address, error) { - if len(input) != allowListInputLen { - return common.Address{}, fmt.Errorf("invalid input length for read allow list: %d", len(input)) - } - return common.BytesToAddress(input), nil -} - -func OldPackModifyAllowList(address common.Address, role Role) ([]byte, error) { - // function selector (4 bytes) + hash for address - input := make([]byte, 0, contract.SelectorLen+common.HashLength) - - switch role { - case AdminRole: - input = append(input, setAdminSignature...) - case ManagerRole: - input = append(input, setManagerSignature...) - case EnabledRole: - input = append(input, setEnabledSignature...) - case NoRole: - input = append(input, setNoneSignature...) - default: - return nil, fmt.Errorf("cannot pack modify list input with invalid role: %s", role) - } - - input = append(input, common.BytesToHash(address[:]).Bytes()...) - return input, nil -} - -func OldUnpackModifyAllowList(input []byte, _ Role) (common.Address, error) { - if len(input) != allowListInputLen { - return common.Address{}, fmt.Errorf("invalid input length for modifying allow list: %d", len(input)) - } - return common.BytesToAddress(input), nil -} - func getRole(roleIndex uint) Role { index := roleIndex % 4 switch index { diff --git a/precompile/contracts/feemanager/unpack_pack_test.go b/precompile/contracts/feemanager/unpack_pack_test.go index ea8cb590f6..8d0507895d 100644 --- a/precompile/contracts/feemanager/unpack_pack_test.go +++ b/precompile/contracts/feemanager/unpack_pack_test.go @@ -24,7 +24,7 @@ var ( testFeeConfig = commontype.ValidTestFeeConfig ) -func FuzzPackGetFeeConfigOutputEqualTest(f *testing.F) { +func FuzzPackGetFeeConfigOutputTest(f *testing.F) { f.Add([]byte{}, uint64(0)) f.Add(big.NewInt(0).Bytes(), uint64(0)) f.Add(big.NewInt(1).Bytes(), uint64(math.MaxUint64)) @@ -47,24 +47,37 @@ func FuzzPackGetFeeConfigOutputEqualTest(f *testing.F) { // otherwise the value will be truncated when packed, // and thus unpacked output will not be equal to the value doCheckOutputs := bigIntVal.Cmp(abi.MaxUint256) <= 0 - testOldPackGetFeeConfigOutputEqual(t, feeConfig, doCheckOutputs) + testPackGetFeeConfigOutput(t, feeConfig, doCheckOutputs) }) } -func TestOldPackGetFeeConfigOutputEqual(t *testing.T) { - testOldPackGetFeeConfigOutputEqual(t, testFeeConfig, true) +func TestPackGetFeeConfigOutput(t *testing.T) { + testPackGetFeeConfigOutput(t, testFeeConfig, true) } -func TestPackGetFeeConfigOutputPanic(t *testing.T) { - require.Panics(t, func() { - _, _ = OldPackFeeConfig(commontype.FeeConfig{}) +func testPackGetFeeConfigOutput(t *testing.T, feeConfig commontype.FeeConfig, checkOutputs bool) { + t.Helper() + t.Run(fmt.Sprintf("TestGetFeeConfigOutput, feeConfig %v", feeConfig), func(t *testing.T) { + input, err := PackGetFeeConfigOutput(feeConfig) + if err != nil { + return + } + + unpacked, err := UnpackGetFeeConfigOutput(input, false) + require.NoError(t, err) + if checkOutputs { + require.True(t, feeConfig.Equal(&unpacked), "not equal: feeConfig %v, unpacked %v", feeConfig, unpacked) + } }) +} + +func TestPackGetFeeConfigOutputPanic(t *testing.T) { require.Panics(t, func() { _, _ = PackGetFeeConfigOutput(commontype.FeeConfig{}) }) } -func TestPackGetFeeConfigOutput(t *testing.T) { +func TestUnpackGetFeeConfigOutput(t *testing.T) { testInputBytes, err := PackGetFeeConfigOutput(testFeeConfig) require.NoError(t, err) tests := []struct { @@ -72,51 +85,44 @@ func TestPackGetFeeConfigOutput(t *testing.T) { 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", + input: []byte{}, + skipLenCheck: false, + expectedErr: ErrInvalidLen.Error(), }, { - name: "empty input skip len check", - input: []byte{}, - skipLenCheck: true, - expectedErr: "attempting to unmarshal an empty string", - expectedOldErr: ErrInvalidLen.Error(), + name: "empty input skip len check", + input: []byte{}, + skipLenCheck: true, + expectedErr: "attempting to unmarshal an empty string", }, { - name: "input with extra bytes", - input: append(testInputBytes, make([]byte, 32)...), - skipLenCheck: false, - expectedErr: ErrInvalidLen.Error(), - expectedOldErr: ErrInvalidLen.Error(), + name: "input with extra bytes", + input: append(testInputBytes, make([]byte, 32)...), + skipLenCheck: false, + expectedErr: 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)", + input: append(testInputBytes, make([]byte, 33)...), + skipLenCheck: false, + expectedErr: 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(), + name: "input with extra bytes (not divisible by 32) skip len check", + input: append(testInputBytes, make([]byte, 33)...), + skipLenCheck: true, + expectedErr: "improperly formatted output", }, } for _, test := range tests { @@ -128,40 +134,10 @@ func TestPackGetFeeConfigOutput(t *testing.T) { 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) { - // Compare OldPackGetFeeConfigInput vs PackGetFeeConfig - // to see if they are equivalent - input := OldPackGetFeeConfigInput() - - input2, err := PackGetFeeConfig() - require.NoError(t, err) - - require.Equal(t, input, input2) -} - -func TestGetLastChangedAtInput(t *testing.T) { - // Compare OldPackGetFeeConfigInput vs PackGetFeeConfigLastChangedAt - // to see if they are equivalent - - input := OldPackGetLastChangedAtInput() - - input2, err := PackGetFeeConfigLastChangedAt() - require.NoError(t, err) - - require.Equal(t, input, input2) -} - func FuzzPackGetLastChangedAtOutput(f *testing.F) { f.Add([]byte{}) f.Add(big.NewInt(0).Bytes()) @@ -175,11 +151,25 @@ func FuzzPackGetLastChangedAtOutput(f *testing.F) { // otherwise the value will be truncated when packed, // and thus unpacked output will not be equal to the value doCheckOutputs := bigIntVal.Cmp(abi.MaxUint256) <= 0 - testOldPackGetLastChangedAtOutputEqual(t, bigIntVal, doCheckOutputs) + testPackGetLastChangedAtOutput(t, bigIntVal, doCheckOutputs) }) } -func FuzzPackSetFeeConfigEqualTest(f *testing.F) { +func testPackGetLastChangedAtOutput(t *testing.T, blockNumber *big.Int, checkOutputs bool) { + t.Helper() + t.Run(fmt.Sprintf("TestGetLastChangedAtOutput, blockNumber %v", blockNumber), func(t *testing.T) { + input, err := PackGetFeeConfigLastChangedAtOutput(blockNumber) + require.NoError(t, err) + + unpacked, err := UnpackGetFeeConfigLastChangedAtOutput(input) + require.NoError(t, err) + if checkOutputs { + require.Zero(t, blockNumber.Cmp(unpacked), "not equal: blockNumber %v, unpacked %v", blockNumber, unpacked) + } + }) +} + +func FuzzPackSetFeeConfigTest(f *testing.F) { f.Add([]byte{}, uint64(0)) f.Add(big.NewInt(0).Bytes(), uint64(0)) f.Add(big.NewInt(1).Bytes(), uint64(math.MaxUint64)) @@ -202,24 +192,38 @@ func FuzzPackSetFeeConfigEqualTest(f *testing.F) { // otherwise the value will be truncated when packed, // and thus unpacked output will not be equal to the value doCheckOutputs := bigIntVal.Cmp(abi.MaxUint256) <= 0 - testOldPackSetFeeConfigInputEqual(t, feeConfig, doCheckOutputs) + testPackSetFeeConfigInput(t, feeConfig, doCheckOutputs) }) } -func TestOldPackSetFeeConfigInputEqual(t *testing.T) { - testOldPackSetFeeConfigInputEqual(t, testFeeConfig, true) +func TestPackSetFeeConfigInput(t *testing.T) { + testPackSetFeeConfigInput(t, testFeeConfig, true) } -func TestPackSetFeeConfigInputPanic(t *testing.T) { - require.Panics(t, func() { - _, _ = OldPackSetFeeConfig(commontype.FeeConfig{}) +func testPackSetFeeConfigInput(t *testing.T, feeConfig commontype.FeeConfig, checkOutputs bool) { + t.Helper() + t.Run(fmt.Sprintf("TestSetFeeConfigInput, feeConfig %v", feeConfig), func(t *testing.T) { + input, err := PackSetFeeConfig(feeConfig) + if err != nil { + return + } + + // exclude 4 bytes for function selector + unpacked, err := UnpackSetFeeConfigInput(input[4:], true) + require.NoError(t, err) + if checkOutputs { + require.True(t, feeConfig.Equal(&unpacked), "not equal: feeConfig %v, unpacked %v", feeConfig, unpacked) + } }) +} + +func TestPackSetFeeConfigInputPanic(t *testing.T) { require.Panics(t, func() { _, _ = PackSetFeeConfig(commontype.FeeConfig{}) }) } -func TestPackSetFeeConfigInput(t *testing.T) { +func TestUnpackSetFeeConfigInput(t *testing.T) { testInputBytes, err := PackSetFeeConfig(testFeeConfig) require.NoError(t, err) // exclude 4 bytes for function selector @@ -229,65 +233,56 @@ func TestPackSetFeeConfigInput(t *testing.T) { input []byte strictMode bool expectedErr string - expectedOldErr string expectedOutput commontype.FeeConfig }{ { - name: "empty input strict mode", - input: []byte{}, - strictMode: true, - expectedErr: ErrInvalidLen.Error(), - expectedOldErr: ErrInvalidLen.Error(), + name: "empty input strict mode", + input: []byte{}, + strictMode: true, + expectedErr: ErrInvalidLen.Error(), }, { - name: "empty input", - input: []byte{}, - strictMode: false, - expectedErr: "attempting to unmarshal an empty string", - expectedOldErr: ErrInvalidLen.Error(), + name: "empty input", + input: []byte{}, + strictMode: false, + expectedErr: "attempting to unmarshal an empty string", }, { - name: "input with insufficient len strict mode", - input: []byte{123}, - strictMode: true, - expectedErr: ErrInvalidLen.Error(), - expectedOldErr: ErrInvalidLen.Error(), + name: "input with insufficient len strict mode", + input: []byte{123}, + strictMode: true, + expectedErr: ErrInvalidLen.Error(), }, { - name: "input with insufficient len", - input: []byte{123}, - strictMode: false, - expectedErr: "length insufficient", - expectedOldErr: ErrInvalidLen.Error(), + name: "input with insufficient len", + input: []byte{123}, + strictMode: false, + expectedErr: "length insufficient", }, { - name: "input with extra bytes strict mode", - input: append(testInputBytes, make([]byte, 32)...), - strictMode: true, - expectedErr: ErrInvalidLen.Error(), - expectedOldErr: ErrInvalidLen.Error(), + name: "input with extra bytes strict mode", + input: append(testInputBytes, make([]byte, 32)...), + strictMode: true, + expectedErr: ErrInvalidLen.Error(), }, { name: "input with extra bytes", input: append(testInputBytes, make([]byte, 32)...), strictMode: false, expectedErr: "", - expectedOldErr: ErrInvalidLen.Error(), expectedOutput: testFeeConfig, }, { - name: "input with extra bytes (not divisible by 32) strict mode", - input: append(testInputBytes, make([]byte, 33)...), - strictMode: true, - expectedErr: ErrInvalidLen.Error(), - expectedOldErr: ErrInvalidLen.Error(), + name: "input with extra bytes (not divisible by 32) strict mode", + input: append(testInputBytes, make([]byte, 33)...), + strictMode: true, + expectedErr: ErrInvalidLen.Error(), }, { name: "input with extra bytes (not divisible by 32)", input: append(testInputBytes, make([]byte, 33)...), strictMode: false, expectedOutput: testFeeConfig, - expectedOldErr: ErrInvalidLen.Error(), }, } for _, test := range tests { @@ -299,13 +294,6 @@ func TestPackSetFeeConfigInput(t *testing.T) { 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) - } }) } } @@ -320,160 +308,3 @@ func TestFunctionSignatures(t *testing.T) { abiGetFeeConfigLastChangedAt := FeeManagerABI.Methods["getFeeConfigLastChangedAt"] require.Equal(t, getFeeConfigLastChangedAtSignature, abiGetFeeConfigLastChangedAt.ID) } - -func testOldPackGetFeeConfigOutputEqual(t *testing.T, feeConfig commontype.FeeConfig, checkOutputs bool) { - t.Helper() - t.Run(fmt.Sprintf("TestGetFeeConfigOutput, feeConfig %v", feeConfig), func(t *testing.T) { - input, err := OldPackFeeConfig(feeConfig) - input2, err2 := PackGetFeeConfigOutput(feeConfig) - if err != nil { - require.ErrorContains(t, err2, err.Error()) - return - } - require.NoError(t, err2) - require.Equal(t, input, input2) - - config, err := OldUnpackFeeConfig(input) - unpacked, err2 := UnpackGetFeeConfigOutput(input, false) - if err != nil { - require.ErrorContains(t, err2, err.Error()) - return - } - require.NoError(t, err2) - require.True(t, config.Equal(&unpacked), "not equal: config %v, unpacked %v", feeConfig, unpacked) - if checkOutputs { - require.True(t, feeConfig.Equal(&unpacked), "not equal: feeConfig %v, unpacked %v", feeConfig, unpacked) - } - }) -} - -func testOldPackGetLastChangedAtOutputEqual(t *testing.T, blockNumber *big.Int, checkOutputs bool) { - t.Helper() - t.Run(fmt.Sprintf("TestGetLastChangedAtOutput, blockNumber %v", blockNumber), func(t *testing.T) { - input := OldPackGetLastChangedAtOutput(blockNumber) - input2, err2 := PackGetFeeConfigLastChangedAtOutput(blockNumber) - require.NoError(t, err2) - require.Equal(t, input, input2) - - value, err := OldUnpackGetLastChangedAtOutput(input) - unpacked, err2 := UnpackGetFeeConfigLastChangedAtOutput(input) - if err != nil { - require.ErrorContains(t, err2, err.Error()) - return - } - require.NoError(t, err2) - require.Zero(t, value.Cmp(unpacked), "not equal: value %v, unpacked %v", value, unpacked) - if checkOutputs { - require.Zero(t, blockNumber.Cmp(unpacked), "not equal: blockNumber %v, unpacked %v", blockNumber, unpacked) - } - }) -} - -func testOldPackSetFeeConfigInputEqual(t *testing.T, feeConfig commontype.FeeConfig, checkOutputs bool) { - t.Helper() - t.Run(fmt.Sprintf("TestSetFeeConfigInput, feeConfig %v", feeConfig), func(t *testing.T) { - input, err := OldPackSetFeeConfig(feeConfig) - input2, err2 := PackSetFeeConfig(feeConfig) - if err != nil { - require.ErrorContains(t, err2, err.Error()) - return - } - require.NoError(t, err2) - require.Equal(t, input, input2) - - value, err := OldUnpackFeeConfig(input) - unpacked, err2 := UnpackSetFeeConfigInput(input, true) - if err != nil { - require.ErrorContains(t, err2, err.Error()) - return - } - require.NoError(t, err2) - require.True(t, value.Equal(&unpacked), "not equal: value %v, unpacked %v", value, unpacked) - if checkOutputs { - require.True(t, feeConfig.Equal(&unpacked), "not equal: feeConfig %v, unpacked %v", feeConfig, unpacked) - } - }) -} - -func OldPackFeeConfig(feeConfig commontype.FeeConfig) ([]byte, error) { - return packFeeConfigHelper(feeConfig, false) -} - -func OldUnpackFeeConfig(input []byte) (commontype.FeeConfig, error) { - if len(input) != feeConfigInputLen { - return commontype.FeeConfig{}, fmt.Errorf("%w: %d", ErrInvalidLen, len(input)) - } - feeConfig := commontype.FeeConfig{} - for i := minFeeConfigFieldKey; i <= numFeeConfigField; i++ { - listIndex := i - 1 - packedElement := contract.PackedHash(input, listIndex) - switch i { - case gasLimitKey: - feeConfig.GasLimit = new(big.Int).SetBytes(packedElement) - case targetBlockRateKey: - feeConfig.TargetBlockRate = new(big.Int).SetBytes(packedElement).Uint64() - case minBaseFeeKey: - feeConfig.MinBaseFee = new(big.Int).SetBytes(packedElement) - case targetGasKey: - feeConfig.TargetGas = new(big.Int).SetBytes(packedElement) - case baseFeeChangeDenominatorKey: - feeConfig.BaseFeeChangeDenominator = new(big.Int).SetBytes(packedElement) - case minBlockGasCostKey: - feeConfig.MinBlockGasCost = new(big.Int).SetBytes(packedElement) - case maxBlockGasCostKey: - feeConfig.MaxBlockGasCost = new(big.Int).SetBytes(packedElement) - case blockGasCostStepKey: - feeConfig.BlockGasCostStep = new(big.Int).SetBytes(packedElement) - default: - // This should never encounter an unknown fee config key - panic(fmt.Sprintf("unknown fee config key: %d", i)) - } - } - return feeConfig, nil -} - -func packFeeConfigHelper(feeConfig commontype.FeeConfig, useSelector bool) ([]byte, error) { - hashes := []common.Hash{ - common.BigToHash(feeConfig.GasLimit), - common.BigToHash(new(big.Int).SetUint64(feeConfig.TargetBlockRate)), - common.BigToHash(feeConfig.MinBaseFee), - common.BigToHash(feeConfig.TargetGas), - common.BigToHash(feeConfig.BaseFeeChangeDenominator), - common.BigToHash(feeConfig.MinBlockGasCost), - common.BigToHash(feeConfig.MaxBlockGasCost), - common.BigToHash(feeConfig.BlockGasCostStep), - } - - if useSelector { - res := make([]byte, len(setFeeConfigSignature)+feeConfigInputLen) - err := contract.PackOrderedHashesWithSelector(res, setFeeConfigSignature, hashes) - return res, err - } - - res := make([]byte, len(hashes)*common.HashLength) - err := contract.PackOrderedHashes(res, hashes) - return res, err -} - -// PackGetFeeConfigInput packs the getFeeConfig signature -func OldPackGetFeeConfigInput() []byte { - return getFeeConfigSignature -} - -// PackGetLastChangedAtInput packs the getFeeConfigLastChangedAt signature -func OldPackGetLastChangedAtInput() []byte { - return getFeeConfigLastChangedAtSignature -} - -func OldPackGetLastChangedAtOutput(lastChangedAt *big.Int) []byte { - return common.BigToHash(lastChangedAt).Bytes() -} - -func OldUnpackGetLastChangedAtOutput(input []byte) (*big.Int, error) { - return new(big.Int).SetBytes(input), nil -} - -func OldPackSetFeeConfig(feeConfig commontype.FeeConfig) ([]byte, error) { - // function selector (4 bytes) + input(feeConfig) - return packFeeConfigHelper(feeConfig, true) -} diff --git a/precompile/contracts/nativeminter/unpack_pack_test.go b/precompile/contracts/nativeminter/unpack_pack_test.go index 88a530815a..b3f78cdd5d 100644 --- a/precompile/contracts/nativeminter/unpack_pack_test.go +++ b/precompile/contracts/nativeminter/unpack_pack_test.go @@ -19,7 +19,7 @@ import ( var mintSignature = contract.CalculateFunctionSelector("mintNativeCoin(address,uint256)") // address, amount -func FuzzPackMintNativeCoinEqualTest(f *testing.F) { +func FuzzPackMintNativeCoinTest(f *testing.F) { key, err := crypto.GenerateKey() require.NoError(f, err) addr := crypto.PubkeyToAddress(key.PublicKey) @@ -36,7 +36,25 @@ func FuzzPackMintNativeCoinEqualTest(f *testing.F) { // otherwise the value will be truncated when packed, // and thus unpacked output will not be equal to the value doCheckOutputs := bigIntVal.Cmp(abi.MaxUint256) <= 0 - testOldPackMintNativeCoinEqual(t, common.BytesToAddress(b), bigIntVal, doCheckOutputs) + testPackMintNativeCoin(t, common.BytesToAddress(b), bigIntVal, doCheckOutputs) + }) +} + +func testPackMintNativeCoin(t *testing.T, addr common.Address, amount *big.Int, checkOutputs bool) { + t.Helper() + t.Run(fmt.Sprintf("TestPackMintNativeCoin, addr: %s, amount: %s", addr.String(), amount.String()), func(t *testing.T) { + input, err := PackMintNativeCoin(addr, amount) + if err != nil { + return + } + + input = input[4:] + unpackedAddr, unpackedAmount, err := UnpackMintNativeCoinInput(input, true) + require.NoError(t, err) + if checkOutputs { + require.Equal(t, addr, unpackedAddr) + require.Equal(t, amount.Bytes(), unpackedAmount.Bytes()) + } }) } @@ -50,46 +68,40 @@ func TestUnpackMintNativeCoinInput(t *testing.T) { input []byte strictMode bool expectedErr string - expectedOldErr string expectedAddr common.Address expectedAmount *big.Int }{ { - name: "empty input strict mode", - input: []byte{}, - strictMode: true, - expectedErr: ErrInvalidLen.Error(), - expectedOldErr: ErrInvalidLen.Error(), + name: "empty input strict mode", + input: []byte{}, + strictMode: true, + expectedErr: ErrInvalidLen.Error(), }, { - name: "empty input", - input: []byte{}, - strictMode: false, - expectedErr: "attempting to unmarshal an empty string", - expectedOldErr: ErrInvalidLen.Error(), + name: "empty input", + input: []byte{}, + strictMode: false, + expectedErr: "attempting to unmarshal an empty string", }, { - name: "input with extra bytes strict mode", - input: append(testInputBytes, make([]byte, 32)...), - strictMode: true, - expectedErr: ErrInvalidLen.Error(), - expectedOldErr: ErrInvalidLen.Error(), + name: "input with extra bytes strict mode", + input: append(testInputBytes, make([]byte, 32)...), + strictMode: true, + expectedErr: ErrInvalidLen.Error(), }, { name: "input with extra bytes", input: append(testInputBytes, make([]byte, 32)...), strictMode: false, expectedErr: "", - expectedOldErr: ErrInvalidLen.Error(), expectedAddr: constants.BlackholeAddr, expectedAmount: common.Big2, }, { - name: "input with extra bytes (not divisible by 32) strict mode", - input: append(testInputBytes, make([]byte, 33)...), - strictMode: true, - expectedErr: ErrInvalidLen.Error(), - expectedOldErr: ErrInvalidLen.Error(), + name: "input with extra bytes (not divisible by 32) strict mode", + input: append(testInputBytes, make([]byte, 33)...), + strictMode: true, + expectedErr: ErrInvalidLen.Error(), }, { name: "input with extra bytes (not divisible by 32)", @@ -97,7 +109,6 @@ func TestUnpackMintNativeCoinInput(t *testing.T) { strictMode: false, expectedAddr: constants.BlackholeAddr, expectedAmount: common.Big2, - expectedOldErr: ErrInvalidLen.Error(), }, } for _, test := range tests { @@ -110,14 +121,6 @@ func TestUnpackMintNativeCoinInput(t *testing.T) { require.Equal(t, test.expectedAddr, unpackedAddress) require.Equal(t, test.expectedAmount, unpackedAmount, "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.Equal(t, test.expectedAmount, oldUnpackedAmount, "expected %s, got %s", test.expectedAmount.String(), oldUnpackedAmount.String()) - } }) } } @@ -127,54 +130,3 @@ func TestFunctionSignatures(t *testing.T) { abiMintNativeCoin := NativeMinterABI.Methods["mintNativeCoin"] require.Equal(t, mintSignature, abiMintNativeCoin.ID) } - -func testOldPackMintNativeCoinEqual(t *testing.T, addr common.Address, amount *big.Int, checkOutputs bool) { - t.Helper() - t.Run(fmt.Sprintf("TestUnpackAndPacks, addr: %s, amount: %s", addr.String(), amount.String()), func(t *testing.T) { - input, err := OldPackMintNativeCoinInput(addr, amount) - input2, err2 := PackMintNativeCoin(addr, amount) - if err != nil { - require.ErrorContains(t, err2, err.Error()) - return - } - require.NoError(t, err2) - require.Equal(t, input, input2) - - input = input[4:] - to, assetAmount, err := OldUnpackMintNativeCoinInput(input) - unpackedAddr, unpackedAmount, err2 := UnpackMintNativeCoinInput(input, true) - if err != nil { - require.ErrorContains(t, err2, err.Error()) - return - } - require.NoError(t, err2) - require.Equal(t, to, unpackedAddr) - require.Equal(t, assetAmount.Bytes(), unpackedAmount.Bytes()) - if checkOutputs { - require.Equal(t, addr, to) - require.Equal(t, amount.Bytes(), assetAmount.Bytes()) - } - }) -} - -func OldPackMintNativeCoinInput(address common.Address, amount *big.Int) ([]byte, error) { - // function selector (4 bytes) + input(hash for address + hash for amount) - res := make([]byte, contract.SelectorLen+mintInputLen) - err := contract.PackOrderedHashesWithSelector(res, mintSignature, []common.Hash{ - common.BytesToHash(address[:]), - common.BigToHash(amount), - }) - - return res, err -} - -func OldUnpackMintNativeCoinInput(input []byte) (common.Address, *big.Int, error) { - mintInputAddressSlot := 0 - mintInputAmountSlot := 1 - if len(input) != mintInputLen { - return common.Address{}, nil, fmt.Errorf("%w: %d", ErrInvalidLen, len(input)) - } - to := common.BytesToAddress(contract.PackedHash(input, mintInputAddressSlot)) - assetAmount := new(big.Int).SetBytes(contract.PackedHash(input, mintInputAmountSlot)) - return to, assetAmount, nil -} From 31ca1e23d546fc14f9d9e0bbf12ec85f5b147ed2 Mon Sep 17 00:00:00 2001 From: Jonathan Oppenheimer Date: Thu, 4 Dec 2025 15:20:40 -0500 Subject: [PATCH 2/2] fix: revert bad merge changes --- .../contracts/feemanager/unpack_pack_test.go | 90 +++++++++---------- .../nativeminter/unpack_pack_test.go | 13 ++- 2 files changed, 51 insertions(+), 52 deletions(-) diff --git a/precompile/contracts/feemanager/unpack_pack_test.go b/precompile/contracts/feemanager/unpack_pack_test.go index f9e6c59de3..18705dba89 100644 --- a/precompile/contracts/feemanager/unpack_pack_test.go +++ b/precompile/contracts/feemanager/unpack_pack_test.go @@ -84,45 +84,45 @@ func TestUnpackGetFeeConfigOutput(t *testing.T) { name string input []byte skipLenCheck bool - expectedErr string + expectedErr error expectedOutput commontype.FeeConfig }{ { name: "empty input", input: []byte{}, skipLenCheck: false, - expectedErr: ErrInvalidLen.Error(), + expectedErr: ErrInvalidLen, }, { name: "empty input skip len check", input: []byte{}, skipLenCheck: true, - expectedErr: "attempting to unmarshal an empty string", + expectedErr: ErrUnpackOutput, }, { name: "input with extra bytes", input: append(testInputBytes, make([]byte, 32)...), skipLenCheck: false, - expectedErr: ErrInvalidLen.Error(), + expectedErr: ErrInvalidLen, }, { name: "input with extra bytes skip len check", input: append(testInputBytes, make([]byte, 32)...), skipLenCheck: true, - expectedErr: "", + expectedErr: nil, expectedOutput: testFeeConfig, }, { name: "input with extra bytes (not divisible by 32)", input: append(testInputBytes, make([]byte, 33)...), skipLenCheck: false, - expectedErr: ErrInvalidLen.Error(), + expectedErr: ErrInvalidLen, }, { name: "input with extra bytes (not divisible by 32) skip len check", input: append(testInputBytes, make([]byte, 33)...), skipLenCheck: true, - expectedErr: "improperly formatted output", + expectedErr: ErrUnpackOutput, }, } for _, test := range tests { @@ -227,60 +227,60 @@ func TestUnpackSetFeeConfigInput(t *testing.T) { // exclude 4 bytes for function selector testInputBytes = testInputBytes[4:] tests := []struct { - name string - input []byte - strictMode bool - expectedErr string - expectedOutput commontype.FeeConfig + name string + input []byte + strictMode bool + wantErr error + wantOutput commontype.FeeConfig }{ { - name: "empty input strict mode", - input: []byte{}, - strictMode: true, - expectedErr: ErrInvalidLen.Error(), + name: "empty input strict mode", + input: []byte{}, + strictMode: true, + wantErr: ErrInvalidLen, }, { - name: "empty input", - input: []byte{}, - strictMode: false, - expectedErr: "attempting to unmarshal an empty string", + name: "empty input", + input: []byte{}, + strictMode: false, + wantErr: ErrUnpackInput, }, { - name: "input with insufficient len strict mode", - input: []byte{123}, - strictMode: true, - expectedErr: ErrInvalidLen.Error(), + name: "input with insufficient len strict mode", + input: []byte{123}, + strictMode: true, + wantErr: ErrInvalidLen, }, { - name: "input with insufficient len", - input: []byte{123}, - strictMode: false, - expectedErr: "length insufficient", + name: "input with insufficient len", + input: []byte{123}, + strictMode: false, + wantErr: ErrUnpackInput, }, { - name: "input with extra bytes strict mode", - input: append(testInputBytes, make([]byte, 32)...), - strictMode: true, - expectedErr: ErrInvalidLen.Error(), + name: "input with extra bytes strict mode", + input: append(testInputBytes, make([]byte, 32)...), + strictMode: true, + wantErr: ErrInvalidLen, }, { - name: "input with extra bytes", - input: append(testInputBytes, make([]byte, 32)...), - strictMode: false, - expectedErr: "", - expectedOutput: testFeeConfig, + name: "input with extra bytes", + input: append(testInputBytes, make([]byte, 32)...), + strictMode: false, + wantErr: nil, + wantOutput: testFeeConfig, }, { - name: "input with extra bytes (not divisible by 32) strict mode", - input: append(testInputBytes, make([]byte, 33)...), - strictMode: true, - expectedErr: ErrInvalidLen.Error(), + name: "input with extra bytes (not divisible by 32) strict mode", + input: append(testInputBytes, make([]byte, 33)...), + strictMode: true, + wantErr: ErrInvalidLen, }, { - name: "input with extra bytes (not divisible by 32)", - input: append(testInputBytes, make([]byte, 33)...), - strictMode: false, - expectedOutput: testFeeConfig, + name: "input with extra bytes (not divisible by 32)", + input: append(testInputBytes, make([]byte, 33)...), + strictMode: false, + wantOutput: testFeeConfig, }, } for _, test := range tests { diff --git a/precompile/contracts/nativeminter/unpack_pack_test.go b/precompile/contracts/nativeminter/unpack_pack_test.go index 007f4ed303..391d2ccc42 100644 --- a/precompile/contracts/nativeminter/unpack_pack_test.go +++ b/precompile/contracts/nativeminter/unpack_pack_test.go @@ -67,7 +67,7 @@ func TestUnpackMintNativeCoinInput(t *testing.T) { name string input []byte strictMode bool - expectedErr string + expectedErr error expectedAddr common.Address expectedAmount *big.Int }{ @@ -75,25 +75,25 @@ func TestUnpackMintNativeCoinInput(t *testing.T) { name: "empty input strict mode", input: []byte{}, strictMode: true, - expectedErr: ErrInvalidLen.Error(), + expectedErr: ErrInvalidLen, }, { name: "empty input", input: []byte{}, strictMode: false, - expectedErr: "attempting to unmarshal an empty string", + expectedErr: ErrUnpackInput, }, { name: "input with extra bytes strict mode", input: append(testInputBytes, make([]byte, 32)...), strictMode: true, - expectedErr: ErrInvalidLen.Error(), + expectedErr: ErrInvalidLen, }, { name: "input with extra bytes", input: append(testInputBytes, make([]byte, 32)...), strictMode: false, - expectedErr: "", + expectedErr: nil, expectedAddr: constants.BlackholeAddr, expectedAmount: common.Big2, }, @@ -101,14 +101,13 @@ func TestUnpackMintNativeCoinInput(t *testing.T) { name: "input with extra bytes (not divisible by 32) strict mode", input: append(testInputBytes, make([]byte, 33)...), strictMode: true, - expectedErr: ErrInvalidLen.Error(), + expectedErr: ErrInvalidLen, }, { name: "input with extra bytes (not divisible by 32)", input: append(testInputBytes, make([]byte, 33)...), strictMode: false, expectedErr: nil, - expectedOldErr: ErrInvalidLen, expectedAddr: constants.BlackholeAddr, expectedAmount: common.Big2, },