This repository has been archived by the owner on Nov 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #152 from nervosnetwork/fix-calculate_fee
Fix the fee calculation
- Loading branch information
Showing
14 changed files
with
196 additions
and
29 deletions.
There are no files selected for viewing
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
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,48 @@ | ||
#include "test_utils.h" | ||
#include "gw_def.h" | ||
#include "generator_utils.h" | ||
#include "../polyjuice_utils.h" | ||
#include <assert.h> | ||
|
||
void test(uint128_t gas_price, uint64_t gas_used, uint256_t expected_fee) { | ||
uint256_t result = calculate_fee(gas_price, gas_used); | ||
assert(gw_uint256_cmp(result, expected_fee) == GW_UINT256_EQUAL); | ||
} | ||
|
||
int main() { | ||
uint256_t expected_result = {0}; | ||
gw_uint256_cmp(expected_result, expected_result); | ||
test(0, 0, expected_result); | ||
test(0, 1, expected_result); | ||
test(1, 0, expected_result); | ||
|
||
gw_uint256_one(&expected_result); | ||
test(1, 1, expected_result); | ||
|
||
uint128_t gas_price = 11; | ||
expected_result.array[0] = 22; | ||
test(gas_price, 2, expected_result); | ||
|
||
gas_price = 0xfedbca9876543210ULL; | ||
expected_result.array[0] = 0x76543210; | ||
expected_result.array[1] = 0xfedbca98; | ||
test(gas_price, 1, expected_result); | ||
test(gas_price, 2, {0xECA86420UL, 0xFDB79530UL, 0x1UL}); | ||
|
||
gas_price = ((uint128_t)0xF0F0F0F0F0F0F0F0 << 64) | 0xF0F0F0F0F0F0F0F0; | ||
gw_uint256_zero(&expected_result); | ||
test(gas_price, 0, expected_result); | ||
test(gas_price, 1, {0xF0F0F0F0UL, 0xF0F0F0F0UL, 0xF0F0F0F0UL, 0xF0F0F0F0UL}); | ||
|
||
uint64_t gas_used = 0xaaaaaaaaaaaaaaaaULL; | ||
test(gas_price, gas_used, {0x5f5f5f60, 0x5f5f5f5f, 0xffffffff, 0xffffffff, | ||
0xA0A0A09F, 0xA0A0A0A0, 0x00000000, 0x00000000}); | ||
|
||
const uint64_t MAX_UINT64 = 0xFFFFFFFFFFFFFFFF; | ||
gas_used = MAX_UINT64; | ||
gas_price = ((uint128_t)MAX_UINT64 << 64) | MAX_UINT64; | ||
test(gas_price, gas_used, {0x00000001, 0x00000000, 0xffffffff, 0xffffffff, | ||
0xFFFFFFFE, 0xFFFFFFFF, 0x00000000, 0x00000000}); | ||
|
||
return 0; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "polyjuice-tests" | ||
version = "1.1.0" | ||
version = "1.1.5" | ||
authors = ["Linfeng Qian <[email protected]>"] | ||
edition = "2018" | ||
|
||
|
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
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 |
---|---|---|
|
@@ -30,3 +30,4 @@ pub(crate) mod sudt_erc20_proxy; | |
mod beacon_proxy; | ||
mod error; | ||
mod eth_addr_reg; | ||
mod utils; |
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,94 @@ | ||
use crate::helper::L2TX_MAX_CYCLES; | ||
use ckb_vm::{ | ||
machine::asm::{AsmCoreMachine, AsmMachine}, | ||
memory::Memory, | ||
registers::{A0, A7}, | ||
DefaultMachineBuilder, Error as VMError, Register, SupportMachine, Syscalls, | ||
}; | ||
use gw_types::bytes::Bytes; | ||
|
||
const BINARY: &[u8] = include_bytes!("../../../build/test_calc_fee"); | ||
const DEBUG_PRINT_SYSCALL_NUMBER: u64 = 2177; | ||
|
||
pub struct L2Syscalls; | ||
|
||
impl<Mac: SupportMachine> Syscalls<Mac> for L2Syscalls { | ||
fn initialize(&mut self, _machine: &mut Mac) -> Result<(), VMError> { | ||
Ok(()) | ||
} | ||
|
||
fn ecall(&mut self, machine: &mut Mac) -> Result<bool, VMError> { | ||
let code = machine.registers()[A7].to_u64(); | ||
if code != DEBUG_PRINT_SYSCALL_NUMBER { | ||
println!("code: {}", code); | ||
} | ||
match code { | ||
DEBUG_PRINT_SYSCALL_NUMBER => { | ||
self.output_debug(machine)?; | ||
Ok(true) | ||
} | ||
_ => Ok(false), | ||
} | ||
} | ||
} | ||
|
||
impl L2Syscalls { | ||
fn output_debug<Mac: SupportMachine>(&self, machine: &mut Mac) -> Result<(), VMError> { | ||
let mut addr = machine.registers()[A0].to_u64(); | ||
let mut buffer = Vec::new(); | ||
|
||
loop { | ||
let byte = machine | ||
.memory_mut() | ||
.load8(&Mac::REG::from_u64(addr))? | ||
.to_u8(); | ||
if byte == 0 { | ||
break; | ||
} | ||
buffer.push(byte); | ||
addr += 1; | ||
} | ||
|
||
let s = String::from_utf8(buffer).map_err(|_| VMError::ParseError)?; | ||
println!("[debug]: {}", s); | ||
Ok(()) | ||
} | ||
} | ||
|
||
// TODO: refactor | ||
struct AsmCoreMachineParams { | ||
pub vm_isa: u8, | ||
pub vm_version: u32, | ||
} | ||
|
||
impl AsmCoreMachineParams { | ||
pub fn with_version(vm_version: u32) -> Result<AsmCoreMachineParams, VMError> { | ||
if vm_version == 0 { | ||
Ok(AsmCoreMachineParams { | ||
vm_isa: ckb_vm::ISA_IMC, | ||
vm_version: ckb_vm::machine::VERSION0, | ||
}) | ||
} else if vm_version == 1 { | ||
Ok(AsmCoreMachineParams { | ||
vm_isa: ckb_vm::ISA_IMC | ckb_vm::ISA_B | ckb_vm::ISA_MOP, | ||
vm_version: ckb_vm::machine::VERSION1, | ||
}) | ||
} else { | ||
Err(VMError::InvalidVersion) | ||
} | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_calc_fee() { | ||
let binary: Bytes = BINARY.to_vec().into(); | ||
|
||
let params = AsmCoreMachineParams::with_version(1).unwrap(); | ||
let core_machine = AsmCoreMachine::new(params.vm_isa, params.vm_version, L2TX_MAX_CYCLES); | ||
let machine_builder = DefaultMachineBuilder::new(core_machine).syscall(Box::new(L2Syscalls)); | ||
let mut machine = AsmMachine::new(machine_builder.build(), None); | ||
|
||
machine.load_program(&binary, &[]).unwrap(); | ||
let code = machine.run().unwrap(); | ||
assert_eq!(code, 0); | ||
} |