Esempio n. 1
0
def interpret_test(test):
    c = chain.Chain()
    b = mk_block_from_prevstate(c, timestamp=c.state.timestamp + 1)
    for tx in casper_init_txs:
        b.transactions.append(tx)
    
    c.add_block(b)
    head = c.head_hash
    validators = [Validator(privkey=sha3(str(i))) for i in range(20)]
    markers = {}
    txs = []
    
    for token in test.split(''):
        letters, numbers = re.match('([A-Za-z]*)([0-9]*)', token).groups()
        if letters+numbers != token:
            raise Exception("Bad token: %s" % token)
        numbers = int(numbers)
        # Adds a block to the current head
        if letters == 'B':
            if head == c.head:
                b = mk_block_from_prevstate(c, timestamp=c.state.timestamp + 1)
            else:
                s = c.mk_poststate_of_blockhash(head)
                b = mk_block_from_prevstate(c, state=s, timestamp=s.timestamp + 1)
            b.txs = txs
            c.add_block(b)
            txs = []
        # Saves a marker (eg "S5" creates a marker called 5 and sets the current head to that)
        elif letters == 'S':
            self.markers[numbers] = head
        # Changes the head to a marker (eg "C5" changes the head to the marker 5)
        elif letters == 'C':
            assert len(txs) == 0
            head = self.markers[numbers]
        # Adds a join transaction to the tx list (eg. "J5" adds a join for validator 5)
        elif letters == 'J':
            txs.append(validators[numbers].mk_join_transaction())
        # Adds a prepare to the tx list (eg. "P5" adds a prepare for validator 5)
        elif letters == 'P':
            txs.append(validators[numbers].mk_prepare())
        # Adds a commit to the tx list (eg. "C5" adds a commit for validator 5)
        elif letters == 'C':
            txs.append(validators[numbers].mk_commit())

    on_longest_chain = head == c.head_hash
    balances = {}
    active = {}
    for i in range(100):
        validator_addr = call_casper(c.head_state, 'get_validator__addr', i)
        if validator_addr in validator_lookup_map:
            balances[validator_lookup_map[validator_addr]] = call_casper(c.head_state, 'get_validator__deposit', i)
            active[validator_lookup_map[validator_addr]] = call_casper(c.head_state, 'is_validator_active', i)
    epoch = call_casper(c.head_state, 'get_current_epoch')
    total_deposits = call_casper(c.head_state, 'get_total_deposits', epoch)
    return {
        'on_longest_chain': on_longest_chain,
        'balances': balances,
        'active': active,
        'total_deposits': total_deposits
    }
Esempio n. 2
0
def verify_collation_header(chain, header):
    """Verify the collation

    Validate the collation header before calling ShardChain.add_collation

    chain: MainChain
    header: the given collation header
    """
    if header.shard_id < 0:
        raise ValueError('Invalid shard_id %d' % header.shard_id)

    # Call contract to verify header
    state = chain.state.ephemeral_clone()
    block = mk_block_from_prevstate(chain,
                                    timestamp=chain.state.timestamp + 14)
    cs = get_consensus_strategy(state.config)
    cs.initialize(state, block)

    try:
        result = call_valmgr(state,
                             'add_header', [rlp.encode(header)],
                             sender_addr=header.coinbase)
        print('result:{}'.format(result))
        if not result:
            raise ValueError('Calling add_header returns False')
    except:
        raise ValueError('Calling add_header failed')
    return True
Esempio n. 3
0
def make_head_candidate(chain,
                        txqueue=None,
                        parent=None,
                        timestamp=None,
                        coinbase='\x35' * 20,
                        extra_data='moo ha ha says the laughing cow.',
                        min_gasprice=0):
    log.info('Creating head candidate')
    if parent is None:
        temp_state = State.from_snapshot(
            chain.state.to_snapshot(root_only=True), chain.env)
    else:
        temp_state = chain.mk_poststate_of_blockhash(parent.hash)

    cs = get_consensus_strategy(chain.env.config)
    # Initialize a block with the given parent and variables
    blk = mk_block_from_prevstate(chain, temp_state, timestamp, coinbase,
                                  extra_data)
    # Find and set the uncles
    blk.uncles = cs.get_uncles(chain, temp_state)
    blk.header.uncles_hash = sha3(rlp.encode(blk.uncles))
    # Call the initialize state transition function
    cs.initialize(temp_state, blk)
    # Add transactions
    add_transactions(temp_state, blk, txqueue, min_gasprice)
    # Call the finalize state transition function
    cs.finalize(temp_state, blk)
    # Set state root, receipt root, etc
    set_execution_results(temp_state, blk)
    log.info('Created head candidate successfully')
    return blk, temp_state
