Format code.

This commit is contained in:
Nathaniel Jensen 2020-01-08 12:03:33 +11:00 committed by zah
parent a37fac864f
commit 9aeef775ae

View File

@ -1,8 +1,9 @@
import import
endians, stew/ptrops, stew/ranges/ptr_arith, endians, stew/ptrops, stew/ranges/ptr_arith,
../beacon_chain/[ssz, state_transition], ../beacon_chain/[ssz, state_transition],
../beacon_chain/spec/[datatypes, helpers, digest, validator, beaconstate, state_transition_block], ../beacon_chain/spec/[datatypes, helpers, digest, validator, beaconstate,
# Required for deserialisation of ValidatorSig in Attestation due to state_transition_block],
# Required for deserialisation of ValidatorSig in Attestation due to
# https://github.com/nim-lang/Nim/issues/11225 # https://github.com/nim-lang/Nim/issues/11225
../beacon_chain/spec/crypto, ../beacon_chain/spec/crypto,
../beacon_chain/extras ../beacon_chain/extras
@ -19,12 +20,12 @@ type
state: BeaconState state: BeaconState
attestation: Attestation attestation: Attestation
# This and AssertionError are raised to indicate programming bugs # This and AssertionError are raised to indicate programming bugs
# Used as a wrapper to allow exception tracking to identify unexpected exceptions # A wrapper to allow exception tracking to identify unexpected exceptions
FuzzCrashError = object of Exception FuzzCrashError = object of Exception
# TODO: change ptr uint to ptr csize_t when available in newer Nim version. # TODO: change ptr uint to ptr csize_t when available in newer Nim version.
proc copyState(state: BeaconState, output: ptr byte, proc copyState(state: BeaconState, output: ptr byte,
output_size: ptr uint): bool {.raises:[FuzzCrashError, Defect].} = output_size: ptr uint): bool {.raises: [FuzzCrashError, Defect].} =
var resultState: seq[byte] var resultState: seq[byte]
try: try:
@ -34,13 +35,13 @@ proc copyState(state: BeaconState, output: ptr byte,
raise newException(FuzzCrashError, "Unexpected failure to serialize.", e) raise newException(FuzzCrashError, "Unexpected failure to serialize.", e)
if unlikely(resultState.len.uint > output_size[]): if unlikely(resultState.len.uint > output_size[]):
let msg = ( let msg = (
"Not enough output buffer provided to nimbus harness. Provided: " & "Not enough output buffer provided to nimbus harness. Provided: " &
$(output_size[]) & $(output_size[]) &
"Required: " & "Required: " &
$resultState.len.uint $resultState.len.uint
) )
raise newException(FuzzCrashError, msg) raise newException(FuzzCrashError, msg)
output_size[] = resultState.len.uint output_size[] = resultState.len.uint
# TODO: improvement might be to write directly to buffer with OutputStream # TODO: improvement might be to write directly to buffer with OutputStream
# and SszWriter (but then need to ensure length doesn't overflow) # and SszWriter (but then need to ensure length doesn't overflow)
@ -49,7 +50,7 @@ proc copyState(state: BeaconState, output: ptr byte,
proc nfuzz_attestation(input: openArray[byte], output: ptr byte, proc nfuzz_attestation(input: openArray[byte], output: ptr byte,
output_size: ptr uint): bool {.exportc, raises:[FuzzCrashError, Defect].} = output_size: ptr uint): bool {.exportc, raises: [FuzzCrashError, Defect].} =
var var
data: AttestationInput data: AttestationInput
cache = get_empty_per_epoch_cache() cache = get_empty_per_epoch_cache()
@ -58,7 +59,11 @@ proc nfuzz_attestation(input: openArray[byte], output: ptr byte,
data = SSZ.decode(input, AttestationInput) data = SSZ.decode(input, AttestationInput)
except MalformedSszError, SszSizeMismatchError: except MalformedSszError, SszSizeMismatchError:
let e = getCurrentException() let e = getCurrentException()
raise newException(FuzzCrashError, "SSZ deserialisation failed, likely bug in preprocessing.", e) raise newException(
FuzzCrashError,
"SSZ deserialisation failed, likely bug in preprocessing.",
e,
)
try: try:
result = process_attestation(data.state, data.attestation, result = process_attestation(data.state, data.attestation,
@ -67,14 +72,18 @@ proc nfuzz_attestation(input: openArray[byte], output: ptr byte,
# These exceptions are expected to be raised by chronicles logging: # These exceptions are expected to be raised by chronicles logging:
# See status-im/nim-chronicles#60 # See status-im/nim-chronicles#60
# TODO remove this when resolved # TODO remove this when resolved
raise newException(FuzzCrashError, "Unexpected (logging?) error in attestation processing", e) raise newException(
FuzzCrashError,
"Unexpected (logging?) error in attestation processing",
e
)
if result: if result:
result = copyState(data.state, output, output_size) result = copyState(data.state, output, output_size)
proc nfuzz_attester_slashing(input: openArray[byte], output: ptr byte, proc nfuzz_attester_slashing(input: openArray[byte], output: ptr byte,
output_size: ptr uint): bool {.exportc, raises:[FuzzCrashError, Defect].} = output_size: ptr uint): bool {.exportc, raises: [FuzzCrashError, Defect].} =
var var
data: AttesterSlashingInput data: AttesterSlashingInput
cache = get_empty_per_epoch_cache() cache = get_empty_per_epoch_cache()
@ -83,34 +92,50 @@ proc nfuzz_attester_slashing(input: openArray[byte], output: ptr byte,
data = SSZ.decode(input, AttesterSlashingInput) data = SSZ.decode(input, AttesterSlashingInput)
except MalformedSszError, SszSizeMismatchError: except MalformedSszError, SszSizeMismatchError:
let e = getCurrentException() let e = getCurrentException()
raise newException(FuzzCrashError, "SSZ deserialisation failed, likely bug in preprocessing.", e) raise newException(
FuzzCrashError,
"SSZ deserialisation failed, likely bug in preprocessing.",
e,
)
try: try:
result = process_attester_slashing(data.state, data.attesterSlashing, cache) result = process_attester_slashing(data.state, data.attesterSlashing, cache)
except ValueError as e: except ValueError as e:
# TODO remove when status-im/nim-chronicles#60 is resolved # TODO remove when status-im/nim-chronicles#60 is resolved
raise newException(FuzzCrashError, "Unexpected (logging?) error in attester slashing", e) raise newException(
FuzzCrashError,
"Unexpected (logging?) error in attester slashing",
e,
)
if result: if result:
result = copyState(data.state, output, output_size) result = copyState(data.state, output, output_size)
proc nfuzz_block(input: openArray[byte], output: ptr byte, proc nfuzz_block(input: openArray[byte], output: ptr byte,
output_size: ptr uint): bool {.exportc, raises:[FuzzCrashError, Defect].} = output_size: ptr uint): bool {.exportc, raises: [FuzzCrashError, Defect].} =
var data: BlockInput var data: BlockInput
try: try:
data = SSZ.decode(input, BlockInput) data = SSZ.decode(input, BlockInput)
except MalformedSszError, SszSizeMismatchError: except MalformedSszError, SszSizeMismatchError:
let e = getCurrentException() let e = getCurrentException()
raise newException(FuzzCrashError, "SSZ deserialisation failed, likely bug in preprocessing.", e) raise newException(
FuzzCrashError,
"SSZ deserialisation failed, likely bug in preprocessing.",
e,
)
try: try:
result = state_transition(data.state, data.beaconBlock, {}) result = state_transition(data.state, data.beaconBlock, {})
except IOError, ValueError: except IOError, ValueError:
# TODO remove when status-im/nim-chronicles#60 is resolved # TODO remove when status-im/nim-chronicles#60 is resolved
let e = getCurrentException() let e = getCurrentException()
raise newException(FuzzCrashError, "Unexpected (logging?) error in state transition", e) raise newException(
FuzzCrashError,
"Unexpected (logging?) error in state transition",
e,
)
except Exception as e: except Exception as e:
# TODO why an Exception? # TODO why an Exception?
# Lots of vendor code looks like it might raise a bare exception type # Lots of vendor code looks like it might raise a bare exception type
@ -121,7 +146,7 @@ proc nfuzz_block(input: openArray[byte], output: ptr byte,
proc nfuzz_block_header(input: openArray[byte], output: ptr byte, proc nfuzz_block_header(input: openArray[byte], output: ptr byte,
output_size: ptr uint): bool {.exportc, raises:[FuzzCrashError, Defect].} = output_size: ptr uint): bool {.exportc, raises: [FuzzCrashError, Defect].} =
var var
data: BlockHeaderInput data: BlockHeaderInput
cache = get_empty_per_epoch_cache() cache = get_empty_per_epoch_cache()
@ -130,7 +155,11 @@ proc nfuzz_block_header(input: openArray[byte], output: ptr byte,
data = SSZ.decode(input, BlockHeaderInput) data = SSZ.decode(input, BlockHeaderInput)
except MalformedSszError, SszSizeMismatchError: except MalformedSszError, SszSizeMismatchError:
let e = getCurrentException() let e = getCurrentException()
raise newException(FuzzCrashError, "SSZ deserialisation failed, likely bug in preprocessing.", e) raise newException(
FuzzCrashError,
"SSZ deserialisation failed, likely bug in preprocessing.",
e,
)
try: try:
# TODO disable bls # TODO disable bls
@ -138,7 +167,11 @@ proc nfuzz_block_header(input: openArray[byte], output: ptr byte,
except IOError, ValueError: except IOError, ValueError:
let e = getCurrentException() let e = getCurrentException()
# TODO remove when status-im/nim-chronicles#60 is resolved # TODO remove when status-im/nim-chronicles#60 is resolved
raise newException(FuzzCrashError, "Unexpected IOError in block header processing", e) raise newException(
FuzzCrashError,
"Unexpected IOError in block header processing",
e,
)
if result: if result:
result = copyState(data.state, output, output_size) result = copyState(data.state, output, output_size)
@ -149,7 +182,7 @@ proc nfuzz_block_header(input: openArray[byte], output: ptr byte,
# TODO: rework to copy immediatly in an uint8 openArray, considering we have to # TODO: rework to copy immediatly in an uint8 openArray, considering we have to
# go over the list anyhow? # go over the list anyhow?
proc nfuzz_shuffle(input_seed: ptr byte, output: var openArray[uint64]): bool proc nfuzz_shuffle(input_seed: ptr byte, output: var openArray[uint64]): bool
{.exportc, raises:[Defect].} = {.exportc, raises: [Defect].} =
var seed: Eth2Digest var seed: Eth2Digest
# Should be OK as max 2 bytes are passed by the framework. # Should be OK as max 2 bytes are passed by the framework.
let list_size = output.len.uint64 let list_size = output.len.uint64
@ -159,7 +192,10 @@ proc nfuzz_shuffle(input_seed: ptr byte, output: var openArray[uint64]): bool
var shuffled_seq: seq[ValidatorIndex] var shuffled_seq: seq[ValidatorIndex]
shuffled_seq = get_shuffled_seq(seed, list_size) shuffled_seq = get_shuffled_seq(seed, list_size)
doAssert(list_size == shuffled_seq.len.uint64, "Shuffled list should be of requested size.") doAssert(
list_size == shuffled_seq.len.uint64,
"Shuffled list should be of requested size."
)
for i in 0..<list_size: for i in 0..<list_size:
# ValidatorIndex is currently wrongly uint32 so we copy this 1 by 1, # ValidatorIndex is currently wrongly uint32 so we copy this 1 by 1,