Skip to content

Commit

Permalink
fix: transaction operatios were not properly built. wrong coin_change
Browse files Browse the repository at this point in the history
The operations were not properly made and were ignored by `rosetta-cli
check:construction` as we were always putting the coins as spent and the
outputs were not even marked as such
  • Loading branch information
AlanVerbner authored and t-dallas committed Aug 25, 2020
1 parent 64d467a commit 65b8e67
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 10 deletions.
14 changes: 10 additions & 4 deletions src/server/services/block-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface BlockService {
findBlock(blockIdentifier: PartialBlockIdentifier): Promise<Block | null>;
}

const COIN_SPENT_ACTION = 'coin_spent';
const COIN_CREATED_ACTION = 'coin_created';

/**
Expand Down Expand Up @@ -67,11 +68,15 @@ const createOperation = (
related_operations: relatedOperations
});

const getCoinChange = (index: number, hash: string): Components.Schemas.CoinChange => ({
const getCoinChange = (
index: number,
hash: string,
coinAction: Components.Schemas.CoinAction
): Components.Schemas.CoinChange => ({
coin_identifier: {
identifier: `${hash}:${index}`
},
coin_action: COIN_CREATED_ACTION
coin_action: coinAction
});

/**
Expand All @@ -89,7 +94,7 @@ const mapToRosettaTransaction = (transaction: TransactionWithInputsAndOutputs):
`-${input.value}`,
undefined,
undefined,
getCoinChange(input.sourceTransactionIndex, input.sourceTransactionHash)
getCoinChange(input.sourceTransactionIndex, input.sourceTransactionHash, COIN_SPENT_ACTION)
)
);
// Output related operations are all the inputs.This will iterate over the collection again
Expand All @@ -106,7 +111,8 @@ const mapToRosettaTransaction = (transaction: TransactionWithInputsAndOutputs):
output.address,
output.value,
relatedOperations,
output.index
output.index,
getCoinChange(output.index, transaction.hash, COIN_CREATED_ACTION)
)
);

Expand Down
42 changes: 36 additions & 6 deletions test/e2e/fixture-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export const block23236WithTransactions = {
}
},
coin_change: {
coin_action: 'coin_created',
coin_action: 'coin_spent',
coin_identifier: {
identifier: '9c239b53d895038e702b7c63b7a9ca3a54385e04a4b8d69227119c2358150a77:0'
}
Expand Down Expand Up @@ -113,6 +113,12 @@ export const block23236WithTransactions = {
symbol: 'ADA',
decimals: 6
}
},
coin_change: {
coin_action: 'coin_created',
coin_identifier: {
identifier: 'abbeb108ebc3990c7f031113bcb8ce8f306a1eec8f313acffcdcd256379208f4:0'
}
}
}
]
Expand All @@ -139,7 +145,7 @@ export const block23236WithTransactions = {
}
},
coin_change: {
coin_action: 'coin_created',
coin_action: 'coin_spent',
coin_identifier: {
identifier: 'd967be1c496a5321e0c9424a8cc33e8c161c79f64dad465dd0bfbe0abcfde1ef:0'
}
Expand Down Expand Up @@ -167,6 +173,12 @@ export const block23236WithTransactions = {
symbol: 'ADA',
decimals: 6
}
},
coin_change: {
coin_action: 'coin_created',
coin_identifier: {
identifier: 'd864f781436f0122a21335b672fe7943ab1d123f6c2ed770541669d70a870d75:0'
}
}
}
]
Expand Down Expand Up @@ -217,7 +229,7 @@ export const block7134WithTxs = {
}
},
coin_change: {
coin_action: 'coin_created',
coin_action: 'coin_spent',
coin_identifier: {
identifier: 'a9e4413a5fb61a7a43c7df006ffcaaf3f2ffc9541f54757023968c5a8f8294fd:0'
}
Expand Down Expand Up @@ -246,6 +258,12 @@ export const block7134WithTxs = {
symbol: 'ADA',
decimals: 6
}
},
coin_change: {
coin_action: 'coin_created',
coin_identifier: {
identifier: 'c950e0d7d898a392ccf458a75728aa35afd4bd3776f118cbf0e0abb315205bc4:0'
}
}
},
{
Expand All @@ -270,6 +288,12 @@ export const block7134WithTxs = {
symbol: 'ADA',
decimals: 6
}
},
coin_change: {
coin_action: 'coin_created',
coin_identifier: {
identifier: 'c950e0d7d898a392ccf458a75728aa35afd4bd3776f118cbf0e0abb315205bc4:1'
}
}
}
]
Expand Down Expand Up @@ -370,7 +394,13 @@ export const transaction987aOnGenesis = {
},
related_operations: [],
status: 'success',
type: 'transfer'
type: 'transfer',
coin_change: {
coin_action: 'coin_created',
coin_identifier: {
identifier: '927edb96f3386ab91b5f5d85d84cb4253c65b1c2f65fa7df25f81fab1d62987a:0'
}
}
}
],
transaction_identifier: {
Expand Down Expand Up @@ -412,7 +442,7 @@ export const CONSTRUCTION_PAYLOADS_REQUEST = {
// eslint-disable-next-line sonarjs/no-duplicate-string
identifier: '2f23fd8cca835af21f3ac375bac601f97ead75f2e79143bdf71fe2c4be043e8f:1'
},
coin_action: 'coin_created'
coin_action: 'coin_spent'
}
},
{
Expand Down Expand Up @@ -523,7 +553,7 @@ export const CONSTRUCTION_PAYLOADS_REQUEST_INVALID_OUTPUTS = {
coin_identifier: {
identifier: '2f23fd8cca835af21f3ac375bac601f97ead75f2e79143bdf71fe2c4be043e8f:1'
},
coin_action: 'coin_created'
coin_action: 'coin_spent'
}
},
{
Expand Down

0 comments on commit 65b8e67

Please sign in to comment.