Correct parsing of the `weak-subjectivity-checkpoint` parameter (#3765)

This commit is contained in:
zah 2022-06-17 14:55:03 +03:00 committed by GitHub
parent d19204ff43
commit 69f505e2ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 68 additions and 4 deletions

View File

@ -958,11 +958,14 @@ func completeCmdArg*(T: type PubKey0x, input: string): seq[string] =
func parseCmdArg*(T: type Checkpoint, input: string): T
{.raises: [ValueError, Defect].} =
let sepIdx = find(input, ':')
if sepIdx == -1:
if sepIdx == -1 or sepIdx == input.len - 1:
raise newException(ValueError,
"The weak subjectivity checkpoint must be provided in the `block_root:epoch_number` format")
T(root: Eth2Digest.fromHex(input[0 ..< sepIdx]),
epoch: parseBiggestUInt(input[sepIdx .. ^1]).Epoch)
var root: Eth2Digest
hexToByteArrayStrict(input.toOpenArray(0, sepIdx - 1), root.data)
T(root: root, epoch: parseBiggestUInt(input[sepIdx + 1 .. ^1]).Epoch)
func completeCmdArg*(T: type Checkpoint, input: string): seq[string] =
return @[]

View File

@ -19,6 +19,7 @@ import # Unit test
./test_block_dag,
./test_block_processor,
./test_block_quarantine,
./test_conf,
./test_datatypes,
./test_discovery,
./test_engine_authentication,

60
tests/test_conf.nim Normal file
View File

@ -0,0 +1,60 @@
import
unittest2,
../beacon_chain/conf
template reject(val: string) =
expect CatchableError:
echo Checkpoint.parseCmdArg(val)
suite "Configuration parsing":
suite "weak-subjectivity-checkpoint":
test "Correct values":
let
c1 = Checkpoint.parseCmdArg("0x3c1e98bf132530c669723f58aa3d395be0d0bfaa653152eecb04605e203bfeb5:31714")
c2 = Checkpoint.parseCmdArg("3c1e98bf132530c669723f58aa3d395be0d0bfaa653152eecb04605e203bfeb5:31714")
check:
c1.epoch == 31714
c1.root == Eth2Digest.fromHex("3c1e98bf132530c669723f58aa3d395be0d0bfaa653152eecb04605e203bfeb5")
c1 == c2
#[
let
c3 = Checkpoint.parseCmdArg("3c1e98bf132530c669723f58aa3d395be0d0bfaa653152eecb04605e203bfeb5:0")
c4 = Checkpoint.parseCmdArg("3c1e98bf132530c669723f58aa3d395be0d0bfaa653152eecb04605e203bfeb5:1")
check:
c3.epoch == 0
c4.epoch == 1
]#
test "missing separator":
reject ""
reject "3c1e98bf132530c669723f58aa3d395be0d0bfaa653152eecb04605e203bfeb5"
reject "0x3c1e98bf132530c669723f58aa3d395be0d0bfaa653152eecb04605e203bfeb5"
reject "3c1e98bf132530c669723f58aa3d395be0d0bfaa653152eecb04605e203bfeb531714"
test "missing root":
reject ":31714"
test "shorter root":
reject "3c1e98bf132530c669723f58aa3d395be0d0bfaa653152eecb04605e203bfe:31714"
reject "3c1e98bf132530c669723f58aa3d395be0d0bfaa653152eecb04605e203bfeb:31714"
test "longer root":
reject "3c1e98bf132530c669723f58aa3d395be0d0bfaa653152eecb04605e203bfeb50:31714"
reject "3c1e98bf132530c669723f58aa3d395be0d0bfaa653152eecb04605e203bfeb500:31714"
test "invalid characters in root":
reject "1x3c1e98bf132530c669723f58aa3d395be0d0bfaa653152eecb04605e203bfeb5:31714"
reject "3g1e98bf132530c669723f58aa3d395be0d0bfaa653152eecb04605e203bfeb5:31714"
test "missing epoch":
reject "3c1e98bf132530c669723f58aa3d395be0d0bfaa653152eecb04605e203bfeb500:"
test "non-number epoch":
reject "3c1e98bf132530c669723f58aa3d395be0d0bfaa653152eecb04605e203bfeb500:123c"
reject "3c1e98bf132530c669723f58aa3d395be0d0bfaa653152eecb04605e203bfeb500:а"
test "negative epoch":
reject "3c1e98bf132530c669723f58aa3d395be0d0bfaa653152eecb04605e203bfeb500:-1000"

2
vendor/nim-stew vendored

@ -1 +1 @@
Subproject commit 419903c9a31ab253cf5cf19f24d9a912dc4b5154
Subproject commit f75c0a273aa34880ae7ac99e7e0c5d16b26ef166