Beispiel #1
0
 def from_bytes(cls, bytes_frontier: bytes):
     front_dict = decode_raw(bytes_frontier)
     return cls(
         front_dict.get(b"t"),
         front_dict.get(b"h"),
         front_dict.get(b"i"),
     )
Beispiel #2
0
    def process_spend(self, spend_block: BamiBlock) -> None:
        # Store spend in the database
        spend_tx = decode_raw(spend_block.transaction)
        spender = spend_block.public_key
        self.verify_spend(spender, spend_tx)

        chain_id = spend_block.com_id
        spend_dot = spend_block.com_dot
        pers_links = spend_block.links

        prev_spend_links = spend_tx.get(b"prev_pairwise_link")
        value = Decimal(spend_tx.get(b"value"), self.context)
        to_peer = spend_tx.get(b"to_peer")
        seq_num = spend_dot[0]

        self.state_db.apply_spend(
            chain_id,
            prev_spend_links,
            pers_links,
            spend_dot,
            spender,
            to_peer,
            value,
            self.should_store_store_update(chain_id, seq_num),
        )

        # Is this block related to my peer?
        if to_peer == self.my_pub_key_bin:
            self.add_block_to_response_processing(spend_block)
Beispiel #3
0
def test_decimal():
    new_con = getcontext()
    new_con.prec = 4
    t = Decimal(2.191, new_con)
    t2 = Decimal(2.11, new_con)

    l = encode_raw({b"value": float(t2)})
    p = decode_raw(l)
    assert p.get(b"value") == float(t2)
Beispiel #4
0
    def test_val(self):
        val = Decimal(10, self.con)
        l = encode_raw({b"123": float(val)})
        d = decode_raw(l)
        print(d)

        l = cachetools.LRUCache(10)
        l[1] = "v"
        int_max = 9999
        k = 4
        try:
            v = min(l.keys(), key=lambda x: abs(x - k) if x >= k else int_max)
            if v < k:
                # No data yet:
                pass
        except ValueError:
            print("No data for the chain")
Beispiel #5
0
    def process_mint(self, mint_blk: BamiBlock) -> None:
        """Process received mint transaction"""
        minter = mint_blk.public_key
        mint_tx = decode_raw(mint_blk.transaction)
        chain_id = mint_blk.com_id
        mint_dot = mint_blk.com_dot
        prev_links = mint_blk.links
        self.verify_mint(chain_id, minter, mint_tx)

        seq_num = mint_dot[0]
        self.state_db.apply_mint(
            chain_id,
            mint_dot,
            prev_links,
            minter,
            Decimal(mint_tx.get(b"value"), self.context),
            self.should_store_store_update(chain_id, seq_num),
        )
Beispiel #6
0
 def process_counter_signing_block(
     self, block: BamiBlock, time_passed: float = None, num_block_passed: int = None,
 ) -> bool:
     """
     Process block that should be counter-signed and return True if the block should be delayed more.
     Args:
         block: Processed block
         time_passed: time passed since first added
         num_block_passed: number of blocks passed since first added
     Returns:
         Should add to queue again.
     """
     res = self.block_response(block, time_passed, num_block_passed)
     if res == BlockResponse.CONFIRM:
         self.confirm(
             block,
             extra_data={b"value": decode_raw(block.transaction).get(b"value")},
         )
         return False
     elif res == BlockResponse.REJECT:
         self.reject(block)
         return False
     return True
Beispiel #7
0
def test_encode_decode_bytelist():
    vals = {b"1", b"2", b"3", b"4", b"100", b"10", b"21", b"5"}
    assert set(decode_raw(encode_raw(list(vals)))) == vals
Beispiel #8
0
def test_encode_decode_raw():
    vals = {b"id": 42}
    assert decode_raw(encode_raw(vals))[b"id"] == 42
Beispiel #9
0
 def from_bytes(cls, bytes_frontier: bytes):
     val_dict = decode_raw(bytes_frontier)
     return cls(val_dict.get(b"m"), val_dict.get(b"c"))
Beispiel #10
0
 def unpack_audit_blob(self, audit_blob: bytes) -> Any:
     """
     Returns:
         decoded audit transaction
     """
     return decode_raw(audit_blob)
Beispiel #11
0
 def process_reject(self, block: BamiBlock) -> None:
     reject_tx = decode_raw(block.transaction)
     self.verify_reject_tx(block.public_key, reject_tx)
     self.apply_reject_tx(block, reject_tx)
Beispiel #12
0
 def process_confirm(self, block: BamiBlock) -> None:
     confirm_tx = decode_raw(block.transaction)
     self.verify_confirm_tx(block.public_key, confirm_tx)
     self.apply_confirm_tx(block, confirm_tx)
Beispiel #13
0
 def unpack_witness_blob(self, witness_blob: bytes) -> Any:
     """
     Returns:
         decoded witness transaction
     """
     return decode_raw(witness_blob)
Beispiel #14
0
 def received_peer_subs(self, peer: Peer, payload: SubscriptionsPayload) -> None:
     subcoms = decode_raw(payload.subcoms)
     self.process_peer_subscriptions(peer, subcoms)
Beispiel #15
0
 def introduction_request_callback(self, peer, dist, payload):
     subcoms = decode_raw(payload.extra_bytes)
     self.process_peer_subscriptions(peer, subcoms)
     # TODO: add subscription strategy
     if self.settings.track_neighbours_chains:
         self.subscribe_to_subcom(peer.public_key.key_to_bin())
Beispiel #16
0
 def received_block_in_order(self, block: BamiBlock) -> None:
     print(block.transaction)
     decoded_tx = decode_raw(block.transaction)
     if decoded_tx.get(b"to_peer",
                       None) == self.my_peer.public_key.key_to_bin():
         self.add_block_to_response_processing(block)
Beispiel #17
0
 def confirm_tx_extra_data(self, block: BamiBlock) -> Dict:
     return {b"value": decode_raw(block.transaction).get(b"value")}