Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/shield-controller/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Added `transactionMeta.rawTx` to the `logTransaction` request body. ([#7178](https://github.com/MetaMask/core/pull/7178))

### Changed

- Skipped transaction coverage check when the transaction is `submitted` or `confirmed`. ([#7178](https://github.com/MetaMask/core/pull/7178))

## [2.1.0]

### Added
Expand Down
13 changes: 13 additions & 0 deletions packages/shield-controller/src/ShieldController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,7 @@ describe('ShieldController', () => {
txMeta: updatedTxMeta,
status: 'shown',
transactionHash: '0x00',
rawTransactionHex: '0xdeadbeef',
});
});

Expand All @@ -478,6 +479,7 @@ describe('ShieldController', () => {
expect(components.backend.logTransaction).toHaveBeenCalledWith({
status: 'not_shown',
transactionHash: '0x00',
rawTransactionHex: '0xdeadbeef',
txMeta: updatedTxMeta,
});
});
Expand All @@ -492,6 +494,17 @@ describe('ShieldController', () => {
// Check that backend was not called
expect(components.backend.logTransaction).not.toHaveBeenCalled();
});

it('does not log when raw transaction hex is missing', async () => {
const components = setup();

await runTest(components, {
updateTransaction: (txMeta) => delete txMeta.rawTx,
});

// Check that backend was not called
expect(components.backend.logTransaction).not.toHaveBeenCalled();
});
});

describe('metadata', () => {
Expand Down
48 changes: 30 additions & 18 deletions packages/shield-controller/src/ShieldController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,24 +284,30 @@ export class ShieldController extends BaseController<
for (const transaction of transactions) {
const previousTransaction = previousTransactionsById.get(transaction.id);

// Check if the simulation data has changed.
const simulationDataChanged =
// only check if the previous transaction has simulation data and if it has changed
// this is to avoid checking coverage for the `TWICE` (once when it's added to the state and once when it's simulated for the first time).
// we only need to update the coverage result when the simulation data has changed.
Boolean(previousTransaction?.simulationData) &&
!isEqual(
transaction.simulationData,
previousTransaction?.simulationData,
);

// Check coverage if the transaction is new or if the simulation data has
// changed.
if (!previousTransaction || simulationDataChanged) {
this.checkCoverage(transaction).catch(
// istanbul ignore next
(error) => log('Error checking coverage:', error),
);
if (
// We don't need to check coverage for submitted or confirmed transactions.
transaction.status !== TransactionStatus.submitted &&
transaction.status !== TransactionStatus.confirmed
) {
// Check if the simulation data has changed.
const simulationDataChanged =
// only check if the previous transaction has simulation data and if it has changed
// this is to avoid checking coverage for the `TWICE` (once when it's added to the state and once when it's simulated for the first time).
// we only need to update the coverage result when the simulation data has changed.
previousTransaction?.simulationData &&
!isEqual(
transaction.simulationData,
previousTransaction.simulationData,
);

// Check coverage if the transaction is new or if the simulation data has
// changed.
if (!previousTransaction || simulationDataChanged) {
this.checkCoverage(transaction).catch(
// istanbul ignore next
(error) => log('Error checking coverage:', error),
);
}
}

// Log transaction once it has been submitted.
Expand Down Expand Up @@ -446,11 +452,17 @@ export class ShieldController extends BaseController<
throw new Error('Transaction hash not found');
}

const rawTransactionHex = txMeta.rawTx;
if (!rawTransactionHex) {
throw new Error('Raw transaction hex not found');
}

const { status } = this.#getCoverageStatus(txMeta.id);

await this.#backend.logTransaction({
txMeta,
transactionHash,
rawTransactionHex,
status,
});
}
Expand Down
2 changes: 2 additions & 0 deletions packages/shield-controller/src/backend.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ describe('ShieldRemoteBackend', () => {
await backend.logTransaction({
txMeta: generateMockTxMeta(),
transactionHash: '0x00',
rawTransactionHex: '0xdeadbeef',
status: 'shown',
});
expect(fetchMock).toHaveBeenCalledTimes(1);
Expand All @@ -377,6 +378,7 @@ describe('ShieldRemoteBackend', () => {
backend.logTransaction({
txMeta: generateMockTxMeta(),
transactionHash: '0x00',
rawTransactionHex: '0xdeadbeef',
status: 'shown',
}),
).rejects.toThrow('Failed to log transaction: 500');
Expand Down
2 changes: 2 additions & 0 deletions packages/shield-controller/src/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,10 @@ export class ShieldRemoteBackend implements ShieldBackend {

async logTransaction(req: LogTransactionRequest): Promise<void> {
const initBody = makeInitCoverageCheckBody(req.txMeta);

const body = {
transactionHash: req.transactionHash,
rawTransactionHex: req.rawTransactionHex,
status: req.status,
...initBody,
};
Expand Down
1 change: 1 addition & 0 deletions packages/shield-controller/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export type LogSignatureRequest = {
export type LogTransactionRequest = {
txMeta: TransactionMeta;
transactionHash: string;
rawTransactionHex: string;
status: string;
};

Expand Down
1 change: 1 addition & 0 deletions packages/shield-controller/tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export function generateMockTxMeta(): TransactionMeta {
type: TransactionType.contractInteraction,
origin: 'https://metamask.io',
submittedTime: Date.now(),
rawTx: '0xdeadbeef',
};
}

Expand Down
Loading