Skip to content

Commit

Permalink
core/vote: skip voting as VoteInterval
Browse files Browse the repository at this point in the history
consensus/parlia: adapt updateAttestation

consensus/parlia: adapt assembleVoteAttestation

consensus/parlia: modify delay

core/vote: fix vote stability check

consensus/parlia: define API GetVoteInterval

cmd/jsutils: adapt voteInterval

params: define IsPauli

consensus/parlia: adapt verifyVoteAttestation

consensus/parlia: define snapshot.VoteInterval
  • Loading branch information
buddh0 authored and NathanBSC committed Sep 4, 2024
1 parent 9598502 commit 5b12f19
Show file tree
Hide file tree
Showing 16 changed files with 327 additions and 81 deletions.
5 changes: 5 additions & 0 deletions cmd/geth/chaincmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ var (
utils.CachePreimagesFlag,
utils.OverridePassedForkTime,
utils.OverrideBohr,
utils.OverridePauli,
utils.OverrideVerkle,
utils.MultiDataBaseFlag,
}, utils.DatabaseFlags),
Expand Down Expand Up @@ -262,6 +263,10 @@ func initGenesis(ctx *cli.Context) error {
v := ctx.Uint64(utils.OverrideBohr.Name)
overrides.OverrideBohr = &v
}
if ctx.IsSet(utils.OverridePauli.Name) {
v := ctx.Uint64(utils.OverridePauli.Name)
overrides.OverridePauli = &v
}
if ctx.IsSet(utils.OverrideVerkle.Name) {
v := ctx.Uint64(utils.OverrideVerkle.Name)
overrides.OverrideVerkle = &v
Expand Down
7 changes: 7 additions & 0 deletions cmd/geth/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ func makeFullNode(ctx *cli.Context) (*node.Node, ethapi.Backend) {
v := ctx.Uint64(utils.OverrideBohr.Name)
cfg.Eth.OverrideBohr = &v
}
if ctx.IsSet(utils.OverridePauli.Name) {
v := ctx.Uint64(utils.OverridePauli.Name)
cfg.Eth.OverridePauli = &v
}
if ctx.IsSet(utils.OverrideVerkle.Name) {
v := ctx.Uint64(utils.OverrideVerkle.Name)
cfg.Eth.OverrideVerkle = &v
Expand All @@ -213,6 +217,9 @@ func makeFullNode(ctx *cli.Context) (*node.Node, ethapi.Backend) {
if ctx.IsSet(utils.OverrideFixedTurnLength.Name) {
params.FixedTurnLength = ctx.Uint64(utils.OverrideFixedTurnLength.Name)
}
if ctx.IsSet(utils.OverrideFixedVoteInterval.Name) {
params.FixedVoteInterval = ctx.Uint64(utils.OverrideFixedVoteInterval.Name)
}

backend, eth := utils.RegisterEthService(stack, &cfg.Eth)

Expand Down
2 changes: 2 additions & 0 deletions cmd/geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,14 @@ var (
utils.RialtoHash,
utils.OverridePassedForkTime,
utils.OverrideBohr,
utils.OverridePauli,
utils.OverrideVerkle,
utils.OverrideFullImmutabilityThreshold,
utils.OverrideMinBlocksForBlobRequests,
utils.OverrideDefaultExtraReserveForBlobRequests,
utils.OverrideBreatheBlockInterval,
utils.OverrideFixedTurnLength,
utils.OverrideFixedVoteInterval,
utils.EnablePersonal,
utils.TxPoolLocalsFlag,
utils.TxPoolNoLocalsFlag,
Expand Down
45 changes: 28 additions & 17 deletions cmd/jsutils/get_perf.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { ethers } from "ethers";
import {
ethers
} from "ethers";
import program from "commander";

program.option("--rpc <rpc>", "Rpc");
Expand All @@ -14,14 +16,20 @@ const main = async () => {
let inturnBlocks = 0;
let justifiedBlocks = 0;
let turnLength = await provider.send("parlia_getTurnLength", [
ethers.toQuantity(program.startNum)]);
ethers.toQuantity(program.startNum)
]);
let voteInterval = await provider.send("parlia_getVoteInterval", [
ethers.toQuantity(program.startNum)
]);
for (let i = program.startNum; i < program.endNum; i++) {
let txCount = await provider.send("eth_getBlockTransactionCountByNumber", [
ethers.toQuantity(i)]);
ethers.toQuantity(i)
]);
txCountTotal += ethers.toNumber(txCount)

let header = await provider.send("eth_getHeaderByNumber", [
ethers.toQuantity(i)]);
ethers.toQuantity(i)
]);
let gasUsed = eval(eval(header.gasUsed).toString(10))
gasUsedTotal += gasUsed
let difficulty = eval(eval(header.difficulty).toString(10))
Expand All @@ -31,32 +39,35 @@ const main = async () => {
let timestamp = eval(eval(header.timestamp).toString(10))

let justifiedNumber = await provider.send("parlia_getJustifiedNumber", [
ethers.toQuantity(i)]);
if (justifiedNumber + 1 == i) {
ethers.toQuantity(i)
]);
if (justifiedNumber + 2 * voteInterval > i) {
justifiedBlocks += 1
} else {
console.log("justified unexpected", "BlockNumber =", i,"justifiedNumber",justifiedNumber)
console.log("justified unexpected", "BlockNumber =", i, "justifiedNumber", justifiedNumber)
}
console.log("BlockNumber =", i, "mod =", i%turnLength, "miner =", header.miner , "difficulty =", difficulty, "txCount =", ethers.toNumber(txCount), "gasUsed", gasUsed, "timestamp", timestamp)
console.log("BlockNumber =", i, "mod =", i % turnLength, "miner =", header.miner, "difficulty =", difficulty, "txCount =", ethers.toNumber(txCount), "gasUsed", gasUsed, "timestamp", timestamp)
}

