Exemplo n.º 1
0
class CentraClient:
    def __init__(self, host='127.0.0.1', port=5000, init_token=None):
        (self.priv, self.pub) = key_gen(setup())
        self.cs_client = ChainspaceClient(host=host, port=port)
        if init_token:
            self.create_centra_client(init_token)

    def create_centra_client(self, token):
        if type(token) is not tuple:
            token = (token, )
        create_centra_client_txn = create_centra_client(
            token,
            None,
            (pack(self.pub), ),
            pack(self.priv),
        )
        self.centra_client = create_centra_client_txn['transaction'][
            'outputs'][0]
        self.vote_slip = create_centra_client_txn['transaction']['outputs'][1]
        self.ebtoken = create_centra_client_txn['transaction']['outputs'][2]
        self.cs_client.process_transaction(create_centra_client_txn)

    def cast_csc_vote(self, client_pub):
        cast_csc_vote_txn = cast_csc_vote(
            (self.vote_slip, ),
            None,
            None,
            pack(self.priv),
            pack(client_pub),
        )
        vote_token = cast_csc_vote_txn['transaction']['outputs'][0]
        self.vote_slip = cast_csc_vote_txn['transaction']['outputs'][1]
        self.cs_client.process_transaction(cast_csc_vote_txn)
        return vote_token

    def cast_lrep_vote(self, rep_pub, queue=None):
        cast_lrep_vote_txn = cast_lrep_vote(
            (self.vote_slip, ),
            None,
            None,
            pack(self.priv),
            pack(rep_pub),
        )
        vote_token = cast_lrep_vote_txn['transaction']['outputs'][0]
        self.vote_slip = cast_lrep_vote_txn['transaction']['outputs'][1]
        self.cs_client.process_transaction(cast_lrep_vote_txn)
        if queue:
            queue.put(vote_token)
        return vote_token

    def submit_bid(self, bid_type, quantity):
        bid_proof_txn = submit_bid_proof((self.ebtoken, ), None, (bid_type, ),
                                         pack(self.priv), quantity)
        bid_proof = bid_proof_txn['transaction']['outputs'][0]
        self.ebtoken = bid_proof_txn['transaction']['outputs'][1]
        self.cs_client.process_transaction(bid_proof_txn)
Exemplo n.º 2
0
class LREPClient(CentraClient):
    def create_lrep_client(self,
                           host='127.0.0.1',
                           lrep_port=5000,
                           vote_tokens=None):
        self.lrep_cs_client = ChainspaceClient(host=host,
                                               port=lrep_port,
                                               max_wait=lrep_port - 5000)
        create_lrep_client_txn = create_lrep_client(
            vote_tokens,
            None,
            (pack(self.pub), ),
            pack(self.priv),
        )
        self.vote_slip = create_lrep_client_txn['transaction']['outputs'][1]
        self.lrep_client = create_lrep_client_txn['transaction']['outputs'][0]
        self.lrep_cs_client.process_transaction(create_lrep_client_txn)

    def accept_bids(self):
        time.sleep(DELTA)
        bid_proofs = self.lrep_cs_client.get_objects({
            'location':
            loads(self.lrep_client)['location'],
            'type':
            'BidProof'
        })
        bidders = {}
        for bid in bid_proofs:
            bid = loads(bid)
            bidders[str(bid['quantity_sig'])] = True

        time.sleep(2 * DELTA)
        buy_bids = self.lrep_cs_client.get_objects({
            'location':
            loads(self.lrep_client)['location'],
            'type':
            'EBBuy'
        })
        sell_bids = self.lrep_cs_client.get_objects({
            'location':
            loads(self.lrep_client)['location'],
            'type':
            'EBSell'
        })
        # process bid
        accepted_bids = []
        for bid in buy_bids:
            if not bidders.has_key(loads(bid)['quantity_sig']):
                continue
            accepted_bids.append(bid)
        for bid in sell_bids:
            if not bidders.has_key(loads(bid)['quantity_sig']):
                continue
            accepted_bids.append(bid)
        if len(accepted_bids) == 0:
            return None
        accept_bids_txn = accept_bids(
            tuple(accepted_bids),
            None,
            (pack(self.pub), ),
            pack(self.priv),
        )
        bid_accept = accept_bids_txn['transaction']['outputs'][0]
        self.lrep_cs_client.process_transaction(accept_bids_txn)
        return bid_accept
Exemplo n.º 3
0
logging.basicConfig(level=logging.INFO,
                    filename="../../../stats/" + RUN_ID + "_execution.log",
                    filemode="a+",
                    format="%(asctime)-15s %(levelname)-8s %(message)s")
logging.info("Starting measure_centra_add_lrep")

logging.info("NUM_SHARDS: %s, NUM_REPLICAS: %s, NUM_CLIENTS: %s " %
             (NUM_SHARDS, NUM_REPLICAS, NUM_CLIENTS))
G = setup()[0]

# In[2]:

global_client = ChainspaceClient(host=CS_HOST, port=5000)
init_transaction = centra.init()
init_tokens = init_transaction['transaction']['outputs']
global_client.process_transaction(init_transaction)
client_divs = centra.eq_div(NUM_CLIENTS, NUM_SHARDS)
# print client_divs

# In[3]:

# Create prosumer clients for locations
clients = []
base_port = 5000
lreps = []

for s in range(0, NUM_SHARDS):
    clients.append([])
    lreps.append(centra.LREPClient(host=CS_HOST, port=base_port + s))

    for c in range(0, client_divs[s]):
# chainsapce
from chainspacecontract import transaction_to_solution
# from chainspacecontract.examples.energy_bidding import contract as energy_bidding_contract
from chainspacecontract.examples import energy_bidding
# crypto
from chainspacecontract.examples.utils import setup, key_gen, pack
from chainspaceapi import ChainspaceClient

G = setup()[0]
(provider_priv, provider_pub) = key_gen(setup())
client = ChainspaceClient(port=5001)

# Init txn
init_transaction = energy_bidding.init()
token = init_transaction['transaction']['outputs'][0]
client.process_transaction(init_transaction)

# # Create meter
# create_meter_transaction = energy_bidding.create_meter(
#     (token,),
#     None,
#     None,
#     pack(provider_pub),
#     'Some info about the meter.',
#     dumps([5, 3, 5, 3, 5]),  # the tariffs
#     dumps(764)               # billing period
# )
# token = create_meter_transaction['transaction']['outputs'][2]
# meter = create_meter_transaction['transaction']['outputs'][1]
# client.process_transaction(create_meter_transaction)
Exemplo n.º 5
0
# crypto
from chainspacecontract.examples.utils import setup, key_gen, pack
from chainspaceapi import ChainspaceClient

G = setup()[0]
(provider_priv1, provider_pub1) = key_gen(setup())
(provider_priv2, provider_pub2) = key_gen(setup())
(provider_priv3, provider_pub3) = key_gen(setup())
client1 = ChainspaceClient(port=5000)
client2 = ChainspaceClient(port=5001)
client3 = ChainspaceClient(port=5000)

# Init txn
init_transaction = centra.init()
init_s0_token1 = init_transaction['transaction']['outputs'][0]
client1.process_transaction(init_transaction)

init_transaction = centra.init()
init_s1_token1 = init_transaction['transaction']['outputs'][0]
client2.process_transaction(init_transaction)

###########################################################################
############################  Client 1  ###################################
###########################################################################

# Create centra client
create_centra_client_txn = centra.create_centra_client(
    (init_s0_token1, ),
    None,
    (pack(provider_pub1), ),
    'Some info about the client.',