-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Add and verify weak-subjectivity-checkpoint
flag
#7256
Changes from all commits
61427a6
272cb48
abef0fc
452d24a
5dd4ee1
1de1c68
40df921
46b9a8a
245507c
74913d7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package node | ||
|
||
import ( | ||
"encoding/hex" | ||
"errors" | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
// Given input string `block_root:epoch_number`, this verifies the input string is valid, and | ||
// returns the block root as bytes and epoch number as unsigned integers. | ||
func convertWspInput(wsp string) ([]byte, uint64, error) { | ||
if wsp == "" { | ||
return nil, 0, nil | ||
} | ||
|
||
// Weak subjectivity input string must contain ":" to separate epoch and block root. | ||
if !strings.Contains(wsp, ":") { | ||
return nil, 0, fmt.Errorf("%s did not contain column", wsp) | ||
} | ||
|
||
// Strip prefix "0x" if it's part of the input string. | ||
if strings.HasPrefix(wsp, "0x") { | ||
wsp = wsp[2:] | ||
} | ||
|
||
// Get the hexadecimal block root from input string. | ||
s := strings.Split(wsp, ":") | ||
if len(s) != 2 { | ||
return nil, 0, errors.New("weak subjectivity checkpoint input should be in `block_root:epoch_number` format") | ||
} | ||
|
||
bRoot, err := hex.DecodeString(s[0]) | ||
if err != nil { | ||
return nil, 0, err | ||
} | ||
if len(bRoot) != 32 { | ||
return nil, 0, errors.New("block root is not length of 32") | ||
} | ||
|
||
// Get the epoch number from input string. | ||
epoch, err := strconv.ParseUint(s[1], 10, 64) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what if len is < 2? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point. I'll add a check |
||
if err != nil { | ||
return nil, 0, err | ||
} | ||
|
||
return bRoot, epoch, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package node | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/prysmaticlabs/prysm/shared/testutil/require" | ||
) | ||
|
||
func TestConvertWspInput(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input string | ||
bRoot []byte | ||
epoch uint64 | ||
wantErr bool | ||
errStr string | ||
}{ | ||
{ | ||
name: "No column in string", | ||
input: "0x111111;123", | ||
wantErr: true, | ||
errStr: "did not contain column", | ||
}, | ||
{ | ||
name: "Too many columns in string", | ||
input: "0x010203:123:456", | ||
wantErr: false, | ||
errStr: "weak subjectivity checkpoint input should be in `block_root:epoch_number` format", | ||
}, | ||
{ | ||
name: "Incorrect block root length", | ||
input: "0x010203:987", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldnt the block root be 32 bytes. Lets add a check for it. |
||
wantErr: false, | ||
errStr: "block root is not length of 32", | ||
}, | ||
{ | ||
name: "Correct input", | ||
input: "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF:123456789", | ||
bRoot: []byte{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, | ||
epoch: 123456789, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Correct input without 0x", | ||
input: "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF:123456789", | ||
bRoot: []byte{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, | ||
epoch: 123456789, | ||
wantErr: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
bRoot, epoch, err := convertWspInput(tt.input) | ||
if (err != nil) != tt.wantErr { | ||
require.ErrorContains(t, tt.errStr, err) | ||
return | ||
} | ||
if !reflect.DeepEqual(bRoot, tt.bRoot) { | ||
t.Errorf("convertWspInput() block root = %v, want %v", bRoot, tt.bRoot) | ||
} | ||
if epoch != tt.epoch { | ||
t.Errorf("convertWspInput() epoch = %v, want %v", epoch, tt.epoch) | ||
} | ||
}) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe output why it is not well formatted, showing them the expected format. Perhaps that should also be part of the flag usage if not specified already
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's part of the flag usage. I'll reiterate in the error again