示例#1
0
def requestGetEntity(net_profile, organization, user, channel, peer, chaincode, function, arg0):
    loop = asyncio.get_event_loop()
    cli = Client(net_profile=net_profile)
    org1_admin = cli.get_user(organization, user)
    cli.new_channel(channel)
    gopath = os.path.normpath(os.path.join(os.path.dirname(
        os.path.realpath('__file__')), '../chaincode'))
    os.environ['GOPATH'] = os.path.abspath(gopath)
    args = [arg0]
    response = loop.run_until_complete(cli.chaincode_invoke(
        requestor=org1_admin,
        channel_name=channel,
        peers=[peer],
        args=args,
        cc_name=chaincode,
        transient_map=None,
        wait_for_event=True,
        fcn=function,
    ))
    return response
示例#2
0
def InvokeServer():
    loop = asyncio.get_event_loop()
    cli = Client(net_profile="../connection-profile/network.json")
    org1_admin = cli.get_user('org1.example.com', 'Admin')
    org2_admin = cli.get_user('org2.example.com', 'Admin')

    # Make the client know there is a channel in the network
    cli.new_channel('modbuschannel')

    # Install Example Chaincode to Peers
    # GOPATH setting is only needed to use the example chaincode inside sdk#
    gopath_bak = os.environ.get('GOPATH', '')
    gopath = os.path.normpath(os.path.join(
        os.path.dirname(os.path.realpath('__file__')),
        '../chaincode'
    ))
    os.environ['GOPATH'] = os.path.abspath(gopath)

# Invoke a chaincode
#args = ["client", "Qmeq4hW6k34a5dbpE2vc7FjSX1xmn1tphg2hGrHFGxqk16"]

    host_ip = socket.gethostbyname(socket.gethostname())
    name=host_ip.encode("utf-8")
    arg2=base64.b64encode(hashlib.sha256(name).digest())
    args = ["server", arg2]

    # The response should be true if succeed
    response = loop.run_until_complete(cli.chaincode_invoke(
        requestor=org2_admin,
        channel_name='modbuschannel',
        peers=['peer0.org2.example.com'],
        args=args,
        cc_name='registration_cc_v2',
        transient_map=None,  # optional, for private data
        # for being sure chaincode invocation has been commited in the ledger, default is on tx event
        wait_for_event=True,
        # cc_pattern='^invoked*' # if you want to wait for chaincode event and you have a `stub.SetEvent("invoked", value)` in your chaincode
    ))
    print(response)

    response = loop.run_until_complete(
        cli.query_info(requestor=admin_owkin,
                       channel_name='mychannel',
                       peers=['peer1-owkin']))
    print(response)

    dir_path = os.path.dirname(os.path.realpath(__file__))

    response = loop.run_until_complete(
        cli.chaincode_invoke(
            requestor=admin_owkin,
            channel_name='mychannel',
            peers=['peer1-owkin'],
            args=[
                'ISIC 2018',
                '59300f1fec4f5cdd3a236c7260ed72bdd24691efdec63b7910ea84136123cecd',
                'http://chunantes.substrabac:8001/media/data_managers/59300f1fec4f5cdd3a236c7260ed72bdd24691efdec63b7910ea84136123cecd/opener.py',
                'Images',
                '59300f1fec4f5cdd3a236c7260ed72bdd24691efdec63b7910ea84136123cecd',
                'http://chunantes.substrabac:8001/media/data_managers/59300f1fec4f5cdd3a236c7260ed72bdd24691efdec63b7910ea84136123cecd/description.md',
                '', 'all'
            ],
            cc_name='mycc',
            cc_version='1.0',
            fcn='registerDataManager',
            wait_for_event=True,
            wait_for_event_timeout=5))
    print(response)
