Skip to content
This repository has been archived by the owner on Feb 1, 2024. It is now read-only.

Commit

Permalink
Add Python Consensus SDK
Browse files Browse the repository at this point in the history
Signed-off-by: Nick Drozd <drozd@bitwise.io>
  • Loading branch information
nick-drozd committed May 4, 2018
1 parent f59a570 commit cdbfd49
Show file tree
Hide file tree
Showing 9 changed files with 1,172 additions and 0 deletions.
14 changes: 14 additions & 0 deletions sdk/python/sawtooth_sdk/consensus/__init__.py
@@ -0,0 +1,14 @@
# Copyright 2018 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# -----------------------------------------------------------------------------
30 changes: 30 additions & 0 deletions sdk/python/sawtooth_sdk/consensus/driver.py
@@ -0,0 +1,30 @@
# Copyright 2018 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# -----------------------------------------------------------------------------

import abc


class Driver(metaclass=abc.ABCMeta):
@abc.abstractmethod
def __init__(self, engine):
pass

@abc.abstractmethod
def start(self, endpoint):
pass

@abc.abstractmethod
def stop(self):
pass
52 changes: 52 additions & 0 deletions sdk/python/sawtooth_sdk/consensus/engine.py
@@ -0,0 +1,52 @@
# Copyright 2018 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# -----------------------------------------------------------------------------

import abc


class Engine(metaclass=abc.ABCMeta):
@abc.abstractmethod
def start(self, updates, service):
'''Called after the engine is initialized, when a connection to the
validator has been established. Notifications from the
validator are sent along UPDATES. SERVICE is used to send
requests to the validator.
Args:
updates (Queue)
service (Service)
'''

@abc.abstractmethod
def stop(self):
'''Called before the engine is dropped in order to give the engine a
chance to notify peers and clean up.'''

@abc.abstractproperty
def version(self):
'''Get the version of this engine.
Return:
str
'''

@abc.abstractproperty
def name(self):
'''Get the name of the engine, typically the algorithm being
implemented.
Return:
str
'''
30 changes: 30 additions & 0 deletions sdk/python/sawtooth_sdk/consensus/exceptions.py
@@ -0,0 +1,30 @@
# Copyright 2018 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# -----------------------------------------------------------------------------


class InvalidState(Exception):
pass


class ReceiveError(Exception):
pass


class UnknownBlock(Exception):
pass


class UnknownPeer(Exception):
pass
151 changes: 151 additions & 0 deletions sdk/python/sawtooth_sdk/consensus/service.py
@@ -0,0 +1,151 @@
# Copyright 2018 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# -----------------------------------------------------------------------------

import abc


class Block:
def __init__(self, block):
self.block_id = block.block_id
self.previous_id = block.previous_id
self.signer_id = block.signer_id
self.block_num = block.block_num
self.payload = block.payload


class Service(metaclass=abc.ABCMeta):
'''Provides methods that allow the consensus engine to issue commands
and requests.'''

# -- P2P --

@abc.abstractmethod
def send_to(self, peer_id, message_type, payload):
'''Send a consensus message to a specific connected peer.
Args:
peer_id (bytes)
message_type (str)
payload (bytes)
'''

@abc.abstractmethod
def broadcast(self, message_type, payload):
'''Broadcast a message to all connected peers.
Args:
message_type (str)
payload (bytes)
'''

# -- Block Creation --

@abc.abstractmethod
def initialize_block(self, previous_id):
'''Initialize a new block with PREVIOUS_ID and begin adding batches to
it. If no PREVIOUS_ID is specified, the current head will be
used.
Args:
previous_id (bytes or None)
'''

@abc.abstractmethod
def finalize_block(self, data):
'''Stop adding batches to the current block and finalize it. Include
DATA in the block. If this call is successful, the consensus
engine will receive it afterwards.
Args:
data (bytes)
Return:
bytes
'''

@abc.abstractmethod
def cancel_block(self):
'''Stop adding batches to the current block and abandon it.'''

# -- Block Directives --

@abc.abstractmethod
def check_blocks(self, priority):
'''Update the prioritization of blocks to check to PRIORITY.
Args:
priority (list[bytes])
'''

@abc.abstractmethod
def commit_block(self, block_id):
'''Update the block that should be committed.
Args:
block_id (bytes)
'''

@abc.abstractmethod
def ignore_block(self, block_id):
'''Signal that this block is no longer being committed.
Args:
block_id (bytes)
'''

@abc.abstractmethod
def fail_block(self, block_id):
'''Mark this block as invalid from the perspective of consensus.
Args:
block_id (bytes)
'''

# -- Queries --

@abc.abstractmethod
def get_blocks(self, block_ids):
'''Retrive consensus-related information about blocks.
Args:
block_ids (list[bytes])
Return:
dict[bytes, block]
'''

@abc.abstractmethod
def get_settings(self, block_id, settings):
'''Read the value of settings as of the given block.
Args:
block_id (bytes)
settings (list[str])
Return:
dict[str, str]
'''

@abc.abstractmethod
def get_state(self, block_id, addresses):
'''Read values in state as of the given block.
Args:
block_id (bytes)
addresses (list[str])
Return:
dict[str, bytes]
'''

0 comments on commit cdbfd49

Please sign in to comment.