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

Call the built-in Metadata contract causes an error. #1457

Closed
4 tasks done
yangby-cryptape opened this issue Sep 29, 2023 · 6 comments · Fixed by #1449
Closed
4 tasks done

Call the built-in Metadata contract causes an error. #1457

yangby-cryptape opened this issue Sep 29, 2023 · 6 comments · Fixed by #1449
Assignees
Labels
agenda t:bug Something isn't working

Comments

@yangby-cryptape
Copy link
Collaborator

yangby-cryptape commented Sep 29, 2023

Current Behavior

Summary

Call the built-in Metadata contract causes an error:

CKB-VM error execution reverted

How to reproduce?

There are several issues:

  • Why the error message contains "CKB-VM"?

    I didn't call any contract which uses CKB-VM.

  • How to call Metadata contract in Solidity scripts with the provided method?

    The provided method is:

    /// The input argument must includes 9 bytes schema:
    /// input[0]: an u8 on behalf of the call type. 0u8 means get metadata by block
    /// number and 1u8 means get metadata by epoch number.
    /// input[1..9]: 8 bytes of a **little endian** u64 value. If call type is 0u8,
    /// it means a block number. If call type is 1u8, it means a epoch number.
    #[derive(Default, Clone)]
    pub struct Metadata;

    p.s. Axon should NOT force users to write assembly.

  • Should there be some tests before new changes on the built-in Metadata contract? (ref: refactor!: insert metadata directly and remove genesis transactions #1454)

    Did I miss some things?
    I didn't find any tests under the test directory for the built-in Metadata contract.

    I'm neither an expert on JavaScript nor an expert on Solidity.
    Also, I haven't understood the storage model of Axon completely, yet.

    I hope someone can add such tests (or examples) to increase confidence for the changes of the built-in Metadata contract.

  • Provide an interface SOL file for the built-in Metadata contract, and use unit tests to make sure it will be updated when the contract is changed.

    Axon should NOT let users to write the interface manually.

    It's better than provide a contract SOL file without code for implementation. (ref: refactor: change metadata system contract and add annotation #1449)

Expected Behavior

.

@yangby-cryptape
Copy link
Collaborator Author

In the issue body, I told one of my opinion:

Provide an interface SOL file for the built-in Metadata contract, and use unit tests to make sure it will be updated when the contract is changed.

Now I withdraw my previous opinion.

We could NOT provide any interface of the Metadata contract.

One of those reasons is: If the interface is valid, then all Metadata items in mapping(uint64 => Metadata) metadata_set; should have the same structure at any block.

// to store all metadata with epoch as key
mapping(uint64 => Metadata) metadata_set;

That means this mapping should be refreshed after any hardfork and fill all new-added fields to all previous items.

@yangby-cryptape
Copy link
Collaborator Author

yangby-cryptape commented Oct 7, 2023

The all data stored for this contract are incompatible with EVM state storage.

Here I just give one example:

self.trie.insert(
metadata.epoch.to_be_bytes().to_vec(),
metadata.encode()?.to_vec(),
)?;

The key for the following mapping should be keccak256(concat(epoch_bytes, mapping_slot_in_bytes)).

// to store all metadata with epoch as key
mapping(uint64 => Metadata) metadata_set;

The slot here should be 0 or 1; I'm not sure now, since the type of 1st internal value in struct definition is constant.

uint64 constant U64_MAX = 2 ** 64 - 1;

I guess a constant may not cost a slot, but I haven't find any docs about it yet.

Summary

This also is a reason for "why we could NOT provide any interface of the Metadata contract".

  • If we want to create an EVM-compatible contract, obviously the Metadata contract is NOT.

  • If the Metadata contract can be EVM-incompatible, then a design document is required.

    To prove all data stored are in control (as design), not just a random implementation picked by the PR authors.

References

@KaoImin
Copy link
Contributor

KaoImin commented Oct 8, 2023

The system contracts do not allow other sol contracts to be called directly, because they are all write functions. If you want to read the data contains by system contracts, Axon provides some precompile to do this.

@yangby-cryptape
Copy link
Collaborator Author

yangby-cryptape commented Oct 8, 2023

@Flouse Flouse linked a pull request Oct 14, 2023 that will close this issue
6 tasks
@Flouse
Copy link
Contributor

Flouse commented Oct 14, 2023

After #1449

The latest interfaces of Metadata contract is

// **Notice**
// This file only defines the interface of metadata contract. The real
// implementation is in `core/executor/src/system_contract/metadata`.
interface MetadataManager {
function appendMetadata(MetadataType.Metadata memory metadata) external;
function updateConsensusConfig(
MetadataType.ConsensusConfig memory config
) external;
function setCkbRelatedInfo(
MetadataType.CkbRelatedInfo memory info
) external;
}

  • The getMetadata function was removed, instead by axon_getMetadataByNumber RPC,
    because the returns (IMetadataManager.Metadata) may be changed if a hardfork happened.

@Flouse
Copy link
Contributor

Flouse commented Oct 18, 2023

  • Why the error message contains "CKB-VM"?
    I didn't call any contract which uses CKB-VM.

fixed by https://github.com/axonweb3/axon/pull/1484/files#diff-7dedadc749f34a439efcf21029dbc46d5551c34bafd11e30faf0f6d3ee688650R57-R137

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

Successfully merging a pull request may close this issue.

3 participants