示例#4
0
        args=args,
        cc_name=CC_NAME,
        cc_version=CC_VERSION))
    if response:
        print("Instantiate chaincode successful")
    else:
        print("Instantiate chaincode failed")
        exit(-1)

    # Invoke a chaincode
    args = ['a', 'b', '100']
    # The response should be true if succeed
    response = loop.run_until_complete(cli.chaincode_invoke(
        requestor=org1_admin,
        channel_name='businesschannel',
        peers=['peer0.org1.example.com'],
        args=args,
        cc_name=CC_NAME
    ))
    print("Invoke chaincode done.")
    print(response)

    # Query a chaincode
    args = ['b']
    # The response should be true if succeed
    response = loop.run_until_complete(cli.chaincode_query(
        requestor=org1_admin,
        channel_name='businesschannel',
        peers=['peer0.org1.example.com'],
        args=args,
        cc_name=CC_NAME
loop = asyncio.get_event_loop()
cli = Client(net_profile="/home/rohan/Documents/cloudledger/network.json")
org1_admin = cli.get_user(org_name='org1.example.com', name='Admin')
peer_target = cli._peers['peer0.org1.example.com']
requestor = org1_admin

loop.run_until_complete(
    cli.init_with_discovery(org1_admin, peer_target, 'mychannel'))

args = ['170020031', 'rohan', 'rohan', '*****@*****.**', '100']

response = loop.run_until_complete(
    cli.chaincode_invoke(requestor=org1_admin,
                         channel_name='mychannel',
                         peers=peers,
                         args=args,
                         cc_name=cc_name,
                         transient_map=None,
                         wait_for_event=True))

responses = loop.run_until_complete(
    cli.chaincode_install(
        requestor=org1_admin,
        peers=['peer0.org1.example.com', 'peer1.org1.example.com'],
        cc_path='github.com/example_cc',
        cc_name='mycc',
        cc_version='v1.0'))

response = loop.run_until_complete(
    cli.chaincode_instantiate(
        requestor=org1_admin,
示例#6
0
inital_savings_balance = '1000'
amount = '1000'
dest_customer_id = '55'
source_customer_id = customer_id

for customer_id in range(99, 110):
    args = [
        str(customer_id), customer_name, inital_checking_balance,
        inital_savings_balance
    ]
    response = loop.run_until_complete(
        cli.chaincode_invoke(
            requestor=org1_admin,
            channel_name='mychannel',
            peers=['peer0.org1.example.com'],
            args=args,
            fcn='create_account',
            cc_name='mysmallbank',
            wait_for_event=
            True  # for being sure chaincode invocation has been commited in the ledger, default is on tx event
        ))
    print("peer0.org1")

for customer_id in range(110, 120):
    args = [
        str(customer_id), customer_name, inital_checking_balance,
        inital_savings_balance
    ]
    response = loop.run_until_complete(
        cli.chaincode_invoke(
            requestor=org1_admin,
            channel_name='mychannel',
示例#7
0
                                         peer_names=['peer0.org1.example.com'],
                                         args=args,
                                         cc_name='example_cc',
                                         cc_version='v1.0')
    if response:
        print("Instantiate chaincode successful")
    else:
        print("Instantiate chaincode failed")
        exit(-1)

    # Invoke a chaincode
    args = ['a', 'b', '100']
    # The response should be true if succeed
    response = cli.chaincode_invoke(requestor=org1_admin,
                                    channel_name='businesschannel',
                                    peer_names=['peer0.org1.example.com'],
                                    args=args,
                                    cc_name='example_cc',
                                    cc_version='v1.0')
    print("Invoke chaincode done.")
    print(response)

    # Query a chaincode
    args = ['b']
    # The response should be true if succeed
    response = cli.chaincode_query(requestor=org1_admin,
                                   channel_name='businesschannel',
                                   peer_names=['peer0.org1.example.com'],
                                   args=args,
                                   cc_name='example_cc',
                                   cc_version='v1.0')
    print("Query chaincode done.")
示例#8
0
gopath_bak = os.environ.get('GOPATH', '')
gopath = os.path.normpath(
    os.path.join(os.path.dirname(os.path.realpath('__file__')),
                 '../chaincode'))
os.environ['GOPATH'] = os.path.abspath(gopath)

# Invoke a chaincode
args = ["client", "Qmeq4hW6k34a5dbpE2vc7FjSX1xmn1tphg2hGrHFGxqk16"]
# The response should be true if succeed
response = loop.run_until_complete(
    cli.chaincode_invoke(
        requestor=org1_admin,
        channel_name='modbuschannel',
        peers=['peer0.org1.example.com'],
        args=args,
        cc_name='registration_cc_v2',
        transient_map=None,  # optional, for private data
        # for being sure chaincode invocation has been commited in the ledger, default is on tx event
        wait_for_event=True,
        # cc_pattern='^invoked*' # if you want to wait for chaincode event and you have a `stub.SetEvent("invoked", value)` in your chaincode
    ))

# response = loop.run_until_complete(cli.chaincode_invoke(
#   requestor=org1_admin,
#   channel_name='modbuschannel',
#   peers=['peer1.org1.example.com'],
#   args=args,
#   cc_name='registration_cc_v2',
#   transient_map=None,  # optional, for private data
#   # for being sure chaincode invocation has been commited in the ledger, default is on tx event
#   wait_for_event=True,
示例#9
0
org1_admin = cli.get_user(org_name='org1.dz', name='Admin')
# Make the client know there is a channel in the network
cli.new_channel('firstchannel')

# Invoke the chaincode
args = [
    '10/07/2020 06:09:30',
    '[2kw, 4kw, 18kw, 21kw, 11kw, 10kw, 9kw, 13kw, 4kw, 15kw, 12kw, 8kw]',
    'sd7f6s7f6s7f8as7fs7f8', '[66, 45, 78, 94, 77, 0, 0, 0, 0, 0, 44, 36]',
    'asf7safs6f5s4df5sd4fas'
]

# The response should be true if succeed
response = loop.run_until_complete(
    cli.chaincode_invoke(
        requestor=org1_admin,
        channel_name='firstchannel',
        peers=[
            'peer0.org1.dz', 'peer1.org1.dz', 'peer2.org1.dz', 'peer3.org1.dz'
        ],
        args=args,
        fcn='NewData',
        cc_name='first_chaincode',
        transient_map=None,  # optional, for private data
        wait_for_event=
        True,  # for being sure chaincode invocation has been  commited in the ledger, default is on tx event
    ))

print(response)
示例#10
0

loop = asyncio.get_event_loop()
cli = Client(net_profile="/home/Adel/Desktop/PFE/Two_Chain_Network_Template/pfe-project/sdk/MyNetwork.json")

org1_admin = cli.get_user('org1.dz', 'Admin')  # user = User1 or User2 ...
# Make the client know there is a channel in the network
cli.new_channel('secondchannel')


SMs = ['SM0', 'SM1', 'SM2', 'SM3', 'SM4', 'SM5', 'SM6', 'SM7', 'SM8', 'SM9', 'SM10', 'SM11', 'B1_votes', 'B2_votes', 'hash_reputations']



for key in SMs:

    args = [key]
 #   print(args)
    # The response should be true if succeed
    response = loop.run_until_complete(cli.chaincode_invoke(
         requestor=org1_admin,
         fcn='Delete',
         channel_name='secondchannel',
         peers=['peer0.org1.dz', 'peer1.org1.dz', 'peer2.org1.dz', 'peer3.org1.dz'], # the peer of the leader "not necessarly"
         args=args,
         cc_name='test_cc2',
         transient_map=None, 
         wait_for_event=True,
        ))
#    print(response)
示例#11
0
#             peers=['peer0.org1.example.com'],
#             args=args,
#             fcn='readMarble',
#             cc_name='mymarbles',
#             wait_for_event=True # for being sure chaincode invocation has been commited in the ledger, default is on tx event
#             ))
# print(response)

for i in range(20):
    args = [name + str(i), color, size, owner + str(i)]
    response = loop.run_until_complete(
        cli.chaincode_invoke(
            requestor=org1_admin,
            channel_name='mychannel',
            peers=['peer0.org1.example.com'],
            args=args,
            fcn='initMarble',
            cc_name='mymarbles',
            wait_for_event=
            True  # for being sure chaincode invocation has been commited in the ledger, default is on tx event
        ))
    print("initMarble")
    # print(response)

for i in range(20):
    args = [name + str(i)]
    response = loop.run_until_complete(
        cli.chaincode_invoke(
            requestor=org1_admin,
            channel_name='mychannel',
            peers=['peer0.org1.example.com'],
            args=args,
示例#12
0
# Make the client know there is a channel in the network
cli.new_channel('common')

# Invoke a chaincode
args = []

# The response should be true if succeed
response = loop.run_until_complete(
    cli.chaincode_invoke(
        requestor=user1Nl,
        channel_name='common',
        peers=['peer0.netherlands.nl'],
        args=args,
        cc_name='access-chaincode',
        cc_type=CC_TYPE_NODE,
        wait_for_event_timeout=3000,
        transient_map=None,  # optional, for private data
        wait_for_event=
        True  # for being sure chaincode invocation has been commited in the ledger, default is on tx event
        #cc_pattern='^invoked*' # if you want to wait for chaincode event and you have a `stub.SetEvent("invoked", value)` in your chaincode
    ))
print(response)

# # Query a chaincode
# args = []
# # The response should be true if succeed
# response = loop.run_until_complete(cli.chaincode_query(
#                requestor=user1Nl,
#                channel_name='common',
#                peers=['peer0.netherlands.nl'],
示例#13
0
        transient_map=None,  # optional, for private data
        wait_for_event=
        True,  # optional, for being sure chaincode is → instantiated
    ))

# Invoke the chaincode
args = ["Data2", "value2"]

# The response should be true if succeed
response = loop.run_until_complete(
    cli.chaincode_invoke(
        requestor=org1_admin,
        channel_name="firstchannel",
        peers=["peer0.org1.dz"],
        args=args,
        fcn="NewData",
        cc_name="firstcc",
        transient_map=None,  # optional, for private data
        wait_for_event=
        True,  # for being sure chaincode invocation has been → commited in the ledger, default is on tx event
    ))

response = loop.run_until_complete(
    cli.chaincode_invoke(
        requestor=org1_admin,
        channel_name="secondchannel",
        peers=["peer0.org1.dz"],
        args=args,
        fcn="NewData",
        cc_name="secondcc",
        transient_map=None,  # optional, for private data
示例#14
0
                 '../chaincode'))
os.environ['GOPATH'] = os.path.abspath(gopath)

###### FOR UNKNOWN REASON THIS FAIL WHEN THE QUERY IS INVOKED #####################################################
###### TO SOLVE THE ISSUE THE TRANSACTION IS EMMITED AS INVOKE API, BUT USING GET STATE INSTEAD OF PUT STATE ######

#Query a chaincode
args = ['a']
# The response should be true if succeed
response = loop.run_until_complete(
    cli.chaincode_invoke(
        requestor=org1_admin,
        channel_name='channel1',
        peers=['peer0.org1.example.com'],
        args=args,
        cc_name='usecase_cc',
        transient_map=None,
        fcn='get',
        wait_for_event=True,
        # cc_pattern='^invoked*' # if you want to wait for chaincode event and you have a `stub.SetEvent("invoked", value)` in your chaincode
    ))

###### THIS WAS THE ORIGINAL CODE ######

#   #Query a chaincode
#   args = ['a']
#   # The response should be true if succeed
#   response = loop.run_until_complete(cli.chaincode_query(
#       requestor=org1_admin,
#       channel_name='channel1',
#       peers=['peer0.org1.example.com'],
示例#15
0
org1_admin = cli.get_user('org1.example.com', 'Admin')

customer_id='74'
customer_name='test'
inital_checking_balance='1000'
inital_savings_balance='1000'
amount='1000'
dest_customer_id='55'
source_customer_id=customer_id

args = [dest_customer_id]
response = loop.run_until_complete(cli.chaincode_invoke(
            requestor=org1_admin,
            channel_name='mychannel',
            peers=['peer1.org1.example.com'],
            args=args,
            fcn='query',
            cc_name='mycc',
            wait_for_event=False # for being sure chaincode invocation has been commited in the ledger, default is on tx event
            ))
print("peer1.org1.example.com, queryy")

for customer_id in range(3000, 4000):
    args = [str(customer_id), customer_name, inital_checking_balance, inital_savings_balance]
    response = loop.run_until_complete(cli.chaincode_invoke(
                requestor=org1_admin,
                channel_name='mychannel',
                peers=['peer1.org1.example.com'],
                args=args,
                fcn='create_account',
                cc_name='mysmallbank',
示例#16
0
class Smartmeter():
    '''A class define what each noeud can do in the consenus process'''

    def __init__(self, me):
        '''Initialisation some attributes'''
        self.me = me  # id of smart meter
        self.users = 12  # number of smart meters in the network
        # list_reputation = []
        self.peers = 4  # number of peers (P% best nodes)

    def get_list_from_bloc(self):

        ''' Get the reputation list from the last bloc in the blockchain '''

        self.loop = asyncio.get_event_loop()
        self.cli = Client(
            net_profile="/home/Adel/Desktop/PFE/Two_Chain_Network_Template/pfe-project/sdk/MyNetwork.json")
        self.org1_admin = self.cli.get_user('org1.dz', 'Admin')
        self.cli.new_channel("firstchannel")

        # first get the hash by calling 'query_info'
        self.response = self.loop.run_until_complete(self.cli.query_info(
            requestor=self.org1_admin,
            channel_name='firstchannel',
            peers=['peer0.org1.dz'],
            decode=True
        ))

        self.test_hash = self.response.currentBlockHash

        self.response = self.loop.run_until_complete(self.cli.query_block_by_hash(
            requestor=self.org1_admin,
            channel_name='firstchannel',
            peers=['peer0.org1.dz'],
            block_hash=self.test_hash,
            decode=True
        ))

        self.List_rep_bloc_byte = \
            self.response.get('data').get('data')[0].get('payload').get('data').get('actions')[0].get('payload').get(
                'action').get(
                'proposal_response_payload').get('extension').get('results').get('ns_rwset')[0].get('rwset').get(
                'writes')[
                0].get('value')
        self.List_rep_bloc_str = self.List_rep_bloc_byte.decode("UTF-8")  # convert byte to string
        self.List_rep_bloc_dic = ast.literal_eval(self.List_rep_bloc_str)  # convert dictionary string to dictionary
        
        for self.k, self.v in self.List_rep_bloc_dic.items():
            if self.k == 'Liste des reputations':
                self.List_rep_bloc = ast.literal_eval(self.v) # convert list from str -> list
        #str to int
        #for self.i in range(0, len(self.List_rep_bloc)):
         #   self.List_rep_bloc[self.i] = int(self.List_rep_bloc[self.i])

        return self.List_rep_bloc


    def get_leader_and_p(self, reputation_list_bloc):
        ''' return the leader and the P% nodes based on the reputation list in the last bloc in the blockchain '''
        self.list_reputation_slice = reputation_list_bloc[:]
        self.leader_id = self.list_reputation_slice.index(
            max(self.list_reputation_slice))  # 1er min id of node -leader- (men les P%) that has max reputation.
        self.list_p = []
        self.list_p.append(self.leader_id)
        self.list_reputation_slice[self.leader_id] = 0

        for i in range(0, self.peers - 1):
            self.p_id = self.list_reputation_slice.index(max(
                self.list_reputation_slice))  # p_1 i.e P%_1 - premier noeud du p% - (# 2eme min id of node (men les P%) that has max reputation.)
            self.list_p.append(self.p_id)  # list_p contient le id du leader et les id des P%.
            self.list_reputation_slice[self.p_id] = 0

        return self.list_p

    def send_mesure_to_p(self, user, list_p):
        '''Send data to all P% -normalment to all nodes-'''

        self.loop = asyncio.get_event_loop()

        self.cli = Client(
            net_profile="/home/Adel/Desktop/PFE/Two_Chain_Network_Template/pfe-project/sdk/MyNetwork.json")
        self.org1_admin = self.cli.get_user('org1.dz', user)  # user = User1 or User2 ...
        # Make the client know there is a channel in the network
        self.cli.new_channel('secondchannel')

        self.my_data = str(random.randint(2, 22)) + "kw"  # data consumed by the SM
        # my_reputation = str(list_reputation[Me])   #reputation of this SM.  On envoi pas la reputation.

        for i in range(0, self.peers):
            '''Send data consumed by this SM
                To all the peers in the network (P%)
                car c eux qui font la verification du bloc
                '''

            self.args = ['SM' + str(list_p[i]), str(self.me), str(self.my_data)]

            # The response should be true if succeed
            self.response = self.loop.run_until_complete(
                self.cli.chaincode_invoke(
                    requestor=self.org1_admin,
                    channel_name='secondchannel',
                    peers=['peer' + str(i) + '.org1.dz'],
                    args=self.args,
                    fcn='NewData',
                    cc_name='test_cc2',
                    transient_map=None,  # optional, for private data
                    wait_for_event=True,
                )
            )

    def retrieve_merkle_data(self, user, list_p):
        self.ret = ""
        self.loop = asyncio.get_event_loop()

        self.cli = Client(net_profile="/home/Adel/Desktop/PFE/Two_Chain_Network_Template/pfe-project/sdk/MyNetwork.json")

        self.org1_admin = self.cli.get_user('org1.dz', user)  # user = User1 or User2 ...
        # Make the client know there is a channel in the network
        self.cli.new_channel('secondchannel')

        self.list_pp = list_p[:]
        self.list_data_retrived = []
        #del self.list_pp[0]
        if 1 == 1:
#            self.list_data_retrived = []
            self.args = ['SM' + str(self.list_pp[0])]
            self.lead_me = '0' # l'id du peer qui est relier avec le leader
            for i in range(0, 2):
                # The response should be true if succeed
                self.response = self.loop.run_until_complete(self.cli.chaincode_query(
                    requestor=self.org1_admin,
                    fcn="GetData",
                    channel_name='secondchannel',
                    peers=['peer' + self.lead_me + '.org1.dz'],
                    args=self.args,
                    cc_name='test_cc2'
                ))
                self.response = json.loads(self.response)
                self.list_data_retrived.append(self.response)
                if self.me == self.list_pp[0] or self.me not in self.list_pp:
                    break
                self.args = ['SM' + str(self.me)]
                self.lead_me = str(self.list_pp.index(self.me))  # mon id in str to use it in cli.chaincode_query "peers"


                #self.response = json.loads(self.response)
#
                #self.list_data_retrived.append(self.response)
#
           # print(self.list_data_retrived)
            

            if self.me in self.list_pp and self.me != self.list_pp[0]:
                if (self.list_data_retrived[0]['MerklerootDATA'] == self.list_data_retrived[1]['MerklerootDATA']):
                    self.ret = "true"
                else:
                    self.ret = "false"

        return (self.list_data_retrived[0]['Consommations'], self.list_data_retrived[0]['MerklerootDATA'], self.list_data_retrived[0]['Timestamp'], self.ret)


    def send_vote_b1(self, user, list_p, result_comparaison):
        
        ''' every user from P% send votes about the validation of B1 (merkle root local comparer avec merkle root du leader)'''

        self.list_pp = list_p[:]
        del self.list_pp[0]
        
        if self.me in self.list_pp:

            if result_comparaison == "true":
                self.vote = "accepter"
            elif result_comparaison == "false":
                self.vote = "rejeter"


            self.loop = asyncio.get_event_loop()

            self.cli = Client(net_profile="/home/Adel/Desktop/PFE/Two_Chain_Network_Template/pfe-project/sdk/MyNetwork.json")

            self.org1_admin = self.cli.get_user('org1.dz', user)  # user = User1 or User2 ...
            # Make the client know there is a channel in the network
            self.cli.new_channel('secondchannel')

        
            self.args = ["B1_votes", str(self.me), self.vote]
    
            # The response should be true if succeed
            self.response = self.loop.run_until_complete(self.cli.chaincode_invoke(
                requestor=self.org1_admin,
                fcn='Votes',
                channel_name='secondchannel',
                peers=['peer0.org1.dz'], # the peer of the leader
                args=self.args,
                cc_name='test_cc2',
                transient_map=None, 
                wait_for_event=True,
                ))


    
    def retrieve_vote_b1(self, user):

        '''Every user retrieve the list of validation of B1 that contain the votes of P%, and if B1 is valid or not'''
        
        self.loop = asyncio.get_event_loop()
        self.cli = Client(net_profile="/home/Adel/Desktop/PFE/Two_Chain_Network_Template/pfe-project/sdk/MyNetwork.json")

        self.org1_admin = self.cli.get_user('org1.dz', user)  # user = User1 or User2 ...
        # Make the client know there is a channel in the network
        self.cli.new_channel('secondchannel')

        self.args = ['B1_votes']
                # The response should be true if succeed
        self.response = self.loop.run_until_complete(self.cli.chaincode_query(
                requestor=self.org1_admin,
                fcn='GetVotes',
                channel_name='secondchannel',
                peers=['peer0.org1.dz'],
                args=self.args,
                cc_name='test_cc2'
                ))

        self.response = ast.literal_eval(self.response)  # convert dictionary string to dictionary
        return self.response["List Votes"]  # return List


    def maj_reputations(self, List, List_vote, leader):
        '''mise a jour de la liste des reputations'''
        self.validation_bloc = List_vote[-1]
        self.List_copy = List[:]
        if self.validation_bloc == 'valide':
            self.List_copy [leader] = self.List_copy [leader] + 10
            for self.i in range(0, len(List_vote)):
                if List_vote[self.i] == 'accepter':
                    self.List_copy[self.i] = self.List_copy[self.i] + 10
                elif List_vote[self.i] == 'rejeter':
                    self.List_copy[self.i] = self.List_copy[self.i] - 10

        if self.validation_bloc == 'notvalide':
            self.List_copy [leader] = self.List_copy [leader] - 10
            for self.i in range(0, len(List_vote)):
                if List_vote[self.i] == 'accepter':
                    self.List_copy[self.i] = self.List_copy[self.i] - 10
                elif List_vote[self.i] == 'rejeter':
                    self.List_copy[self.i] = self.List_copy[self.i] + 10

        for self.i in range(0, len(List)):
            if self.List_copy[self.i] > 100:
                self.List_copy[self.i] = 100
            elif self.List_copy[self.i] < 0:
                self.List_copy[self.i] = 0


        return self.List_copy



    def select_new_leader(self, p_leader, validation_b1, liste_vote_b1):
        '''change leader only if the validation of bloc B1 (validation_b1) is = notvalid '''
        self.new_leader = p_leader[0]
        self.p_leader_new = p_leader[:]
        self.boo = 1
        
        if validation_b1 == 'notvalid':
            while self.boo:
                self.ancien_leader = self.p_leader_new.pop(0)
                self.p_leader_new.append(self.ancien_leader)
                self.new_leader = self.p_leader_new[0]
                if list_vote_b1[self.new_leader] == 'rejeter':
                    self.boo = 0

        return (self.p_leader_new, self.new_leader)



    def calculate_merkle_reputations(self, p_leader_new, list_rep_updated):
        '''Calculer le merkle root de la liste des reputations par chaque noeud des P%'''
        self.liste_maj_rep = list_rep_updated[:]
        self.mt = MerkleTools(hash_type="sha256")
        for self.i in range(0, len(self.liste_maj_rep)):
            self.liste_maj_rep[self.i] = str(self.liste_maj_rep[self.i])
        self.mt.add_leaf(self.liste_maj_rep, True)
        self.mt.make_tree()
        self.is_ready = self.mt.is_ready
       	self.root_value = self.mt.get_merkle_root()
        return self.root_value



    def send_maj_reputations(self, user, p_leader, p_leader_new, liste_maj_rep_hash):
        # mypeer is the peer of the user(node) from the updated list p_leader "p_leader_new".
        '''send the new reputations list to the actual peer of the leader in order to make a merkle root for it'''
        if self.me == p_leader_new[0]:
            self.loop = asyncio.get_event_loop()

            self.cli = Client(net_profile="/home/Adel/Desktop/PFE/Two_Chain_Network_Template/pfe-project/sdk/MyNetwork.json")

            self.org1_admin = self.cli.get_user('org1.dz', user)  # user = User1 or User2 ...
            # Make the client know there is a channel in the network
            self.cli.new_channel('secondchannel')

            self.mypeer = p_leader.index(p_leader_new[0])
            self.args = ["hash_reputations", liste_maj_rep_hash]

            # The response should be true if succeed
            self.response = self.loop.run_until_complete(self.cli.chaincode_invoke(
                requestor=self.org1_admin,
                fcn='NewRep',
                channel_name='secondchannel',
                peers=['peer'+str(self.mypeer)+'.org1.dz'], # the peer of the leader
                args=self.args,
                cc_name='test_cc2',
                transient_map=None,
                wait_for_event=True,
                ))
        

    def retrieve_merkle_reputations(self, user, p_leader, p_leader_new, local_hash_rep): 
        # l'id du peer qui est relier avec le leader
        '''recuperer le merkle root de la liste de reputations a partir du peer du leader'''
        self.result_comparaison2 = ""
        self.loop = asyncio.get_event_loop()

        self.cli = Client(net_profile="/home/Adel/Desktop/PFE/Two_Chain_Network_Template/pfe-project/sdk/MyNetwork.json")

        self.org1_admin = self.cli.get_user('org1.dz', user)  # user = User1 or User2 ...
        # Make the client know there is a channel in the network
        self.cli.new_channel('secondchannel')
        self.list_pp = p_leader_new[:]
        del self.list_pp[0]
        if self.me in self.list_pp:
            self.list_data_retrived = []
            self.args = ["hash_reputations"]

            self.peer_leader = p_leader.index(p_leader_new[0])
            # The response should be true if succeed
            self.response = self.loop.run_until_complete(self.cli.chaincode_query(
                requestor=self.org1_admin,
                fcn='GetRep',
                channel_name='secondchannel',
                peers=['peer' + str(self.peer_leader) + '.org1.dz'],
                args=self.args,
                cc_name='test_cc2'
                ))

            #self.lead_me = str(self.list_pp.index(self.me) + 1)  # mon id in str to use it in cli.chaincode_query "peers"


            self.response = json.loads(self.response)
            #print(type(self.response))
            #self.list_data_retrived.append(self.response)
#
#            print(self.list_data_retrived)
            if self.response['MerklerootReputations'] == local_hash_rep:
            	self.result_comparaison2 = "true"
            else:
            	self.result_comparaison2 = "false"
        
        return self.result_comparaison2



    def send_vote_b2(self, user, list_p, result_comparaison):
        
        ''' every user from P% send votes about the validation of B2 (merkle root local comparer avec merkle root du leader)'''

        self.list_pp = list_p[:]
        del self.list_pp[0]
        
        if self.me in self.list_pp:

            if result_comparaison == "true":
                self.vote = "accepter"
            elif result_comparaison == "false":
                self.vote = "rejeter"


            self.loop = asyncio.get_event_loop()

            self.cli = Client(net_profile="/home/Adel/Desktop/PFE/Two_Chain_Network_Template/pfe-project/sdk/MyNetwork.json")

            self.org1_admin = self.cli.get_user('org1.dz', user)  # user = User1 or User2 ...
            # Make the client know there is a channel in the network
            self.cli.new_channel('secondchannel')

        
            self.args = ["B2_votes", str(self.me), self.vote]
    
            # The response should be true if succeed
            self.response = self.loop.run_until_complete(self.cli.chaincode_invoke(
                requestor=self.org1_admin,
                fcn='Votes',
                channel_name='secondchannel',
                peers=['peer0.org1.dz'], # the peer of the leader "not necessarly"
                args=self.args,
                cc_name='test_cc2',
                transient_map=None, 
                wait_for_event=True,
                ))


    def retrieve_vote_b2(self, user):

        '''Every user retrieve the list of validation of B2 that contain the votes of P%, and if B1 is valid or not'''
        
        self.loop = asyncio.get_event_loop()
        self.cli = Client(net_profile="/home/Adel/Desktop/PFE/Two_Chain_Network_Template/pfe-project/sdk/MyNetwork.json")

        self.org1_admin = self.cli.get_user('org1.dz', user)  # user = User1 or User2 ...
        # Make the client know there is a channel in the network
        self.cli.new_channel('secondchannel')

        self.args = ["B2_votes"]
                # The response should be true if succeed
        self.response = self.loop.run_until_complete(self.cli.chaincode_query(
                requestor=self.org1_admin,
                fcn='GetVotes',
                channel_name='secondchannel',
                peers=['peer0.org1.dz'],
                args=self.args,
                cc_name='test_cc2',
                ))

        self.response = ast.literal_eval(self.response)  # convert dictionary string to dictionary
        return self.response["List Votes"]  # return List




    def validation_b2(self,user, state_b2, state_b1, p_leader_new, time, data, mr_data, list_reputations, mr_reputations):
        ''' '''
        self.value = ""
        self.k = 0
        if state_b2 == 'notvalide':
            self.value = 'notvalide'
        elif state_b2 == 'valide' and state_b1 == 'valide':
            self.value = 'valide'
            self.k = 1
        elif state_b2 == 'valide' and state_b1 == 'notvalide':
            self.value = 'valide'
            self.k = 1
            for self.i in range(0, len(data)):
                data[self.i] = "0" 
            self.mt = MerkleTools(hash_type="sha256")
            self.mt.add_leaf(data, True)
            self.mt.make_tree()
            mr_data = self.mt.get_merkle_root()
        
        #print(self.value)
        #print(p_leader_new[0])
        if self.k == 1 and self.me == p_leader_new[0]:
            #print(self.value)
            #print(p_leader_new[0])
            self.loop = asyncio.get_event_loop()
            self.cli = Client(net_profile="/home/Adel/Desktop/PFE/Two_Chain_Network_Template/pfe-project/sdk/MyNetwork.json")
            self.org1_admin = self.cli.get_user('org1.dz', user)  # user = User1 or User2 ...
            # Make the client know there is a channel in the network
            self.cli.new_channel('firstchannel')
            self.args = [str(time), str(data), str(mr_data), str(list_reputations), str(mr_reputations)]
    
           # The response should be true if succeed
            self.response = self.loop.run_until_complete(self.cli.chaincode_invoke(
               	requestor=self.org1_admin,
               	fcn='NewData',
               	channel_name='firstchannel',
               	peers=['peer0.org1.dz'], 
               	args=self.args,
               	cc_name='first_chaincode',
               	transient_map=None, 
               	wait_for_event=True,
	                ))
            #print(self.response)
            #exit()
            
            return self.value
示例#17
0
    # The instantiate command after all is a transaction which has to be endorsed, ordered and committed.
    # What probably confuses you is the fact that peer cli succeeded to endorse instantiate transaction and submit it to the ordering service.
    # While it might not be committed, hence chaincode container not started and as a result you are not able to query your chaincode.
    print(
        ">--- WAITING FOR chaincode container to start after Instantiate! ---<"
    )
    time.sleep(15)
    print(">--- End waiting ---<")

    # Invoke a chaincode
    args = ['a', 'b', '100']
    # The response should be true if succeed
    response = loop.run_until_complete(
        cli.chaincode_invoke(requestor=org1_admin,
                             channel_name='businesschannel',
                             peers=['peer0.org1.example.com'],
                             args=args,
                             cc_name='example_cc'))
    print("Invoke chaincode done.")
    print(response)

    # Query a chaincode
    args = ['b']
    # The response should be true if succeed
    response = loop.run_until_complete(
        cli.chaincode_query(requestor=org1_admin,
                            channel_name='businesschannel',
                            peers=['peer0.org1.example.com'],
                            args=args,
                            cc_name='example_cc'))
    print("Query chaincode done.")
示例#18
0
"""
# This part is used to check the channel information
response = loop.run_until_complete(cli.query_peers(
               requestor=org1_admin,
               peer='peer0.org1.example.com',
               channel='mychannel',
               local=True,
               decode=True
               ))
print("response=", response)
"""

for i in range(400000000, 5000000000):
    time_1 = time.time()
    args = [str(i), "red", "202", "Jack"]
    response = loop.run_until_complete(cli.chaincode_invoke(
                   requestor=org1_admin,
                   channel_name='mychannel',
                   peers=['peer0.org1.example.com', 'peer0.org2.example.com'],
                   args=args,
                   cc_name='marbles',
                   fcn='initMarble',
                   transient_map=None, # optional, for private data
                   wait_for_event=False, # for being sure chaincode invocation has been commited in the ledger, default is on tx event
                   #cc_pattern='^invoked*' # if you want to wait for chaincode event and you have a `stub.SetEvent("invoked", value)` in your chaincode
                   ))
    time_2 = time.time() - time_1
    print("throughput= ", 1/time_2)

#print("response=", response)