Exemple #1
0
 def test_init(self):
     pn = MemoryPatriciaAuthTree()
     pl = pn.link_class(Bits(b'0b0'), pn)
     self.assertEqual(pl.prefix, Bits(bytes=b'\x00', length=1))
     self.assertEqual(pl.node, pn)
     self.assertFalse(pl.pruned)
     pl2 = pn.link_class(Bits(bytes=b'\x80', length=1), MemoryPatriciaAuthTree())
     self.assertEqual(pl2.prefix.uint, 0x01)
     self.assertEqual(pl2.node, pn)
     self.assertFalse(pl2.pruned)
     pl3 = pn.link_class(Bits(bytes=b'\x80'), MemoryPatriciaAuthTree())
     self.assertEqual(pl3.prefix.uint, 0x80)
     self.assertEqual(pl3.node, pn)
     self.assertFalse(pl3.pruned)
     pl4 = pn.link_class(b'\x80', MemoryPatriciaAuthTree())
     self.assertEqual(pl4.prefix.uint, 0x80)
     self.assertEqual(pl4.node, pn)
     self.assertFalse(pl4.pruned)
     pl5 = pn.link_class(b'\x80', MemoryPatriciaAuthTree())
     self.assertEqual(pl5.prefix.uint, 0x80)
     self.assertEqual(pl5.node, pn)
     self.assertFalse(pl5.pruned)
     pl6 = pn.link_class(b'\x80', hash=0, size=1)
     self.assertEqual(pl6.prefix.uint, 0x80)
     self.assertEqual(pl6.hash, 0)
     self.assertIs(pl6.node, None)
     self.assertTrue(pl6.pruned)
     pl7 = pn.link_class(b'\x80', 0, size=1)
     self.assertEqual(pl7.prefix.uint, 0x80)
     self.assertEqual(pl7.hash, 0)
     self.assertIs(pl7.node, None)
     self.assertTrue(pl7.pruned)
Exemple #2
0
 def test_trim(self):
     for flags in xrange(16):
         pn = MemoryPatriciaAuthTree()
         pn.update((k, v) for k, v in six.iteritems(SCENARIOS[flags])
                   if k != 'hash_')
         pn2 = MemoryPatriciaAuthTree()
         pn2.update(pn)
         res = pn2.trim(['abc'])
         self.assertEqual(res, gmpy2.popcount(flags >> 1))
         del pn2.hash
         self.assertEqual(pn2.hash, SCENARIOS[flags]['hash_'], flags)
         self.assertEqual(pn2.size, gmpy2.popcount(flags))
         self.assertEqual(pn2.length, gmpy2.popcount(flags & 1))
         if pn2.children:
             self.assertEqual(pn2.children[0].size,
                              gmpy2.popcount(flags) - (flags & 1))
             self.assertEqual(pn2.children[0].length, 0)
             self.assertTrue(pn2.children[0].pruned)
         pn3 = MemoryPatriciaAuthTree()
         pn3.update(pn)
         res = pn3.trim([Bits(bytes='abc\x00', length=25)])
         self.assertEqual(res, gmpy2.popcount(flags >> 2))
         del pn3.hash
         self.assertEqual(pn3.hash, SCENARIOS[flags]['hash_'])
         self.assertEqual(pn3.size, gmpy2.popcount(flags))
         self.assertEqual(pn3.length, gmpy2.popcount(flags & 3))
Exemple #3
0
        0xa6a357cc11c024e266fed4b3cecddc75f17161bb309ad35db4c1577b4882be3e,
        0xeebf6d83f16d17938f9e2850feaae2040ead54dabc3648897093f67645af7f7f,
        0xe138846eab6d3debd1be0b6fe54aec976aa67a09812d2a64194e317c7374d8a6,
    )

