示例#1
0
    def update_state(self, block):
        self.driver.clear_pending_state()

        # Check if the block is valid
        if self.should_process(block):
            self.log.info('Storing new block.')
            # Commit the state changes and nonces to the database
            storage.update_state_with_block(
                block=block,
                driver=self.driver,
                nonces=self.nonces
            )

            self.log.info('Issuing rewards.')
            # Calculate and issue the rewards for the governance nodes
            self.reward_manager.issue_rewards(
                block=block,
                client=self.client
            )

        self.log.info('Updating metadata.')
        self.current_height = storage.get_latest_block_height(self.driver)
        self.current_hash = storage.get_latest_block_hash(self.driver)

        self.new_block_processor.clean(self.current_height)
示例#2
0
    def test_update_with_block_sets_hash_and_height(self):
        _hash = storage.get_latest_block_hash(self.driver)
        num = storage.get_latest_block_height(self.driver)

        self.assertEqual(_hash, '0' * 64)
        self.assertEqual(num, 0)

        storage.update_state_with_block(block=block,
                                        driver=self.driver,
                                        nonces=self.nonces)

        _hash = storage.get_latest_block_hash(self.driver)
        num = storage.get_latest_block_height(self.driver)

        self.assertEqual(_hash, 'f' * 64)
        self.assertEqual(num, 555)
示例#3
0
    def test_generate_environment_creates_block_hash(self):
        timestamp = time.time()

        exe = execution.SerialExecutor(executor=self.client.executor)

        e = exe.generate_environment(self.client.raw_driver, timestamp, 'A' * 64)

        self.assertEqual(e['block_hash'], storage.get_latest_block_hash(self.client.raw_driver))
示例#4
0
    def test_process_new_block_updates_state(self):
        block = canonical.block_from_subblocks(subblocks=[],
                                               previous_hash='0' * 64,
                                               block_num=1)

        driver = ContractDriver(driver=InMemDriver())
        node = base.Node(socket_base='tcp://127.0.0.1:18002',
                         ctx=self.ctx,
                         wallet=Wallet(),
                         constitution={
                             'masternodes': [Wallet().verifying_key],
                             'delegates': [Wallet().verifying_key]
                         },
                         driver=driver)

        node.process_new_block(block)

        self.assertEqual(storage.get_latest_block_height(node.driver), 1)
        self.assertEqual(storage.get_latest_block_hash(node.driver),
                         block['hash'])
示例#5
0
文件: base.py 项目: Lamden/lamden
    def __init__(self,
                 socket_base,
                 ctx: zmq.asyncio.Context,
                 wallet,
                 constitution: dict,
                 bootnodes={},
                 blocks=storage.BlockStorage(),
                 driver=ContractDriver(),
                 debug=True,
                 store=False,
                 seed=None,
                 bypass_catchup=False,
                 node_type=None,
                 genesis_path=lamden.contracts.__path__[0],
                 reward_manager=rewards.RewardManager(),
                 nonces=storage.NonceStorage()):

        self.driver = driver
        self.nonces = nonces
        self.store = store

        self.seed = seed

        self.blocks = blocks
        self.event_writer = EventWriter()

        self.log = get_logger('Base')
        self.log.propagate = debug
        self.socket_base = socket_base
        self.wallet = wallet
        self.ctx = ctx

        self.genesis_path = genesis_path

        self.client = ContractingClient(driver=self.driver,
                                        submission_filename=genesis_path +
                                        '/submission.s.py')

        self.bootnodes = bootnodes
        self.constitution = constitution

        self.seed_genesis_contracts()

        self.socket_authenticator = authentication.SocketAuthenticator(
            bootnodes=self.bootnodes, ctx=self.ctx, client=self.client)

        self.upgrade_manager = upgrade.UpgradeManager(client=self.client,
                                                      wallet=self.wallet,
                                                      node_type=node_type)

        self.router = router.Router(socket_id=socket_base,
                                    ctx=self.ctx,
                                    wallet=wallet,
                                    secure=True)

        self.network = network.Network(wallet=wallet,
                                       ip_string=socket_base,
                                       ctx=self.ctx,
                                       router=self.router)

        self.new_block_processor = NewBlock(driver=self.driver)
        self.router.add_service(
            NEW_BLOCK_SERVICE,
            self.new_block_processor)  # Add this after catch up?

        self.running = False
        self.upgrade = False

        self.reward_manager = reward_manager

        self.current_height = storage.get_latest_block_height(self.driver)
        self.current_hash = storage.get_latest_block_hash(self.driver)

        self.bypass_catchup = bypass_catchup
