-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merged in cita_system_contract_read (pull request #633)
Cita system contract read Approved-by: kaikaifeng <kaikai@cryptape.com> Approved-by: zhangyaning <u2@cryptape.com>
- Loading branch information
Showing
8 changed files
with
194 additions
and
20 deletions.
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
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,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); | ||
} | ||
} |
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
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,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; | ||
} | ||
} |
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,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); | ||
} |