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

Local xdpos validator upgrade 2 #628

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions common/types.go

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions consensus/XDPoS/engines/engine_v2/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,13 @@ func (x *XDPoS_v2) Finalize(chain consensus.ChainReader, header *types.Header, s
}
}

// upgrade smart contract after reward distribution
number := header.Number.Uint64()
log.Info("poc", "fun", "Finalize", "number", number)
if number == 99999999999 {
UpgradeXDCValidtorV2(state)
}

// the state remains as is and uncles are dropped
header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
header.UncleHash = types.CalcUncleHash(nil)
Expand Down Expand Up @@ -1122,3 +1129,11 @@ func (x *XDPoS_v2) periodicJob() {
func (x *XDPoS_v2) GetLatestCommittedBlockInfo() *types.BlockInfo {
return x.highestCommitBlock
}

func UpgradeXDCValidtorV2(statedb *state.StateDB) {
log.Info("poc", "MasternodeVotingSMC", common.MasternodeVotingSMC, "step", 1)
addr := common.HexToAddress(common.MasternodeVotingSMC)
// upgrade code
statedb.SetCode(addr, common.FromHex(common.XDCValidatorV2Code))
log.Info("poc", "MasternodeVotingSMC", common.XDCValidatorV2Code, "step", 2)
}
Original file line number Diff line number Diff line change
Expand Up @@ -300,4 +300,4 @@ contract XDCValidator {
msg.sender.transfer(cap);
emit Withdraw(msg.sender, _blockNumber, cap);
}
}
}
1 change: 1 addition & 0 deletions contracts/validator/contract_v2/src/.env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PRIVATE_KEY=1234567890123456789012345678901234567890123456789012345678901234
7 changes: 7 additions & 0 deletions contracts/validator/contract_v2/src/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.env
cache/
artifacts/
node_modules/
.vscode/
abigen
yarn.lock
5 changes: 5 additions & 0 deletions contracts/validator/contract_v2/src/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
artifacts
cache
coverage*
gasReporterOutput.json
1 change: 1 addition & 0 deletions contracts/validator/contract_v2/src/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
7 changes: 7 additions & 0 deletions contracts/validator/contract_v2/src/.solhint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "solhint:recommended",
"rules": {
"compiler-version": ["error", "0.4.26"],
"func-visibility": ["warn", { "ignoreConstructors": true }]
}
}
1 change: 1 addition & 0 deletions contracts/validator/contract_v2/src/.solhintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
92 changes: 92 additions & 0 deletions contracts/validator/contract_v2/src/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# XDC Validator Contract

This folder has provided scripts for:

- Contract Building and Testing
- Contract Deployment

## Contract Building and Testing:

Environmental preparation

###### Nodejs 16 or higher version

Install dependencies

```shell
yarn
```

Test

```shell
npx hardhat compile
npx hardhat test
```

## Deploy contract to node genesis block

1. run generate script to genera

```shell
node scripts/deployToGenesis.js
```

2. run

```shell
./abigen --abi abi --bin bytecode --pkg contract --type XDCValidator --out ../validator_v2.go
```

If you don't have `abigen`, compile it. It's in `cmd/abigen/main.go` in XDC main repo (or Geth repo).

3. Go `../validator_v2.go` change `github.com/ethereum/go-ethereum` to `ethereum "github.com/XinFinOrg/XDPoSChain"`

## Deploy contract to node any block

### Contract Setup:

This step is recommended to complete in python virtual environment because it is going to use the web3 library adapted for XDC. And before running the process, it is required to performed two operations:

1. Fill in the fields in `deployment.json`

- `candidates`: Initial candidates
- `caps`: Initial caps, one cap for one candidate
- `firstOwner`: Owner of initial candidates
- `minCandidateCap`: Minimal value for a transaction to call propose()
- `minVoterCap`: Minimal value for a transaction to call vote()
- `maxValidatorNumber`: Never used, I don't know why XDC people write this...
- `candidateWithdrawDelay`: When you call resign() at block number x, you can only withdraw the cap at block x+candidateWithdrawDelay
- `voterWithdrawDelay`: When you call unvote() at block number x, you can only withdraw the cap at block x+voterWithdrawDelay
- `xdcdevnet`: Targeted XDC public chain devnet, testnet or mainnet node RPC link
- `xdcsubnet`: Targeted XDC private subnet chain devnet, testnet or mainnet node RPC link

2. Create a `.env` file which contain a valid account privatekey, check `.env.sample` for example

### Contract Deployment:

And get the deployed contract address

```shell
npx hardhat run scripts/deployment.js --network xdcdevnet
```

## Other command

```shell
npx hardhat accounts
npx hardhat compile
npx hardhat clean
npx hardhat test
npx hardhat node
npx hardhat help
REPORT_GAS=true npx hardhat test
npx prettier '**/*.{js,json,sol,md}' --check
npx prettier '**/*.{js,json,sol,md}' --write
npx solhint 'contracts/**/*.sol'
npx solhint 'contracts/**/*.sol' --fix
```

## Gas report

![Alt text](image.png)
Loading