def _containerize(value): if isinstance(value, Agent): return AgentContainer(entries=[value]) elif isinstance(value, Application): return ApplicationContainer(entries=[value]) elif isinstance(value, Record): return RecordContainer(entries=[value]) raise Exception("Unknown data type")
def _apply_agent_change(cur, block_num, agent_state_change): agent_container = AgentContainer() agent_container.ParseFromString(agent_state_change.value) for agent in agent_container.entries: cur.execute( 'UPDATE agent SET end_block_num = %s ' 'WHERE end_block_num = %s AND identifier = %s', [block_num, MAX_BLOCK_NUMBER, agent.identifier]) if agent_state_change.type == StateChange.SET: cur.execute( 'INSERT INTO agent ' '(start_block_num, end_block_num, ' ' identifier, name) ' 'VALUES (%s, %s, %s, %s)', [ block_num, MAX_BLOCK_NUMBER, agent.identifier, agent.name ])
def _get(state, addresses): entries = state.get(addresses) if entries: out = {} for e in entries: addr = e.address if e.data: if addr.startswith(Addressing.agent_namespace()): container = AgentContainer() elif addr.startswith(Addressing.application_namespace()): container = ApplicationContainer() elif addr.startswith(Addressing.record_namespace()): container = RecordContainer() else: raise InvalidTransaction("Unknown namespaces.") else: container = None container.ParseFromString(e.data) out[addr] = container return out return {}
def _agent_create(self, state, originator, data): txn_data = AgentCreatePayload() txn_data.ParseFromString(data) agent_addr = Addressing.agent_address(originator) state_items = self._get(state, [agent_addr]) agents = state_items.get(agent_addr, AgentContainer()) # check that the agent does not exists agent = self._find_agent(agents, originator) if agent is not None: raise InvalidTransaction("Agent already exists.") # create the new agent agent = agents.entries.add() agent.identifier = originator agent.name = txn_data.name # send back the updated agents list self._set(state, [(agent_addr, agents)])
def _decode_agent_container(encoded): return _pb_loads(AgentContainer(), encoded)