def test_state_sync(): raw_db, state_root, contents = make_random_state(1000) dest_db = MemoryDB() scheduler = StateSync(state_root, dest_db) requests = scheduler.next_batch(10) while requests: results = [] for request in requests: results.append([request.node_key, raw_db[request.node_key]]) scheduler.process(results) requests = scheduler.next_batch(10) result_account_db = AccountDB(dest_db, state_root) for addr, account_data in contents.items(): balance, nonce, storage, code = account_data assert result_account_db.get_balance(addr) == balance assert result_account_db.get_nonce(addr) == nonce assert result_account_db.get_storage(addr, 0) == storage assert result_account_db.get_code(addr) == code
async def test_state_sync(): raw_db, state_root, contents = make_random_state(1000) dest_db = FakeAsyncAtomicDB() nodes_cache = AtomicDB() scheduler = StateSync(state_root, dest_db, nodes_cache, ExtendedDebugLogger('test')) requests = scheduler.next_batch(10) while requests: results = [] for request in requests: results.append([request.node_key, raw_db[request.node_key]]) await scheduler.process(results) requests = scheduler.next_batch(10) result_account_db = AccountDB(dest_db, state_root) for addr, account_data in contents.items(): balance, nonce, storage, code = account_data assert result_account_db.get_balance(addr) == balance assert result_account_db.get_nonce(addr) == nonce assert result_account_db.get_storage(addr, 0) == storage assert result_account_db.get_code(addr) == code
def make_random_state(n): raw_db = MemoryDB() account_db = AccountDB(raw_db) contents = {} for _ in range(n): addr = os.urandom(20) account_db.touch_account(addr) balance = random.randint(0, 10000) account_db.set_balance(addr, balance) nonce = random.randint(0, 10000) account_db.set_nonce(addr, nonce) storage = random.randint(0, 10000) account_db.set_storage(addr, 0, storage) code = b'not-real-code' account_db.set_code(addr, code) contents[addr] = (balance, nonce, storage, code) account_db.persist() return raw_db, account_db.state_root, contents
def account_db(base_db): return AccountDB(base_db)
OTHER_ADDRESS = b'\xbb' * 20 INVALID_ADDRESS = b'aa' * 20 @pytest.fixture def base_db(): return AtomicDB() @pytest.fixture def account_db(base_db): return AccountDB(base_db) @pytest.mark.parametrize("state", [ AccountDB(MemoryDB()), ]) def test_balance(state): assert state.get_balance(ADDRESS) == 0 state.set_balance(ADDRESS, 1) assert state.get_balance(ADDRESS) == 1 assert state.get_balance(OTHER_ADDRESS) == 0 with pytest.raises(ValidationError): state.get_balance(INVALID_ADDRESS) with pytest.raises(ValidationError): state.set_balance(INVALID_ADDRESS, 1) with pytest.raises(ValidationError): state.set_balance(ADDRESS, 1.0)
def storage_root(chain, simple_contract_address): state_root = chain.get_canonical_head().state_root account_db = AccountDB(chain.chaindb.db, state_root) return account_db._get_storage_root(simple_contract_address)