def test_signer_is_set(self): bc = Blockchain(NEW_USER_PUB_KEY) bc.new_block() random_sign(bc.last_block()) assert bc.last_block().signer == REF_PUB_KEY
def test_return_all_dates(self): bc = UserBlockchain(NEW_USER_PUB_KEY) bc.new_block() bc.last_block().total = 4**3 bc._add_transaction(random_sign(bc.make_daily_guzis_tx())) bc._add_transaction(random_sign(bc.make_daily_guzis_tx())) bc._add_transaction(random_sign(bc.make_daily_guzis_tx())) # Act result = bc._get_available_guzis() expected = [ ("2011-12-13", 0), ("2011-12-13", 1), ("2011-12-13", 2), ("2011-12-13", 3), ("2011-12-13", 4), ("2011-12-14", 0), ("2011-12-14", 1), ("2011-12-14", 2), ("2011-12-14", 3), ("2011-12-14", 4), ("2011-12-15", 0), ("2011-12-15", 1), ("2011-12-15", 2), ("2011-12-15", 3), ("2011-12-15", 4), ] # Assert assert expected == result
def test_return_part_of_blockchain_at_second_contact(self): # Arrange blockchain = Blockchain(NEW_USER_PUB_KEY) blockchain.new_block() tx0 = Transaction(VERSION, Transaction.GUZI_CREATE, KEY_POOL[0]["pub"], 0) tx1 = Transaction(VERSION, Transaction.GUZI_CREATE, KEY_POOL[1]["pub"], 0) tx2 = Transaction(VERSION, Transaction.GUZI_CREATE, KEY_POOL[2]["pub"], 0) tx3 = Transaction(VERSION, Transaction.GUZI_CREATE, KEY_POOL[3]["pub"], 0) tx4 = Transaction(VERSION, Transaction.GUZI_CREATE, KEY_POOL[4]["pub"], 0) blockchain[0].add_transactions([tx0, tx1, tx2]) random_sign(blockchain.last_block()) blockchain.new_block() blockchain[0].add_transactions([tx3, tx4]) # Act result = blockchain._reduce(KEY_POOL[3]["pub"]) # Assert assert len(result) == 1
def test_signed_block_return_true(self): # Arrange block = Block() random_sign(block, REF_PUB_KEY, REF_PRIV_KEY) # Assert assert block.is_signed() is True
def test_birth_only_ok(self): bc = UserBlockchain(NEW_USER_PUB_KEY) birth_tx = bc.make_birth_tx() random_sign(birth_tx, REF_PUB_KEY, REF_PRIV_KEY) bc._add_transaction(birth_tx) result = bc.is_valid() assert result is True
def pay_to(self, target_user, amount): """Simulates a payment to another user.""" try: note("paying amount {}".format(amount)) tx = self.bc.make_pay_tx(target_user, amount) random_sign(tx, self.key_pair["pub"], self.key_pair["priv"]) self.bc._add_transaction(tx) self.guzis -= amount except (InsufficientFundsError, NegativeAmountError): pass
def test_raise_exception_if_block_is_already_signed(self): # Arrange block = Block() tx = Transaction(VERSION, Transaction.GUZI_CREATE, EMPTY_HASH, 0) random_sign(block, REF_PUB_KEY, REF_PRIV_KEY) # Act with pytest.raises(FullBlockError): block.add_transaction(tx)
def test_birth_false_signature_ko(self): bc = UserBlockchain(NEW_USER_PUB_KEY) birth_tx = bc.make_birth_tx() # Fails because REF_PRIV_KEY should be NEW_USER_PRIV_KEY random_sign(birth_tx, NEW_USER_PUB_KEY, REF_PRIV_KEY) bc._add_transaction(birth_tx) result = bc.is_valid() assert result is False
def test_basic_ok(self): # Arrange blockchain = Blockchain(NEW_USER_PUB_KEY) blockchain.new_block() # Act random_sign(blockchain.last_block()) # Assert assert blockchain[0].is_signed() is True
def test_set_previous_block_hash(self): # Arrange blockchain = Blockchain(NEW_USER_PUB_KEY) blockchain.new_block() random_sign(blockchain.last_block()) # Act blockchain.new_block() # Assert assert blockchain[1].previous_block_signature == blockchain[ 0].signature
def make_daily_guzis(self): """Simulates the creation of daily Guzis for the blockchain. Note that if it was already made today, it shouldn't create those again. """ try: tx = self.bc.make_daily_guzis_tx(self.current_date) random_sign(tx, self.key_pair["pub"], self.key_pair["priv"]) self.bc._add_transaction(tx) if not self.guzis_made_today: self.guzis += self.bc._get_guzis_amount() self.guzis_made_today = True except GuziError: pass
def init_blockchain(self, birthdate, my_key_pair, ref_key_pair): """Created a validated Blockchain and initialize some control attributes """ self.bc = UserBlockchain(my_key_pair["pub"]) birth_tx = self.bc.make_birth_tx(birthdate) random_sign(birth_tx, my_key_pair["pub"], my_key_pair["priv"]) self.bc._add_transaction(birth_tx) tx_guzis = self.bc.make_daily_guzis_tx(birthdate) random_sign(tx_guzis, my_key_pair["pub"], my_key_pair["priv"]) self.bc._add_transaction(tx_guzis) tx_guziboxes = self.bc.make_daily_guziboxes_tx(birthdate) random_sign(tx_guziboxes, my_key_pair["pub"], my_key_pair["priv"]) self.bc._add_transaction(tx_guziboxes) init_block = self.bc.fill_init_block(birthdate) random_sign(init_block, ref_key_pair["pub"], ref_key_pair["priv"]) self.bc.new_block() self.guzis = 1 self.current_date = birthdate self.guzis_made_today = True self.key_pair = my_key_pair
def test_create_base_block(self): bc = UserBlockchain(NEW_USER_PUB_KEY) birth_tx = bc.make_birth_tx(date(1998, 12, 21).isoformat()) random_sign(birth_tx, NEW_USER_PUB_KEY, NEW_USER_PRIV_KEY) bc._add_transaction(birth_tx) init_block = bc.last_block() assert init_block.version == VERSION assert init_block.close_date is None assert init_block.previous_block_signature is None assert init_block.merkle_root is None assert init_block.signer is None assert len(init_block.transactions) == 1 assert len(init_block.engagements) == 0 assert init_block.is_signed() is False assert init_block.is_valid() is True
def test_signature_is_set(self): tx = random_transaction() random_sign(tx) assert tx.signature is not None