Skip to content

Commit

Permalink
Merged in cita_system_contract_read (pull request #633)
Browse files Browse the repository at this point in the history
Cita system contract read

Approved-by: kaikaifeng <kaikai@cryptape.com>
Approved-by: zhangyaning <u2@cryptape.com>
  • Loading branch information
raowenbo authored and u2 committed Jan 31, 2018
2 parents 2a6b9f4 + f78f75a commit 8c7460c
Show file tree
Hide file tree
Showing 8 changed files with 194 additions and 20 deletions.
36 changes: 22 additions & 14 deletions cita-executor/core/genesis.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions cita-executor/core/src/contracts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
pub mod node_manager;
pub mod account_manager;
pub mod quota_manager;
pub mod param_constant;

pub use self::account_manager::AccountManager;
pub use self::node_manager::NodeManager;
pub use self::param_constant::ParamConstant;
pub use self::quota_manager::{AccountGasLimit, QuotaManager};

use libexecutor::call_request::CallRequest;
Expand Down
8 changes: 4 additions & 4 deletions cita-executor/core/src/contracts/node_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ mod tests {
assert_eq!(
nodes,
vec![
H160::from_str("0b69ddaa0077b74d8573b09ef001452a8dd4bb4f").unwrap(),
H160::from_str("41ab058a475327a2ac2607f2a4e9c65263e7e7ad").unwrap(),
H160::from_str("6b70dc983106f701e1eb2ce6b22a4139a83a8ac3").unwrap(),
H160::from_str("93895adf32fb397120061ab858b378c7203bee32").unwrap(),
H160::from_str("9d1ce3f86579a0aeee74ca408a0a5d072325a38b").unwrap(),
H160::from_str("b207dd72051697ebd8fd9b5296136787ef7f7e1d").unwrap(),
H160::from_str("a7844522abf3d37ba16445167d0b7e88efa81584").unwrap(),
H160::from_str("2b3389b307c8fcf56bc8da72a1ebcadb939cc764").unwrap(),
]
)
}
Expand Down
115 changes: 115 additions & 0 deletions cita-executor/core/src/contracts/param_constant.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// CITA
// Copyright 2016-2017 Cryptape Technologies LLC.

// This program 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/>.

//! Constant system contract parameters

use super::ContractCallExt;
use super::encode_contract_name;
use ethabi::{decode, ParamType};
use libexecutor::executor::Executor;
use std::str::FromStr;
use util::*;

const VALID_NUMBER: &'static [u8] = &*b"getNumber()";
const PERMISSION_CHECK: &'static [u8] = &*b"getPermissionCheck()";
const QUOTA_CHECK: &'static [u8] = &*b"getQuotaCheck()";

lazy_static! {
static ref VALID_NUMBER_ENCODED: Vec<u8> = encode_contract_name(VALID_NUMBER);
static ref PERMISSION_CHECK_ENCODED: Vec<u8> = encode_contract_name(PERMISSION_CHECK);
static ref QUOTA_CHECK_ENCODED: Vec<u8> = encode_contract_name(QUOTA_CHECK);
static ref CONTRACT_ADDRESS: H160 = H160::from_str("0000000000000000000000000000000031415926").unwrap();
}

pub struct ParamConstant;

impl ParamConstant {
/// Delay block number before validate
pub fn valid_number(executor: &Executor) -> u64 {
let output = executor.call_contract_method(&*CONTRACT_ADDRESS, &*VALID_NUMBER_ENCODED.as_slice());
trace!("delay block number output: {:?}", output);

let mut decoded = decode(&[ParamType::Uint(256)], &output).expect("decode delay number");
let delay_number = decoded.remove(0);
let delay_number = delay_number.to_uint();

let h256 = H256::from(delay_number.expect("decode delay number"));
debug!("delay block number: {:?}", h256.low_u64());
h256.low_u64()
}

/// Whether check permission or not
pub fn permission_check(executor: &Executor) -> bool {
let output = executor.call_contract_method(&*CONTRACT_ADDRESS, &*PERMISSION_CHECK_ENCODED.as_slice());
trace!("check permission output: {:?}", output);

let mut decoded = decode(&[ParamType::Bool], &output).expect("decode check permission");
let check_permission = decoded.remove(0);
let check_permission = check_permission.to_bool();

let check = check_permission.expect("decode check permission");
debug!("check permission: {:?}", check);
check
}

/// Whether check quota or not
pub fn quota_check(executor: &Executor) -> bool {
let output = executor.call_contract_method(&*CONTRACT_ADDRESS, &*QUOTA_CHECK_ENCODED.as_slice());
trace!("check quota output: {:?}", output);

let mut decoded = decode(&[ParamType::Bool], &output).expect("decode check quota");
let check_quota = decoded.remove(0);
let check_quota = check_quota.to_bool();

let check = check_quota.expect("decode check quota");
debug!("check quota: {:?}", check);
check
}
}

#[cfg(test)]
mod tests {
extern crate logger;
extern crate mktemp;

use super::*;
use tests::helpers::init_executor;

#[test]
fn test_valid_number() {
let executor = init_executor();
let number = ParamConstant::valid_number(&executor);

assert_eq!(number, 1);
}

#[test]
fn test_permission_check() {
let executor = init_executor();
let check_permission = ParamConstant::permission_check(&executor);

assert_eq!(check_permission, true);
}

#[test]
fn test_quota_check() {
let executor = init_executor();
let check_quota = ParamConstant::quota_check(&executor);

assert_eq!(check_quota, true);
}
}
8 changes: 6 additions & 2 deletions scripts/admintool/create_genesis.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
'0x00000000000000000000000000000000013241a4': {'file': 'system/permission_manager.sol',
'name': 'PermissionManager'},
'0x00000000000000000000000000000000013241a5': {'file': 'permission/permission_system.sol',
'name': 'PermissionSystem'}
'name': 'PermissionSystem'},
'0x0000000000000000000000000000000031415926': {'file': 'system/param_constant.sol',
'name': 'ParamConstant'}
}