示例#6
0
 async def get_latest_block_hash(self, request):
     return response.json(
         {'latest_block_hash': storage.get_latest_block_hash(self.driver)},
         headers={'Access-Control-Allow-Origin': '*'})
示例#7
0
 def test_get_latest_block_hash_correct_after_set(self):
     storage.set_latest_block_hash('a' * 64, self.driver)
     h = storage.get_latest_block_hash(self.driver)
     self.assertEqual(h, 'a' * 64)
示例#8
0
 def test_get_latest_block_hash_0s_if_none(self):
     h = storage.get_latest_block_hash(self.driver)
     self.assertEqual(h, '0' * 64)
示例#9
0
    def test_start_boots_up_normally(self):
        # This MN will also provide 'catch up' services

        mn_bootnode = 'tcp://127.0.0.1:18001'
        mn_wallet = Wallet()
        mn_router = router.Router(socket_id=mn_bootnode,
                                  ctx=self.ctx,
                                  secure=True,
                                  wallet=mn_wallet)

        mn_network = network.Network(wallet=mn_wallet,
                                     ip_string=mn_bootnode,
                                     ctx=self.ctx,
                                     router=mn_router)

        blocks = generate_blocks(4)

        self.blocks.store_block(blocks[0])
        self.blocks.store_block(blocks[1])
        self.blocks.store_block(blocks[2])

        storage.set_latest_block_height(3, self.driver)

        mn_router.add_service(
            base.BLOCK_SERVICE,
            masternode.BlockService(self.blocks, self.driver))

        dl_bootnode = 'tcp://127.0.0.1:18002'
        dl_wallet = Wallet()
        dl_router = router.Router(socket_id=dl_bootnode,
                                  ctx=self.ctx,
                                  secure=True,
                                  wallet=dl_wallet)
        dl_network = network.Network(wallet=dl_wallet,
                                     ip_string=dl_bootnode,
                                     ctx=self.ctx,
                                     router=dl_router)

        constitution = {
            'masternodes': [mn_wallet.verifying_key],
            'delegates': [dl_wallet.verifying_key]
        }

        bootnodes = {
            mn_wallet.verifying_key: mn_bootnode,
            dl_wallet.verifying_key: dl_bootnode
        }

        node_w = Wallet()
        driver = ContractDriver(driver=InMemDriver())
        node = base.Node(socket_base='tcp://127.0.0.1:18003',
                         ctx=self.ctx,
                         wallet=node_w,
                         constitution=constitution,
                         driver=driver,
                         store=False,
                         bootnodes=bootnodes)

        self.authenticator.add_verifying_key(mn_wallet.verifying_key)
        self.authenticator.add_verifying_key(dl_wallet.verifying_key)
        self.authenticator.add_verifying_key(node_w.verifying_key)

        self.authenticator.configure()

        vks = [mn_wallet.verifying_key, dl_wallet.verifying_key]

        tasks = asyncio.gather(
            mn_router.serve(),
            dl_router.serve(),
            mn_network.start(bootnodes, vks),
            dl_network.start(bootnodes, vks),
            stop_server(mn_router, 0.2),
            stop_server(dl_router, 0.2),
        )

        self.loop.run_until_complete(tasks)

        tasks = asyncio.gather(mn_router.serve(), dl_router.serve(),
                               node.start(), stop_server(mn_router, 1),
                               stop_server(dl_router, 1),
                               stop_server(node.router, 1))

        self.loop.run_until_complete(tasks)

        self.assertEqual(storage.get_latest_block_height(node.driver), 3)
        self.assertEqual(storage.get_latest_block_hash(node.driver),
                         blocks[2]['hash'])