Skip to content
This repository has been archived by the owner on Sep 6, 2024. It is now read-only.

feat: remove tips parameter from getInclusionStates #484

Merged
merged 4 commits into from
Jun 14, 2020
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
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,6 @@ For details on all available API methods, see the [reference page](api_reference

* [.getInputs(seed, [options], [callback])](api_reference.md#module_core.getInputs)

* [.getLatestInclusion(transactions, tips, [callback])](api_reference.md#module_core.getLatestInclusion)

* [.getNeighbors([callback])](api_reference.md#module_core.getNeighbors)

* [.getNewAddress(seed, [options], [callback])](api_reference.md#module_core.getNewAddress)
Expand Down
42 changes: 1 addition & 41 deletions api_reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -320,10 +320,6 @@ Converts an integer value to trits

* [.getInputs(seed, [options], [callback])](#module_core.getInputs)

* [.createGetLatestInclusion(provider)](#module_core.createGetLatestInclusion)

* [.getLatestInclusion(transactions, tips, [callback])](#module_core.getLatestInclusion)

* [.createGetNeighbors(provider)](#module_core.createGetNeighbors)

* [.getNeighbors([callback])](#module_core.getNeighbors)
Expand Down Expand Up @@ -947,42 +943,6 @@ getInputs(seed, { start: 0, threhold })
// ...
})
```
<a name="module_core.createGetLatestInclusion"></a>

### *core*.createGetLatestInclusion(provider)

| Param | Type | Description |
| --- | --- | --- |
| provider | <code>Provider</code> | Network provider for accessing IRI |

**Returns**: <code>function</code> - [`getLatestInclusion`](#module_core.getLatestInclusion)
<a name="module_core.getLatestInclusion"></a>

### *core*.getLatestInclusion(transactions, tips, [callback])
**Fulfil**: <code>boolean[]</code> List of inclusion states
**Reject**: <code>Error</code>
- `INVALID_HASH`: Invalid transaction hash
- Fetch error

| Param | Type | Description |
| --- | --- | --- |
| transactions | <code>Array.&lt;Hash&gt;</code> | List of transactions hashes |
| tips | <code>number</code> | List of tips to check if transactions are referenced by |
| [callback] | <code>Callback</code> | Optional callback |

Fetches inclusion states of given transactions and a list of tips,
by calling [`getInclusionStates`](#module_core.getInclusionStates) on `latestSolidSubtangleMilestone`.

**Example**
```js
getLatestInclusion(hashes)
.then(states => {
// ...
})
.catch(err => {
// handle error
})
```
<a name="module_core.createGetNeighbors"></a>

### *core*.createGetNeighbors(provider)
Expand Down Expand Up @@ -1241,7 +1201,7 @@ or should be [_reattached_](#module_core.replayBundle)
// We need to monitor inclusion states of all tail transactions (original tail & reattachments)
const tails = [tail]

getLatestInclusion(tails)
getInclusionStates(tails)
.then(states => {
// Check if none of transactions confirmed
if (states.indexOf(true) === -1) {
Expand Down
2 changes: 1 addition & 1 deletion packages/account/src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export interface Network {
readonly getBalances: API['getBalances']
readonly getBalance: (address: Trytes) => Promise<number>
readonly getConsistency: API['checkConsistency']
readonly getLatestInclusion: API['getLatestInclusion']
readonly getInclusionStates: API['getInclusionStates']
readonly getTrytes: API['getTrytes']
readonly sendTrytes: API['sendTrytes']
readonly setSettings: API['setSettings']
Expand Down
10 changes: 5 additions & 5 deletions packages/account/src/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
createFindTransactions,
createGetBalances,
createGetBundlesFromAddresses,
createGetLatestInclusion,
createGetInclusionStates,
createGetTransactionsToApprove,
createGetTrytes,
createIsAddressUsed,
Expand Down Expand Up @@ -76,14 +76,14 @@ export function networkAdapter({ provider }: NetworkParams): Network {
const httpClient = createHttpClient({ provider })
const getBalances = createGetBalances(httpClient)
const getTrytes = createGetTrytes(httpClient)
const getLatestInclusion = createGetLatestInclusion(httpClient)
const getInclusionStates = createGetInclusionStates(httpClient)

return {
getTrytes: hashes => (hashes.length > 0 ? getTrytes(hashes) : Promise.resolve([])),
getBalance: (address): Promise<number> => getBalances([address]).then(({ balances }) => balances[0]),
getBalances,
getConsistency: createCheckConsistency(httpClient),
getLatestInclusion: hashes => (hashes.length > 0 ? getLatestInclusion(hashes) : Promise.resolve([])),
getInclusionStates: hashes => (hashes.length > 0 ? getInclusionStates(hashes) : Promise.resolve([])),
getBundlesFromAddresses: createGetBundlesFromAddresses(httpClient, 'lib'),
findTransactions: createFindTransactions(httpClient),
sendTrytes: createSendTrytes(httpClient),
Expand Down Expand Up @@ -407,7 +407,7 @@ export function transactionAttachment(this: any, params: TransactionAttachmentPa
getTransactionsToApprove,
attachToTangle,
getTrytes,
getLatestInclusion,
getInclusionStates,
getConsistency,
} = network

Expand Down Expand Up @@ -439,7 +439,7 @@ export function transactionAttachment(this: any, params: TransactionAttachmentPa
pastAttachments.map(trytes => tritsToTrytes(transactionHash(trytesToTrits(trytes))))
)
.then(pastAttachmentHashes =>
getLatestInclusion(pastAttachmentHashes).tap(inclusionStates => {
getInclusionStates(pastAttachmentHashes).tap(inclusionStates => {
if (inclusionStates.indexOf(true) > -1) {
return persistence.del(['0', tritsToTrytes(bundleHash(bundle))].join(':'))
}
Expand Down
50 changes: 3 additions & 47 deletions packages/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,10 @@ yarn add @iota/core

* [.getBundle(tailTransactionHash, [callback])](#module_core.getBundle)

* [.getInclusionStates(transactions, tips, [callback])](#module_core.getInclusionStates)
* [.getInclusionStates(transactions, [callback])](#module_core.getInclusionStates)

* [.getInputs(seed, [options], [callback])](#module_core.getInputs)

* [.getLatestInclusion(transactions, [callback])](#module_core.getLatestInclusion)

* [.getNeighbors([callback])](#module_core.getNeighbors)

* [.getNewAddress(seed, [options], [callback])](#module_core.getNewAddress)
Expand Down Expand Up @@ -545,7 +543,7 @@ getBundle(tail)
```
<a name="module_core.getInclusionStates"></a>

### *core*.getInclusionStates(transactions, tips, [callback])
### *core*.getInclusionStates(transactions, [callback])
**Summary**: Finds out if one or more given transactions are referenced by one or more other given transactions.
**Fulfil**: <code>boolean[]</code> states - Array of inclusion states, where `true` means that the transaction is referenced by the given transacions and `false` means that it's not.
**Reject**: <code>Error</code> error - An error that contains one of the following:
Expand All @@ -555,7 +553,6 @@ getBundle(tail)
| Param | Type | Description |
| --- | --- | --- |
| transactions | <code>Array.&lt;Hash&gt;</code> | Array of transaction hashes to check |
| tips | <code>Array.&lt;Hash&gt;</code> | Array of transaction hashes that should directly or indirectly reference the given transactions |
| [callback] | <code>Callback</code> | Optional callback function |

This method uses the connected IRI node's [`getInclusionStates`](https://docs.iota.org/docs/node-software/0.1/iri/references/api-reference#getinclusionstates) endpoint.
Expand All @@ -564,13 +561,9 @@ If the given tip transactions reference a given transaction, the returned state

If the given tip transactions do not reference a given transaction, the returned state is `false`.

## Related methods

To find out if one or more transactions are confirmed, use the [`getLatestInclusion()`](#module_core.getLatestInclusion) method.

**Example**
```js
getInclusionStates(transactions, )
getInclusionStates(transactions)
.then(states => {
for(let i = 0; i < states.length; i++){
states? console.log(`Transaction ${i} is referenced by the given transactions`) :
Expand Down Expand Up @@ -637,43 +630,6 @@ getInputs(seed)
}
});
```
<a name="module_core.getLatestInclusion"></a>

### *core*.getLatestInclusion(transactions, [callback])
**Summary**: Finds out if one or more given transactions are [confirmed or pending](https://docs.iota.org/docs/getting-started/0.1/network/the-tangle#transaction-states).
**Fulfil**: <code>boolean[]</code> states - Array of inclusion states, where `true` means that the transaction is confirmed and `false` means that it's not.
**Reject**: <code>Error</code> error - An error that contains one of the following:
- `INVALID_HASH`: Make sure that the transaction hashes are 81 trytes long
- Fetch error: The connected IOTA node's API returned an error. See the [list of error messages](https://docs.iota.org/docs/node-software/0.1/iri/references/api-errors)

| Param | Type | Description |
| --- | --- | --- |
| transactions | <code>Array.&lt;Hash&gt;</code> | List of transactions hashes to check |
| [callback] | <code>Callback</code> | Optional callback function |

This method uses the node's `latestSolidSubtangleMilestone` field as the `tips` argument to make sure that the given transactions are referenced by the node's latest solid milestone.

An invalid transaction will always remain in a pending state.

**Note:** If a valid transaction is in a pending state for too long, you can [increase its chances of being confirmed](https://docs.iota.org/docs/client-libraries/0.1/how-to-guides/js/confirm-pending-bundle).

## Related methods

To check if transactions are referenced by a non-milestone transaction, use the [`getInclusionStates()`](#module_core.getInclusionStates) method.

**Example**
```js
iota.getLatestInclusionState(['transactionHash'])
.then(states => {
for(let i = 0; i < states.length; i++){
states[i]? console.log(`Transaction ${i} is confirmed`) :
console.log(`transaction ${i} is pending`);
}
})
.catch(error => {
console.log(`Something went wrong: ${error}`);
});
```
<a name="module_core.getNeighbors"></a>

### *core*.getNeighbors([callback])
Expand Down
2 changes: 0 additions & 2 deletions packages/core/src/composeAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import {
createGetBundle,
createGetInclusionStates,
createGetInputs,
createGetLatestInclusion,
createGetNeighbors,
createGetNewAddress,
createGetNodeInfo,
Expand Down Expand Up @@ -183,7 +182,6 @@ export const composeAPI = (settings: Partial<Settings> = {}) => {
getAccountData: createGetAccountData(provider),
getBundle: createGetBundle(provider),
getBundlesFromAddresses: createGetBundlesFromAddresses(provider),
getLatestInclusion: createGetLatestInclusion(provider),
getNewAddress: createGetNewAddress(provider),
getTransactionObjects: createGetTransactionObjects(provider),
findTransactionObjects: createFindTransactionObjects(provider),
Expand Down
15 changes: 7 additions & 8 deletions packages/core/src/createGetBundlesFromAddresses.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as Promise from 'bluebird'
import { Bundle, Callback, Hash, Provider, Transaction } from '../../types'
import { createFindTransactionObjects, createGetLatestInclusion } from './'
import { createFindTransactionObjects, createGetInclusionStates } from './'

export const createGetBundlesFromAddresses = (provider: Provider, caller?: string) => {
const findTransactionObjects = createFindTransactionObjects(provider)
const getLatestInclusion = createGetLatestInclusion(provider)
const getInclusionStates = createGetInclusionStates(provider)

/* tslint:disable-next-line:only-arrow-functions */
return function(
Expand Down Expand Up @@ -36,9 +36,8 @@ export const createGetBundlesFromAddresses = (provider: Provider, caller?: strin
.then(groupTransactionsIntoBundles)

// 4. If requested, add persistence status to each bundle
.then(
(bundles: ReadonlyArray<Bundle>) =>
inclusionStates ? addPersistence(getLatestInclusion, bundles) : bundles
.then((bundles: ReadonlyArray<Bundle>) =>
inclusionStates ? addPersistence(getInclusionStates, bundles) : bundles
)

// 5. Sort bundles by timestamp
Expand Down Expand Up @@ -96,16 +95,16 @@ export const zipPersistence = (bundles: ReadonlyArray<Bundle>) => (
// Since bundles are atomic, all transactions have the same state
zip2(bundles, states).map(([bundle, state]) => bundle.map(tx => ({ ...tx, persistence: state })))

type GetLatestInclusion = (
type GetInclusionStates = (
transactions: ReadonlyArray<Hash>,
callback?: Callback<ReadonlyArray<boolean>>
) => Promise<ReadonlyArray<boolean>>

export const addPersistence = (getLatestInclusion: GetLatestInclusion, bundles: ReadonlyArray<Bundle>) => {
export const addPersistence = (getInclusionStates: GetInclusionStates, bundles: ReadonlyArray<Bundle>) => {
// Get the first hash of each bundle
const hashes = bundles.map(bundle => bundle[0].hash)

return getLatestInclusion(hashes).then(zipPersistence(bundles))
return getInclusionStates(hashes).then(zipPersistence(bundles))
}

export const sortByTimestamp = (bundles: ReadonlyArray<Bundle>) =>
Expand Down
43 changes: 14 additions & 29 deletions packages/core/src/createGetInclusionStates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import {

/**
* @method createGetInclusionStates
*
*
* @summary Creates a new `getInclusionStates()` method, using a custom Provider instance.
*
* @memberof module:core
*
*
* @ignore
*
* @param {Provider} provider - The Provider object that the method should use to call the node's API endpoints.
Expand All @@ -26,28 +26,23 @@ import {
export const createGetInclusionStates = ({ send }: Provider) =>
/**
* This method uses the connected IRI node's [`getInclusionStates`](https://docs.iota.org/docs/node-software/0.1/iri/references/api-reference#getinclusionstates) endpoint.
*
*
* If the given tip transactions reference a given transaction, the returned state is `true`.
*
*
* If the given tip transactions do not reference a given transaction, the returned state is `false`.
*
* ## Related methods
*
* To find out if one or more transactions are confirmed, use the [`getLatestInclusion()`]{@link #module_core.getLatestInclusion} method.
*
*
* @method getInclusionStates
*
*
* @summary Finds out if one or more given transactions are referenced by one or more other given transactions.
*
*
* @memberof module:core
*
* @param {Hash[]} transactions - Array of transaction hashes to check
* @param {Hash[]} tips - Array of transaction hashes that should directly or indirectly reference the given transactions
* @param {Callback} [callback] - Optional callback function
*
*
* @example
* ```js
* getInclusionStates(transactions, )
* getInclusionStates(transactions)
* .then(states => {
* for(let i = 0; i < states.length; i++){
* states? console.log(`Transaction ${i} is referenced by the given transactions`) :
Expand All @@ -60,29 +55,19 @@ export const createGetInclusionStates = ({ send }: Provider) =>
* ```
*
* @return {Promise}
*
*
* @fulfil {boolean[]} states - Array of inclusion states, where `true` means that the transaction is referenced by the given transacions and `false` means that it's not.
*
*
* @reject {Error} error - An error that contains one of the following:
* - `INVALID_TRANSACTION_HASH`: Make sure that the transaction hashes are 81 trytes long
* - Fetch error: The connected IOTA node's API returned an error. See the [list of error messages](https://docs.iota.org/docs/node-software/0.1/iri/references/api-errors)
* - Fetch error: The connected IOTA node's API returned an error. See the [list of error messages](https://docs.iota.org/docs/node-software/0.1/iri/references/api-errors)
*/
(
transactions: ReadonlyArray<Hash>,
tips: ReadonlyArray<Hash>,
callback?: Callback<ReadonlyArray<boolean>>
): Promise<ReadonlyArray<boolean>> =>
Promise.resolve(
validate(
arrayValidator(hashValidator)(transactions, errors.INVALID_TRANSACTION_HASH),
arrayValidator(hashValidator)(tips, errors.INVALID_TRANSACTION_HASH)
)
)
(transactions: ReadonlyArray<Hash>, callback?: Callback<ReadonlyArray<boolean>>): Promise<ReadonlyArray<boolean>> =>
Promise.resolve(validate(arrayValidator(hashValidator)(transactions, errors.INVALID_TRANSACTION_HASH)))
.then(() =>
send<GetInclusionStatesCommand, GetInclusionStatesResponse>({
command: IRICommand.GET_INCLUSION_STATES,
transactions,
tips,
})
)
.then(({ states }) => states)
Expand Down
Loading