leaf_value = BaseAuthTreeNode(b'123')
SCENARIOS = [
    dict(value = None, children = [],
         str_  = b'\x00\x00',
         composable = 0x52e48cb2ad44028bb41e99d3d7727b866589b6b1314c6055bea5dad3520252cd,
         patricia   = 0x52e48cb2ad44028bb41e99d3d7727b866589b6b1314c6055bea5dad3520252cd),
    dict(value = b'abc', children = [],
         str_  = b'\x10' b'\x00' b'\x03abc',
         composable = 0x5fd655e94e5f3d638a651e172efe8a6a94667cc5e6b187f0d66be17affc3f5e0,
         patricia   = 0x5fd655e94e5f3d638a651e172efe8a6a94667cc5e6b187f0d66be17affc3f5e0),
    dict(value = b'abc', children = [(Bits('0b0'), leaf_value)],
         str_  = b'\x11' b'\x00' b'\x03abc' b'\x10\x00\x03123',
         composable = 0xf23372d0378733fe5086b0304ce912cc7ce5af8a011b8bc427f280f6e0a5a943,
         patricia   = 0xf23372d0378733fe5086b0304ce912cc7ce5af8a011b8bc427f280f6e0a5a943),
    dict(value = b'abc', children = [(Bits('0b00'), leaf_value)],
         str_  = b'\x12' b'\x00' b'\x03abc' b'\x02' b'\x10\x00\x03123',
         composable = 0xae58714f66551df9c417007d77ac907843c62f64a05ee2b7e0344626ef7b9db1,
         patricia   = 0x86dff1db1951234bfa86a4fde66ba911d88e7a844e97a02758a9c99e3cce1514),
    dict(value = b'abc', children = [(Bits('0b010'), leaf_value)],
         str_  = b'\x12' b'\x00' b'\x03abc' b'\x05' b'\x10\x00\x03123',
         composable = 0x8ac3e3326269dcd08a85c1b4be9ecb4bf7545e2ab309cb6aff651cee862d4251,
         patricia   = 0x8d9d0b874b05464271c07cf2e8764e7baf7c628f5be4aa4f82d1022f8ec395c2),
    dict(value = b'abc', children = [(Bits('0b0110'), leaf_value)],
         str_  = b'\x12' b'\x00' b'\x03abc' b'\x0b' b'\x10\x00\x03123',
         composable = 0xcb4be3823588a7b0126144a244e91b55945348630424d694ac18c29dc8f7550e,
         patricia   = 0xa9b63c7d6ea52a23787a339cb34b92e0f8d1038def61ac14ec27d627cfed71a9),
Exemple #4
0
class PatriciaNode(HybridHashableMixin, core.PatriciaNode, Base):
    __slots__ = ('value prune_value '
                 'left_prefix left_node left_hash '
                 'right_prefix right_node right_hash '
                 '_hash size length').split()
    __tablename__ = 'bitcoin_patricia_node'
    __table_args__ = (Index('__'.join(['ix', __tablename__, 'hash']),
                            'hash'), )
    __lazy_slots__ = ('hash', )
    id = Column(Integer,
                Sequence('__'.join([__tablename__, 'id', 'seq'])),
                primary_key=True)

    type = Column(Enum('txid_index',
                       'contract_index',
                       name='__'.join([__tablename__, 'type', 'enum'])),
                  nullable=False)
    __mapper_args__ = {
        'polymorphic_on': type,
    }

    value = Column(LargeBinary)
    prune_value = Column(Boolean)

    left_prefix = Column(BitField(implicit=Bits('0b0')))
    left_node_id = Column(Integer, ForeignKey('bitcoin_patricia_node.id'))
    left_node = orm.relationship(
        lambda: PatriciaNode,
        primaryjoin='PatriciaNode.id == PatriciaNode.left_node_id',
        uselist=False)
    left_hash = Column(Hash256(length=32))

    right_prefix = Column(BitField(implicit=Bits('0b1')))
    right_node_id = Column(Integer, ForeignKey('bitcoin_patricia_node.id'))
    right_node = orm.relationship(
        lambda: PatriciaNode,
        primaryjoin='PatriciaNode.id == PatriciaNode.right_node_id',
        uselist=False)
    right_hash = Column(Hash256(length=32))

    @property
    def children(self):
        link_class = getattr(
            self, 'get_link_class',
            lambda: getattr(self, 'link_class', core.PatriciaLink))()

        class _Children(list):
            def append(children, link):
                if not link.prefix[0]:
                    self.left_prefix, self.left_node, self.left_hash = (
                        link.prefix, link.node, link._hash)
                else:
                    self.right_prefix, self.right_node, self.right_hash = (
                        link.prefix, link.node, link._hash)

            def extend(children, links):
                for link in links:
                    children.append(link)

        children = ()
        if self.left_prefix is not None:
            children += (link_class(prefix=self.left_prefix,
                                    node=self.left_node,
                                    hash=self.left_hash), )
        if self.right_prefix is not None:
            children += (link_class(prefix=self.right_prefix,
                                    node=self.right_node,
                                    hash=self.right_hash), )
        return _Children(children)

    def children_create(self):
        pass

    # The digest value which results from applying the double-SHA256 function
    # to the serial representation of this node.
    _hash = Column('hash', Hash256(length=32), nullable=False)

    size = Column(Integer, nullable=False)

    length = Column(Integer, nullable=False)
Exemple #5
0
      str_=b'\x00\x00',
      composable=
      0x52e48cb2ad44028bb41e99d3d7727b866589b6b1314c6055bea5dad3520252cd,
      patricia=
      0x52e48cb2ad44028bb41e99d3d7727b866589b6b1314c6055bea5dad3520252cd),
 dict(value=b'abc',
      children=[],
      str_=b'\x10'
      b'\x00'
      b'\x03abc',
      composable=
      0x5fd655e94e5f3d638a651e172efe8a6a94667cc5e6b187f0d66be17affc3f5e0,
      patricia=
      0x5fd655e94e5f3d638a651e172efe8a6a94667cc5e6b187f0d66be17affc3f5e0),
 dict(value=b'abc',
      children=[(Bits('0b0'), leaf_value)],
      str_=b'\x11'
      b'\x00'
      b'\x03abc'
      b'\x10\x00\x03123',
      composable=
      0xf23372d0378733fe5086b0304ce912cc7ce5af8a011b8bc427f280f6e0a5a943,
      patricia=
      0xf23372d0378733fe5086b0304ce912cc7ce5af8a011b8bc427f280f6e0a5a943),
 dict(value=b'abc',
      children=[(Bits('0b00'), leaf_value)],
      str_=b'\x12'
      b'\x00'
      b'\x03abc'
      b'\x02'
      b'\x10\x00\x03123',
Exemple #6
0
 def process_result_value(self, value, dialect):
     if value is None:
         return None
     file_ = StringIO(value)
     bitlength = deserialize_varint(file_)
     return self._implicit + Bits(bytes=file_.read(), length=bitlength)
Exemple #7
0
 def __init__(self, implicit=Bits(), *args, **kwargs):
     super(BitField, self).__init__(*args, **kwargs)
     self._implicit = implicit