def init_contracts(nodes):
Expand All @@ -46,8 +48,10 @@ def init_contracts(nodes):
)

ct = ContractTranslator(simple_data['abi'])
if (address == '0x00000000000000000000000000000000013241a3'):
if address == '0x00000000000000000000000000000000013241a3':
extra = (ct.encode_constructor_arguments([nodes[address]]) if nodes[address] else b'')
elif address == '0x0000000000000000000000000000000031415926':
extra = (ct.encode_constructor_arguments([nodes[address][0], nodes[address][1], nodes[address][2]]) if nodes[address] else b'')
else:
extra = (ct.encode_constructor_arguments([nodes[address][0], nodes[address][1]]) if nodes[address] else b'')

Expand Down
5 changes: 5 additions & 0 deletions scripts/admintool/init_data_example.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,10 @@
[
"0xb3b5da596b4ba5caa603afd98c08c294a68ff34d"
]
],
"0x0000000000000000000000000000000031415926": [
1,
true,
true
]
}
29 changes: 29 additions & 0 deletions scripts/contracts/system/param_constant.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
pragma solidity ^0.4.18;

import "./param_interface.sol";

contract ParamConstant is ParamConstantInterface {

uint valid_number;
bool check_permission;
bool check_quota;

/// Setup
function ParamConstant(uint _num, bool _perm, bool _quota) public {
valid_number = _num;
check_permission = _perm;
check_quota = _quota;
}

function getNumber() public view returns (uint) {
return valid_number;
}

function getPermissionCheck() public view returns (bool) {
return check_permission;
}

function getQuotaCheck() public view returns (bool) {
return check_quota;
}
}
11 changes: 11 additions & 0 deletions scripts/contracts/system/param_interface.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
pragma solidity ^0.4.18;

interface ParamConstantInterface {

/// Get the valid number in the system
function getNumber() public view returns (uint);
/// Whether check permission in the system or not, true represents check and false represents don't check.
function getPermissionCheck() public view returns (bool);
/// Whether check quota in the system or not, true represents check and false represents don't check.
function getQuotaCheck() public view returns (bool);
}

0 comments on commit 8c7460c

Please sign in to comment.