예제 #1
0
파일: Main.py 프로젝트: wunder3605/BlockSim
def main():
    for i in range(p.Runs):

        from Models.Trias.Node import Node
        p.NODES = []
        for i in range(p.Nn):
            p.NODES.append(Node(id=i, hashPower=100 / p.Nn))

        clock = 0  # set clock to 0 at the start of the simulation
        if p.hasTrans:
            if p.Ttechnique == "Light":
                LT.create_transactions()  # generate pending transactions
            elif p.Ttechnique == "Full":
                FT.create_transactions()  # generate pending transactions

        Node.generate_gensis_block(
            p.NODES)  # generate the gensis block for all miners
        BlockCommit.generate_initial_events(
        )  # initiate initial events >= 1 to start with

        if p.attack:
            Security.generate_initial_events()

        while not Queue.isEmpty() and clock <= p.simTime:
            next_event = Queue.pop_event()
            clock = next_event.time  # move clock to the time of the event
            BlockCommit.handle_event(next_event)

        Output.output_to_xlsx("data_v5.xlsx")
        # Output.calculate()
        Output.reset()
예제 #2
0
def main():
    for i in range (p.Runs):
        clock =0 # set clock to 0 at the start of the simulation
        if p.hasTrans:
            if p.Ttechnique == "Light": LT.create_transactions() # generate pending transactions
            elif p.Ttechnique == "Full": FT.create_transactions() # generate pending transactions

        Node.generate_gensis_block() # generate the gensis block for all miners
        BlockCommit.generate_initial_events() # initiate initial events >= 1 to start with

        while  not Queue.isEmpty() and clock <= p.simTime:
            next_event = Queue.get_next_event()
            clock = next_event.time # move clock to the time of the event
            BlockCommit.handle_event(next_event)
            Queue.remove_event(next_event)

        Consensus.fork_resolution() # apply the longest chain to resolve the forks
        Incentives.distribute_rewards()# distribute the rewards between the particiapting nodes
        Statistics.calculate() # calculate the simulation results (e.g., block statstics and miners' rewards)

		########## reset all global variable before the next run #############
        Statistics.reset() # reset all variables used to calculate the results
        Node.resetState() # reset all the states (blockchains) for all nodes in the network
    fname = os.getenv('OUTPUT', "(Allverify)1day_{0}M_{1}K".format(
    	p.Bsize/1000000, p.Tn/1000))+".xlsx"
    Statistics.print_to_excel(fname) # print all the simulation results in an excel file
    Statistics.reset2() # reset profit results
예제 #3
0
 def receive_tx_list_event(txList, gatewayId, tokenTime, eventTime):
     eventType = "receive_tx_list"
     if eventTime <= p.simTime:
         block = AB()
         block.transactions = txList.copy()
         block.timestamp = tokenTime
         event = Event(eventType, gatewayId, eventTime, block)
         Queue.add_event(event)
예제 #4
0
파일: Main.py 프로젝트: michaeljyt/BlockSim
def main():
    for i in range(p.Runs):
        clock = 0  # set clock to 0 at the start of the simulation
        if p.hasTrans:
            if p.Ttechnique == "Light":
                LT.create_transactions()  # generate pending transactions
            elif p.Ttechnique == "Full":
                FT.create_transactions()  # generate pending transactions

        Node.generate_gensis_block(
        )  # generate the gensis block for all miners
        # initiate initial events >= 1 to start with
        BlockCommit.generate_initial_events()

        while not Queue.isEmpty() and clock <= p.simTime:
            next_event = Queue.get_next_event()
            clock = next_event.time  # move clock to the time of the event
            BlockCommit.handle_event(next_event)
            Queue.remove_event(next_event)

        # for the AppendableBlock process transactions and
        # optionally verify the model implementation
        if p.model == 3:
            BlockCommit.process_gateway_transaction_pools()

            if i == 0 and p.VerifyImplemetation:
                Verification.perform_checks()

        Consensus.fork_resolution(
        )  # apply the longest chain to resolve the forks
        # distribute the rewards between the particiapting nodes
        Incentives.distribute_rewards()
        # calculate the simulation results (e.g., block statstics and miners' rewards)
        Statistics.calculate()

        if p.model == 3:
            Statistics.print_to_excel(i, True)
            Statistics.reset()
        else:
            ########## reset all global variable before the next run #############
            Statistics.reset(
            )  # reset all variables used to calculate the results
            Node.resetState(
            )  # reset all the states (blockchains) for all nodes in the network
            fname = "(Allverify)1day_{0}M_{1}K.xlsx".format(
                p.Bsize / 1000000, p.Tn / 1000)
            # print all the simulation results in an excel file
            Statistics.print_to_excel(fname)
            fname = "(Allverify)1day_{0}M_{1}K.xlsx".format(
                p.Bsize / 1000000, p.Tn / 1000)
            # print all the simulation results in an excel file
            Statistics.print_to_excel(fname)
            Statistics.reset2()  # reset profit results
