def as_create_spend_bundle(self, as_puzzlehash, as_amount, as_timelock_block, as_secret_hash, as_pubkey_sender=None, as_pubkey_receiver=None, who=None, as_sec_to_try=None): utxos = self.as_select_coins(as_amount, as_puzzlehash) spends = [] for coin in utxos: puzzle = self.as_make_puzzle(as_pubkey_sender, as_pubkey_receiver, as_amount, as_timelock_block, as_secret_hash) if who == "sender": solution = self.as_make_solution_sender() elif who == "receiver": solution = self.as_make_solution_receiver(as_sec_to_try) pair = solution.to([puzzle, solution]) signer = self.make_signer() spend_bundle = build_spend_bundle(coin, Program(pair), sign_f=signer) spends.append(spend_bundle) return SpendBundle.aggregate(spends)
async def new_chain_view(self, chain_view, spend_bundles, coinbase_puzzle_hash, fees_puzzle_hash, storage, unspent_db): block_number = chain_view.tip_index + 1 REWARD = int(1e9) timestamp = uint64(self._now / 1000) pool_public_key = get_pool_public_key() plot_public_key = get_plot_public_key() pos = ProofOfSpace(pool_public_key, plot_public_key) coinbase_coin, coinbase_signature = create_coinbase_coin_and_signature( block_number, coinbase_puzzle_hash, REWARD, pool_public_key) spend_bundle = SpendBundle.aggregate(spend_bundles) header, body = farm_new_block(chain_view.tip_hash, chain_view.tip_signature, block_number, pos, spend_bundle, coinbase_coin, coinbase_signature, fees_puzzle_hash, timestamp) header_signature = sign_header(header, plot_public_key) [await storage.add_preimage(bytes(_)) for _ in (header, body)] chain_view = await chain_view.augment_chain_view( header, header_signature, storage, unspent_db, REWARD, self._now) return chain_view
def test_farm_block_one_spendbundle(): REWARD = 10000 unspent_db = RAM_DB() chain_view = ChainView.for_genesis_hash(GENESIS_BLOCK, unspent_db) pos = ProofOfSpace(get_pool_public_key(), get_plot_public_key()) puzzle_hash = puzzle_hash_for_index(1) empty_spend_bundle = SpendBundle.aggregate([]) header, header_signature, body = farm_block(GENESIS_BLOCK, Signature.zero(), 1, pos, empty_spend_bundle, puzzle_hash, REWARD) coinbase_coin = body.coinbase_coin conditions = standard_conditions() spend_bundle = spend_coin(coin=coinbase_coin, conditions=conditions, index=1) header, header_signature, body = farm_block(GENESIS_BLOCK, Signature.zero(), 1, pos, spend_bundle, puzzle_hash, REWARD) removals = removals_for_body(body) assert len(removals) == 1 assert removals[0] == list(spend_bundle.coin_solutions)[0].coin.name() run = asyncio.get_event_loop().run_until_complete additions, removals = run( chain_view.accept_new_block(header, unspent_db, REWARD, 0)) assert len(additions) == 4 assert len(removals) == 1
def test_farm_two_blocks(): """ In this test, we farm two blocks: one empty block, then one block which spends the coinbase transaction from the empty block. """ REWARD = 10000 unspent_db = RAM_DB() chain_view = ChainView.for_genesis_hash(GENESIS_BLOCK, unspent_db) assert chain_view.genesis_hash == GENESIS_BLOCK assert chain_view.tip_hash == HeaderHash(GENESIS_BLOCK) assert chain_view.tip_index == 0 assert chain_view.unspent_db == unspent_db pos_1 = ProofOfSpace(get_pool_public_key(), get_plot_public_key()) puzzle_hash = puzzle_hash_for_index(1) empty_spend_bundle = SpendBundle.aggregate([]) header, header_signature, body = farm_block(GENESIS_BLOCK, Signature.zero(), 1, pos_1, empty_spend_bundle, puzzle_hash, REWARD) run = asyncio.get_event_loop().run_until_complete additions, removals = run( chain_view.accept_new_block(header, unspent_db, REWARD, 0)) assert len(additions) == 2 assert len(removals) == 0 # TODO: check additions assert additions[1].puzzle_hash == body.fees_coin.puzzle_hash assert additions[1].amount == 0 chain_view = run( chain_view.augment_chain_view(header, header_signature, unspent_db, unspent_db, REWARD, 0)) assert chain_view.genesis_hash == GENESIS_BLOCK assert chain_view.tip_hash == HeaderHash(header) assert chain_view.tip_index == 1 assert chain_view.unspent_db == unspent_db conditions = standard_conditions() spend_bundle_2 = spend_coin(coin=additions[0], conditions=conditions, index=1) assert validate_spend_bundle_signature(spend_bundle_2) pos_2 = ProofOfSpace(get_pool_public_key(1), get_plot_public_key()) header_2, header_signature_2, body_2 = farm_block(header, header_signature, 2, pos_2, spend_bundle_2, puzzle_hash, REWARD) print(header_2) print(header_signature_2) removals = removals_for_body(body_2) assert len(removals) == 1 assert removals[0] == list(spend_bundle_2.coin_solutions)[0].coin.name()
def test_farm_block_empty(): REWARD = 10000 unspent_db = RAM_DB() chain_view = ChainView.for_genesis_hash(GENESIS_BLOCK, unspent_db) pos = ProofOfSpace(get_pool_public_key(), get_plot_public_key()) puzzle_hash = puzzle_hash_for_index(1) spend_bundle = SpendBundle.aggregate([]) header, header_signature, body = farm_block(GENESIS_BLOCK, Signature.zero(), 1, pos, spend_bundle, puzzle_hash, REWARD) removals = removals_for_body(body) assert len(removals) == 0 run = asyncio.get_event_loop().run_until_complete additions, removals = run( chain_view.accept_new_block(header, unspent_db, REWARD, 0)) assert len(additions) == 2 assert len(removals) == 0
def collect_best_bundle(known_bundles) -> SpendBundle: # this is way too simple spend_bundle = SpendBundle.aggregate(known_bundles) assert spend_bundle.fees() >= 0 return spend_bundle