Ejemplo n.º 1
0
def test_base_consensus():
    node = Node('n1', Endpoint('http', 'localhost', 5001))
    consensus_module = get_fba_module('isaac')
    consensus = consensus_module.IsaacConsensus(
        node,
        100,
        tuple(),
    )

    assert hash(consensus) == 183908261690725173

    consensus.save_message(Message('id1', 'message1'))
    consensus.save_message(Message('id2', 'message2'))
    consensus.save_message(Message('id3', 'message3'))
    consensus.save_message(Message('id4', 'message4'))

    assert hash(consensus) == 1841909880308066977

    node2 = Node('n2', Endpoint('http', 'localhost', 5002))
    consensus_module = get_fba_module('isaac')
    consensus_2 = consensus_module.IsaacConsensus(
        node2,
        100,
        tuple(),
    )

    assert hash(consensus_2) == 183908261690725173

    consensus_2.save_message(Message('id1', 'message1'))
    consensus_2.save_message(Message('id2', 'message2'))
    consensus_2.save_message(Message('id3', 'message3'))
    consensus_2.save_message(Message('id4', 'message4'))

    assert hash(consensus_2) == 1841909880308066977
Ejemplo n.º 2
0
def main(options):
    config = collections.namedtuple(
        'Config',
        ('node_name', 'port', 'threshold', 'validators', 'faulty_percent'),
    )(uuid.uuid1().hex, 8001, 51, [], 0)

    if not pathlib.Path(options.conf).exists():
        parser.error('conf file, `%s` does not exists.' % options.conf)

    if not pathlib.Path(options.conf).is_file():
        parser.error('conf file, `%s` is not valid file.' % options.conf)

    conf = configparser.ConfigParser()
    conf.read(options.conf)
    log.info('conf file, `%s` was loaded', options.conf)

    config = config._replace(node_name=conf['node']['name'])
    config = config._replace(port=int(conf['node']['port']))
    config = config._replace(threshold=int(conf['node']['threshold_percent']))

    if conf.has_option('faulty', 'faulty_percent'):
        config = config._replace(
            faulty_percent=int(conf['faulty']['faulty_percent']))

    log.debug('loaded conf: %s', config)

    validator_list = []
    for i in filter(lambda x: len(x.strip()) > 0,
                    conf['node']['validator_list'].split(',')):
        validator_list.append(Endpoint.from_uri(i.strip()))

    config = config._replace(validators=validator_list)
    log.debug('Validators: %s' % config.validators)

    node = node_factory(
        config.node_name,
        Endpoint(NETWORK_MODULE.SCHEME, get_local_ipaddress(), config.port),
        config.faulty_percent)

    consensus_module = get_fba_module('isaac')
    consensus = consensus_module.IsaacConsensus(
        node,
        config.threshold,
        tuple(map(lambda x: Node(x.extras['name'], x), config.validators)),
    )

    log.metric(node=node.name, data=node.to_dict())

    transport = NETWORK_MODULE.Transport(bind=('0.0.0.0', config.port))

    blockchain = Blockchain(consensus, transport)

    base_server = BaseServer(blockchain)
    base_server.start()

    return
def main(options):
    nodes = options.nodes
    threshold = options.threshold
    ballots = options.ballots
    sending = options.sending
    consensus_protocol = options.consensus_protocol

    log.fatal(f'Number of nodes={nodes}')
    log.fatal(f'Threshold={threshold}')
    log.fatal(f'Number of ballots in slot={ballots}')
    log.fatal(f'Number of sending ballots={sending}')
    assert consensus_protocol in (
        'isaac',
        'instantsend',
    )
    log.fatal(f'Consensus protocol={consensus_protocol}')

    log.metric(
        action='performance-test',
        kind=f'whole process',
        timing='begin',
        nodes=nodes,
        Threshold=threshold,
        ballots=ballots,
        sending=sending,
    )

    start = time.time()

    Consensus = get_fba_module(consensus_protocol).Consensus
    test_performance_init_to_all_confirm_sequence(Consensus, nodes, threshold,
                                                  ballots, sending)
    end = time.time()
    log.fatal(f'Total Elapsed time={end - start:.3} sec')

    log.metric(
        action='performance-test',
        kind=f'whole process',
        timing='end',
        elapsed_time=f'{end-start:.3} sec',
    )
import copy
import random

from bos_consensus.common import Ballot
from bos_consensus.consensus import get_fba_module
from bos_consensus.network import get_network_module
from bos_consensus.util import logger

CONSENSUS_MODULE = get_fba_module('isaac')

NETWORK_MODULE = get_network_module('default_http')


class StateRegressionTransport(NETWORK_MODULE.Transport):
    def __init__(self, faulty, **config):
        super(StateRegressionTransport, self).__init__(**config)

        assert hasattr(faulty, 'frequency')
        assert hasattr(faulty, 'target_nodes')
        assert type(faulty.target_nodes) in (list, tuple)

        self.faulty = faulty

    def start(self, blockchain, *a, **kw):
        self.log_faulty = logger.get_logger(
            'transport.faulty', node=blockchain.consensus.node.name)

        return super(StateRegressionTransport,
                     self).start(blockchain, *a, **kw)

    AVAILABLE_STATE = (
Ejemplo n.º 5
0
from bos_consensus.network import Endpoint
from .blockchain import Blockchain
from .util import StubTransport


def copy_ballot(ballot, node_name, state):
    new_ballot = copy.copy(ballot)
    new_ballot.node_name = node_name
    new_ballot.timestamp = ballot.timestamp
    if state is not None:
        new_ballot.state = state

    return new_ballot


Consensus = get_fba_module('isaac').Consensus
IsaacState = get_fba_module('isaac').State


class SimpleBlockchain(Blockchain):
    def __init__(self, *a, **kw):
        super(SimpleBlockchain, self).__init__(*a, **kw)

    def receive_ballots(self, *ballots):
        for ballot in ballots:
            self.receive_ballot(ballot)

        return


def simple_blockchain_factory(name, address, threshold, validator_endpoint_uris):
import copy

from bos_consensus.common import Ballot, BallotVotingResult, Message, node_factory
from bos_consensus.blockchain import Blockchain
from bos_consensus.network import Endpoint
from bos_consensus.consensus import get_fba_module
from bos_consensus.consensus.fba.isaac import IsaacState
from bos_consensus.blockchain.util import StubTransport

IsaacConsensus = get_fba_module('isaac').IsaacConsensus


def receive_copy_ballot(self, ballot):
    new_ballot = copy.deepcopy(ballot)
    self.consensus.handle_ballot(new_ballot)


def check_slot_time_stub(ballot):
    pass


def blockchain_factory(name, address, threshold, validator_endpoint_uris,
                       slot_size):
    node = node_factory(name, Endpoint.from_uri(address))
    validators = list()
    for uri in validator_endpoint_uris:
        validators.append(node_factory(uri, Endpoint.from_uri(uri)), )

    consensus = IsaacConsensus(node, threshold, validators, slot_size)
    consensus._check_slot_time = check_slot_time_stub
    Blockchain.receive_ballot = receive_copy_ballot