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

imp(staking): implement the CreateValidator function for staking precompiled contract #2030

Merged
merged 20 commits into from Nov 16, 2023

Conversation

luchenqun
Copy link
Contributor

@luchenqun luchenqun commented Nov 11, 2023

Description

I noticed that evmos have already implemented functions like delegate, undelegate, and redelegate and so on in the precompiled contract. However, I think the important createValidator function is missing. Additionally, the createValidator function is not currently implemented in evmosjs either, so the only way to create a validator is through the CLI command evmosd tx staking create-validator. Due to these reasons, I have implemented the createValidator function in the precompiled contract.

Prerequisites

Start two nodes, named node0 and node1. Node0 is a validator node and node1 is a non-validator node. Node0 and node1 have already been linked via p2p.

Currently, there is only one validator node produces and signs the block
1

Check the list of validators, which currently has only one validator.
2

Objective

Call the createValidator method in the Staking precompiled contract make node1 become a validator node.

Steps

Step 1

Use the command evmosd tendermint show-validator --home=./nodes/node1/evmosd to obtain the node public key for node1. Let's assume the obtained content is as follows:

{
    "@type":"/cosmos.crypto.ed25519.PubKey",
    "key":"OD/HHcEPGg9z+sGO1OCFItC1oup2pHUVbyXZKxOtI/o="
}

The key is the node1 public key.

Step 2

Use Remix to connect to node1. Note: Be sure to connect to node1 using Remix's External HTTP Provider method, as calling the contract requires online signing.

Then, call the createValidator method in the Staking precompiled contract with the following parameters:

  • description: ["node1","I'm identity","http://cosmos.lucq.fun","0x0000000000000000000000000000000000000800","I'm details"]
  • commissionRates: ["100000000000000000","100000000000000000","100000000000000000"]
  • minSelfDelegation: 1
  • delegatorAddress: 0xC79b690bA90B8e04625b8a11C20e8b356f5996d2
  • validatorAddress: evmosvaloper1c7dkjzafpw8qgcjm3gguyr5tx4h4n9kjuquq78
  • pubkey: OD/HHcEPGg9z+sGO1OCFItC1oup2pHUVbyXZKxOtI/o=
  • value: 100000000000000000000

Note that the input parameters in the commissionRates field should be decimal numbers with a precision of 18. So, the decimal values shown above are all 0.1. Replace delegatorAddress, validatorAddress, and pubkey with your actual values. Then send the transaction using Remix.
3

Note: During the test, I found that the estimated gas was not very accurate, and the transaction failed due to insufficient gas. After repeated testing, gas consumption is approximately 158870, so it is recommended to set gasLimit to 200000 for testing. Therefore, transactions sent using remix may fail due to insufficient gas, so it is recommended to assemble the transaction yourself and set the gaslimit to 20,000.
For example, the following is a test case I sent using eth_sendTransaction. 0x30d40 = 200000

{
    "method":"eth_sendTransaction",
    "jsonrpc":"2.0",
    "id":172353,
    "params":[
        {
            "from":"0x293468ba3822d32e6bdE800ed9e49dB40D77aa2a",
            "to":"0x0000000000000000000000000000000000000800",
            "gas":"0x30d40",
            "gasPrice":"0x0",
            "value":"0x0",
            "data":"0x458186450000000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000016345785d8a0000000000000000000000000000000000000000000000000000016345785d8a0000000000000000000000000000000000000000000000000000016345785d8a00000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000293468ba3822d32e6bde800ed9e49db40d77aa2a000000000000000000000000000000000000000000000000000000000000032000000000000000000000000000000000000000000000000000000000000003800000000000000000000000000000000000000000000000056bc75e2d6310000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001c000000000000000000000000000000000000000000000000000000000000000056e6f646531000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c49276d206964656e7469747900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000013687474703a2f2f7777772e6c7563712e66756e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000002a30783030303030303030303030303030303030303030303030303030303030303030303030303038303000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000b49276d2064657461696c73000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003365766d6f7376616c6f7065723139793678337733637974666a75363737737138646e6579616b737868303233327339326c653800000000000000000000000000000000000000000000000000000000000000000000000000000000000000002c306d313575694a4e55466f7a58576754675445374f63514e434b4d47317478676b696e46794463335948593d0000000000000000000000000000000000000000"
        }
    ]
}

