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

Staking precompiles and caller lib #77

Closed
27 tasks
JackyWYX opened this issue Oct 6, 2021 · 4 comments
Closed
27 tasks

Staking precompiles and caller lib #77

JackyWYX opened this issue Oct 6, 2021 · 4 comments
Labels
defi DeFi applications

Comments

@JackyWYX
Copy link
Contributor

JackyWYX commented Oct 6, 2021

1. Description

Currently, Harmony staking transactions are executed in a different logic other than the normal transaction. User can either send a normal transaction to do transfer or EVM call, or do staking transaction. This approach has restricted the use of staking protocol, and some DeFi products, E.g. liquidity staking, does not have access to the staking assets in consensus.

To improve the composability of the staking, we want to add the staking functions to the precompiles, so that the staking can be called from smart contract.

For this bounty, the features to be implemented includes:

  1. Add the staking operations to precompiles in core protocol.
  2. Implement and test the hard fork logic for the protocol change.
  3. Implement and deploy a smart contract which serves as a library with all pure functions for staking features.
  4. A full test report.

2. Some background and instructions

2.1 Staking transaction logic

The following code analysis is follows git commit dc27135dff1c948d186d345721edd26b4cf7edb9

When leader propose blocks (node/worker/worker.go:164) and node insert chain (core/blockchain.go:1451), the staking transactions will follow the logic at ApplyStakingTransaction (core/state_processor.go:112) to execute the staking transaction.

Staking transaction it has its own data structure where the staking directive type and data is inlcuded�:

// staking/types/transaction.go:21
type txdata struct {
	Directive
	StakeMsg     interface{}
	...
}
// core/state_processor.go:265
func ApplyStakingTransaction(
	config *params.ChainConfig, bc ChainContext, author *common.Address, gp *GasPool, statedb *state.DB,
	header *block.Header, tx *staking.StakingTransaction, usedGas *uint64, cfg vm.Config) (receipt *types.Receipt, gas uint64, err error) {...}

One of this bounty's requirements is to add the staking processing logic to the precompiles (core/vm/contracts.go:101) and return the result as necessary.

2.2 Fork logic

It is also needed to add a hard fork logic for adding new precompiles.

type Rules struct {
	ChainID                                                                         *big.Int
	EthChainID                                                                      *big.Int
	IsCrossLink, IsEIP155, IsS3, IsReceiptLog, IsIstanbul, IsVRF, IsPrevVRF, IsSHA3 bool
}

func (c *ChainConfig) Rules(epoch *big.Int) Rules {...}

If you need more instruction, please add me on Discord Jacky | Harmony#9576 for further discussion

3. Features and acceptance criteria

3.1 Core protocol

  • Add staking logic to precompiles.
    • Precompiles can be directly called with staking data as arguments.
    • The execution of the precompiles will return result and error as necessary.
    • The execution logic will be exactly the same as ApplyStakingTransaction.
    • Staking directive:
      • Create Validator
      • Edit validator
      • Delegate
      • Undelegate
      • Collect rewards
  • Add hard fork logic for the new precompiles.
    • Aggregate the new precompile contracts to a new set.
    • Add the fork logic in evm run and Call.
    • Add the fork epoch number to localnet at LocalnetChainConfig.
  • Misc
    • New added code shall have at least 80% coverage unit test.
    • Propose PRs to merge code.
    • Test reports running localnet with detailed test steps and results.
    • Test fork logic so that there is no compatibility issue.

3.2 Smart contract

  • A smart contract served as a staking library
    • Contains five pure methods which is a one to one mapping to staking directive.
    • Smart contract deployed to testnet and mainnet after the fork.
  • Tests and examples
    • Unit test included in the repository.
    • Add example contracts to call the staking library.
    • The integration test that include some corner cases of staking.
    • Test cases can be referenced from harmony integration test.

3.3 Test reports

All test reports shall contain several test cases that include:

  1. Setup the test environment.
  2. Test cases for each test scenario.
  3. Reproducible steps and results.

The tests can be run manually. Another bounty can be added later to integrate the test cases to harmony-test repository.

3.4 Notice

The contracts and code changes are subject to code security audition, and developer may need to do some technically support even after the bounty closes.

4. Reward

$30,000 ONE

@LeoHChen LeoHChen added the defi DeFi applications label Oct 6, 2021
@gitcoinbot
Copy link

Issue Status: 1. Open 2. Started 3. Submitted 4. Done


This issue now has a funding of 174102.9244 ONE (30009.08 USD @ $0.17/ONE) attached to it.

@gitcoinbot
Copy link

gitcoinbot commented Oct 7, 2021

Issue Status: 1. Open 2. Started 3. Submitted 4. Done


Work has been started.

These users each claimed they can complete the work by 264 years, 10 months from now.
Please review their action plans below:

1) ndionxerz99 has started work.

Saya akan melakukan apa saja untuk memikki tiket
2) aungjr has started work.

What is the minimum amount of staking?
3) peekpi has started work.

great idea, I'll take care of it.
4) maxmustermann2 has started work.

Please refer to the following PR to see my proposed implementation harmony-one/harmony#3906; the smart contract implementation is available at https://github.com/MaxMustermann2/harmony-staking-precompiles
5) ckanyemba has started work.

I want to work on this project, I am developer working towards building web3 programs

Learn more on the Gitcoin Issue Details page.

MaxMustermann2 added a commit to MaxMustermann2/harmony that referenced this issue Oct 22, 2021
Create write capable precompiles that can perform staking transactions
Add hard fork logic (EpochTBD) for these precompiles
Tests for new code with at least 80% unit test coverage
Staking library + tests in MaxMustermann2/harmony-staking-precompiles
@gitcoinbot
Copy link

Issue Status: 1. Open 2. Started 3. Submitted 4. Done


Work for 174102.9244 ONE (42901.40 USD @ $0.26/ONE) has been submitted by:


@gitcoinbot
Copy link

Issue Status: 1. Open 2. Started 3. Submitted 4. Done


Work for 174102.9244 ONE (50313.48 USD @ $0.31/ONE) has been submitted by:

  1. @maxmustermann2

@LeoHChen please take a look at the submitted work:


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

No branches or pull requests

3 participants