LOGFILE = '/tmp/lightchain-shell.log' LOGLEVEL = logging.INFO parser = argparse.ArgumentParser() parser.add_argument('-db', type=str, required=True) parser.add_argument('-debug', action='store_true') args = parser.parse_args() print("Logging to", LOGFILE) if args.debug: LOGLEVEL = logging.DEBUG logging.basicConfig(level=LOGLEVEL, filename=LOGFILE) DemoLightChain = LightChain.configure( name='Demo LightChain', privkey=ecies.generate_privkey(), vm_configuration=MAINNET_VM_CONFIGURATION, network_id=ROPSTEN_NETWORK_ID, ) chaindb = BaseChainDB(LevelDB(args.db)) try: chaindb.get_canonical_head() except CanonicalHeadNotFound: # We're starting with a fresh DB. chain = DemoLightChain.from_genesis_header(chaindb, ROPSTEN_GENESIS_HEADER) else: # We're reusing an existing db. chain = DemoLightChain(chaindb) loop = asyncio.get_event_loop() t = threading.Thread(target=loop.run_until_complete,
LOGFILE = '/tmp/lightchain-shell.log' LOGLEVEL = logging.INFO parser = argparse.ArgumentParser() parser.add_argument('-db', type=str, required=True) parser.add_argument('-debug', action='store_true') args = parser.parse_args() print("Logging to", LOGFILE) if args.debug: LOGLEVEL = logging.DEBUG logging.basicConfig(level=LOGLEVEL, filename=LOGFILE) DemoLightChain = LightChain.configure( name='Demo LightChain', vm_configuration=MAINNET_VM_CONFIGURATION, network_id=ROPSTEN_NETWORK_ID, ) chaindb = ChainDB(LevelDB(args.db)) peer_pool = PeerPool(LESPeer, chaindb, ROPSTEN_NETWORK_ID, ecies.generate_privkey()) try: chaindb.get_canonical_head() except CanonicalHeadNotFound: # We're starting with a fresh DB. chain = DemoLightChain.from_genesis_header(chaindb, ROPSTEN_GENESIS_HEADER, peer_pool) else: # We're reusing an existing db. chain = DemoLightChain(chaindb, peer_pool)
class MockPeerPool: def __init__(self, *args, **kwargs): pass async def run(self): pass async def stop(self): pass LightChainForTests = LightChain.configure( 'LightChainForTests', vm_configuration=MAINNET_VM_CONFIGURATION, network_id=MAINNET_NETWORK_ID, peer_pool_class=MockPeerPool, ) def assert_canonical_chains_are_equal(chaindb1, chaindb2, block_number=None): """Assert that the canonical chains in both DBs are identical up to block_number.""" if block_number is None: block_number = chaindb1.get_canonical_head().block_number assert block_number == chaindb2.get_canonical_head().block_number for i in range(0, block_number + 1): assert chaindb1.get_canonical_block_header_by_number(i) == ( chaindb2.get_canonical_block_header_by_number(i)) @pytest.fixture
logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s') logging.getLogger("evm.p2p.lightchain").setLevel(logging.DEBUG) parser = argparse.ArgumentParser() parser.add_argument('-db', type=str, required=True) parser.add_argument('-mainnet', action="store_true") args = parser.parse_args() GENESIS_HEADER = ROPSTEN_GENESIS_HEADER NETWORK_ID = ROPSTEN_NETWORK_ID if args.mainnet: GENESIS_HEADER = MAINNET_GENESIS_HEADER NETWORK_ID = MAINNET_NETWORK_ID DemoLightChain = LightChain.configure( 'RPCDemoLightChain', vm_configuration=MAINNET_VM_CONFIGURATION, network_id=NETWORK_ID, privkey=ecies.generate_privkey(), ) chaindb = ChainDB(LevelDB(args.db)) try: chaindb.get_canonical_head() except CanonicalHeadNotFound: # We're starting with a fresh DB. chain = DemoLightChain.from_genesis_header(chaindb, GENESIS_HEADER) else: # We're reusing an existing db. chain = DemoLightChain(chaindb) app = App(chain) web.run_app(app, port=8080)
from evm.chains.ropsten import ROPSTEN_NETWORK_ID, ROPSTEN_GENESIS_HEADER from evm.chains.mainnet import MAINNET_VM_CONFIGURATION from evm.db.backends.memory import MemoryDB from evm.db.chain import ChainDB from evm.utils.keccak import keccak from evm.vm.forks.frontier import FrontierBlock from evm.p2p import ecies from evm.p2p.integration_test_helpers import LocalGethPeerPool from evm.p2p.lightchain import LightChain from evm.p2p.peer import LESPeer IntegrationTestLightChain = LightChain.configure( name='IntegrationTest LightChain', vm_configuration=MAINNET_VM_CONFIGURATION, network_id=ROPSTEN_NETWORK_ID, max_consecutive_timeouts=1, ) @pytest.mark.asyncio async def test_lightchain_integration(request, event_loop): """Test LightChain against a local geth instance. This test assumes a geth/ropsten instance is listening on 127.0.0.1:30303 and serving light clients. In order to achieve that, simply run it with the following command line: $ geth -nodekeyhex 45a915e4d060149eb4365960e6a7a45f334393093061116b197e3240065ff2d8 \ -testnet -lightserv 90 """ # TODO: Implement a pytest fixture that runs geth as above, so that we don't need to run it
async def get_nodes_to_connect(self): nodekey = keys.PrivateKey( decode_hex( "45a915e4d060149eb4365960e6a7a45f334393093061116b197e3240065ff2d8" )) remoteid = nodekey.public_key.to_hex() return [ kademlia.Node(keys.PublicKey(decode_hex(remoteid)), kademlia.Address('127.0.0.1', 30303, 30303)) ] IntegrationTestLightChain = LightChain.configure( name='IntegrationTest LightChain', privkey=ecies.generate_privkey(), vm_configuration=MAINNET_VM_CONFIGURATION, network_id=ROPSTEN_NETWORK_ID, peer_pool_class=LocalGethPeerPool, max_consecutive_timeouts=1, ) @pytest.mark.asyncio async def test_lightchain_integration(request, event_loop): """Test LightChain against a local geth instance. This test assumes a geth/ropsten instance is listening on 127.0.0.1:30303 and serving light clients. In order to achieve that, simply run it with the following command line: $ geth -nodekeyhex 45a915e4d060149eb4365960e6a7a45f334393093061116b197e3240065ff2d8 \ -testnet -lightserv 90 """
def __init__(self, *args, **kwargs): pass def subscribe(self, subscriber): pass async def run(self): pass async def stop(self): pass LightChainForTests = LightChain.configure( 'LightChainForTests', vm_configuration=MAINNET_VM_CONFIGURATION, network_id=MAINNET_NETWORK_ID, ) def assert_canonical_chains_are_equal(chaindb1, chaindb2, block_number=None): """Assert that the canonical chains in both DBs are identical up to block_number.""" if block_number is None: block_number = chaindb1.get_canonical_head().block_number assert block_number == chaindb2.get_canonical_head().block_number for i in range(0, block_number + 1): assert chaindb1.get_canonical_block_header_by_number(i) == ( chaindb2.get_canonical_block_header_by_number(i)) @pytest.fixture
"receiptsRoot": encode_hex(block.header.receipt_root), "sha3Uncles": encode_hex(block.header.uncles_hash), "stateRoot": encode_hex(block.header.state_root), "timestamp": hex(block.header.timestamp), "totalDifficulty": hex(self.chain.chaindb.get_score(block.hash)), "transactions": [encode_hex(tx.hash) for tx in block.transactions], "transactionsRoot": encode_hex(block.header.transaction_root), "uncles": [encode_hex(uncle.hash) for uncle in block.uncles], "size": hex(len(rlp.encode(block))), "miner": encode_hex(block.header.coinbase), } DemoLightChain = LightChain.configure( 'RPCDemoLightChain', vm_configuration=MAINNET_VM_CONFIGURATION, network_id=NETWORK_ID, ) if __name__ == '__main__': import argparse from evm.db.backends.level import LevelDB from evm.db.chain import BaseChainDB from evm.exceptions import CanonicalHeadNotFound logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s') logging.getLogger("evm.p2p.lightchain").setLevel(logging.DEBUG) parser = argparse.ArgumentParser() parser.add_argument('-db', type=str, required=True) args = parser.parse_args()