2019-03-18 16:18:57 +00:00
|
|
|
import sys
|
|
|
|
import function_puller
|
2019-05-07 11:13:22 +00:00
|
|
|
from optparse import OptionParser
|
2019-03-18 16:18:57 +00:00
|
|
|
|
2019-04-07 07:02:20 +00:00
|
|
|
def build_phase0_spec(sourcefile, outfile):
|
2019-03-18 18:51:52 +00:00
|
|
|
code_lines = []
|
|
|
|
code_lines.append("""
|
2019-05-07 09:12:33 +00:00
|
|
|
|
2019-03-18 16:18:57 +00:00
|
|
|
from typing import (
|
|
|
|
Any,
|
2019-04-07 07:02:20 +00:00
|
|
|
Dict,
|
2019-03-18 16:18:57 +00:00
|
|
|
List,
|
|
|
|
NewType,
|
|
|
|
Tuple,
|
|
|
|
)
|
2019-05-09 05:45:00 +00:00
|
|
|
from eth2spec.utils.minimal_ssz import (
|
|
|
|
SSZType,
|
|
|
|
hash_tree_root,
|
|
|
|
signing_root,
|
|
|
|
)
|
|
|
|
from eth2spec.utils.bls_stub import (
|
|
|
|
bls_aggregate_pubkeys,
|
|
|
|
bls_verify,
|
|
|
|
bls_verify_multiple,
|
|
|
|
)
|
2019-05-09 06:25:08 +00:00
|
|
|
from eth2spec.utils.hash_function import hash
|
2019-03-27 17:21:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
# stub, will get overwritten by real var
|
|
|
|
SLOTS_PER_EPOCH = 64
|
|
|
|
|
2019-03-18 16:18:57 +00:00
|
|
|
Slot = NewType('Slot', int) # uint64
|
|
|
|
Epoch = NewType('Epoch', int) # uint64
|
|
|
|
Shard = NewType('Shard', int) # uint64
|
|
|
|
ValidatorIndex = NewType('ValidatorIndex', int) # uint64
|
|
|
|
Gwei = NewType('Gwei', int) # uint64
|
|
|
|
Bytes32 = NewType('Bytes32', bytes) # bytes32
|
|
|
|
BLSPubkey = NewType('BLSPubkey', bytes) # bytes48
|
|
|
|
BLSSignature = NewType('BLSSignature', bytes) # bytes96
|
|
|
|
Store = None
|
2019-04-25 08:03:02 +00:00
|
|
|
""")
|
2019-03-18 18:51:52 +00:00
|
|
|
|
2019-04-07 07:02:20 +00:00
|
|
|
code_lines += function_puller.get_spec(sourcefile)
|
2019-03-18 18:51:52 +00:00
|
|
|
|
|
|
|
code_lines.append("""
|
2019-05-01 18:56:48 +00:00
|
|
|
# Monkey patch validator compute committee code
|
|
|
|
_compute_committee = compute_committee
|
2019-03-19 17:15:51 +00:00
|
|
|
committee_cache = {}
|
2019-03-27 17:21:07 +00:00
|
|
|
|
|
|
|
|
2019-05-01 18:56:48 +00:00
|
|
|
def compute_committee(indices: List[ValidatorIndex], seed: Bytes32, index: int, count: int) -> List[ValidatorIndex]:
|
|
|
|
param_hash = (hash_tree_root(indices), seed, index, count)
|
2019-03-18 18:51:52 +00:00
|
|
|
|
2019-03-19 17:15:51 +00:00
|
|
|
if param_hash in committee_cache:
|
|
|
|
return committee_cache[param_hash]
|
2019-03-18 18:51:52 +00:00
|
|
|
else:
|
2019-05-01 18:56:48 +00:00
|
|
|
ret = _compute_committee(indices, seed, index, count)
|
2019-03-19 17:15:51 +00:00
|
|
|
committee_cache[param_hash] = ret
|
2019-03-18 18:51:52 +00:00
|
|
|
return ret
|
2019-03-18 16:18:57 +00:00
|
|
|
|
|
|
|
|
2019-03-18 18:51:52 +00:00
|
|
|
# Monkey patch hash cache
|
|
|
|
_hash = hash
|
|
|
|
hash_cache = {}
|
2019-03-27 17:21:07 +00:00
|
|
|
|
|
|
|
|
2019-03-18 18:51:52 +00:00
|
|
|
def hash(x):
|
|
|
|
if x in hash_cache:
|
|
|
|
return hash_cache[x]
|
|
|
|
else:
|
|
|
|
ret = _hash(x)
|
|
|
|
hash_cache[x] = ret
|
|
|
|
return ret
|
2019-04-07 07:02:20 +00:00
|
|
|
|
2019-05-09 06:25:08 +00:00
|
|
|
|
2019-04-07 07:02:20 +00:00
|
|
|
# Access to overwrite spec constants based on configuration
|
|
|
|
def apply_constants_preset(preset: Dict[str, Any]):
|
|
|
|
global_vars = globals()
|
2019-04-07 13:36:05 +00:00
|
|
|
for k, v in preset.items():
|
2019-04-07 07:02:20 +00:00
|
|
|
global_vars[k] = v
|
|
|
|
|
|
|
|
# Deal with derived constants
|
2019-04-07 13:36:05 +00:00
|
|
|
global_vars['GENESIS_EPOCH'] = slot_to_epoch(GENESIS_SLOT)
|
2019-04-25 08:03:02 +00:00
|
|
|
|
2019-04-07 13:36:05 +00:00
|
|
|
# Initialize SSZ types again, to account for changed lengths
|
|
|
|
init_SSZ_types()
|
2019-04-07 07:02:20 +00:00
|
|
|
""")
|
2019-03-18 16:18:57 +00:00
|
|
|
|
2019-03-18 18:51:52 +00:00
|
|
|
with open(outfile, 'w') as out:
|
|
|
|
out.write("\n".join(code_lines))
|
2019-03-18 16:18:57 +00:00
|
|
|
|
|
|
|
|
2019-05-07 11:13:22 +00:00
|
|
|
def build_phase1_spec(sourcefile, outfile):
|
|
|
|
code_lines = []
|
|
|
|
code_lines.append("""
|
|
|
|
from eth2spec.phase0.spec import *
|
2019-05-08 22:30:08 +00:00
|
|
|
from eth2spec.phase0.spec import apply_constants_preset as apply_constants_preset_phase0
|
2019-05-07 11:13:22 +00:00
|
|
|
|
|
|
|
""")
|
|
|
|
|
|
|
|
code_lines += function_puller.get_spec(sourcefile)
|
|
|
|
|
2019-05-08 22:30:08 +00:00
|
|
|
code_lines.append("""
|
|
|
|
# Access to overwrite spec constants based on configuration
|
|
|
|
def apply_constants_preset(preset: Dict[str, Any]):
|
|
|
|
|
|
|
|
apply_constants_preset_phase0(preset)
|
|
|
|
|
|
|
|
global_vars = globals()
|
|
|
|
for k, v in preset.items():
|
|
|
|
global_vars[k] = v
|
|
|
|
|
|
|
|
# Deal with derived constants
|
|
|
|
global_vars['GENESIS_EPOCH'] = slot_to_epoch(GENESIS_SLOT)
|
|
|
|
|
|
|
|
# Initialize SSZ types again, to account for changed lengths
|
|
|
|
init_SSZ_types()
|
|
|
|
""")
|
|
|
|
|
2019-05-07 11:13:22 +00:00
|
|
|
with open(outfile, 'w') as out:
|
|
|
|
out.write("\n".join(code_lines))
|
|
|
|
|
|
|
|
|
2019-03-18 18:51:52 +00:00
|
|
|
if __name__ == '__main__':
|
2019-05-07 11:13:22 +00:00
|
|
|
parser = OptionParser()
|
|
|
|
parser.add_option("-p", "--phase", dest="phase", type="int", default=0,
|
|
|
|
help="Build for phase #")
|
|
|
|
|
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
|
|
|
|
if len(args) < 2:
|
|
|
|
parser.print_help()
|
|
|
|
|
|
|
|
if options.phase == 0:
|
|
|
|
build_phase0_spec(args[0], args[1])
|
|
|
|
print(args[0])
|
|
|
|
print(args[1])
|
|
|
|
elif options.phase == 1:
|
|
|
|
build_phase1_spec(args[0], args[1])
|
|
|
|
else:
|
|
|
|
print("Invalid phase: {0}".format(options["phase"]))
|
2019-04-07 07:02:20 +00:00
|
|
|
|