Make fields serialization easier to spot from hex

This commit is contained in:
mratsim 2018-08-07 13:12:31 +02:00
parent b2d2f7039a
commit 9e142a68d5
1 changed files with 21 additions and 15 deletions

View File

@ -3,7 +3,7 @@ import
eth_common, stint, nimcrypto, byteutils
type
ValidatorRecord = object
ValidatorRecord {.packed.} = object
# The validator's public key
pubkey: Uint256
# What shard the validator's balance will be sent to
@ -52,12 +52,14 @@ proc appendBigEndianInt(dst: var seq[byte], src: SomeNumber) =
proc serializeETH[T](x: T): seq[byte] =
## Serialize an Ethereum type to the PoC serialization format
const
magic = mapLiterals(['\x61','E','T','H','E','R','E','U','M'], byte)
magic = "\x7FETHEREUM"
version = [byte 1, 0]
schema = typeToJson(T)
# Write magic string and version
result = @[]
result.add magic
for chr in magic:
result.add cast[byte](chr)
result.add version
# Offset of the raw data (stored as int64 even on 32-bit platform):
@ -77,8 +79,11 @@ proc serializeETH[T](x: T): seq[byte] =
# Reserve space for Blake2 hash (256-bit / 32 bytes)
result.setLen(result.len + sizeof(Hash256))
# Write the schema (we need to reinterpret the string literal as an array of byte)
result.add cast[array[schema.len, byte]](schema)
# Write the schema
for chr in schema:
result.add byte(chr)
assert result.len == offset
# Write raw data - this is similar to SimpleSerialize
for field in fields(x):
@ -93,20 +98,21 @@ proc serializeETH[T](x: T): seq[byte] =
else:
raise newException(ValueError, "Not implemented")
assert result.len == offset + sizeof(T)
# Compute the hash
result[metadataStart .. metadataStart + sizeof(Hash256)] = blake2_256.digest(result[offset ..< result.len]).data
result[metadataStart ..< metadataStart + sizeof(Hash256)] = blake2_256.digest(result[offset ..< result.len]).data
# Some reports
echo "Schema: " & $schema
echo "Schema size: " & $schema.len
echo "Raw data offset (== metadata size including schema): " & $offset
echo "Raw data size (bytes): " & $(result.len - offset)
echo "Raw data size (bytes): " & $sizeof(T)
echo "Total size (bytes): " & $result.len
when isMainModule:
let x = ValidatorRecord(
pubkey: 123456789.u256,
pubkey: high(Uint256), # 0xFFFF...FFFF
withdrawal_shard: 4455,
withdrawal_address: hexToPaddedByteArray[20]("0x1234"),
randao_commitment: Hash256(data: hexToPaddedByteArray[32]("0xAABBCCDDEEFF")),
@ -139,24 +145,24 @@ when isMainModule:
# Schema: {"pubkey":"UInt256","withdrawal_shard":"int16","withdrawal_address":"EthAddress","randao_commitment":"Hash256","balance":"int64","start_dynasty":"int64","end_dynasty":"int64"}
# Schema size: 175
# Raw data offset: 226
# Raw data size (bytes): 109
# Total size (bytes): 335
# Raw data offset (== metadata size including schema): 226
# Raw data size (bytes): 110
# Total size (bytes): 336
#
# #####################
#
# Byte representation
#
# @[97, 69, 84, 72, 69, 82, 69, 85, 77, 1, 0, 0, 0, 0, 0, 0, 0, 0, 226, 213, 67, 87, 156, 127, 178, 250, 140, 247, 198, 251, 179, 75, 124, 44, 121, 216, 1, 99, 44, 174, 253, 237, 4, 78, 77, 191, 227, 39, 25, 132, 187, 192, 52, 3, 1, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 13, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 26, 0,0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 13, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 1, 160, 183, 145, 236, 254, 127, 0, 0, 45, 169, 48, 3, 1, 0, 0, 0, 169, 13, 0, 0, 0, 0, 0, 0, 53, 158, 48, 3, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 32, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0,0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 91, 205, 21, 17, 103, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 18, 52, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 170, 187, 204, 221, 238, 255, 0, 0, 0, 0, 0, 1, 134, 160, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2]
# @[127, 69, 84, 72, 69, 82, 69, 85, 77, 1, 0, 0, 0, 0, 0, 0, 0, 0, 226, 57, 0, 86, 134, 122, 192, 114, 196, 207, 203, 93, 74, 188, 96, 189, 200, 234, 140, 195, 148, 28, 78, 203, 152, 116, 37, 74, 241, 189, 75, 40, 29, 123, 34, 112, 117, 98, 107, 101, 121, 34, 58, 34, 85, 73, 110, 116, 50, 53, 54, 34, 44, 34, 119, 105, 116, 104, 100, 114, 97, 119, 97, 108, 95, 115, 104, 97, 114, 100, 34, 58, 34, 105, 110, 116, 49, 54, 34, 44, 34, 119, 105, 116, 104, 100, 114, 97, 119, 97, 108, 95, 97, 100, 100, 114, 101, 115, 115, 34, 58, 34, 69, 116, 104, 65, 100, 100, 114, 101, 115, 115, 34, 44, 34, 114, 97, 110, 100, 97, 111, 95, 99, 111, 109, 109, 105, 116, 109, 101, 110, 116, 34, 58, 34, 72, 97, 115, 104, 50, 53, 54, 34, 44, 34, 98, 97, 108,97, 110, 99, 101, 34, 58, 34, 105, 110, 116, 54, 52, 34, 44, 34, 115, 116, 97, 114, 116, 95, 100, 121, 110, 97, 115, 116, 121, 34, 58, 34, 105, 110, 116, 54, 52, 34, 44, 34, 101, 110, 100, 95, 100, 121, 110, 97, 115, 116, 121, 34, 58, 34, 105, 110, 116, 54, 52, 34, 125, 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, 17, 103, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 18, 52, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 170, 187, 204, 221, 238, 255, 0, 0, 0, 0, 0, 1, 134, 160, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2]
#
# #####################
#
# Char representation
#
# @['a', 'E', 'T', 'H', 'E', 'R', 'E', 'U', 'M', '\x01', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\xE2', '\xD5', 'C', 'W', '\x9C', '\x7F', '\xB2', '\xFA', '\x8C', '\xF7', '\xC6', '\xFB', '\xB3', 'K', '|', ',', 'y', '\xD8', '\x01', 'c', ',', '\xAE', '\xFD', '\xED', '\x04', 'N', 'M', '\xBF', '\xE3', '\'', '\x19', '\x84', '\xBB', '\xC0', '4', '\x03', '\x01', '\x00', '\x00', '\x00', '\x0E', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\c', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x04', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x1A', '\x00','\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x06', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\c', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\f', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x0E', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x01', '\xA0', '\xB7', '\x91', '\xEC', '\xFE', '\x7F', '\x00', '\x00', '-','\xA9', '0', '\x03', '\x01', '\x00', '\x00', '\x00', '\xA9', '\c', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '5', '\x9E', '0', '\x03', '\x01', '\x00', '\x00', '\x00', '\x00', '\x00', '\x01', '\x00', '\x01', '\x00', '\x00', '\x00', '\x04', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', ' ', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '@', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\f', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\f', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x02', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\a', '[', '\xCD', '\x15', '\x11', 'g', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x12', '4', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\xAA', '\xBB', '\xCC', '\xDD', '\xEE', '\xFF', '\x00', '\x00', '\x00', '\x00', '\x00', '\x01', '\x86', '\xA0', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x01', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x02']
# @['\x7F', 'E', 'T', 'H', 'E', 'R', 'E', 'U', 'M', '\x01', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\xE2', '9', '\x00', 'V', '\x86', 'z', '\xC0', 'r', '\xC4', '\xCF', '\xCB', ']', 'J', '\xBC', '`', '\xBD', '\xC8', '\xEA', '\x8C', '\xC3', '\x94', '\x1C', 'N', '\xCB', '\x98', 't', '%', 'J', '\xF1', '\xBD', 'K', '(', '\x1D', '{', '\"', 'p', 'u', 'b', 'k', 'e', 'y', '\"', ':', '\"', 'U', 'I', 'n', 't', '2', '5', '6', '\"', ',', '\"', 'w', 'i', 't', 'h', 'd', 'r', 'a', 'w', 'a', 'l', '_', 's', 'h', 'a', 'r', 'd', '\"', ':', '\"', 'i', 'n', 't', '1', '6', '\"', ',', '\"', 'w', 'i', 't', 'h', 'd', 'r', 'a', 'w', 'a', 'l', '_', 'a', 'd', 'd', 'r', 'e', 's', 's', '\"', ':', '\"', 'E', 't', 'h', 'A', 'd', 'd', 'r','e', 's', 's', '\"', ',', '\"', 'r', 'a', 'n', 'd', 'a', 'o', '_', 'c', 'o', 'm', 'm', 'i', 't', 'm', 'e', 'n', 't', '\"', ':', '\"', 'H', 'a', 's', 'h', '2', '5', '6', '\"', ',', '\"', 'b', 'a', 'l', 'a', 'n', 'c', 'e', '\"', ':', '\"', 'i', 'n', 't', '6', '4', '\"', ',', '\"', 's', 't', 'a', 'r', 't', '_', 'd', 'y', 'n', 'a', 's', 't', 'y', '\"', ':', '\"', 'i', 'n', 't', '6', '4', '\"', ',', '\"', 'e', 'n', 'd', '_', 'd', 'y', 'n', 'a', 's', 't', 'y', '\"', ':', '\"', 'i', 'n', 't', '6', '4', '\"', '}', '\xFF', '\xFF', '\xFF', '\xFF', '\xFF', '\xFF', '\xFF', '\xFF', '\xFF', '\xFF', '\xFF', '\xFF', '\xFF', '\xFF', '\xFF', '\xFF', '\xFF', '\xFF', '\xFF', '\xFF', '\xFF', '\xFF', '\xFF', '\xFF', '\xFF', '\xFF', '\xFF', '\xFF', '\xFF', '\xFF', '\xFF', '\xFF', '\x11', 'g', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x12', '4', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\xAA', '\xBB', '\xCC', '\xDD', '\xEE', '\xFF', '\x00', '\x00', '\x00', '\x00', '\x00', '\x01', '\x86', '\xA0', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x01', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x02']
#
# #####################
#
# Hex representation
#
# 61455448455245554d010000000000000000e2d543579c7fb2fa8cf7c6fbb34b7c2c79d801632caefded044e4dbfe3271984bb501306010000000e000000000000000d000000000000000400000000000000000000000000000000000000000000001a0000000000000006000000000000000d000000000000000c000000000000000e000000000000019827b3e9fe7f000039390f0601000000a90d0000000000002d2e0f060100000000000100010000000400000000000000200000000000000040000000000000000c000000000000000c000000000000000200000000000000000000000000000000000000000000000000000000000000000000075bcd15116700000000000000000000000000000000000012340000000000000000000000000000000000000000000000000000aabbccddeeff00000000000186a000000000000000010000000000000002
# 7f455448455245554d010000000000000000e2390056867ac072c4cfcb5d4abc60bdc8ea8cc3941c4ecb9874254af1bd4b281d7b227075626b6579223a2255496e74323536222c227769746864726177616c5f7368617264223a22696e743136222c227769746864726177616c5f61646472657373223a2245746841646472657373222c2272616e64616f5f636f6d6d69746d656e74223a2248617368323536222c2262616c616e6365223a22696e743634222c2273746172745f64796e61737479223a22696e743634222c22656e645f64796e61737479223a22696e743634227dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff116700000000000000000000000000000000000012340000000000000000000000000000000000000000000000000000aabbccddeeff00000000000186a000000000000000010000000000000002