Step 3

Verify that node1 has become a validator.

The list of validators should now display an additional validator called node1.
4

There are currently two validators node produces and signs the block
5

Query evm type transactions.
8

Query cosmos type transactions.
7

The tool website used in some of the above screenshots is http://cosmos.lucq.fun/

TODO

@luchenqun luchenqun requested a review from a team as a code owner November 11, 2023 14:55
@luchenqun luchenqun requested review from facs95 and 0xstepit and removed request for a team November 11, 2023 14:55
precompiles/staking/events.go Dismissed Show dismissed Hide dismissed
Copy link

codecov bot commented Nov 11, 2023

Codecov Report

Merging #2030 (4afa195) into main (17c0a46) will decrease coverage by 0.40%.
Report is 5 commits behind head on main.
The diff coverage is 0.00%.

❗ Current head 4afa195 differs from pull request most recent head 5c4756f. Consider uploading reports for the commit 5c4756f to get more accurate results

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #2030      +/-   ##
==========================================
- Coverage   70.39%   69.99%   -0.40%     
==========================================
  Files         338      335       -3     
  Lines       25223    25228       +5     
==========================================
- Hits        17756    17659      -97     
- Misses       6558     6663     +105     
+ Partials      909      906       -3     
Files Coverage Δ
precompiles/staking/staking.go 82.95% <0.00%> (-1.93%) ⬇️
precompiles/staking/tx.go 79.31% <0.00%> (-10.31%) ⬇️
precompiles/staking/events.go 63.42% <0.00%> (-11.85%) ⬇️
precompiles/staking/types.go 73.40% <0.00%> (-14.61%) ⬇️

... and 4 files with indirect coverage changes

Copy link
Contributor

@Vvaradinov Vvaradinov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome work @luchenqun thank you for this PR. Left some comments on ways you can refactor to improve readability and consistency.

precompiles/staking/events.go Outdated Show resolved Hide resolved
precompiles/staking/tx.go Outdated Show resolved Hide resolved
precompiles/staking/tx.go Outdated Show resolved Hide resolved
precompiles/staking/tx.go Outdated Show resolved Hide resolved
precompiles/staking/types.go Outdated Show resolved Hide resolved
precompiles/staking/types.go Outdated Show resolved Hide resolved
precompiles/staking/events.go Outdated Show resolved Hide resolved
precompiles/common/abi.go Outdated Show resolved Hide resolved
precompiles/staking/events.go Outdated Show resolved Hide resolved
precompiles/staking/StakingI.sol Outdated Show resolved Hide resolved
precompiles/staking/events.go Outdated Show resolved Hide resolved
precompiles/staking/tx.go Outdated Show resolved Hide resolved
precompiles/staking/tx.go Outdated Show resolved Hide resolved
precompiles/staking/types.go Outdated Show resolved Hide resolved
precompiles/staking/types.go Outdated Show resolved Hide resolved
precompiles/staking/types.go Outdated Show resolved Hide resolved
Copy link
Contributor

@fedekunze fedekunze left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left some final comments

precompiles/staking/staking.go Show resolved Hide resolved
precompiles/staking/StakingI.sol Outdated Show resolved Hide resolved
precompiles/staking/StakingI.sol Outdated Show resolved Hide resolved
precompiles/staking/StakingI.sol Outdated Show resolved Hide resolved
@CLAassistant
Copy link

CLAassistant commented Nov 15, 2023

CLA assistant check
All committers have signed the CLA.

precompiles/staking/events.go Outdated Show resolved Hide resolved
precompiles/staking/abi.json Outdated Show resolved Hide resolved
…datorAddress to address indexed validatorAddress
Copy link
Contributor

@Vvaradinov Vvaradinov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @luchenqun great work and thank you for updating this. Left some additional comments.

precompiles/staking/StakingI.sol Outdated Show resolved Hide resolved
precompiles/staking/events.go Outdated Show resolved Hide resolved
precompiles/staking/tx.go Outdated Show resolved Hide resolved
precompiles/staking/tx.go Outdated Show resolved Hide resolved
precompiles/staking/staking.go Show resolved Hide resolved
Copy link
Contributor

@fedekunze fedekunze left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM thanks!

Copy link
Contributor

@Vvaradinov Vvaradinov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks for all the work @luchenqun

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants