コード例 #1
0
ファイル: api.py プロジェクト: mcne65/fumblechain
def create_block():
    """Add and broadcast the given block.
    Returns HTTP 400 if the block is considered invalid."""
    try:
        # retrieve block from request body
        jso = request.get_data(as_text=True)
        b = Block.from_json(jso)

        # add block to local blockchain
        success = app.p2p.bc.discard_block(b)

        if success:
            # broadcast block to p2p network
            app.p2p.broadcast_block(b)

            logger.debug(f"block {b.index} added")
            return Response(b.to_json(), status=HTTP_CREATED)
        else:
            logger.debug("failed to add block (discard)")
            raise BadRequest()
    except BadRequest:
        raise
    except BaseException as e:
        logger.debug(e)
        raise BadRequest()
コード例 #2
0
ファイル: test.py プロジェクト: mcne65/fumblechain
    def test_post_transaction(self):
        self.prepare_app()

        # prepare a valid transaction
        w = Wallet()
        w.create_keys()
        tx = Transaction("0", w.get_address(), 1)
        w.sign_transaction(tx)

        client = app.test_client()

        response = client.post("/block/new", data=tx.to_json())
        self.assertEqual(response.status_code, 201)

        b = Block.from_json(response.get_data())
        mine(b)
        response = client.post("/block", data=b.to_json())
        self.assertEqual(response.status_code, 201)

        tx = Transaction(w.get_address(), 1, 0.5)

        # test without signature
        response = client.post("/transaction", data=tx.to_json())
        self.assertEqual(response.status_code, 400)

        # test with signature
        w.sign_transaction(tx)
        response = client.post("/transaction", data=tx.to_json())
        self.assertEqual(response.status_code, 201)
コード例 #3
0
ファイル: test.py プロジェクト: mcne65/fumblechain
    def test_block_serialize(self):
        a = Block(0, 1, datetime.datetime.utcnow().timestamp())
        t1 = Transaction(0, 1, 2)
        a.add_transaction(t1)
        t2 = Transaction(0, 1, 2)
        a.add_transaction(t2)

        b = Block.from_json(a.to_json())
        self.assertEqual(a.get_hash(), b.get_hash())
コード例 #4
0
    def do_block_raw(self, arg):
        """Send raw block from JSON"""
        block_json = input("enter block JSON : ")
        try:
            blk = Block.from_json(block_json)
        except ValueError:
            print("Invalid block JSON")
            return

        if self.api.push_block(blk):
            print("OK")
        else:
            print("KO")
コード例 #5
0
ファイル: scli.py プロジェクト: mcne65/fumblechain
def do_block_raw(args):
    """Send raw block from JSON input"""
    block_json = sys.stdin.read()
    try:
        blk = Block.from_json(block_json)
    except ValueError:
        print("Invalid block JSON")
        return

    if args.api.push_block(blk):
        print("OK")
    else:
        print("KO")
コード例 #6
0
ファイル: messages.py プロジェクト: mcne65/fumblechain
 def deserialize(self, data):
     nb = data[self.NB]
     objs = data[self.OBJECTS]
     if nb != len(objs):
         logger.error("bad number of objects")
         raise DeserializationError("bad number of objects")
     self.objects = []
     for ot in objs:
         otype = ot[0]
         obj = ot[1]
         if otype == MsgInv.TYPE_BLOCK:
             obj = Block.from_json(json.dumps(obj))
         elif otype == MsgInv.TYPE_TX:
             obj = Transaction.from_json(json.dumps(obj))
         self.objects.append((otype, obj))
コード例 #7
0
ファイル: test.py プロジェクト: mcne65/fumblechain
    def test_post_new_block(self):
        self.prepare_app()

        # prepare a valid transaction
        w = Wallet()
        w.create_keys()
        tx = Transaction(0, w.get_address(), 1)
        w.sign_transaction(tx)

        client = app.test_client()

        response = client.post("/block/new", data=tx.to_json())
        self.assertEqual(response.status_code, 201)

        b = Block.from_json(response.get_data())
        self.assertTrue(type(b) == Block)
コード例 #8
0
ファイル: test.py プロジェクト: mcne65/fumblechain
 def test_serialize(self):
     a = Block(0, 1, datetime.datetime.utcnow().timestamp())
     b = Block.from_json(a.to_json())
     self.assertEqual(a.get_hash(), b.get_hash())
コード例 #9
0
ファイル: messages.py プロジェクト: mcne65/fumblechain
 def deserialize(self, data):
     b = Block.from_json(json.dumps(data))
     self.block = b