3540: small improvements to the validator (#3747)

This commit is contained in:
Alex Beregszaszi 2021-08-20 11:55:04 +01:00 committed by GitHub
parent 7a7149bf0c
commit b9e1c6f167
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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: