def make_casper_genesis(alloc, epoch_length, withdrawal_delay, base_interest_factor, base_penalty_factor): # The Casper-specific config declaration casper_config = copy.deepcopy(config.default_config) casper_config['HOMESTEAD_FORK_BLKNUM'] = 0 casper_config['ANTI_DOS_FORK_BLKNUM'] = 0 casper_config['CLEARING_FORK_BLKNUM'] = 0 casper_config['CONSENSUS_STRATEGY'] = 'hybrid_casper' casper_config['NULL_SENDER'] = utils.sha3('NULL_SENDER') casper_config['EPOCH_LENGTH'] = epoch_length casper_config['WITHDRAWAL_DELAY'] = withdrawal_delay casper_config['OWNER'] = a0 casper_config['BASE_INTEREST_FACTOR'] = base_interest_factor casper_config['BASE_PENALTY_FACTOR'] = base_penalty_factor # Get initialization txs init_txs, casper_address = mk_initializers(casper_config, casper_config['NULL_SENDER']) casper_config['CASPER_ADDRESS'] = casper_address # Create state and apply required state_transitions for initializing Casper state = genesis_helpers.mk_basic_state( alloc, None, env=config.Env(config=casper_config)) state.gas_limit = 10**8 for tx in init_txs: state.set_balance(utils.privtoaddr(casper_config['NULL_SENDER']), 15**18) success, output = apply_transaction(state, tx) assert success state.gas_used = 0 state.set_balance(utils.privtoaddr(casper_config['NULL_SENDER']), 0) consensus.initialize(state) state.commit() return state
def induct_validator(chain, casper, key, value): valcode_addr = chain.tx(key, "", 0, mk_validation_code(utils.privtoaddr(key))) assert utils.big_endian_to_int( chain.tx(key, purity_checker_address, 0, purity_translator.encode('submit', [valcode_addr]))) == 1 casper.deposit(valcode_addr, utils.privtoaddr(key), value=value)
def initialize(state, block=None): config = state.config state.txindex = 0 state.gas_used = 0 state.bloom = 0 state.receipts = [] if block is not None: update_block_env_variables(state, block) # Initalize the next epoch in the Casper contract if state.block_number % state.config['EPOCH_LENGTH'] == 0 and state.block_number != 0: key, account = state.config['NULL_SENDER'], privtoaddr(state.config['NULL_SENDER']) data = casper_utils.casper_translator.encode('initialize_epoch', [state.block_number // state.config['EPOCH_LENGTH']]) transaction = transactions.Transaction(state.get_nonce(account), 0, 3141592, state.config['CASPER_ADDRESS'], 0, data).sign(key) success, output = apply_transaction(state, transaction) assert success if state.is_DAO(at_fork_height=True): for acct in state.config['CHILD_DAO_LIST']: state.transfer_value( acct, state.config['DAO_WITHDRAWER'], state.get_balance(acct)) if state.is_METROPOLIS(at_fork_height=True): state.set_code(utils.normalize_address( config["METROPOLIS_STATEROOT_STORE"]), config["METROPOLIS_GETTER_CODE"]) state.set_code(utils.normalize_address( config["METROPOLIS_BLOCKHASH_STORE"]), config["METROPOLIS_GETTER_CODE"])
def direct_tx(self, transaction): self.last_tx = transaction if self.last_sender is not None and privtoaddr( self.last_sender) != transaction.sender: self.last_sender = None success, output = apply_transaction(self.head_state, transaction) self.block.transactions.append(transaction) if not success: raise TransactionFailed() return output
def tx(self, sender=k0, to=b'\x00' * 20, value=0, data=b'', startgas=STARTGAS, gasprice=GASPRICE): sender_addr = privtoaddr(sender) self.last_sender = sender transaction = Transaction(self.head_state.get_nonce(sender_addr), gasprice, startgas, to, value, data).sign(sender) output = self.direct_tx(transaction) return output
def tx(self, sender=k0, to=b'\x00' * 20, value=0, data=b'', startgas=STARTGAS, gasprice=GASPRICE): sender_addr = privtoaddr(sender) transaction = Transaction(self.state.get_nonce(sender_addr), gasprice, startgas, to, value, data).sign(sender) success, output = apply_transaction(self.state, transaction) if not success: raise TransactionFailed() return output
def test_ecrecover(): c = tester.Chain() x = c.contract(ecrecover_code, language='serpent') priv = utils.sha3('some big long brainwallet password') pub = bitcoin.privtopub(priv) msghash = utils.sha3('the quick brown fox jumps over the lazy dog') V, R, S = utils.ecsign(msghash, priv) assert bitcoin.ecdsa_raw_verify(msghash, (V, R, S), pub) addr = utils.big_endian_to_int( utils.sha3(bitcoin.encode_pubkey(pub, 'bin')[1:])[12:]) assert utils.big_endian_to_int(utils.privtoaddr(priv)) == addr result = x.test_ecrecover(utils.big_endian_to_int(msghash), V, R, S) assert result == addr
def call(self, sender=k0, to=b'\x00' * 20, value=0, data=b'', startgas=STARTGAS, gasprice=GASPRICE): self.state.commit() sender_addr = privtoaddr(sender) result = apply_message(self.state.ephemeral_clone(), sender=sender_addr, to=to, code_address=to, value=value, data=data, gas=startgas) if result is None: raise TransactionFailed() return result
def __init__(self, key, genesis, network, valcode_addr=None, mining=False): self.key = key self.coinbase = utils.privtoaddr(self.key) self.chain = chain.Chain(genesis=genesis, reset_genesis=True, coinbase=self.coinbase, new_head_cb=self._on_new_head) self.mining = mining self.nonce = self.chain.state.get_nonce(self.coinbase) self.valcode_tx = None self.deposit_tx = None self.valcode_addr = valcode_addr self.prepares = dict() self.prev_prepare_epoch = 0 self.prev_commit_epoch = 0 self.epoch_length = self.chain.env.config['EPOCH_LENGTH'] # When the transaction_queue is modified, we must set # self._head_candidate_needs_updating to True in order to force the # head candidate to be updated. self.transaction_queue = TransactionQueue() self._head_candidate_needs_updating = True # Add validator to the network self.network = network self.network.join(self)
def sign(self, key, network_id=None): """Sign this transaction with a private key. A potentially already existing signature would be overridden. """ if network_id is None: rawhash = utils.sha3(rlp.encode(self, UnsignedTransaction)) else: assert 1 <= network_id < 2**63 - 18 rlpdata = rlp.encode( rlp.infer_sedes(self).serialize(self)[:-3] + [network_id, b'', b'']) rawhash = utils.sha3(rlpdata) key = normalize_key(key) self.v, self.r, self.s = ecsign(rawhash, key) if network_id is not None: self.v += 8 + network_id * 2 self._sender = utils.privtoaddr(key) return self
import serpent from biblecoin.config import default_config, Env import copy import time import rlp # config_string = ':info,bible.vm.log:trace,bible.vm.op:trace,bible.vm.stack:trace,bible.vm.exit:trace,bible.pb.msg:trace,bible.pb.tx:debug' config_string = ':info,bible.vm.log:trace' configure_logging(config_string=config_string) NUM_PARTICIPANTS = 10 BLOCK_MAKING_PPB = 10 print('Initializing privkeys, addresses and randaos for validators') privkeys = [utils.sha3(str(i)) for i in range(NUM_PARTICIPANTS)] addrs = [utils.privtoaddr(k) for k in privkeys] randaos = [RandaoManager(utils.sha3(str(i))) for i in range(NUM_PARTICIPANTS)] deposit_sizes = [i * 500 + 500 for i in range(NUM_PARTICIPANTS)] vcodes = [generate_validation_code(a) for a in addrs] vchashes = [utils.sha3(c) for c in vcodes] assert len(privkeys) == len(addrs) == len(randaos) == len( deposit_sizes) == len(vcodes) == len(vchashes) == NUM_PARTICIPANTS # Creating casper contract translator ct = get_casper_ct() assert ct print('Constructing genesis') s = make_casper_genesis(validators=[ (generate_validation_code(a), ds * 10**18, r.get(9999), a) for a, ds, r in zip(addrs, deposit_sizes, randaos) ][:-1],
def join(self, number): withdrawal_addr = privtoaddr(tester.keys[number]) casper_utils.induct_validator(self.t, self.casper, tester.keys[number], 200 * 10**18) self.validators[number] = Validator(withdrawal_addr, tester.keys[number])
from biblecoin.transactions import Transaction from biblecoin.consensus_strategy import get_consensus_strategy from biblecoin.config import config_homestead, config_tangerine, config_spurious, config_metropolis, default_config, Env from biblecoin.pow.ethpow import Miner from biblecoin.messages import apply_transaction, apply_message from biblecoin.common import verify_execution_results, mk_block_from_prevstate, set_execution_results from biblecoin.meta import make_head_candidate from biblecoin.abi import ContractTranslator import rlp # Initialize accounts accounts = [] keys = [] for account_number in range(10): keys.append(sha3(to_string(account_number))) accounts.append(privtoaddr(keys[-1])) k0, k1, k2, k3, k4, k5, k6, k7, k8, k9 = keys[:10] a0, a1, a2, a3, a4, a5, a6, a7, a8, a9 = accounts[:10] base_alloc = {} minimal_alloc = {} for a in accounts: base_alloc[a] = {'balance': 10**24} for i in range(1, 9): base_alloc[int_to_addr(i)] = {'balance': 1} minimal_alloc[int_to_addr(i)] = {'balance': 1} minimal_alloc[accounts[0]] = {'balance': 10**18} # Initialize languages languages = {}
def accounts(): k = utils.sha3(b'cow') v = utils.privtoaddr(k) k2 = utils.sha3(b'horse') v2 = utils.privtoaddr(k2) return k, v, k2, v2