Esempio n. 4
0
    def __init__(self,
                 alloc=None,
                 env=None,
                 deploy_sharding_contracts=False,
                 genesis=None):
        # MainChain
        if genesis is None:
            genesis = mk_basic_state(base_alloc if alloc is None else alloc,
                                     None, get_env(env))
        self.chain = MainChain(genesis=genesis, reset_genesis=True)
        self.cs = get_consensus_strategy(self.chain.env.config)
        self.block = mk_block_from_prevstate(
            self.chain, timestamp=self.chain.state.timestamp + 1)
        self.head_state = self.chain.state.ephemeral_clone()
        self.cs.initialize(self.head_state, self.block)
        self.last_sender = None
        self.last_tx = None

        # ShardChains
        self.collation = {}
        self.shard_head_state = {}
        self.shard_last_sender = {}
        self.shard_last_tx = {}
        self.add_header_logs = []

        # validator manager contract and other pre-compiled contracts
        self.is_sharding_contracts_deployed = False
        if deploy_sharding_contracts:
            self.is_sharding_contracts_deployed = True
            self.deploy_initializing_contracts(k0)
            self.last_sender = k0
            self.mine(1)
Esempio n. 5
0
 def change_head(self, parent, coinbase=a0):
     self.head_state = self.chain.mk_poststate_of_blockhash(
         parent).ephemeral_clone()
     self.block = mk_block_from_prevstate(
         self.chain,
         self.head_state,
         timestamp=self.chain.state.timestamp,
         coinbase=coinbase)
     self.cs.initialize(self.head_state, self.block)
Esempio n. 6
0
 def __init__(self, alloc=None, env=None):
     self.chain = chain.Chain(genesis=mk_basic_state(
         base_alloc if alloc is None else alloc, None, get_env(env)),
                              reset_genesis=True)
     self.cs = get_consensus_strategy(self.chain.env.config)
     self.block = mk_block_from_prevstate(
         self.chain, timestamp=self.chain.state.timestamp + 1)
     self.head_state = self.chain.state.ephemeral_clone()
     self.cs.initialize(self.head_state, self.block)
     self.last_sender = None
     self.last_tx = None
Esempio n. 7
0
 def mine(self, number_of_blocks=1, coinbase=a0):
     self.cs.finalize(self.head_state, self.block)
     set_execution_results(self.head_state, self.block)
     self.block = Miner(self.block).mine(rounds=100, start_nonce=0)
     assert self.chain.add_block(self.block)
     assert self.head_state.trie.root_hash == self.chain.state.trie.root_hash
     for i in range(1, number_of_blocks):
         b, _ = make_head_candidate(self.chain,
                                    timestamp=self.chain.state.timestamp +
                                    14)
         b = Miner(b).mine(rounds=100, start_nonce=0)
         assert self.chain.add_block(b)
     self.block = mk_block_from_prevstate(
         self.chain, timestamp=self.chain.state.timestamp + 14)
     self.head_state = self.chain.state.ephemeral_clone()
     self.cs.initialize(self.head_state, self.block)
Esempio n. 8
0
 def __init__(self, alloc=base_alloc, env=None, genesis=None):
     from ethereum.pow import chain as pow_chain
     if genesis:
         if type(genesis)!=dict and genesis.env.config['CONSENSUS_STRATEGY'] == 'hybrid_casper':
             from ethereum.hybrid_casper import chain as hybrid_casper_chain
             self.chain = hybrid_casper_chain.Chain(genesis, reset_genesis=True)
         else:
             self.chain = pow_chain.Chain(genesis, env=env, reset_genesis=True)
     else:
         self.chain = pow_chain.Chain(mk_basic_state(alloc, None, get_env(env)), reset_genesis=True)
     self.cs = get_consensus_strategy(self.chain.env.config)
     self.block = mk_block_from_prevstate(
         self.chain, timestamp=self.chain.state.timestamp + 1)
     self.head_state = self.chain.state.ephemeral_clone()
     self.cs.initialize(self.head_state, self.block)
     self.last_sender = None
     self.last_tx = None