Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System contracts ckb_light_client and image_cell are saved into different MPT trie. #1441

Closed
yangby-cryptape opened this issue Sep 20, 2023 · 0 comments · Fixed by #1449
Closed
Assignees
Labels
document t:bug Something isn't working

Comments

@yangby-cryptape
Copy link
Collaborator

yangby-cryptape commented Sep 20, 2023

Current Behavior

  • Initialize system contracts when Axon starts.

    system_contract::init(db_group.inner_db(), &mut backend);

  • Generate MPT changes.

    if current_cell_root.is_zero() {
    let changes = generate_mpt_root_changes(adapter, IMAGE_CELL_CONTRACT_ADDRESS);
    adapter.apply(changes, vec![], false);
    }

  • Two changes are created.

    vec![
    Apply::Modify {
    address: CKB_LIGHT_CLIENT_CONTRACT_ADDRESS,
    basic: backend.basic(CKB_LIGHT_CLIENT_CONTRACT_ADDRESS),
    code: None,
    storage: storage_changes.clone(),
    reset_storage: false,
    },
    Apply::Modify {
    address: IMAGE_CELL_CONTRACT_ADDRESS,
    basic: backend.basic(IMAGE_CELL_CONTRACT_ADDRESS),
    code: None,
    storage: storage_changes,
    reset_storage: false,
    },
    ]

  • Two changes are applied one by one.

    for apply in values.into_iter() {
    match apply {
    Apply::Modify {
    address,
    basic,
    code,
    storage,
    reset_storage,
    } => {
    let is_empty = self.apply(address, basic, code, storage, reset_storage);
    if is_empty && delete_empty {
    self.inner.trie.remove(address.as_bytes()).unwrap();
    }
    }

  • Since there two changes have different addresses.

    • Each time a new old_account is initialized.

      let old_account = match self.inner.trie.get(address.as_bytes()) {
      Ok(Some(raw)) => Account::decode(raw).unwrap(),
      _ => Account {
      nonce: U256::zero(),
      balance: U256::zero(),
      storage_root: RLP_NULL,
      code_hash: NIL_DATA,
      },
      };

    • Then, each time a new storage_trie is initialized.

      let storage_root = if reset_storage {
      RLP_NULL
      } else {
      old_account.storage_root
      };

      let mut storage_trie = if storage_root == RLP_NULL {
      MPTTrie::new(Arc::clone(&self.inner.db))
      } else {
      MPTTrie::from_root(old_account.storage_root, Arc::clone(&self.inner.db)).unwrap()
      };

  • So, these two system contracts are insert into two different MPT trie.

  • Let's go back to the system contracts initialization.

    • When try to get the current_cell_root, only address CKB_LIGHT_CLIENT_CONTRACT_ADDRESS is used.

      let current_cell_root =
      adapter.storage(CKB_LIGHT_CLIENT_CONTRACT_ADDRESS, *HEADER_CELL_ROOT_KEY);

      ❓ So, its storage_root won't contains the image cell contract. I'm not sure. Is this correct?

    • When try to generate MPT changes, only address IMAGE_CELL_CONTRACT_ADDRESS is used.

      let changes = generate_mpt_root_changes(adapter, IMAGE_CELL_CONTRACT_ADDRESS);

      ❓ Can anyone guess the fact this function call also generate changes for the ckb light client contract too, even just an address IMAGE_CELL_CONTRACT_ADDRESS is passed.

    • ❓ Can anyone guess that the above two function calls are a pair: one is getter, and one is setter, even they use different addresses as arguments.

Expected Behavior

.

Axon version

main branch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
document t:bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants