-
Notifications
You must be signed in to change notification settings - Fork 245
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor
header.Extra
verification (#788)
- Loading branch information
1 parent
a0e0781
commit e501c94
Showing
6 changed files
with
152 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// (c) 2025, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package header | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/ava-labs/subnet-evm/params" | ||
) | ||
|
||
var errInvalidExtraLength = errors.New("invalid header.Extra length") | ||
|
||
// VerifyExtra verifies that the header's Extra field is correctly formatted for | ||
// [rules]. | ||
func VerifyExtra(rules params.AvalancheRules, extra []byte) error { | ||
extraLen := len(extra) | ||
switch { | ||
case rules.IsDurango: | ||
if extraLen < params.DynamicFeeExtraDataSize { | ||
return fmt.Errorf( | ||
"%w: expected >= %d but got %d", | ||
errInvalidExtraLength, | ||
params.DynamicFeeExtraDataSize, | ||
extraLen, | ||
) | ||
} | ||
case rules.IsSubnetEVM: | ||
if extraLen != params.DynamicFeeExtraDataSize { | ||
return fmt.Errorf( | ||
"%w: expected %d but got %d", | ||
errInvalidExtraLength, | ||
params.DynamicFeeExtraDataSize, | ||
extraLen, | ||
) | ||
} | ||
default: | ||
if uint64(extraLen) > params.MaximumExtraDataSize { | ||
return fmt.Errorf( | ||
"%w: expected <= %d but got %d", | ||
errInvalidExtraLength, | ||
params.MaximumExtraDataSize, | ||
extraLen, | ||
) | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// (c) 2025, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package header | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/ava-labs/subnet-evm/params" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestVerifyExtra(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
rules params.AvalancheRules | ||
extra []byte | ||
expected error | ||
}{ | ||
{ | ||
name: "initial_valid", | ||
rules: params.AvalancheRules{}, | ||
extra: make([]byte, params.MaximumExtraDataSize), | ||
expected: nil, | ||
}, | ||
{ | ||
name: "initial_invalid", | ||
rules: params.AvalancheRules{}, | ||
extra: make([]byte, params.MaximumExtraDataSize+1), | ||
expected: errInvalidExtraLength, | ||
}, | ||
{ | ||
name: "subnet_evm_valid", | ||
rules: params.AvalancheRules{ | ||
IsSubnetEVM: true, | ||
}, | ||
extra: make([]byte, params.DynamicFeeExtraDataSize), | ||
expected: nil, | ||
}, | ||
{ | ||
name: "subnet_invalid_less", | ||
rules: params.AvalancheRules{ | ||
IsSubnetEVM: true, | ||
}, | ||
extra: make([]byte, params.DynamicFeeExtraDataSize-1), | ||
expected: errInvalidExtraLength, | ||
}, | ||
{ | ||
name: "subnet_invalid_more", | ||
rules: params.AvalancheRules{ | ||
IsSubnetEVM: true, | ||
}, | ||
extra: make([]byte, params.DynamicFeeExtraDataSize+1), | ||
expected: errInvalidExtraLength, | ||
}, | ||
{ | ||
name: "durango_valid_min", | ||
rules: params.AvalancheRules{ | ||
IsDurango: true, | ||
}, | ||
extra: make([]byte, params.DynamicFeeExtraDataSize), | ||
expected: nil, | ||
}, | ||
{ | ||
name: "durango_valid_extra", | ||
rules: params.AvalancheRules{ | ||
IsDurango: true, | ||
}, | ||
extra: make([]byte, params.DynamicFeeExtraDataSize+1), | ||
expected: nil, | ||
}, | ||
{ | ||
name: "durango_invalid", | ||
rules: params.AvalancheRules{ | ||
IsDurango: true, | ||
}, | ||
extra: make([]byte, params.DynamicFeeExtraDataSize-1), | ||
expected: errInvalidExtraLength, | ||
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
err := VerifyExtra(test.rules, test.extra) | ||
require.ErrorIs(t, err, test.expected) | ||
}) | ||
} | ||
} |