From b9e1c6f167a5179999a648880d76a5c5e877633c Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Fri, 20 Aug 2021 11:55:04 +0100 Subject: [PATCH] 3540: small improvements to the validator (#3747) --- EIPS/eip-3540.md | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/EIPS/eip-3540.md b/EIPS/eip-3540.md index fab85885..81cf42cf 100644 --- a/EIPS/eip-3540.md +++ b/EIPS/eip-3540.md @@ -274,6 +274,8 @@ All cases should be checked for creation transaction, `CREATE` and `CREATE2`. ## Reference Implementation +### Generic Implementation + ```python FORMAT = 0xEF MAGIC = 0x00 # To be defined @@ -288,9 +290,7 @@ def validate_eof(code: bytes): return # Validate format and magic - assert(len(code) >= 3) - assert(code[1] == MAGIC) - assert(code[2] == VERSION) + assert(len(code) >= 3 and code[1] == MAGIC and code[2] == VERSION) # Process section headers section_sizes = {S_CODE: 0, S_DATA: 0} @@ -303,30 +303,31 @@ def validate_eof(code: bytes): if section_id == S_TERMINATOR: break - # Unknown section - if not section_id in section_sizes: - break + # Disallow unknown sections + assert(section_id in section_sizes) # Data section preceding code section - assert(!(section_id == S_DATA and section_sizes[S_CODE] == 0)) + assert(not (section_id == S_DATA and section_sizes[S_CODE] == 0)) - # Multiple sections + # Multiple sections with the same id assert(section_sizes[section_id] == 0) + # Truncated section size assert((pos + 1) < len(code)) section_sizes[section_id] = (code[pos] << 8) | code[pos + 1] pos += 2 + # Empty section assert(section_sizes[section_id] != 0) # Code section cannot be absent assert(section_sizes[S_CODE] != 0) - # The entire container was scanned + # The entire container must be scanned assert(len(code) == (pos + section_sizes[S_CODE] + section_sizes[S_DATA])) ``` -### Simplified implementations +### Simplified Implementation Given the rigid rules of EOF1 it is possible to implement support for the container in clients using very simple pattern matching: