Skip to content
This repository has been archived by the owner on Nov 18, 2022. It is now read-only.

Commit

Permalink
Merge pull request #128 from nervosnetwork/fix-chain_id
Browse files Browse the repository at this point in the history
fix: chain_id = compatible_chain_id(u32) | creator_account_id(u32)
  • Loading branch information
Flouse authored Mar 10, 2022
2 parents 7c986af + 50cd9fc commit ef0ff0a
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 10 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,12 @@ jobs:

- name: Checksums of generator and validator
run: sha256sum build/generator build/generator_log build/validator build/validator_log


integration-test:
uses: nervosnetwork/godwoken-tests/.github/workflows/reusable-integration-test-v1.yml@develop
with:
# github.head_ref: The head_ref or source branch of the pull request in a workflow run. This property is only available when the event that triggers a workflow run is either pull_request or pull_request_target.
# github.ref: The branch or tag ref that triggered the workflow run. For branches this is the format refs/heads/<branch_name>, and for tags it is refs/tags/<tag_name>.
polyjuice_ref: ${{ github.head_ref || github.ref }}
kicker_ref: refs/pull/179/head
19 changes: 18 additions & 1 deletion c/polyjuice.h
Original file line number Diff line number Diff line change
Expand Up @@ -327,12 +327,19 @@ struct evmc_tx_context get_tx_context(struct evmc_host_context* context) {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x08, 0xe1, 0xbc, 0x9b, 0xf0, 0x40, 0x00,
};
/* chain_id = creator_account_id */

/* chain_id = compatible_chain_id(u32) | creator_account_id(u32) */
uint8_t *compatible_chain_id_ptr = (uint8_t *)(&g_compatible_chain_id);
ctx.chain_id.bytes[27] = compatible_chain_id_ptr[0];
ctx.chain_id.bytes[26] = compatible_chain_id_ptr[1];
ctx.chain_id.bytes[25] = compatible_chain_id_ptr[2];
ctx.chain_id.bytes[24] = compatible_chain_id_ptr[3];
uint8_t *creator_account_id_ptr = (uint8_t *)(&g_creator_account_id);
ctx.chain_id.bytes[31] = creator_account_id_ptr[0];
ctx.chain_id.bytes[30] = creator_account_id_ptr[1];
ctx.chain_id.bytes[29] = creator_account_id_ptr[2];
ctx.chain_id.bytes[28] = creator_account_id_ptr[3];

return ctx;
}

Expand Down Expand Up @@ -719,6 +726,7 @@ int check_destructed(gw_context_t* ctx, uint32_t to_id) {

/**
* load the following global values:
* - g_compatible_chain_id
* - g_creator_account_id
* - g_script_hash_type
* - g_rollup_script_hash
Expand Down Expand Up @@ -775,6 +783,15 @@ int load_globals(gw_context_t* ctx, uint32_t to_id) {
debug_print_data("invalid to account script args", raw_args_seg.ptr, raw_args_seg.size);
return FATAL_POLYJUICE;
}
/** read g_compatible_chain_id from Godwoken RollupConfig */
mol_seg_t rollup_config_seg;
rollup_config_seg.ptr = ctx->rollup_config;
rollup_config_seg.size = ctx->rollup_config_size;
mol_seg_t id_u32_seg = MolReader_RollupConfig_get_compatible_chain_id(&rollup_config_seg);
memcpy(&g_compatible_chain_id, id_u32_seg.ptr, id_u32_seg.size);
debug_print_int("compatible_chain_id", g_compatible_chain_id);
debug_print_int("creator_account_id", g_creator_account_id);

/** read rollup_script_hash and g_sudt_id from creator account */
memcpy(g_rollup_script_hash, creator_raw_args_seg.ptr, 32);
memcpy(&g_sudt_id, creator_raw_args_seg.ptr + 32, sizeof(uint32_t));
Expand Down
6 changes: 5 additions & 1 deletion c/polyjuice_globals.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef POLYJUICE_GLOBALS_H
#define POLYJUICE_GLOBALS_H

#define POLYJUICE_VERSION "v1.0.0-alpha"
#define POLYJUICE_VERSION "v1.0.0-beta"

#define DEFAULT_SHORT_SCRIPT_HASH_LEN 20
#define ETH_ADDRESS_LEN 20
Expand All @@ -24,6 +24,10 @@ static uint32_t g_sudt_id = UINT32_MAX;
static uint8_t g_created_address[20] = {0};
static uint32_t g_created_id = UINT32_MAX;

/**
* @brief compatible_chain_id in Godwoken RollupConfig
*/
static uint32_t g_compatible_chain_id = UINT32_MAX;
/**
* creator_account, known as root account
* @see https://github.com/nervosnetwork/godwoken/blob/5735d8f/docs/life_of_a_polyjuice_transaction.md#root-account--deployment
Expand Down
5 changes: 3 additions & 2 deletions docs/EVM-compatible.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ When you send an ethereum transaction, the transaction is converted to Godwoken
| EVM Opcode | Solidity Usage | Behavior in Polyjuice | Behavior in EVM |
| - | - | - | - |
| COINBASE | `block.coinbase` | address of the block_producer | address of the current block's miner |
| CHAINID | `chain_id()` | creator_account_id | Istanbul hardfork, EIP-1344: current network's chain id |
| GASLIMIT | `block.gaslimit` | 12,500,000 | current block's gas limit |
| DIFFICULTY | `block.difficulty` | 2,500,000,000,000,000 | current block's difficulty |

## Others

* transaction context
* chain_id is [creator_account_id](https://github.com/nervosnetwork/godwoken/blob/5735d8f/docs/life_of_a_polyjuice_transaction.md#root-account--deployment)
* `chain_id` consists up of two parts: [**compatible_chain_id(u32) | [creator_account_id]()(u32)**]
- `compatible_chain_id` is defined in Godwoken [RollupConfig](https://github.com/nervosnetwork/godwoken/blob/acc6614/crates/types/schemas/godwoken.mol#L64).
- `creator_account` is known as [the root account of Polyjuice](https://github.com/nervosnetwork/godwoken/blob/5735d8f/docs/life_of_a_polyjuice_transaction.md#root-account--deployment).
* block gas limit is `12500000`, and is not block level limit, every transaction can reach the limit
* block difficulty is always `2500000000000000`
* The `transfer value` can not exceed uint128:MAX
Expand Down
2 changes: 2 additions & 0 deletions polyjuice-tests/src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ use std::{fs, io::Read, path::PathBuf};
pub use gw_common::builtins::{CKB_SUDT_ACCOUNT_ID, RESERVED_ACCOUNT_ID};
pub const ETH_ADDRESS_REGISTRY_ACCOUNT_ID: u32 = 2;
pub const CREATOR_ACCOUNT_ID: u32 = 3;
pub const COMPATIBLE_CHAIN_ID: u32 = 202203;

pub const L2TX_MAX_CYCLES: u64 = 7000_0000;

Expand Down Expand Up @@ -484,6 +485,7 @@ pub fn setup() -> (Store, DummyState, Generator) {
account_lock_manage
.register_lock_algorithm(SECP_LOCK_CODE_HASH.into(), Box::new(Secp256k1::default()));
let rollup_config = RollupConfig::new_builder()
.compatible_chain_id(COMPATIBLE_CHAIN_ID.pack())
.l2_sudt_validator_script_type_hash(SUDT_VALIDATOR_SCRIPT_TYPE_HASH.pack())
.allowed_contract_type_hashes(
vec![
Expand Down
13 changes: 7 additions & 6 deletions polyjuice-tests/src/test_cases/get_chain_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::helper::{
build_eth_l2_script, new_block_info, new_contract_account_script, setup, PolyjuiceArgsBuilder,
CKB_SUDT_ACCOUNT_ID, CREATOR_ACCOUNT_ID, L2TX_MAX_CYCLES,
CKB_SUDT_ACCOUNT_ID, CREATOR_ACCOUNT_ID, L2TX_MAX_CYCLES, COMPATIBLE_CHAIN_ID,
};
use gw_common::state::State;
use gw_generator::traits::StateExt;
Expand Down Expand Up @@ -75,7 +75,6 @@ fn test_get_chain_id() {
.unwrap();
println!("balance of {} = {}", from_id, from_balance2);

let chain_id: u32 = CREATOR_ACCOUNT_ID;
{
// call GetChainId.get()
let block_info = new_block_info(0, 3, 0);
Expand Down Expand Up @@ -104,9 +103,11 @@ fn test_get_chain_id() {
)
.expect("construct");
state.apply_run_result(&run_result).expect("update state");
let mut expected_return_data = vec![0u8; 32];
expected_return_data[28..32].copy_from_slice(&chain_id.to_be_bytes()[..]);
assert_eq!(run_result.return_data, expected_return_data);
// println!("result {:?}", run_result);

/* chain_id = compatible_chain_id(u32) | creator_account_id(u32) */
let mut expected_chain_id = vec![0u8; 32];
expected_chain_id[28..32].copy_from_slice(&CREATOR_ACCOUNT_ID.to_be_bytes()[..]);
expected_chain_id[24..28].copy_from_slice(&COMPATIBLE_CHAIN_ID.to_be_bytes()[..]);
assert_eq!(run_result.return_data, expected_chain_id);
}
}

0 comments on commit ef0ff0a

Please sign in to comment.