예제 #5
0
 def create_block_event_AB(node, eventTime, receiverGatewayId):
     eventType = "create_block"
     if eventTime <= p.simTime:
         # Populate event attributes
         block = AB()
         block.id = random.randrange(100000000000)
         block.timestamp = eventTime
         block.nodeId = node.id
         block.gatewayIds = node.gatewayIds
         block.receiverGatewayId = receiverGatewayId
         event = Event(eventType, node.id, eventTime, block)
         Queue.add_event(event)  # add the event to the queue
예제 #6
0
    def create_block_event(miner, eventTime):
        eventType = "create_block"
        if eventTime <= p.simTime:
            # prepare attributes for the event
            block = Block()
            block.miner = miner.id
            block.depth = len(miner.blockchain)
            block.id = random.randrange(100000000000)
            block.previous = miner.last_block().id
            block.timestamp = eventTime

            event = Event(eventType, block.miner, eventTime,
                          block)  # create the event
            Queue.add_event(event)  # add the event to the queue
예제 #7
0
    def create_block_event(miner, eventTime):
        eventType = "create_block"
        if eventTime <= p.simTime:
            # prepare attributes for the event
            block = Block()
            block.miner = miner.id
            block.id = random.randrange(100000000000)
            block.timestamp = eventTime
            if p.model == 3:
                virtual_block = miner.next_unfinished_virtual_block()
                block.depth = virtual_block.depth
                block.branch_id = virtual_block.next_branch_id()
                miner.mining_branch = block
            else:
                block.depth = len(miner.blockchain)
                block.previous = miner.last_block().id

            event = Event(eventType, block.miner, eventTime,
                          block)  # create the event
            Queue.add_event(event)  # add the event to the queue
예제 #8
0
 def receive_block_event(recipient, block, event_time):
     if event_time <= p.simTime:
         e = Event("receive_block", recipient.id, event_time, block)
         Queue.add_event(e)
예제 #9
0
 def receive_block_event(recipient, block, blockDelay):
     receive_block_time = block.timestamp + blockDelay
     if receive_block_time <= p.simTime:
         e = Event("receive_block", recipient.id, receive_block_time, block)
         Queue.add_event(e)
예제 #10
0
 def process_queue():
     while not Queue.isEmpty():
         next_event = Queue.get_next_event()
         BlockCommit.handle_event(next_event)
         Queue.remove_event(next_event)
예제 #11
0
파일: Main.py 프로젝트: mannmann2/BlockSim
def main():
    for i in range(p.Runs):
        print('-' * 10, f'Run: {i+1}', '-' * 10)
        print(p.sim_type)
        print('No. of Miners:', len(p.NODES))

        hash_power = 0
        # Giving every pool a reference to the nodes it contains. Also, update the total hashrate of a pool.
        print('SOLO Nodes: ', end='')
        for node in p.NODES:
            hash_power += node.hashPower
            if node.pool:
                node.pool.nodes.append(node)
                node.pool.hash_power += node.hashPower
            else:
                print(node.id, end=', ')
        print()

        print('Pools:')
        for pool in p.POOLS:
            print(' -', pool.id, pool.strategy, 'Fee Rate:', pool.fee_rate,
                  'Nodes:', [node.id for node in pool.nodes], 'Hash power:',
                  pool.hash_power)
        print('Total hash power:', hash_power, '\n')

        clock = 0  # set clock to 0 at the start of the simulation
        if p.hasTrans:
            if p.Ttechnique == "Light":
                LT.create_transactions()  # generate pending transactions
            elif p.Ttechnique == "Full":
                FT.create_transactions()  # generate pending transactions

        Node.generate_gensis_block(
        )  # generate the gensis block for all miners
        # initiate initial events >= 1 to start with
        BlockCommit.generate_initial_events()

        while not Queue.isEmpty() and clock <= p.simTime:
            next_event = Queue.get_next_event()
            clock = next_event.time  # move clock to the time of the event
            BlockCommit.handle_event(next_event)
            Queue.remove_event(next_event)

        # for the AppendableBlock process transactions and
        # optionally verify the model implementation
        if p.model == 3:
            BlockCommit.process_gateway_transaction_pools()

            if i == 0 and p.VerifyImplemetation:
                Verification.perform_checks()

        Consensus.fork_resolution(
        )  # apply the longest chain to resolve the forks
        # distribute the rewards between the particiapting nodes
        Incentives.distribute_rewards()
        # calculate the simulation results (e.g., block statstics and miners' rewards)
        Statistics.calculate(i)

        if p.model == 3:
            Statistics.print_to_excel(i, True)
            Statistics.reset()
        else:
            ########## reset all global variable before the next run #############
            Statistics.reset(
            )  # reset all variables used to calculate the results
            Node.resetState(
            )  # reset all the states (blockchains) for all nodes in the network
            Pool.resetState()  # reset all pools in the network

    # set file name for results
    fname = f"{p.sim_type}_{int(p.simTime/(24*60*60))}days_{datetime.now()}.xlsx".replace(
        ':', '_')
    # fname = f"(Allverify)1day_{p.Bsize/1000000}M_{p.Tn/1000}K-{i}-{datetime.now()}.xlsx".replace(':', '_')
    # print all the simulation results in an excel file
    Statistics.print_to_excel(fname)
예제 #12
0
 def create_attack_event(node_num, event_time):
     event_type = "attack_node"
     event = Event(event_type, node_num, event_time, None) 
     Queue.add_event(event)