Skip to content

Commit

Permalink
adjust which field to check
Browse files Browse the repository at this point in the history
  • Loading branch information
grod220 committed Dec 3, 2024
1 parent f3584e7 commit e12b45a
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 47 deletions.
67 changes: 35 additions & 32 deletions packages/query/src/helpers/identify-txs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,15 +355,12 @@ describe('identifyTransactions', () => {
test('identifies relevant MsgRecvPacket', async () => {
const txA = new Transaction({
body: {
actions: [
createMsgReceiveAction(knownAddr, 'MsgRecvPacket'),
createMsgReceiveAction(unknownAddr, 'MsgRecvPacket'),
],
actions: [createMsgReceive(knownAddr), createMsgReceive(unknownAddr)],
},
});
const txB = new Transaction({
body: {
actions: [createMsgReceiveAction(unknownAddr, 'MsgRecvPacket')],
actions: [createMsgReceive(unknownAddr)],
},
});
const blockTx = [txA, txB];
Expand All @@ -382,15 +379,12 @@ describe('identifyTransactions', () => {
test('identifies relevant MsgAcknowledgement', async () => {
const txA = new Transaction({
body: {
actions: [
createMsgReceiveAction(knownAddr, 'MsgAcknowledgement'),
createMsgReceiveAction(unknownAddr, 'MsgAcknowledgement'),
],
actions: [createMsgAcknowledgement(knownAddr), createMsgAcknowledgement(unknownAddr)],
},
});
const txB = new Transaction({
body: {
actions: [createMsgReceiveAction(unknownAddr, 'MsgAcknowledgement')],
actions: [createMsgAcknowledgement(unknownAddr)],
},
});
const blockTx = [txA, txB];
Expand All @@ -409,15 +403,12 @@ describe('identifyTransactions', () => {
test('identifies relevant MsgTimeout', async () => {
const txA = new Transaction({
body: {
actions: [
createMsgReceiveAction(knownAddr, 'MsgTimeout'),
createMsgReceiveAction(unknownAddr, 'MsgTimeout'),
],
actions: [createMsgTimeout(knownAddr), createMsgTimeout(unknownAddr)],
},
});
const txB = new Transaction({
body: {
actions: [createMsgReceiveAction(unknownAddr, 'MsgTimeout')],
actions: [createMsgTimeout(unknownAddr)],
},
});
const blockTx = [txA, txB];
Expand All @@ -437,9 +428,9 @@ describe('identifyTransactions', () => {
const tx = new Transaction({
body: {
actions: [
createMsgReceiveAction(unknownAddr, 'MsgTimeout'),
createMsgReceiveAction(unknownAddr, 'MsgAcknowledgement'),
createMsgReceiveAction(unknownAddr, 'MsgRecvPacket'),
createMsgReceive(unknownAddr),
createMsgAcknowledgement(unknownAddr),
createMsgTimeout(unknownAddr),
],
},
});
Expand All @@ -456,15 +447,24 @@ describe('identifyTransactions', () => {
});
});

const createMsgReceiveAction = (
receiver: string,
action: 'MsgRecvPacket' | 'MsgAcknowledgement' | 'MsgTimeout',
): Action => {
const createMsgReceive = (receiver: string): Action => {
const tokenPacketData = new FungibleTokenPacketData({ receiver });
const encoder = new TextEncoder();
const Msg = getMsgType(action);
const relevantRelay = Any.pack(
new Msg({
new MsgRecvPacket({
packet: new Packet({ data: encoder.encode(tokenPacketData.toJsonString()) }),
}),
);
return new Action({
action: { case: 'ibcRelayAction', value: new IbcRelay({ rawAction: relevantRelay }) },
});
};

const createMsgAcknowledgement = (sender: string): Action => {
const tokenPacketData = new FungibleTokenPacketData({ sender });
const encoder = new TextEncoder();
const relevantRelay = Any.pack(
new MsgAcknowledgement({
packet: new Packet({ data: encoder.encode(tokenPacketData.toJsonString()) }),
}),
);
Expand All @@ -473,12 +473,15 @@ const createMsgReceiveAction = (
});
};

const getMsgType = (action: 'MsgRecvPacket' | 'MsgAcknowledgement' | 'MsgTimeout') => {
if (action === 'MsgRecvPacket') {
return MsgRecvPacket;
} else if (action === 'MsgAcknowledgement') {
return MsgAcknowledgement;
} else {
return MsgTimeout;
}
const createMsgTimeout = (sender: string): Action => {
const tokenPacketData = new FungibleTokenPacketData({ sender });
const encoder = new TextEncoder();
const relevantRelay = Any.pack(
new MsgTimeout({
packet: new Packet({ data: encoder.encode(tokenPacketData.toJsonString()) }),
}),
);
return new Action({
action: { case: 'ibcRelayAction', value: new IbcRelay({ rawAction: relevantRelay }) },
});
};
39 changes: 24 additions & 15 deletions packages/query/src/helpers/identify-txs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,41 +42,50 @@ const hasRelevantIbcRelay = (
return false;
}

let packet: Packet | undefined;

if (rawAction.is(MsgRecvPacket.typeName)) {
const recvPacket = new MsgRecvPacket();
rawAction.unpackTo(recvPacket);
packet = recvPacket.packet;
} else if (rawAction.is(MsgAcknowledgement.typeName)) {
if (!recvPacket.packet) {
return false;
}
return isControlledByUser(recvPacket.packet, isControlledAddr, 'receiver');
}

if (rawAction.is(MsgAcknowledgement.typeName)) {
const ackPacket = new MsgAcknowledgement();
rawAction.unpackTo(ackPacket);
packet = ackPacket.packet;
} else if (rawAction.is(MsgTimeout.typeName)) {
if (!ackPacket.packet) {
return false;
}
return isControlledByUser(ackPacket.packet, isControlledAddr, 'sender');
}

if (rawAction.is(MsgTimeout.typeName)) {
const timeout = new MsgTimeout();
rawAction.unpackTo(timeout);
packet = timeout.packet;
if (!timeout.packet) {
return false;
}
return isControlledByUser(timeout.packet, isControlledAddr, 'sender');
}

// Not a potentially relevant ibc relay action
if (!packet) {
return false;
}

return isControlledByUser(packet, isControlledAddr);
return false;
});
};

// Determines if the packet data points to the user as the receiver
const isControlledByUser = (
packet: Packet,
isControlledAddr: ViewServerInterface['isControlledAddress'],
entityToCheck: 'sender' | 'receiver',
): boolean => {
try {
const dataString = new TextDecoder().decode(packet.data);
const { receiver } = FungibleTokenPacketData.fromJsonString(dataString);
const receivingAddr = parseIntoAddr(receiver);
return isControlledAddr(receivingAddr);
const { sender, receiver } = FungibleTokenPacketData.fromJsonString(dataString);
const addrStr = entityToCheck === 'sender' ? sender : receiver;
const addrToCheck = parseIntoAddr(addrStr);
return isControlledAddr(addrToCheck);
// eslint-disable-next-line @typescript-eslint/no-unused-vars -- On error, ignore and continue
} catch (e) {
return false;
Expand Down

0 comments on commit e12b45a

Please sign in to comment.