let blockCount = program.endNum - program.startNum
let txCountPerBlock = txCountTotal/blockCount
let txCountPerBlock = txCountTotal / blockCount

let startHeader = await provider.send("eth_getHeaderByNumber", [
ethers.toQuantity(program.startNum)]);
ethers.toQuantity(program.startNum)
]);
let startTime = eval(eval(startHeader.timestamp).toString(10))
let endHeader = await provider.send("eth_getHeaderByNumber", [
ethers.toQuantity(program.endNum)]);
ethers.toQuantity(program.endNum)
]);
let endTime = eval(eval(endHeader.timestamp).toString(10))
let timeCost = endTime - startTime
let avgBlockTime = timeCost/blockCount
let inturnBlocksRatio = inturnBlocks/blockCount
let justifiedBlocksRatio = justifiedBlocks/blockCount
let tps = txCountTotal/timeCost
let avgBlockTime = timeCost / blockCount
let inturnBlocksRatio = inturnBlocks / blockCount
let justifiedBlocksRatio = justifiedBlocks / blockCount
let tps = txCountTotal / timeCost
let M = 1000000
let avgGasUsedPerBlock = gasUsedTotal/blockCount/M
let avgGasUsedPerSecond = gasUsedTotal/timeCost/M
let avgGasUsedPerBlock = gasUsedTotal / blockCount / M
let avgGasUsedPerSecond = gasUsedTotal / timeCost / M

