Added RLP library optimizations

This commit is contained in:
vub 2017-04-14 14:59:33 -04:00
parent 941e8a6de5
commit 470e7671fd
4 changed files with 123 additions and 90 deletions

View File

@ -1,9 +1,33 @@
from ethereum import transactions, utils
import serpent import serpent
import rlp import rlp
from ethereum import utils
from ethereum import tester
from ethereum import transactions
sighash = serpent.compile('sighash.se.py') sighash = serpent.compile('sighash.se.py')
tests = [
[b"\x01"],
[b"\x80", "a"],
[b"\x81", "b"],
[b""],
[b"", b"\x01", b""],
[b"", b"\x81", b""],
[b"dog", b"c" * 54, b"\x01"],
[b"\x01", b"c" * 55, b"pig"],
[b"moose", b"c" * 56, b"\x00"],
[b'\x01', b'55555555555555555555555555555555', b'\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', b'', b'\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', b'\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\x1b\x88\xa7\x85r\x1b3\x17\xcaP\x96\xca\xd3S\xfcgM\xec\xe0\xf5!\xc8\xb4m\xd9\xb7E\xf3\x81d\x87\x93VD\xe0Ej\xcd\xec\x80\x11\x86(qZ\x9b\x80\xbf\xce\xe5*\r\x9d.o\xcd\x11s\xc5\xbc\x8c\xcb\xb9\xa9 ']
]
s = tester.state()
c = s.evm(sighash, sender=tester.k0, endowment=0)
for test in tests:
z = s.send(tester.k0, c, 0, rlp.encode(test))
assert z == utils.sha3(rlp.encode(test[:-1]))
print("Passed test, gas consumed: ", s.state.receipts[-1].gas_used - s.state.receipts[-2].gas_used - s.last_tx.intrinsic_gas_used)
# Create transaction # Create transaction
t = transactions.Transaction(0, 30 * 10**9, 2999999, '', 0, sighash) t = transactions.Transaction(0, 30 * 10**9, 2999999, '', 0, sighash)
t.startgas = t.intrinsic_gas_used + 50000 + 200 * len(sighash) t.startgas = t.intrinsic_gas_used + 50000 + 200 * len(sighash)
@ -16,18 +40,3 @@ print('Send %d wei to %s' % (t.startgas * t.gasprice,
print('Contract address: 0x'+utils.encode_hex(utils.mk_contract_address(t.sender, 0))) print('Contract address: 0x'+utils.encode_hex(utils.mk_contract_address(t.sender, 0)))
print('Code: 0x'+utils.encode_hex(rlp.encode(t))) print('Code: 0x'+utils.encode_hex(rlp.encode(t)))
sighash = serpent.compile('sqrt.se.py')
# Create transaction
t = transactions.Transaction(0, 30 * 10**9, 2999999, '', 0, sighash)
t.startgas = t.intrinsic_gas_used + 50000 + 200 * len(sighash)
t.v = 27
t.r = 45
t.s = 79
print("Sqrt")
print('Send %d wei to %s' % (t.startgas * t.gasprice,
'0x'+utils.encode_hex(t.sender)))
print('Contract address: 0x'+utils.encode_hex(utils.mk_contract_address(t.sender, 0)))
print('Code: 0x'+utils.encode_hex(rlp.encode(t)))

View File

@ -1,58 +1,64 @@
# Fetches the char from calldata at position $x
macro calldatachar($x): macro calldatachar($x):
div(calldataload($x), 2**248) div(calldataload($x), 2**248)
# Fetches the next $b bytes from calldata starting at position $x
# Assumes that there is nothing important in memory at bytes 0..63
macro calldatabytes_as_int($x, $b): macro calldatabytes_as_int($x, $b):
div(calldataload($x), 256**(32-$b)) ~mstore(32-$b, calldataload($x))
~mload(0)
# Position in calldata
c0 = calldatachar(0) with pos = 0:
if c0 < 192: # First char in calldata
~invalid() with c0 = calldatachar(0):
elif c0 < 248: # The start of the array must be in 192...255 because it represents
# a list length
# Length ++ body case
if c0 < 248:
pos = 1 pos = 1
L = c0 - 192 # Length of length ++ length ++ body case
else:
pos = 1 + (c0 - 247)
if calldatachar(1) == 0:
~invalid()
L = calldatabytes_as_int(1, c0 - 247)
if pos + L != ~calldatasize():
~invalid()
startpos = pos
lastpos = 0
while pos < ~calldatasize():
c = calldatachar(pos)
if c < 128:
lastpos = pos
pos += 1
elif c < 184:
L = c - 128
lastpos = pos
pos += L + 1
elif c < 192:
if calldatachar(pos + 1) == 0:
~invalid()
L = calldatabytes_as_int(pos + 1, c - 183)
lastpos = pos
pos += L + (c - 183) + 1
else: else:
~invalid() pos = (c0 - 246)
# Start position of the list (save it)
with startpos = pos:
# Start position of the previous element
with lastpos = 0:
# Keep looping until we hit the end of the input
while pos < ~calldatasize():
# Next char in calldata
with c = calldatachar(pos):
lastpos = pos
# Single byte 0x00...0x7f body case
if c < 128:
pos += 1
# Length ++ body case
elif c < 184:
pos += c - 127
# Length of length ++ length ++ body case
elif c < 192:
pos += calldatabytes_as_int(pos + 1, c - 183) + (c - 182)
# Length of new RLP list
newlen = lastpos - startpos with newlen = lastpos - startpos:
newmemindex = 1000 # Length ++ body case
if newlen < 56: if newlen < 56:
~mstore8(newmemindex, 192 + newlen) # Store length in the first byte
~calldatacopy(newmemindex + 1, startpos, newlen) ~mstore8(0, 192 + newlen)
return(~sha3(newmemindex, 1 + newlen)) # Copy calldata right after length
else: ~calldatacopy(1, startpos, newlen)
_log = 0 # Return the hash
_newlen = newlen return(~sha3(0, 1 + newlen))
while _newlen: else:
_log += 1 # The log256 of the length (ie. length of length)
_newlen = ~div(_newlen, 256) # Can't go higher than 16777216 bytes due to gas limits
~mstore8(newmemindex, 247 + _log) with _log = if(newlen < 256, 1, if(newlen < 65536, 2, 3)):
~mstore(newmemindex + 1, newlen * (256 ** (32 - _log))) # Store the length
~calldatacopy(newmemindex + 1 + _log, startpos, newlen) ~mstore(0, newlen)
return(~sha3(newmemindex, 1 + _log + newlen)) # Store the length of the length right before the length
with 31minuslog = 31 - _log:
~mstore8(31minuslog, 247 + _log)
# Store the rest of the data
~calldatacopy(32, startpos, newlen)
# Return the hash
return(~sha3(31minuslog, 1 + _log + newlen))

View File

@ -356,14 +356,18 @@ def prepare(validator_index: num, prepare_msg: bytes <= 1024):
# self.validators[validator_index].max_prepared = epoch # self.validators[validator_index].max_prepared = epoch
# Record that this prepare took place # Record that this prepare took place
new_ancestry_hash = sha3(concat(hash, ancestry_hash)) new_ancestry_hash = sha3(concat(hash, ancestry_hash))
curdyn_prepares = self.consensus_messages[epoch].prepares[sighash]
if in_current_dynasty: if in_current_dynasty:
self.consensus_messages[epoch].prepares[sighash] += this_validators_deposit curdyn_prepares += this_validators_deposit
self.consensus_messages[epoch].prepares[sighash] = curdyn_prepares
prevdyn_prepares = self.consensus_messages[epoch].prev_dyn_prepares[sighash]
if in_prev_dynasty: if in_prev_dynasty:
self.consensus_messages[epoch].prev_dyn_prepares[sighash] += this_validators_deposit prevdyn_prepares += this_validators_deposit
self.consensus_messages[epoch].prev_dyn_prepares[sighash] = prevdyn_prepares
# If enough prepares with the same epoch_source and hash are made, # If enough prepares with the same epoch_source and hash are made,
# then the hash value is justified for commitment # then the hash value is justified for commitment
if (self.consensus_messages[epoch].prepares[sighash] >= self.total_deposits[self.dynasty] * 2 / 3 and \ if (curdyn_prepares >= self.total_deposits[self.dynasty] * 2 / 3 and \
self.consensus_messages[epoch].prev_dyn_prepares[sighash] >= self.total_deposits[self.dynasty - 1] * 2 / 3) and \ prevdyn_prepares >= self.total_deposits[self.dynasty - 1] * 2 / 3) and \
not self.consensus_messages[epoch].ancestry_hash_justified[new_ancestry_hash]: not self.consensus_messages[epoch].ancestry_hash_justified[new_ancestry_hash]:
self.consensus_messages[epoch].ancestry_hash_justified[new_ancestry_hash] = True self.consensus_messages[epoch].ancestry_hash_justified[new_ancestry_hash] = True
self.consensus_messages[epoch].hash_justified[hash] = True self.consensus_messages[epoch].hash_justified[hash] = True
@ -403,18 +407,19 @@ def commit(validator_index: num, commit_msg: bytes <= 1024):
assert self.validators[validator_index].prev_commit_epoch == prev_commit_epoch assert self.validators[validator_index].prev_commit_epoch == prev_commit_epoch
assert prev_commit_epoch < epoch assert prev_commit_epoch < epoch
self.validators[validator_index].prev_commit_epoch = epoch self.validators[validator_index].prev_commit_epoch = epoch
this_validators_deposit = self.validators[validator_index].deposit
# Pay the reward if the blockhash is correct # Pay the reward if the blockhash is correct
if True: #if blockhash(epoch * self.epoch_length) == hash: if True: #if blockhash(epoch * self.epoch_length) == hash:
reward = floor(self.validators[validator_index].deposit * self.reward_factor) reward = floor(this_validators_deposit * self.reward_factor)
self.validators[validator_index].deposit += reward self.validators[validator_index].deposit += reward
self.total_deposits[self.dynasty] += reward self.total_deposits[self.dynasty] += reward
# Can't commit for this epoch again # Can't commit for this epoch again
# self.validators[validator_index].max_committed = epoch # self.validators[validator_index].max_committed = epoch
# Record that this commit took place # Record that this commit took place
if in_current_dynasty: if in_current_dynasty:
self.consensus_messages[epoch].commits[hash] += self.validators[validator_index].deposit self.consensus_messages[epoch].commits[hash] += this_validators_deposit
if in_prev_dynasty: if in_prev_dynasty:
self.consensus_messages[epoch].prev_dyn_commits[hash] += self.validators[validator_index].deposit self.consensus_messages[epoch].prev_dyn_commits[hash] += this_validators_deposit
# Record if sufficient commits have been made for the block to be finalized # Record if sufficient commits have been made for the block to be finalized
if (self.consensus_messages[epoch].commits[hash] >= self.total_deposits[self.dynasty] * 2 / 3 and \ if (self.consensus_messages[epoch].commits[hash] >= self.total_deposits[self.dynasty] * 2 / 3 and \
self.consensus_messages[epoch].prev_dyn_commits[hash] >= self.total_deposits[self.dynasty - 1] * 2 / 3) and \ self.consensus_messages[epoch].prev_dyn_commits[hash] >= self.total_deposits[self.dynasty - 1] * 2 / 3) and \

View File

@ -11,34 +11,36 @@ t.gas_limit = 9999999
EPOCH_LENGTH = 100 EPOCH_LENGTH = 100
def inject_tx(txhex):
tx = rlp.decode(utils.decode_hex(txhex[2:]), transactions.Transaction)
s.state.set_balance(tx.sender, tx.startgas * tx.gasprice)
state_transition.apply_transaction(s.state, tx)
contract_address = utils.mk_contract_address(tx.sender, 0)
assert s.state.get_code(contract_address)
return contract_address
# Install RLP decoder library # Install RLP decoder library
s.state.set_balance('0x0A51b02F77E7c8dc64962B9d5FdAb8D9eC6cfBbe', 9366960000000000) rlp_decoder_address = inject_tx( '0xf90237808506fc23ac00830330888080b902246102128061000e60003961022056600060007f010000000000000000000000000000000000000000000000000000000000000060003504600060c082121515585760f882121561004d5760bf820336141558576001905061006e565b600181013560f783036020035260005160f6830301361415585760f6820390505b5b368112156101c2577f010000000000000000000000000000000000000000000000000000000000000081350483602086026040015260018501945060808112156100d55760018461044001526001828561046001376001820191506021840193506101bc565b60b881121561014357608081038461044001526080810360018301856104600137608181141561012e5760807f010000000000000000000000000000000000000000000000000000000000000060018401350412151558575b607f81038201915060608103840193506101bb565b60c08112156101b857600182013560b782036020035260005160388112157f010000000000000000000000000000000000000000000000000000000000000060018501350402155857808561044001528060b6838501038661046001378060b6830301830192506020810185019450506101ba565bfe5b5b5b5061006f565b601f841315155857602060208502016020810391505b6000821215156101fc578082604001510182826104400301526020820391506101d8565b808401610420528381018161044003f350505050505b6000f31b2d4f')
state_transition.apply_transaction(s.state, rlp.decode(utils.decode_hex('f903bf808506fc23ac008304c3a88080b903ac61039a8061000e6000396103a85660006101df5361202059905901600090526101008152602081019050602052600060605261040036018060200159905901600090528181526020810190509050608052600060e0527f0100000000000000000000000000000000000000000000000000000000000000600035046101005260c061010051121561007e57fe5b60f86101005112156100a95760c061010051036001013614151561009e57fe5b6001610120526100ec565b60f761010051036020036101000a600161012051013504610140526101405160f7610100510360010101361415156100dd57fe5b60f76101005103600101610120525b5b366101205112156102ec577f01000000000000000000000000000000000000000000000000000000000000006101205135046101005260e0516060516020026020510152600160605101606052608061010051121561017a57600160e0516080510152600161012051602060e0516080510101376001610120510161012052602160e0510160e0526102da565b60b8610100511215610218576080610100510360e05160805101526080610100510360016101205101602060e05160805101013760816101005114156101ef5760807f010000000000000000000000000000000000000000000000000000000000000060016101205101350412156101ee57fe5b5b600160806101005103016101205101610120526020608061010051030160e0510160e0526102d9565b60c06101005112156102d65760b761010051036020036101000a6001610120510135046101405260007f0100000000000000000000000000000000000000000000000000000000000000600161012051013504141561027357fe5b603861014051121561028157fe5b6101405160e05160805101526101405160b761010051600161012051010103602060e05160805101013761014051600160b7610100510301016101205101610120526020610140510160e0510160e0526102d8565bfe5b5b5b602060605113156102e757fe5b6100ed565b60e051606051602002602051015261082059905901600090526108008152602081019050610160526000610120525b6060516101205113151561035c576020602060605102610120516020026020510151010161012051602002610160510152600161012051016101205261031b565b60e051600a8105601201816020602060605102610160510101836080516000600486f161038557fe5b5050602060e051602060605102010161016051f35b6000f31b2d4f'), transactions.Transaction))
rlp_decoder_address = utils.normalize_address('0x84E7F44DdDc2Eaf6cdd08C18c80d1c4E15F99FF8')
assert s.state.get_code(rlp_decoder_address)
# Install sig hasher # Install sig hasher
s.state.set_balance('0x6e7406512b244843c1171840dfcd3d7532d979fe', 7291200000000000) s.state.set_balance('0x6e7406512b244843c1171840dfcd3d7532d979fe', 7291200000000000)
state_transition.apply_transaction(s.state, rlp.decode(utils.decode_hex('f902b9808506fc23ac008303b5608080b902a66102948061000e6000396102a2567f01000000000000000000000000000000000000000000000000000000000000006000350460205260c0602051121561003857fe6100a7565b60f8602051121561005657600160405260c0602051036060526100a6565b60f76020510360010160405260007f010000000000000000000000000000000000000000000000000000000000000060013504141561009157fe5b60f7602051036020036101000a600135046060525b5b36606051604051011415156100b857fe5b604051608052600060a0525b3660405112156101c0577f0100000000000000000000000000000000000000000000000000000000000000604051350460c052608060c05112156101165760405160a0526001604051016040526101bb565b60b860c051121561014257608060c0510360605260405160a052600160605101604051016040526101ba565b60c060c05112156101b75760007f01000000000000000000000000000000000000000000000000000000000000006001604051013504141561018057fe5b60b760c051036020036101000a600160405101350460605260405160a052600160b760c051036060510101604051016040526101b9565bfe5b5b5b6100c4565b60805160a0510360e0526103e861010052603860e051121561020f5760e05160c001610100515360e051608051600161010051013760e0516001016101005120610120526020610120f3610293565b60006101405260e051610160525b610160511561024257600161014051016101405261010061016051046101605261021d565b6101405160f7016101005153610140516020036101000a60e05102600161010051015260e0516080516101405160016101005101013760e05161014051600101016101005120610180526020610180f35b5b6000f31b2d4f'), transactions.Transaction)) sighasher_address = inject_tx( '0xf9016d808506fc23ac0083026a508080b9015a6101488061000e6000396101565660007f01000000000000000000000000000000000000000000000000000000000000006000350460f8811215610038576001915061003f565b60f6810391505b508060005b368312156100c8577f01000000000000000000000000000000000000000000000000000000000000008335048391506080811215610087576001840193506100c2565b60b881121561009d57607f8103840193506100c1565b60c08112156100c05760b68103600185013560b783036020035260005101840193505b5b5b50610044565b81810360388112156100f4578060c00160005380836001378060010160002060e052602060e0f3610143565b61010081121561010557600161011b565b6201000081121561011757600261011a565b60035b5b8160005280601f038160f701815382856020378282600101018120610140526020610140f350505b505050505b6000f31b2d4f')
sighasher_address = utils.normalize_address('0x476c2ca9a7f3b16feca86512276271faf63b6a24')
assert s.state.get_code(sighasher_address)
# Install purity checker # Install purity checker
s.state.set_balance('0xea0f0d55ee82edf248ed648a9a8d213fba8b5081', 10842480000000000) purity_checker_address = inject_tx( '0xf90467808506fc23ac00830583c88080b904546104428061000e60003961045056600061033f537c0100000000000000000000000000000000000000000000000000000000600035047f80010000000000000000000000000000000000000030ffff1c0e00000000000060205263a1903eab8114156103f7573659905901600090523660048237600435608052506080513b806020015990590160009052818152602081019050905060a0526080513b600060a0516080513c6080513b8060200260200159905901600090528181526020810190509050610100526080513b806020026020015990590160009052818152602081019050905061016052600060005b602060a05103518212156103c957610100601f8360a051010351066020518160020a161561010a57fe5b80606013151561011e57607f811315610121565b60005b1561014f5780607f036101000a60018460a0510101510482602002610160510152605e8103830192506103b2565b60f18114801561015f5780610164565b60f282145b905080156101725780610177565b60f482145b9050156103aa5760028212151561019e5760606001830360200261010051015112156101a1565b60005b156101bc57607f6001830360200261010051015113156101bf565b60005b156101d157600282036102605261031e565b6004821215156101f057600360018303602002610100510151146101f3565b60005b1561020d57605a6002830360200261010051015114610210565b60005b1561022b57606060038303602002610100510151121561022e565b60005b1561024957607f60038303602002610100510151131561024c565b60005b1561025e57600482036102605261031d565b60028212151561027d57605a6001830360200261010051015114610280565b60005b1561029257600282036102605261031c565b6002821215156102b157609060018303602002610100510151146102b4565b60005b156102c657600282036102605261031b565b6002821215156102e65760806001830360200261010051015112156102e9565b60005b156103035760906001830360200261010051015112610306565b60005b1561031857600282036102605261031a565bfe5b5b5b5b5b604060405990590160009052600081526102605160200261016051015181602001528090502054156103555760016102a052610393565b60306102605160200261010051015114156103755760016102a052610392565b60606102605160200261010051015114156103915760016102a0525b5b5b6102a051151561039f57fe5b6001830192506103b1565b6001830192505b5b8082602002610100510152600182019150506100e0565b50506001604060405990590160009052600081526080518160200152809050205560016102e05260206102e0f35b63c23697a8811415610440573659905901600090523660048237600435608052506040604059905901600090526000815260805181602001528090502054610300526020610300f35b505b6000f31b2d4f')
state_transition.apply_transaction(s.state, rlp.decode(utils.decode_hex('f90467808506fc23ac00830583c88080b904546104428061000e60003961045056600061033f537c0100000000000000000000000000000000000000000000000000000000600035047f80010000000000000000000000000000000000000030ffff1c0e00000000000060205263a1903eab8114156103f7573659905901600090523660048237600435608052506080513b806020015990590160009052818152602081019050905060a0526080513b600060a0516080513c6080513b8060200260200159905901600090528181526020810190509050610100526080513b806020026020015990590160009052818152602081019050905061016052600060005b602060a05103518212156103c957610100601f8360a051010351066020518160020a161561010a57fe5b80606013151561011e57607f811315610121565b60005b1561014f5780607f036101000a60018460a0510101510482602002610160510152605e8103830192506103b2565b60f18114801561015f5780610164565b60f282145b905080156101725780610177565b60f482145b9050156103aa5760028212151561019e5760606001830360200261010051015112156101a1565b60005b156101bc57607f6001830360200261010051015113156101bf565b60005b156101d157600282036102605261031e565b6004821215156101f057600360018303602002610100510151146101f3565b60005b1561020d57605a6002830360200261010051015114610210565b60005b1561022b57606060038303602002610100510151121561022e565b60005b1561024957607f60038303602002610100510151131561024c565b60005b1561025e57600482036102605261031d565b60028212151561027d57605a6001830360200261010051015114610280565b60005b1561029257600282036102605261031c565b6002821215156102b157609060018303602002610100510151146102b4565b60005b156102c657600282036102605261031b565b6002821215156102e65760806001830360200261010051015112156102e9565b60005b156103035760906001830360200261010051015112610306565b60005b1561031857600282036102605261031a565bfe5b5b5b5b5b604060405990590160009052600081526102605160200261016051015181602001528090502054156103555760016102a052610393565b60306102605160200261010051015114156103755760016102a052610392565b60606102605160200261010051015114156103915760016102a0525b5b5b6102a051151561039f57fe5b6001830192506103b1565b6001830192505b5b8082602002610100510152600182019150506100e0565b50506001604060405990590160009052600081526080518160200152809050205560016102e05260206102e0f35b63c23697a8811415610440573659905901600090523660048237600435608052506040604059905901600090526000815260805181602001528090502054610300526020610300f35b505b6000f31b2d4f'), transactions.Transaction))
purity_checker_address = utils.normalize_address('0x9f56d05661285a8fcc0dbdb3c8070ad024030af3')
assert s.state.get_code(purity_checker_address)
ct = abi.ContractTranslator([{'name': 'check(address)', 'type': 'function', 'constant': True, 'inputs': [{'name': 'addr', 'type': 'address'}], 'outputs': [{'name': 'out', 'type': 'bool'}]}, {'name': 'submit(address)', 'type': 'function', 'constant': False, 'inputs': [{'name': 'addr', 'type': 'address'}], 'outputs': [{'name': 'out', 'type': 'bool'}]}]) ct = abi.ContractTranslator([{'name': 'check(address)', 'type': 'function', 'constant': True, 'inputs': [{'name': 'addr', 'type': 'address'}], 'outputs': [{'name': 'out', 'type': 'bool'}]}, {'name': 'submit(address)', 'type': 'function', 'constant': False, 'inputs': [{'name': 'addr', 'type': 'address'}], 'outputs': [{'name': 'out', 'type': 'bool'}]}])
# Check that the RLP decoding library and the sig hashing library are "pure"
assert utils.big_endian_to_int(s.send(t.k0, purity_checker_address, 0, ct.encode('submit', [rlp_decoder_address]))) == 1 assert utils.big_endian_to_int(s.send(t.k0, purity_checker_address, 0, ct.encode('submit', [rlp_decoder_address]))) == 1
assert utils.big_endian_to_int(s.send(t.k0, purity_checker_address, 0, ct.encode('submit', [sighasher_address]))) == 1 assert utils.big_endian_to_int(s.send(t.k0, purity_checker_address, 0, ct.encode('submit', [sighasher_address]))) == 1
# Install Casper # Install Casper
casper_code = open('simple_casper.v.py').read().replace('0x1Db3439a222C519ab44bb1144fC28167b4Fa6EE6', utils.checksum_encode(t.a0)) casper_code = open('simple_casper.v.py').read().replace('0x1Db3439a222C519ab44bb1144fC28167b4Fa6EE6', utils.checksum_encode(t.a0)) \
.replace('0x476c2cA9a7f3B16FeCa86512276271FAf63B6a24', utils.checksum_encode(sighasher_address))
print('Casper code length', len(compiler.compile(casper_code))) print('Casper code length', len(compiler.compile(casper_code)))
@ -87,8 +89,10 @@ start = s.snapshot()
print("Epoch initialized") print("Epoch initialized")
print("Reward factor: %.8f" % (casper.get_reward_factor() * 2 / 3)) print("Reward factor: %.8f" % (casper.get_reward_factor() * 2 / 3))
# Send a prepare message # Send a prepare message
#configure_logging(config_string=config_string)
casper.prepare(0, mk_prepare(1, '\x35' * 32, '\x00' * 32, 0, '\x00' * 32, t.k0)) casper.prepare(0, mk_prepare(1, '\x35' * 32, '\x00' * 32, 0, '\x00' * 32, t.k0))
print('Gas consumed for a prepare', s.state.receipts[-1].gas_used - s.state.receipts[-2].gas_used) print('Gas consumed for a prepare: %d (including %d intrinsic gas)' %
(s.state.receipts[-1].gas_used - s.state.receipts[-2].gas_used, s.last_tx.intrinsic_gas_used))
epoch_1_anchash = utils.sha3(b'\x35' * 32 + b'\x00' * 32) epoch_1_anchash = utils.sha3(b'\x35' * 32 + b'\x00' * 32)
assert casper.get_consensus_messages__hash_justified(1, b'\x35' * 32) assert casper.get_consensus_messages__hash_justified(1, b'\x35' * 32)
assert casper.get_consensus_messages__ancestry_hash_justified(1, epoch_1_anchash) assert casper.get_consensus_messages__ancestry_hash_justified(1, epoch_1_anchash)
@ -102,7 +106,8 @@ assert not success
print("Prepare message fails the second time") print("Prepare message fails the second time")
# Send a commit message # Send a commit message
casper.commit(0, mk_commit(1, '\x35' * 32, 0, t.k0)) casper.commit(0, mk_commit(1, '\x35' * 32, 0, t.k0))
print('Gas consumed for a commit', s.state.receipts[-1].gas_used - s.state.receipts[-2].gas_used) print('Gas consumed for a commit: %d (including %d intrinsic gas)' %
(s.state.receipts[-1].gas_used - s.state.receipts[-2].gas_used, s.last_tx.intrinsic_gas_used))
# Check that we committed # Check that we committed
assert casper.get_consensus_messages__committed(1) assert casper.get_consensus_messages__committed(1)
print("Commit message processed") print("Commit message processed")
@ -342,12 +347,20 @@ print("Epoch 7 initialized")
# Here three prepares and three commits should be sufficient! # Here three prepares and three commits should be sufficient!
epoch_7_anchash = utils.sha3(b'\x70' * 32 + epoch_6_anchash) epoch_7_anchash = utils.sha3(b'\x70' * 32 + epoch_6_anchash)
for i, k in enumerate([t.k0, t.k1, t.k2]): for i, k in enumerate([t.k0, t.k1, t.k2]):
#if i == 1:
# configure_logging(config_string=config_string)
casper.prepare(i, mk_prepare(7, b'\x70' * 32, epoch_6_anchash, 6, epoch_6_anchash, k)) casper.prepare(i, mk_prepare(7, b'\x70' * 32, epoch_6_anchash, 6, epoch_6_anchash, k))
#if i == 1:
# import sys
# sys.exit()
print('Gas consumed for first prepare', s.state.receipts[-1].gas_used - s.state.receipts[-2].gas_used) print('Gas consumed for first prepare', s.state.receipts[-1].gas_used - s.state.receipts[-2].gas_used)
print('Gas consumed for second prepare', s.state.receipts[-2].gas_used - s.state.receipts[-3].gas_used) print('Gas consumed for second prepare', s.state.receipts[-2].gas_used - s.state.receipts[-3].gas_used)
print('Gas consumed for third prepare', s.state.receipts[-3].gas_used - s.state.receipts[-4].gas_used) print('Gas consumed for third prepare', s.state.receipts[-3].gas_used - s.state.receipts[-4].gas_used)
for i, k in enumerate([t.k0, t.k1, t.k2]): for i, k in enumerate([t.k0, t.k1, t.k2]):
casper.commit(i, mk_commit(7, b'\x70' * 32, 6, k)) casper.commit(i, mk_commit(7, b'\x70' * 32, 6, k))
print('Gas consumed for first commit', s.state.receipts[-1].gas_used - s.state.receipts[-2].gas_used)
print('Gas consumed for second commit', s.state.receipts[-2].gas_used - s.state.receipts[-3].gas_used)
print('Gas consumed for third commit', s.state.receipts[-3].gas_used - s.state.receipts[-4].gas_used)
assert casper.get_consensus_messages__committed(7) assert casper.get_consensus_messages__committed(7)
print("Three of four prepares and commits sufficient") print("Three of four prepares and commits sufficient")
# Start epoch 8 / dynasty 7 # Start epoch 8 / dynasty 7