mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-09 23:48:06 -05:00
Fix Small Bug in Genesis Validators Root Validation Func (#10183)
* initial commit * switching to table driven test
This commit is contained in:
@@ -406,7 +406,17 @@ func ReverseByteOrder(input []byte) []byte {
|
|||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
// NonZeroRoot returns whether or not a root is of proper length and non-zero hash.
|
// ZeroRoot returns whether or not a root is of proper length and non-zero hash.
|
||||||
func NonZeroRoot(root []byte) bool {
|
func ZeroRoot(root []byte) bool {
|
||||||
return len(root) == fieldparams.RootLength && string(make([]byte, fieldparams.RootLength)) != string(root)
|
return string(make([]byte, fieldparams.RootLength)) == string(root)
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsRoot checks whether the byte array is a root.
|
||||||
|
func IsRoot(root []byte) bool {
|
||||||
|
return len(root) == fieldparams.RootLength
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsValidRoot checks whether the byte array is a valid root.
|
||||||
|
func IsValidRoot(root []byte) bool {
|
||||||
|
return IsRoot(root) && !ZeroRoot(root)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -518,12 +518,67 @@ func TestSafeCopy2d32Bytes(t *testing.T) {
|
|||||||
assert.DeepEqual(t, input, output)
|
assert.DeepEqual(t, input, output)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNonZeroRoot(t *testing.T) {
|
func TestZeroRoot(t *testing.T) {
|
||||||
input := make([]byte, fieldparams.RootLength)
|
input := make([]byte, fieldparams.RootLength)
|
||||||
output := bytesutil.NonZeroRoot(input)
|
output := bytesutil.ZeroRoot(input)
|
||||||
assert.Equal(t, false, output)
|
assert.Equal(t, true, output)
|
||||||
copy(input[2:], "a")
|
copy(input[2:], "a")
|
||||||
copy(input[3:], "b")
|
copy(input[3:], "b")
|
||||||
output = bytesutil.NonZeroRoot(input)
|
output = bytesutil.ZeroRoot(input)
|
||||||
|
assert.Equal(t, false, output)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIsRoot(t *testing.T) {
|
||||||
|
input := make([]byte, fieldparams.RootLength)
|
||||||
|
output := bytesutil.IsRoot(input)
|
||||||
assert.Equal(t, true, output)
|
assert.Equal(t, true, output)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestIsValidRoot(t *testing.T) {
|
||||||
|
|
||||||
|
zeroRoot := make([]byte, fieldparams.RootLength)
|
||||||
|
|
||||||
|
validRoot := make([]byte, fieldparams.RootLength)
|
||||||
|
validRoot[0] = 'a'
|
||||||
|
|
||||||
|
wrongLengthRoot := make([]byte, fieldparams.RootLength-4)
|
||||||
|
wrongLengthRoot[0] = 'a'
|
||||||
|
|
||||||
|
type args struct {
|
||||||
|
root []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args args
|
||||||
|
want bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "Is ZeroRoot",
|
||||||
|
args: args{
|
||||||
|
root: zeroRoot,
|
||||||
|
},
|
||||||
|
want: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Is ValidRoot",
|
||||||
|
args: args{
|
||||||
|
root: validRoot,
|
||||||
|
},
|
||||||
|
want: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Is NonZeroRoot but not length 32",
|
||||||
|
args: args{
|
||||||
|
root: wrongLengthRoot,
|
||||||
|
},
|
||||||
|
want: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
got := bytesutil.IsValidRoot(tt.args.root)
|
||||||
|
require.Equal(t, got, tt.want)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -311,7 +311,7 @@ func (w *Wallet) InitializeKeymanager(ctx context.Context, cfg iface.InitKeymana
|
|||||||
}
|
}
|
||||||
// TODO(9883): future work needs to address how initialize keymanager is called for web3signer.
|
// TODO(9883): future work needs to address how initialize keymanager is called for web3signer.
|
||||||
// an error may be thrown for genesis validators root for some InitializeKeymanager calls.
|
// an error may be thrown for genesis validators root for some InitializeKeymanager calls.
|
||||||
if !bytesutil.NonZeroRoot(config.GenesisValidatorsRoot) {
|
if !bytesutil.IsValidRoot(config.GenesisValidatorsRoot) {
|
||||||
return nil, errors.New("web3signer requires a genesis validators root value")
|
return nil, errors.New("web3signer requires a genesis validators root value")
|
||||||
}
|
}
|
||||||
km, err = remote_web3signer.NewKeymanager(ctx, config)
|
km, err = remote_web3signer.NewKeymanager(ctx, config)
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ type Keymanager struct {
|
|||||||
|
|
||||||
// NewKeymanager instantiates a new web3signer key manager.
|
// NewKeymanager instantiates a new web3signer key manager.
|
||||||
func NewKeymanager(_ context.Context, cfg *SetupConfig) (*Keymanager, error) {
|
func NewKeymanager(_ context.Context, cfg *SetupConfig) (*Keymanager, error) {
|
||||||
if cfg.BaseEndpoint == "" || !bytesutil.NonZeroRoot(cfg.GenesisValidatorsRoot) {
|
if cfg.BaseEndpoint == "" || !bytesutil.IsValidRoot(cfg.GenesisValidatorsRoot) {
|
||||||
return nil, fmt.Errorf("invalid setup config, one or more configs are empty: BaseEndpoint: %v, GenesisValidatorsRoot: %#x", cfg.BaseEndpoint, cfg.GenesisValidatorsRoot)
|
return nil, fmt.Errorf("invalid setup config, one or more configs are empty: BaseEndpoint: %v, GenesisValidatorsRoot: %#x", cfg.BaseEndpoint, cfg.GenesisValidatorsRoot)
|
||||||
}
|
}
|
||||||
if cfg.PublicKeysURL != "" && len(cfg.ProvidedPublicKeys) != 0 {
|
if cfg.PublicKeysURL != "" && len(cfg.ProvidedPublicKeys) != 0 {
|
||||||
@@ -103,7 +103,7 @@ func getSignRequestJson(ctx context.Context, validator *validator.Validate, requ
|
|||||||
if request == nil {
|
if request == nil {
|
||||||
return nil, errors.New("nil sign request provided")
|
return nil, errors.New("nil sign request provided")
|
||||||
}
|
}
|
||||||
if !bytesutil.NonZeroRoot(genesisValidatorsRoot) {
|
if !bytesutil.IsValidRoot(genesisValidatorsRoot) {
|
||||||
return nil, fmt.Errorf("invalid genesis validators root length, genesis root: %v", genesisValidatorsRoot)
|
return nil, fmt.Errorf("invalid genesis validators root length, genesis root: %v", genesisValidatorsRoot)
|
||||||
}
|
}
|
||||||
switch request.Object.(type) {
|
switch request.Object.(type) {
|
||||||
|
|||||||
Reference in New Issue
Block a user