Esempio n. 1
0
class MockRaidenService:
    def __init__(self, message_handler=None, state_transition=None):
        self.chain = MockChain()
        self.private_key, self.address = factories.make_privatekey_address()

        self.chain.node_address = self.address
        self.message_handler = message_handler

        if state_transition is None:
            state_transition = node.state_transition

        serializer = JSONSerializer
        state_manager = StateManager(state_transition, None)
        storage = SQLiteStorage(':memory:', serializer)
        self.wal = WriteAheadLog(state_manager, storage)

        state_change = ActionInitChain(
            random.Random(),
            0,
            self.chain.node_address,
            self.chain.network_id,
        )

        self.wal.log_and_dispatch(state_change)

    def on_message(self, message):
        if self.message_handler:
            self.message_handler.on_message(self, message)

    def handle_state_change(self, state_change):
        pass

    def sign(self, message):
        message.sign(self.private_key)
Esempio n. 2
0
def new_wal(state_transition: Callable, state: State = None) -> WriteAheadLog:
    serializer = JSONSerializer()
    state = state or Empty()

    storage = SerializedSQLiteStorage(":memory:", serializer)
    storage.write_first_state_snapshot(state)

    return WriteAheadLog(state, storage, state_transition)
Esempio n. 3
0
def new_wal(state_transition):
    state = None
    serializer = JSONSerializer

    state_manager = StateManager(state_transition, state)
    storage = SQLiteStorage(':memory:', serializer)
    wal = WriteAheadLog(state_manager, storage)
    return wal
Esempio n. 4
0
    def __init__(self,
                 message_handler=None,
                 state_transition=None,
                 private_key=None):
        if private_key is None:
            self.privkey, self.address = factories.make_privkey_address()
        else:
            self.privkey = private_key
            self.address = privatekey_to_address(private_key)

        self.rpc_client = MockJSONRPCClient(self.address)
        self.proxy_manager = MockProxyManager(node_address=self.address)
        self.signer = LocalSigner(self.privkey)

        self.message_handler = message_handler
        self.routing_mode = RoutingMode.PRIVATE
        self.config = RaidenConfig(chain_id=self.rpc_client.chain_id,
                                   environment_type=Environment.DEVELOPMENT)

        self.user_deposit = Mock()
        self.default_registry = Mock()
        self.default_registry.address = factories.make_address()
        self.default_one_to_n_address = factories.make_address()
        self.default_msc_address = factories.make_address()

        self.targets_to_identifiers_to_statuses: Dict[Address,
                                                      dict] = defaultdict(dict)
        self.route_to_feedback_token: dict = {}

        if state_transition is None:
            state_transition = node.state_transition

        serializer = JSONSerializer()
        state_manager = StateManager(state_transition, None)
        storage = SerializedSQLiteStorage(":memory:", serializer)
        self.wal = WriteAheadLog(state_manager, storage)

        state_change = ActionInitChain(
            pseudo_random_generator=random.Random(),
            block_number=BlockNumber(0),
            block_hash=factories.make_block_hash(),
            our_address=self.rpc_client.address,
            chain_id=self.rpc_client.chain_id,
        )

        self.wal.log_and_dispatch([state_change])
Esempio n. 5
0
def test_wal():
    state = None
    serializer = PickleSerializer

    state_manager = StateManager(state_transition_noop, state)
    ###storage = SQLiteStorage('/home/wk/work/Raiden/test.txt', serializer)
    storage = SQLiteStorage('/Users/vincent/Downloads/test.txt', serializer)
    wal = WriteAheadLog(state_manager, storage)
    return wal
Esempio n. 6
0
class MockRaidenService:
    def __init__(self, message_handler=None, state_transition=None):
        self.chain = MockChain(network_id=17,
                               node_address=factories.make_address())
        self.private_key, self.address = factories.make_privatekey_address()
        self.signer = LocalSigner(self.private_key)

        self.chain.node_address = self.address
        self.message_handler = message_handler

        self.user_deposit = Mock()

        if state_transition is None:
            state_transition = node.state_transition

        serializer = JSONSerializer
        state_manager = StateManager(state_transition, None)
        storage = SerializedSQLiteStorage(':memory:', serializer)
        self.wal = WriteAheadLog(state_manager, storage)

        state_change = ActionInitChain(
            pseudo_random_generator=random.Random(),
            block_number=0,
            block_hash=factories.make_block_hash(),
            our_address=self.chain.node_address,
            chain_id=self.chain.network_id,
        )

        self.wal.log_and_dispatch(state_change)

    def on_message(self, message):
        if self.message_handler:
            self.message_handler.on_message(self, message)

    def handle_and_track_state_change(self, state_change):
        pass

    def handle_state_change(self, state_change):
        pass

    def sign(self, message):
        message.sign(self.signer)
Esempio n. 7
0
    def __init__(self, message_handler=None, state_transition=None):
        self.chain = MockChain()
        self.private_key, self.address = factories.make_privatekey_address()

        self.chain.node_address = self.address
        self.message_handler = message_handler

        if state_transition is None:
            state_transition = node.state_transition

        serializer = JSONSerializer
        state_manager = StateManager(state_transition, None)
        storage = SQLiteStorage(':memory:', serializer)
        self.wal = WriteAheadLog(state_manager, storage)

        state_change = ActionInitChain(
            random.Random(),
            0,
            self.chain.node_address,
            self.chain.network_id,
        )

        self.wal.log_and_dispatch(state_change)
Esempio n. 8
0
def replay_wal(
    storage: SerializedSQLiteStorage,
    token_network_address: TokenNetworkAddress,
    partner_address: Address,
    translator: Optional[Translator] = None,
) -> None:
    all_state_changes = storage.get_statechanges_by_range(
        RANGE_ALL_STATE_CHANGES)

    state_manager = StateManager(state_transition=node.state_transition,
                                 current_state=None)
    wal = WriteAheadLog(state_manager, storage)

    for _, state_change in enumerate(all_state_changes):
        # Dispatching the state changes one-by-one to easy debugging
        _, events = wal.state_manager.dispatch([state_change])

        chain_state = wal.state_manager.current_state
        msg = "Chain state must never be cleared up."
        assert chain_state, msg

        channel_state = views.get_channelstate_by_token_network_and_partner(
            chain_state,
            to_canonical_address(token_network_address),
            to_canonical_address(partner_address),
        )

        if channel_state is None:
            continue

        ###
        # Customize this to filter things further somewhere around here.
        # An example would be to add `breakpoint()`
        # and inspect the state.
        ###
        print_state_change(state_change, translator=translator)
        print_events(chain.from_iterable(events), translator=translator)

        # Enable to print color coded presence state of channel partners
        # print_presence_view(chain_state, translator)

        # Enable to print balances & balance sum with all channel partners
        # print_node_balances(chain_state, token_network_address, translator)

        print_nl()
Esempio n. 9
0
def dispatch(wal: WriteAheadLog, state_changes: List[StateChange]):
    with wal.process_state_change_atomically() as dispatcher:
        for state_change in state_changes:
            dispatcher.dispatch(state_change)
Esempio n. 10
0
def new_wal():
    serializer = PickleSerializer
    storage = SQLiteStorage(':memory:', serializer)
    return WriteAheadLog(state_transition_noop, storage)