예제 #1
0
def test_wellformed():
    resources = [hexlify(urandom(16)) for _ in range(1000)]
    # def packageTx(data, deps, num_out)
    transactions = []
    for x in range(100):
        deps = sample(resources, 2)
        data = json.dumps({"ID": x})
        tx = packageTx(data, deps, 2)
        transactions.append((tx, data))
    # [(hexlify(urandom(16)), sample(resources,2), []) for x in range(300)]

    n = Node(resources, 1)
    n.quiet = True
    shuffle(transactions)
    # tx_list = sample(transactions, 100)

    with Timer() as t:
        for tx, data in transactions:
            idx, deps, out, txdata = tx

            ## First perform the Tx checks
            assert packageTx(data, deps, 2) == tx

            ## Now process this transaction
            n.process(tx)

    print "Time taken: %2.2f sec" % (t.interval)
예제 #2
0
    def __init__(self, start = [], quorum=1, name = None, 
            shard=None, shard_map=None,
            host='localhost', port=9092):
        self.client = None
        self.channel_name = None

        self.kafka = KafkaClient('%s:%s' % (host,port)) # redis.StrictRedis(host, port, db)
        self.producer = SimpleProducer(self.kafka)

        self.shard_id = shard
        self.shard_map = shard_map

        # Configure the shard
        if shard is not None:
            assert shard in shard_map
            shard = shard_map[shard]

            self.channel_name = 'votes%s' % self.shard_id
            self.client = Listener(self, self.channel_name)
            print "Listen on: %s" % self.channel_name
        else:
            self.channel_name = 'votes'
            self.client = Listener(self, self.channel_name)

        # Initialize the node for consensus.
        Node.__init__(self, start, quorum, name, shard)

        # Start the subscription loop and log.        
        self.client.start()
        self.RLogger = logging.getLogger()

        print "Init done"
예제 #3
0
def test_wellformed():
    resources = [hexlify(urandom(16)) for _ in range(1000)]
    # def packageTx(data, deps, num_out)
    transactions = []
    for x in range(100):
        deps = sample(resources,2)
        data = json.dumps({"ID":x})
        tx = packageTx(data, deps, 2)
        transactions.append((tx, data))
    # [(hexlify(urandom(16)), sample(resources,2), []) for x in range(300)]

    n = Node(resources, 1)
    n.quiet = True
    shuffle(transactions)
    # tx_list = sample(transactions, 100)

    with Timer() as t:
        for tx, data in transactions:
            idx, deps, out, txdata = tx

            ## First perform the Tx checks
            assert packageTx(data, deps, 2) == tx

            ## Now process this transaction
            n.process(tx)
            
    print "Time taken: %2.2f sec" % (t.interval) 
예제 #4
0
def test_small_chain():
    T1 = ("T1", ["A"], ["B"], "")
    T2 = ("T2", ["B"], ["C"], "")

    n = Node(["A"], 1)
    n.process(T1)
    n.process(T2)
    assert "C" in n.pending_available
예제 #5
0
def test_small_chain():
    T1 = ("T1", ["A"], ["B"], "")
    T2 = ("T2", ["B"], ["C"], "")

    n = Node(["A"],1)
    n.process(T1)
    n.process(T2)
    assert "C" in n.pending_available
예제 #6
0
def test_small():
    T1 = ("T1", ["A", "B"], [], "")
    T2 = ("T2", ["B", "C"], [], "")

    n = Node(["A", "B", "C"], 1)
    n.process(T1)
    n.process(T2)
    assert "T1" in n.commit_yes
    assert "T2" not in n.commit_yes
예제 #7
0
def test_small():
    T1 = ("T1", ["A", "B"], [], "")
    T2 = ("T2", ["B", "C"], [], "")

    n = Node(["A", "B", "C"],1)
    n.process(T1)
    n.process(T2)
    assert "T1" in n.commit_yes
    assert "T2" not in n.commit_yes
예제 #8
0
def test_chain_conflict():
    T1 = ("T1", ["A"], ["B"], "")
    T2 = ("T2", ["A"], ["C"], "")
    T3 = ("T3", ["B"], ["D"], "")
    T4 = ("T4", ["C"], ["F"], "")

    n = Node(["A"],1)
    for tx in [T1, T2, T3, T4]:
        n.process(tx)
예제 #9
0
def test_chain_conflict():
    T1 = ("T1", ["A"], ["B"], "")
    T2 = ("T2", ["A"], ["C"], "")
    T3 = ("T3", ["B"], ["D"], "")
    T4 = ("T4", ["C"], ["F"], "")

    n = Node(["A"], 1)
    for tx in [T1, T2, T3, T4]:
        n.process(tx)
예제 #10
0
def test_random():
    resources = [hexlify(urandom(16)) for _ in range(300)]
    transactions = [(hexlify(urandom(16)), sample(resources, 2), [], "")
                    for _ in range(300)]

    n = Node(resources, 2)
    shuffle(transactions)
    tx_list = sample(transactions, 100)
    for tx in transactions:
        n.process(tx)

    n2 = Node(resources, 2)
    n.gossip_towards(n2)
    for tx in transactions:
        n2.process(tx)
예제 #11
0
def test_shard_simple():
    T1 = ("333", ["444", "ccc"], [], "")
    T2 = ("bbb", ["444", "ddd"], [], "")

    n1 = Node(["444"], 1, name="n1", shard=["000", "aaa"])
    n2 = Node(["ccc", "ddd"], 1, name="n2", shard=["aaa", "fff"])

    n1.process(T1)
    n1.process(T2)
    print n1.pending_vote
    n2.process(T2)
    n2.process(T1)

    n1.gossip_towards(n2)

    n2.process(T1)
    n2.process(T2)
    
    assert '333' in n2.commit_yes
예제 #12
0
    def __init__(self, start = [], quorum=1, name = None, 
            shard=None, shard_map=None,
            host='localhost', port=6379, db=0):
        self.r = redis.StrictRedis(host, port, db)
        self.shard_id = shard
        self.shard_map = shard_map

        # Configure the shard
        if shard is not None:
            assert shard in shard_map
            shard = shard_map[shard]
            self.client = Listener(self, ['votes:%s' % self.shard_id])
        else:
            self.client = Listener(self, ['votes'])

        # Initialize the node for consensus.
        Node.__init__(self, start, quorum, name, shard)

        # Start the subscription loop and log.        
        self.client.start()
        self.RLogger = logging.getLogger()
예제 #13
0
def test_random():
    resources = [hexlify(urandom(16)) for _ in range(300)]
    transactions = [(hexlify(urandom(16)), sample(resources,2), [], "") for _ in range(300)]

    n = Node(resources, 2)
    shuffle(transactions)
    tx_list = sample(transactions, 100)
    for tx in transactions:
        n.process(tx)

    n2 = Node(resources,2)
    n.gossip_towards(n2)
    for tx in transactions:
        n2.process(tx)
예제 #14
0
    def __init__(self,
                 start=[],
                 quorum=1,
                 name=None,
                 shard=None,
                 shard_map=None,
                 host='localhost',
                 port=9092):
        self.client = None
        self.channel_name = None

        self.kafka = KafkaClient(
            '%s:%s' % (host, port))  # redis.StrictRedis(host, port, db)
        self.producer = SimpleProducer(self.kafka)

        self.shard_id = shard
        self.shard_map = shard_map

        # Configure the shard
        if shard is not None:
            assert shard in shard_map
            shard = shard_map[shard]

            self.channel_name = 'votes%s' % self.shard_id
            self.client = Listener(self, self.channel_name)
            print "Listen on: %s" % self.channel_name
        else:
            self.channel_name = 'votes'
            self.client = Listener(self, self.channel_name)

        # Initialize the node for consensus.
        Node.__init__(self, start, quorum, name, shard)

        # Start the subscription loop and log.
        self.client.start()
        self.RLogger = logging.getLogger()

        print "Init done"
예제 #15
0
def test_shard_many():
    limits = sorted([hexlify(urandom(32)) for _ in range(100)])
    limits = ["0" * 64] + limits + ["f" * 64]

    pre = ["444", "ccc", "ddd"]
    nodes = [
        Node(pre, 1, name="n%s" % i, shard=[b0, b1])
        for i, (b0, b1) in enumerate(zip(limits[:-1], limits[1:]))
    ]

    T1 = ("333", ["444", "ccc"], [], "")
    T2 = ("bbb", ["444", "ddd"], [], "")

    n1 = [n for n in nodes if n._within_TX(T1)]
    n2 = [n for n in nodes if n._within_TX(T2)]

    assert len(n1) == 3 and len(n2) == 3

    for n in n1:
        n.process(T1)

    for n in n2:
        n.process(T2)
예제 #16
0
def test_quorum_simple():
    T1 = ("T1", ["A", "B"], [], "")
    T2 = ("T2", ["B", "C"], [], "")

    n1 = Node(["A", "B", "C"], 2)
    n2 = Node(["A", "B", "C"], 2)
    n3 = Node(["A", "B", "C"], 2)

    n1.process(T1)
    n2.process(T2)
    n2.process(T1)
    n3.process(T1)

    n1.gossip_towards(n2)
    n3.gossip_towards(n2)

    n2.process(T1)
    assert "T1" in n2.commit_yes
예제 #17
0
def test_quorum_simple():
    T1 = ("T1", ["A", "B"], [], "")
    T2 = ("T2", ["B", "C"], [], "")

    n1 = Node(["A", "B", "C"], 2)
    n2 = Node(["A", "B", "C"], 2)
    n3 = Node(["A", "B", "C"], 2)

    n1.process(T1)
    n2.process(T2)
    n2.process(T1)
    n3.process(T1)

    n1.gossip_towards(n2)
    n3.gossip_towards(n2)

    n2.process(T1)
    assert "T1" in n2.commit_yes
예제 #18
0
def test_quorum_threesome():
    T1 = ("T1", ["A", "B"], [], "")
    T2 = ("T2", ["B", "C"], [], "")
    T3 = ("T3", ["A", "C"], [], "")

    n1 = Node(["A", "B", "C"], 2)
    n2 = Node(["A", "B", "C"], 2)
    n3 = Node(["A", "B", "C"], 2)

    n1.process(T1)
    n2.process(T2)
    n3.process(T3)

    n1.process(T2)
    n1.process(T3)
    n2.process(T1)
    n2.process(T3)
    n3.process(T1)
    n3.process(T2)

    n1.gossip_towards(n3)
    n2.gossip_towards(n3)

    n3.process(T1)
    n3.process(T2)
    n3.process(T3)
    assert "T1" in n3.commit_no
    assert "T2" in n3.commit_no
    assert "T3" in n3.commit_no
예제 #19
0
def test_shard_simple():
    T1 = ("333", ["444", "ccc"], [], "")
    T2 = ("bbb", ["444", "ddd"], [], "")

    n1 = Node(["444"], 1, name="n1", shard=["000", "aaa"])
    n2 = Node(["ccc", "ddd"], 1, name="n2", shard=["aaa", "fff"])

    n1.process(T1)
    n1.process(T2)
    print n1.pending_vote
    n2.process(T2)
    n2.process(T1)

    n1.gossip_towards(n2)

    n2.process(T1)
    n2.process(T2)

    assert '333' in n2.commit_yes
예제 #20
0
import numpy as np
from consensus import Node

# To Create a Formation
# ... add an object with `name`, `nodes`, `full`, `tree`

formations = [
    # circular formation
    {
        "name":
        "Circular",
        "nodes":
        [Node(4.0),
         Node(2.0),
         Node(-1.0),
         Node(3.0),
         Node(0.0),
         Node(-3.0)],
        "full":
        np.array([[0, 1, 1, 1, 1, 1], [1, 0, 1, 1, 1, 1], [1, 1, 0, 1, 1, 1],
                  [1, 1, 1, 0, 1, 1], [1, 1, 1, 1, 0, 1], [1, 1, 1, 1, 1, 0]]),
        "tree":
        np.array([[0, 1, 0, 0, 0, 0], [1, 0, 1, 0, 0, 0], [0, 1, 0, 1, 0, 0],
                  [0, 0, 1, 0, 1, 0], [0, 0, 0, 1, 0, 1], [0, 0, 0, 0, 1, 0]])
    },

    # wedge formation
    {
        "name":
        "Wedge",
        "nodes": [
예제 #21
0
def test_quorum_threesome():
    T1 = ("T1", ["A", "B"], [], "")
    T2 = ("T2", ["B", "C"], [], "")
    T3 = ("T3", ["A", "C"], [], "")

    n1 = Node(["A", "B", "C"], 2)
    n2 = Node(["A", "B", "C"], 2)
    n3 = Node(["A", "B", "C"], 2)

    n1.process(T1)
    n2.process(T2)
    n3.process(T3)

    n1.process(T2)
    n1.process(T3)
    n2.process(T1)
    n2.process(T3)
    n3.process(T1)
    n3.process(T2)

    n1.gossip_towards(n3)
    n2.gossip_towards(n3)

    n3.process(T1)
    n3.process(T2)  
    n3.process(T3)
    assert "T1" in n3.commit_no
    assert "T2" in n3.commit_no
    assert "T3" in n3.commit_no