def test_init(self):
        with self.subTest("An empty new chain creates a genesis block"):
            self.assertEqual(Block.objects.count(), 0)
            self.assertEqual(BlockChain().head, Block.objects.first())
            self.assertEqual(Block.objects.count(), 1)

        with self.subTest("An existing chain doesn't create more blocks"):
            BlockChain()
            self.assertEqual(Block.objects.count(), 1)
            BlockChain()
            self.assertEqual(Block.objects.count(), 1)
Example #2
0
 def post(self, request):
     generator = serializers.deserialize('json', request.body)
     chain = [block for block in generator]
     chain_queryset = BlockChain().fork_choice(chain)
     if self.unseen_chain(chain_queryset):
         Gossip.all_about_it(chain)
         return HttpResponse("Accepted new blocks.", status=200)
     else:
         return HttpResponse("Rejected blocks. Chain has been seen.",
                             status=202)
Example #3
0
    def test_blockchain_view(self):
        with self.subTest("No blocks exist"):
            response = self.client.get('/blockchain')
            self.assertEqual(response.status_code, 200)
            self.assertTemplateUsed(response, 'blockchain.html')
            self.assertContains(response, "This must be a crap coin...")

        with self.subTest("With many blocks, can view as a list"):
            chain = BlockChain()
            response = self.client.get('/blockchain')
            self.assertEqual(response.status_code, 200)
            self.assertTemplateUsed(response, 'blockchain.html')
            self.assertContains(response, chain.head.hash_id)
Example #4
0
 def post(self, request):
     """ Used for internal transactions where this node creates a
     transaction. """
     form = BlockCreateForm(request.POST)
     if form.is_valid():
         block = Block(
             payee=form.data['payee'],
             payer=form.data['payer'],
             amount=form.data['amount'],
         )
     try:
         chain = BlockChain().add_block(block)
         Gossip.all_about_it(chain)
         return redirect('blockchain')
     except ValueError:
         return HttpResponse("Invalid form--did you include all fields?")
    def test_is_valid_block(self):
        with self.subTest("True with valid pow solution"):
            chain = BlockChain()
            transaction = Faker().sha256()
            new_block = chain.mint(transaction)
            chain.is_valid_block(new_block)

        with self.subTest("False with invalid pow solution"):
            chain = BlockChain()
            self.assertEqual(chain.is_valid_pow("fake_hash"))

        with self.subTest("True if transaction output <= input"):
            raise

        with self.subTest("False if transaction output > input"):
            raise

        with self.subTest("False if doublespend"):
            raise
Example #6
0
 def __init__(self):
     chain = BlockChain()
     for _ in range(0, 10):
         content = Faker().sha256()
         chain.mint(content)
     return chain
Example #7
0
 def unseen_chain(self, chain):
     current_head = BlockChain().last().hash_id
     new_chain_head = chain.order_by('-number').first().hash_id
     return current_head != new_chain_head
 def test_mint(self):
     with self.subTest("Can mine new blocks"):
         transaction = Faker().sha256()
         BlockChain().mint(transaction)
         self.assertEqual(Block.objects.count(), 2)