console.log("Get the performance between [", program.startNum, ",", program.endNum, ")");
console.log("txCountPerBlock =", txCountPerBlock, "txCountTotal =", txCountTotal, "BlockCount =", blockCount, "avgBlockTime =", avgBlockTime, "inturnBlocksRatio =", inturnBlocksRatio, "justifiedBlocksRatio =", justifiedBlocksRatio);
Expand Down
11 changes: 11 additions & 0 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,11 @@ var (
Usage: "Manually specify the Bohr fork timestamp, overriding the bundled setting",
Category: flags.EthCategory,
}
OverridePauli = &cli.Uint64Flag{
Name: "override.pauli",
Usage: "Manually specify the Pauli fork timestamp, overriding the bundled setting",
Category: flags.EthCategory,
}
OverrideVerkle = &cli.Uint64Flag{
Name: "override.verkle",
Usage: "Manually specify the Verkle fork timestamp, overriding the bundled setting",
Expand Down Expand Up @@ -350,6 +355,12 @@ var (
Value: params.FixedTurnLength,
Category: flags.EthCategory,
}
OverrideFixedVoteInterval = &cli.Uint64Flag{
Name: "override.fixedvoteinterval",
Usage: "It use fixed values for voting interval, only for testing purpose",
Value: params.FixedVoteInterval,
Category: flags.EthCategory,
}
SyncModeFlag = &flags.TextMarshalerFlag{
Name: "syncmode",
Usage: `Blockchain sync mode ("snap" or "full")`,
Expand Down
19 changes: 16 additions & 3 deletions consensus/parlia/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (api *API) GetValidatorsAtHash(hash common.Hash) ([]common.Address, error)

func (api *API) GetJustifiedNumber(number *rpc.BlockNumber) (uint64, error) {
header := api.getHeader(number)
// Ensure we have an actually valid block and return the validators from its snapshot
// Ensure we have an actually valid block and return the justifiedNumber from its snapshot
if header == nil {
return 0, errUnknownBlock
}
Expand All @@ -90,7 +90,7 @@ func (api *API) GetJustifiedNumber(number *rpc.BlockNumber) (uint64, error) {

func (api *API) GetTurnLength(number *rpc.BlockNumber) (uint8, error) {
header := api.getHeader(number)
// Ensure we have an actually valid block and return the validators from its snapshot
// Ensure we have an actually valid block and return the turnLength from its snapshot
if header == nil {
return 0, errUnknownBlock
}
Expand All @@ -101,9 +101,22 @@ func (api *API) GetTurnLength(number *rpc.BlockNumber) (uint8, error) {
return snap.TurnLength, nil
}

func (api *API) GetVoteInterval(number *rpc.BlockNumber) (uint8, error) {
header := api.getHeader(number)
// Ensure we have an actually valid block and return the voteInterval from its snapshot
if header == nil {
return 0, errUnknownBlock
}
snap, err := api.parlia.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil)
if err != nil || snap.VoteInterval == 0 {
return 0, err
}
return snap.VoteInterval, nil
}

func (api *API) GetFinalizedNumber(number *rpc.BlockNumber) (uint64, error) {
header := api.getHeader(number)
// Ensure we have an actually valid block and return the validators from its snapshot
// Ensure we have an actually valid block and return the finalizedNumber from its snapshot
if header == nil {
return 0, errUnknownBlock
}
Expand Down
34 changes: 34 additions & 0 deletions consensus/parlia/bohrFork.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,37 @@ func (p *Parlia) getRandTurnLength(header *types.Header) (turnLength *big.Int, e
lengthIndex := int(r.Int31n(int32(len(turnLengths))))
return big.NewInt(int64(turnLengths[lengthIndex])), nil
}

func (p *Parlia) getVoteInterval(chain consensus.ChainHeaderReader, header *types.Header) (*uint8, error) {
parent := chain.GetHeaderByHash(header.ParentHash)
if parent == nil {
return nil, errors.New("parent not found")
}

var voteInterval uint8
if p.chainConfig.IsPauli(parent.Number, parent.Time) {
voteIntervalFromContract, err := p.getVoteIntervalFromContract(parent)
if err != nil {
return nil, err
}
if voteIntervalFromContract == nil {
return nil, errors.New("unexpected error when getVoteIntervalFromContract")
}
voteInterval = uint8(voteIntervalFromContract.Int64())
} else {
voteInterval = defaultVoteInterval
}
log.Debug("getVoteInterval", "voteInterval", voteInterval)

return &voteInterval, nil
}

func (p *Parlia) getVoteIntervalFromContract(header *types.Header) (voteInterval *big.Int, err error) {
// mock to get voteInterval from the contract
if params.FixedVoteInterval >= 1 && params.FixedVoteInterval <= 3 {
return big.NewInt(int64(params.FixedVoteInterval)), nil
}

// TODO: read from contract
return big.NewInt(int64(defaultVoteInterval)), nil
}
Loading

0 comments on commit 5b12f19

Please sign in to comment.