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
2 changes: 1 addition & 1 deletion core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub use crate::block::Block;
pub use crate::client::Error::Database;
pub use crate::client::{
AccountData, AssetClient, BlockChainClient, BlockChainTrait, ChainNotify, Client, ClientConfig, DatabaseClient,
EngineClient, EngineInfo, ExecuteClient, ImportBlock, MiningBlockChainClient, Shard, StateInfo,
EngineClient, EngineInfo, ExecuteClient, ImportBlock, MetadataInfo, MiningBlockChainClient, Shard, StateInfo,
TestBlockChainClient, TextClient,
};
pub use crate::consensus::{EngineType, TimeGapParams};
Expand Down
2 changes: 1 addition & 1 deletion json/src/scheme/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use ckey::NetworkId;
use crate::uint::Uint;

/// Scheme params.
#[derive(Debug, Default, PartialEq, Deserialize)]
#[derive(Debug, Default, PartialEq, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Params {
/// Maximum size of extra data.
Expand Down
49 changes: 43 additions & 6 deletions json/src/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,15 @@ impl From<Uint> for u32 {
}
}

impl From<u64> for Uint {
fn from(f: u64) -> Self {
Uint(f.into())
impl From<Uint> for u16 {
fn from(f: Uint) -> Self {
u64::from(f.0) as u16
}
}

impl From<Uint> for u8 {
fn from(f: Uint) -> Self {
u64::from(f.0) as u8
}
}

Expand All @@ -56,9 +62,40 @@ impl From<Uint> for usize {
}
}

impl From<Uint> for u8 {
fn from(f: Uint) -> Self {
u64::from(f.0) as u8
impl From<u64> for Uint {
fn from(f: u64) -> Self {
Uint(f.into())
}
}

impl From<u32> for Uint {
fn from(f: u32) -> Self {
Uint(f.into())
}
}

impl From<u16> for Uint {
fn from(f: u16) -> Self {
Uint(f.into())
}
}

impl From<u8> for Uint {
fn from(f: u8) -> Self {
Uint(f.into())
}
}

impl From<usize> for Uint {
fn from(f: usize) -> Self {
Uint(f.into())
}
}

// This impl is to support expressions like `0.into()`
impl From<i32> for Uint {
fn from(f: i32) -> Self {
Uint(f.into())
}
}

Expand Down
17 changes: 16 additions & 1 deletion rpc/src/v1/impls/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@
use std::convert::{TryFrom, TryInto};
use std::sync::Arc;

use ccore::{AccountData, AssetClient, BlockId, EngineInfo, ExecuteClient, MiningBlockChainClient, Shard, TextClient};
use ccore::{
AccountData, AssetClient, BlockId, EngineInfo, ExecuteClient, MetadataInfo, MiningBlockChainClient, Shard,
TextClient,
};
use ccrypto::Blake;
use cjson::scheme::Params;
use cjson::uint::Uint;
use ckey::{public_to_address, NetworkId, PlatformAddress, Public};
use cstate::FindActionHandler;
Expand Down Expand Up @@ -59,6 +63,7 @@ where
+ EngineInfo
+ FindActionHandler
+ TextClient
+ MetadataInfo
+ 'static,
{
fn get_transaction(&self, transaction_hash: H256) -> Result<Option<Transaction>> {
Expand Down Expand Up @@ -294,6 +299,16 @@ where
Ok(self.client.common_params(BlockId::Latest).unwrap().network_id())
}

fn get_common_params(&self, block_number: Option<u64>) -> Result<Option<Params>> {
let block_id = block_number.map(BlockId::Number).unwrap_or(BlockId::Latest);
Ok(self.client.common_params(block_id).map(Params::from))
}

fn get_term_metadata(&self, block_number: Option<u64>) -> Result<Option<(u64, u64)>> {
let block_id = block_number.map(BlockId::Number).unwrap_or(BlockId::Latest);
Ok(self.client.metadata(block_id).map(|m| (m.last_term_finished_block_num(), m.current_term_id())))
}

fn execute_transaction(&self, tx: UnsignedTransaction, sender: PlatformAddress) -> Result<Option<String>> {
let sender_address = sender.try_address().map_err(errors::core)?;
let action = Action::try_from(tx.action).map_err(errors::conversion)?;
Expand Down
9 changes: 9 additions & 0 deletions rpc/src/v1/traits/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use cjson::scheme::Params;
use cjson::uint::Uint;
use ckey::{NetworkId, PlatformAddress, Public};
use ctypes::{BlockNumber, ShardId};
Expand Down Expand Up @@ -136,6 +137,14 @@ build_rpc_trait! {
# [rpc(name = "chain_getNetworkId")]
fn get_network_id(&self) -> Result<NetworkId>;

/// Return common params at given block number
#[rpc(name = "chain_getCommonParams")]
fn get_common_params(&self, Option<u64>) -> Result<Option<Params>>;

/// Return the current term id at given block number
#[rpc(name = "chain_getTermMetadata")]
fn get_term_metadata(&self, Option<u64>) -> Result<Option<(u64, u64)>>;

/// Execute Transactions
# [rpc(name = "chain_executeTransaction")]
fn execute_transaction(&self, UnsignedTransaction, PlatformAddress) -> Result<Option<String>>;
Expand Down
125 changes: 125 additions & 0 deletions spec/JSON-RPC.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,40 @@ When `Transaction` is included in any response, there will be an additional fiel
## Signature
`H520` for ECDSA signature | `H512` for Schnorr signature

## CommonParams

- maxExtraDataSize: `U64`
- maxAssetSchemeMetadataSize: `U64`
- maxTransferMetadataSize: `U64`
- maxTextContentSize: `U64`
- networkID: `string`
- minPayCost: `U64`
- minSetRegularKeyCost: `U64`
- minCreateShardCost: `U64`
- minSetShardOwnersCost: `U64`
- minSetShardUsersCost: `U64`
- minWrapCccCost: `U64`
- minCustomCost: `U64`
- minStoreCost: `U64`
- minRemoveCost: `U64`
- minMintAssetCost: `U64`
- minTransferAssetCost: `U64`
- minChangeAssetSchemeCost: `U64`
- minIncreaseAssetSupplyCost: `U64`
- minComposeAssetCost: `U64`
- minDecomposeAssetCost: `U64`
- minUnwrapCccCost: `U64`
- maxBodySize: `U64`
- snapshotPeriod: `U64`
- term_seconds?: `U64`
- nomination_expiration?: `U64`
- custody_period?: `U64`
- release_period?: `U64`
- max_num_of_validators?: `U64`
- min_num_of_validators?: `U64`
- delegation_threshold?: `U64`
- min_deposit?: `U64`

# Error codes

| Code | Message | Description |
Expand Down Expand Up @@ -300,6 +334,8 @@ When `Transaction` is included in any response, there will be an additional fiel
* [chain_getShardUsers](#chain_getshardusers)
* [chain_getMiningReward](#chain_getminingreward)
* [chain_getMinTransactionFee](#chain_getmintransactionfee)
* [chain_getCommonParams](#chain_getcommonparams)
* [chain_getTermMetadata](#chain_gettermmetadata)
* [chain_executeTransaction](#chain_executetransaction)
* [chain_executeVM](#chain_executevm)
* [chain_getNetworkId](#chain_getnetworkid)
Expand Down Expand Up @@ -1341,6 +1377,95 @@ Errors: `Invalid Params`

[Back to **List of methods**](#list-of-methods)

# chain_getCommonParams
Gets the common parameters.
It returns null if the block number parameter is larger than the current best block.

### Params
1. block number - `number` | `null`

### Returns
`CommonParams` | `null`

Errors: `Invalid Params`

### Request Example
```
curl \
-H 'Content-Type: application/json' \
-d '{"jsonrpc": "2.0", "method": "chain_getCommonParams", "params": [3], "id": 7}' \
localhost:8080
```

### Response Example
```
{
"jsonrpc":"2.0",
"result":{
"maxExtraDataSize":"0x20",
"maxAssetSchemeMetadataSize":"0x0400",
"maxTransferMetadataSize":"0x0100",
"maxTextContentSize":"0x0200",
"networkID":"tc",
"minPayCost":10,
"minSetRegularKeyCost":10,
"minCreateShardCost":10,
"minSetShardOwnersCost":10,
"minSetShardUsersCost":10,
"minWrapCccCost":10,
"minCustomCost":10,
"minStoreCost":10,
"minRemoveCost":10,
"minMintAssetCost":10,
"minTransferAssetCost":10,
"minChangeAssetSchemeCost":10,
"minIncreaseAssetSupplyCost":10,
"minComposeAssetCost":10,
"minDecomposeAssetCost":10,
"minUnwrapCccCost":10,
"maxBodySize":4194304,
"snapshotPeriod":16384
},
"id":7
}
```

[Back to **List of methods**](#list-of-methods)

# chain_getTermMetadata
Gets the term metadata.
It returns null if the block number parameter is larger than the current best block.

### Params
1. block number - `number` | `null`

### Returns
`[number, number]` | `null`

- The first item is the last block number that the term is closed.
- The second item is the current term id.

Errors: `Invalid Params`

### Request Example
```
curl \
-H 'Content-Type: application/json' \
-d '{"jsonrpc": "2.0", "method": "chain_getTermMetadata", "params": [53], "id": 7}' \
localhost:8080
```

### Response Example
```
{
"jsonrpc":"2.0",
"result":[43,4],
"id":7
}
```

[Back to **List of methods**](#list-of-methods)

## chain_executeTransaction
Executes the transactions and returns whether the execution is successful.

Expand Down
4 changes: 4 additions & 0 deletions state/src/item/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ impl Metadata {
pub fn last_term_finished_block_num(&self) -> u64 {
self.term.last_term_finished_block_num
}

pub fn current_term_id(&self) -> u64 {
self.term.current_term_id
}
}

impl Default for Metadata {
Expand Down
16 changes: 15 additions & 1 deletion test/src/e2e/changeParams.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.

import { expect } from "chai";
import { H256, PlatformAddress } from "codechain-primitives/lib";
import { H256, PlatformAddress, U64 } from "codechain-primitives/lib";
import { blake256 } from "codechain-sdk/lib/utils";
import "mocha";
import {
Expand Down Expand Up @@ -108,6 +108,20 @@ describe("ChangeParams", function() {
} catch (err) {
expect(err.message).contains("Too Low Fee");
}

const params = await node.sdk.rpc.sendRpcRequest(
"chain_getCommonParams",
[null]
);
expect(U64.ensure(params.minPayCost)).to.be.deep.equal(new U64(11));
});

it("should keep default common params value", async function() {
const params = await node.sdk.rpc.sendRpcRequest(
"chain_getCommonParams",
[null]
);
expect(U64.ensure(params.minPayCost)).to.be.deep.equal(new U64(10));
});

it("the parameter is applied from the next block", async function() {
Expand Down
Loading