-
Notifications
You must be signed in to change notification settings - Fork 158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test: add test for fee-estimate fix #1506
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
// Copyright 2017-2024 Parity Technologies (UK) Ltd. | ||
// This file is part of Substrate API Sidecar. | ||
// | ||
// Substrate API Sidecar is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
import { ApiPromise } from '@polkadot/api'; | ||
import { GenericExtrinsic } from '@polkadot/types'; | ||
import { AccountId, Block, Extrinsic, Hash, RuntimeDispatchInfo } from '@polkadot/types/interfaces'; | ||
|
||
import { polkadotMetadata } from '../../../test-helpers/metadata/metadata'; | ||
import { polkadotRegistryV1003000 } from '../../../test-helpers/registries'; | ||
import { balancesTransferKeepAliveValid, blockHash789629, mockBlock789629 } from '.'; | ||
|
||
export const getBlock22887036 = (_hash: Hash): Promise<{ block: Block }> => | ||
Promise.resolve().then(() => { | ||
return { | ||
block: mockBlock789629, | ||
}; | ||
}); | ||
|
||
export const deriveGetBlock22887036 = (_hash: Hash): Promise<{ block: Block; author: AccountId }> => | ||
Promise.resolve().then(() => { | ||
return { | ||
author: polkadotRegistryV1003000.createType('AccountId', '1zugcajGg5yDD9TEqKKzGx7iKuGWZMkRbYcyaFnaUaEkwMK'), | ||
block: mockBlock789629, | ||
}; | ||
}); | ||
|
||
const getHeader = (_hash: Hash) => Promise.resolve().then(() => mockBlock789629.header); | ||
|
||
const runtimeVersion = { | ||
specName: polkadotRegistryV1003000.createType('Text', 'polkadot'), | ||
specVersion: polkadotRegistryV1003000.createType('u32', 16), | ||
transactionVersion: polkadotRegistryV1003000.createType('u32', 2), | ||
implVersion: polkadotRegistryV1003000.createType('u32', 0), | ||
implName: polkadotRegistryV1003000.createType('Text', 'parity-polkadot'), | ||
authoringVersion: polkadotRegistryV1003000.createType('u32', 0), | ||
}; | ||
|
||
const getRuntimeVersion = () => | ||
Promise.resolve().then(() => { | ||
return runtimeVersion; | ||
}); | ||
|
||
const getMetadata = () => Promise.resolve().then(() => polkadotMetadata); | ||
|
||
// For getting the blockhash of the genesis block | ||
const getBlockHashGenesis = (_zero: number) => | ||
Promise.resolve().then(() => | ||
polkadotRegistryV1003000.createType( | ||
'BlockHash', | ||
'0x91b171bb158e2d3848fa23a9f1c25182fb8e20313b2c1eb49219da7a70ce90c3', | ||
), | ||
); | ||
|
||
const runtimeDispatchInfo = polkadotRegistryV1003000.createType('RuntimeDispatchInfo', { | ||
weight: { | ||
refTime: '145570000', | ||
proofSize: '3593', | ||
}, | ||
class: 'Normal', | ||
partialFee: '159154905', | ||
}); | ||
|
||
export const queryInfoCall22887036 = ( | ||
_extrinsic: GenericExtrinsic, | ||
_length: Uint8Array, | ||
): Promise<RuntimeDispatchInfo> => Promise.resolve().then(() => runtimeDispatchInfo); | ||
|
||
export const queryInfoAt22887036 = (_extrinsic: string, _hash: Hash): Promise<RuntimeDispatchInfo> => | ||
Promise.resolve().then(() => runtimeDispatchInfo); | ||
|
||
export const submitExtrinsic22887036 = (_extrinsic: string): Promise<Hash> => | ||
Promise.resolve().then(() => polkadotRegistryV1003000.createType('Hash')); | ||
|
||
const getStorage = () => Promise.resolve().then(() => polkadotRegistryV1003000.createType('Option<Raw>', '0x00')); | ||
|
||
const getFinalizedHead = () => Promise.resolve().then(() => blockHash789629); | ||
|
||
export const tx22887036 = (): Extrinsic => | ||
polkadotRegistryV1003000.createType('Extrinsic', balancesTransferKeepAliveValid); | ||
|
||
/** | ||
* Minimal mock polkadot-js ApiPromise. Values are largely meant to be accurate for block | ||
* #22887036, which is what most Service unit tests are based on. | ||
*/ | ||
export const mockApiBlock22887036 = { | ||
runtimeVersion, | ||
call: { | ||
transactionPaymentApi: { | ||
queryInfo: queryInfoCall22887036, | ||
}, | ||
}, | ||
createType: polkadotRegistryV1003000.createType.bind(polkadotRegistryV1003000), | ||
registry: polkadotRegistryV1003000, | ||
rpc: { | ||
chain: { | ||
getHeader, | ||
getBlock22887036, | ||
getBlockHash: getBlockHashGenesis, | ||
getFinalizedHead, | ||
}, | ||
state: { | ||
getRuntimeVersion, | ||
getMetadata, | ||
getStorage, | ||
}, | ||
payment: { | ||
queryInfo: queryInfoAt22887036, | ||
}, | ||
author: { | ||
submitExtrinsic22887036, | ||
}, | ||
}, | ||
} as unknown as ApiPromise; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright 2017-2024 Parity Technologies (UK) Ltd. | ||
// This file is part of Substrate API Sidecar. | ||
// | ||
// Substrate API Sidecar is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
import { polkadotRegistryV1003000 } from '../../../test-helpers/registries'; | ||
import block22887036 from './data/block22887036.json'; | ||
|
||
/** | ||
* Mock for Polkadot block #22887036. | ||
*/ | ||
export const mockBlock22887036 = polkadotRegistryV1003000.createType('Block', block22887036); | ||
|
||
/** | ||
* BlockHash for Polkadot block #22887036. | ||
*/ | ||
export const blockHash22887036 = polkadotRegistryV1003000.createType( | ||
'BlockHash', | ||
'0x7b713de604a99857f6c25eacc115a4f28d2611a23d9ddff99ab0e4f1c17a8578', | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
src/services/test-helpers/responses/transaction/feeEstimateValidRuntimeCall22887036.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"weight": { | ||
"refTime": "145570000", | ||
"proofSize": "3593" | ||
}, | ||
"class": "Normal", | ||
"partialFee": "159154905" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
transactionPaymentApi.queryInfo
doesn't care of the block number, it is always queried against the current runtime version. So being strict, we don't need here the detail at what block it should work.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for this very good point!
In production environment, yes you are absolutely right, the
transactionPaymentApi.queryInfo
call will work against the finalized block (related line of code here). However in the test environment, I cannot (and I should not) query the live chain and get the finalized block. That is why I need to specify a block height manually and pass it as an argument which is what happens here.Manually setting all these different parameters in tests is expected because we want to have control of the setting in which the tests are run. That is why we mock everything (or what we need) and we can simulate being at a specific block height using that specific type registry and metadata, while also mocking the expected response.