rousseau-chain icon indicating copy to clipboard operation
rousseau-chain copied to clipboard

[WIP] Customizable blocks and nodes API

Open bogdan-kulynych opened this issue 7 years ago • 0 comments

We need to allow custom blocks formats with custom serialization, and more flexibility about backends. All of these are breaking changes.

Progress:

  • [x] Simple dict-based object store
  • [x] New Chain API
  • [ ] New Tree API
  • [ ] Redis-based object store
  • [ ] Default msgpack, json serialization mixins for blocks

Tests that are working:

py.test tests/test_chain.py
py.test hippiehug/Utils.py

Object Store

An object store verifiably stores and retrieves (binary) objects by their hashes.

from hippiehug.Store import DictionaryStore

store = DictionaryStore(backend={})
store.put(b'object')
store.get('2958d416d08aa5a472d7b509036cb7eafd542add84527e66a145ea64cb4cdc75')  # b'object'

Primarily, object stores keep chain blocks and tree nodes. Custom blocks can also use object stores for reading offloaded data.

Chain

  1. To work with chains, first, define own block format.
from hippiehug.Chain import BaseBlock

class JsonBlock(BaseBlock):
    def serialize(self):
        return json.dumps(
        # ...
        # Things to be serialized
        # ...
        ).encode('utf-8')
    
    @staticmethod
    def deserialize(serialized):
        vals = json.loads(serialized)
        # ...
        # Do deserialization
        # ...
        return JsonBlock(**vals)
  1. Instantiate a chain using this block format
from hippiehug.Chain import Chain
chain = Chain(block_cls=JsonBlock)

# or alternatively, subclass:

class AwesomeChain(Chain):
    block_cls = JsonBlock
    # ...
    # Some custom logic can go here
    # ...

TODO: Trees

Presumably, we'll have a unified serialization usage for tree nodes and blocks:

class AwesomeBlock(JsonMixin, BaseBlock):
    json_fields = ['index', 'foo', 'bar', 'fingers']  # index and fingers probably should be by default
    # Custom logic

chain = Chain(block_cls=AwesomeBlock)

class AwesomeNode(JsonMixin, BaseNode):
    json_fields = ['baz', 'qaz', 'left', 'right']  # left and right probably should be by default
    # Custom logic

tree = Tree(node_cls=AwesomeNode)

bogdan-kulynych avatar May 04 '17 07:05 bogdan-kulynych