From fc07a59533a6fe53fcc28f83fe4b777cc1216fff Mon Sep 17 00:00:00 2001 From: wanwiset25 Date: Wed, 26 Jun 2024 11:52:06 +0400 Subject: [PATCH] add local xdpos function add readme changes to support xdpos2 protocol from block 0 --- cicd/Dockerfile | 9 +- cicd/entry.sh | 2 +- cicd/local/README.md | 10 ++ cicd/local/start.sh | 83 +++++++++ cmd/puppeth/wizard_genesis.go | 12 +- common/constants/constants.go.local | 158 ++++++++++++++++++ consensus/XDPoS/engines/engine_v2/engine.go | 13 +- .../XDPoS/engines/engine_v2/epochSwitch.go | 20 +++ consensus/XDPoS/engines/engine_v2/snapshot.go | 4 + consensus/XDPoS/engines/engine_v2/timeout.go | 8 + consensus/XDPoS/engines/engine_v2/vote.go | 4 + eth/hooks/engine_v2_hooks.go | 9 + 12 files changed, 322 insertions(+), 10 deletions(-) create mode 100644 cicd/local/README.md create mode 100755 cicd/local/start.sh create mode 100644 common/constants/constants.go.local diff --git a/cicd/Dockerfile b/cicd/Dockerfile index bf5b4b5c7988..2def0346fb46 100644 --- a/cicd/Dockerfile +++ b/cicd/Dockerfile @@ -5,11 +5,14 @@ RUN apk add make build-base linux-headers COPY . /builder RUN cd /builder && make && mv /builder/build/bin/XDC /builder/build/bin/XDC-mainnet +RUN mv /builder/common/constants/constants.go.testnet /builder/common/constants.go +RUN cd /builder && make && mv /builder/build/bin/XDC /builder/build/bin/XDC-testnet + RUN mv /builder/common/constants/constants.go.devnet /builder/common/constants.go RUN cd /builder && make && mv /builder/build/bin/XDC /builder/build/bin/XDC-devnet -RUN mv /builder/common/constants/constants.go.testnet /builder/common/constants.go -RUN cd /builder && make && mv /builder/build/bin/XDC /builder/build/bin/XDC-testnet +RUN mv /builder/common/constants/constants.go.local /builder/common/constants.go +RUN cd /builder && make && mv /builder/build/bin/XDC /builder/build/bin/XDC-local # The runtime image FROM alpine:3 @@ -18,11 +21,13 @@ WORKDIR /work RUN apk add --no-cache bash curl +COPY --from=builder /builder/build/bin/XDC-local /usr/bin COPY --from=builder /builder/build/bin/XDC-devnet /usr/bin COPY --from=builder /builder/build/bin/XDC-testnet /usr/bin COPY --from=builder /builder/build/bin/XDC-mainnet /usr/bin # # Copy over files +ADD cicd/local /work/local ADD cicd/devnet /work/devnet ADD cicd/testnet /work/testnet ADD cicd/mainnet /work/mainnet diff --git a/cicd/entry.sh b/cicd/entry.sh index 777bd58692e6..e7518fe9f603 100755 --- a/cicd/entry.sh +++ b/cicd/entry.sh @@ -1,7 +1,7 @@ #!/bin/bash if test -z "$NETWORK" then - echo "NETWORK env Must be set, mainnet/testnet/devnet" + echo "NETWORK env Must be set, mainnet/testnet/devnet/local" exit 1 fi diff --git a/cicd/local/README.md b/cicd/local/README.md new file mode 100644 index 000000000000..83d8dad0b179 --- /dev/null +++ b/cicd/local/README.md @@ -0,0 +1,10 @@ +To set up local xdpos you need pass env NETWORK=local and inject 2 files when starting the container +1. genesis.json - deploy to path "/work/genesis.json" in the container. + - Creating genesis.json using puppeth + 1. "make puppeth" from base repo directory + 2. run the binary (genesis wizard) "./build/bin/puppeth" + 3. the output genesis.json will be in your ~/.puppeth directory + +2. bootnodes.list - deploy to path "/work/bootnodes.list" in the container. + - check example bootnode format in cicd/devnet or cicd/testnet + - REQUIRES newline at the end of the file, or the last line won't read diff --git a/cicd/local/start.sh b/cicd/local/start.sh new file mode 100755 index 000000000000..cfd877de4066 --- /dev/null +++ b/cicd/local/start.sh @@ -0,0 +1,83 @@ +#!/bin/bash +if [ ! -d /work/xdcchain/XDC/chaindata ] +then + if test -z "$PRIVATE_KEY" + then + echo "PRIVATE_KEY environment variable has not been set." + exit 1 + fi + echo $PRIVATE_KEY >> /tmp/key + wallet=$(XDC account import --password .pwd --datadir /work/xdcchain /tmp/key | awk -F '[{}]' '{print $2}') + XDC --datadir /work/xdcchain init /work/genesis.json +else + wallet=$(XDC account list --datadir /work/xdcchain | head -n 1 | awk -F '[{}]' '{print $2}') +fi + +input="/work/bootnodes.list" +bootnodes="" +while IFS= read -r line +do + if [ -z "${bootnodes}" ] + then + bootnodes=$line + else + bootnodes="${bootnodes},$line" + fi +done < "$input" + +log_level=3 +if test -z "$LOG_LEVEL" +then + echo "Log level not set, default to verbosity of $log_level" +else + echo "Log level found, set to $LOG_LEVEL" + log_level=$LOG_LEVEL +fi + +port=30303 +if test -z "$PORT" +then + echo "PORT not set, default to $port" +else + echo "PORT found, set to $PORT" + port=$PORT +fi + +rpc_port=8545 +if test -z "$RPC_PORT" +then + echo "RPC_PORT not set, default to $rpc_port" +else + echo "RPC_PORT found, set to $RPC_PORT" + rpc_port=$RPC_PORT +fi + +ws_port=8555 +if test -z "$WS_PORT" +then + echo "WS_PORT not set, default to $ws_port" +else + echo "WS_PORT found, set to $WS_PORT" + ws_port=$WS_PORT +fi + +netstats="${NODE_NAME}-${wallet}:xinfin_xdpos_hybrid_network_stats@devnetstats.apothem.network:2000" + + +echo "Running a node with wallet: ${wallet}" +echo "Starting nodes with $bootnodes ..." + +# Note: --gcmode=archive means node will store all historical data. This will lead to high memory usage. But sync mode require archive to sync +# https://github.com/XinFinOrg/XDPoSChain/issues/268 + +XDC --ethstats ${netstats} --gcmode archive \ +--bootnodes ${bootnodes} --syncmode full \ +--datadir /work/xdcchain --networkid 551 \ +-port $port --rpc --rpccorsdomain "*" --rpcaddr 0.0.0.0 \ +--rpcport $rpc_port \ +--rpcapi db,eth,debug,net,shh,txpool,personal,web3,XDPoS \ +--rpcvhosts "*" --unlock "${wallet}" --password /work/.pwd --mine \ +--gasprice "1" --targetgaslimit "420000000" --verbosity ${log_level} \ +--debugdatadir /work/xdcchain \ +--ws --wsaddr=0.0.0.0 --wsport $ws_port \ +--wsorigins "*" 2>&1 >>/work/xdcchain/xdc.log | tee -a /work/xdcchain/xdc.log diff --git a/cmd/puppeth/wizard_genesis.go b/cmd/puppeth/wizard_genesis.go index c1e077c59513..7a8148decf1d 100644 --- a/cmd/puppeth/wizard_genesis.go +++ b/cmd/puppeth/wizard_genesis.go @@ -51,11 +51,11 @@ func (w *wizard) makeGenesis() { Difficulty: big.NewInt(524288), Alloc: make(core.GenesisAlloc), Config: ¶ms.ChainConfig{ - HomesteadBlock: big.NewInt(1), - EIP150Block: big.NewInt(2), - EIP155Block: big.NewInt(3), - EIP158Block: big.NewInt(3), - ByzantiumBlock: big.NewInt(4), + HomesteadBlock: big.NewInt(0), + EIP150Block: big.NewInt(0), + EIP155Block: big.NewInt(0), + EIP158Block: big.NewInt(0), + ByzantiumBlock: big.NewInt(0), }, } // Figure out which consensus engine to choose @@ -147,7 +147,7 @@ func (w *wizard) makeGenesis() { fmt.Println() fmt.Printf("Proportion of total masternodes v2 vote collection to generate a QC (float value), should be two thirds of masternodes? (default = %f)\n", 0.667) genesis.Config.XDPoS.V2.CurrentConfig.CertThreshold = w.readDefaultFloat(0.667) - + genesis.Config.XDPoS.V2.CurrentConfig.MaxMasternodes = 108 genesis.Config.XDPoS.V2.AllConfigs[0] = genesis.Config.XDPoS.V2.CurrentConfig fmt.Println() diff --git a/common/constants/constants.go.local b/common/constants/constants.go.local new file mode 100644 index 000000000000..6c5114313728 --- /dev/null +++ b/common/constants/constants.go.local @@ -0,0 +1,158 @@ +package common + +import ( + "math/big" +) + +const ( + RewardMasterPercent = 90 + RewardVoterPercent = 0 + RewardFoundationPercent = 10 + HexSignMethod = "e341eaa4" + HexSetSecret = "34d38600" + HexSetOpening = "e11f5ba2" + EpocBlockSecret = 800 + EpocBlockOpening = 850 + EpocBlockRandomize = 900 + MaxMasternodes = 18 + MaxMasternodesV2 = 108 // Last v1 masternodes + LimitPenaltyEpoch = 4 + LimitPenaltyEpochV2 = 0 + BlocksPerYearTest = uint64(200000) + BlocksPerYear = uint64(15768000) + LimitThresholdNonceInQueue = 10 + DefaultMinGasPrice = 250000000 + MergeSignRange = 15 + RangeReturnSigner = 150 + MinimunMinerBlockPerEpoch = 1 + + OneYear = uint64(365 * 86400) + LiquidateLendingTradeBlock = uint64(100) +) + +var Rewound = uint64(0) + +var TIP2019Block = big.NewInt(0) +var TIPSigning = big.NewInt(0) +var TIPRandomize = big.NewInt(0) + +var TIPV2SwitchBlock = big.NewInt(0) + +var TIPIncreaseMasternodes = big.NewInt(0) // Upgrade MN Count at Block. +var TIPNoHalvingMNReward = big.NewInt(0) // hardfork no halving masternodes reward +var BlackListHFNumber = uint64(0) +var TIPXDCX = big.NewInt(0) +var TIPXDCXLending = big.NewInt(0) +var TIPXDCXCancellationFee = big.NewInt(0) +var TIPXDCXCancellationFeeTestnet = big.NewInt(0) +var TIPXDCXMinerDisable = big.NewInt(0) +var TIPXDCXReceiverDisable = big.NewInt(0) +var BerlinBlock = big.NewInt(0) +var LondonBlock = big.NewInt(0) +var MergeBlock = big.NewInt(0) +var ShanghaiBlock = big.NewInt(0) +var Eip1559Block = big.NewInt(9999999999) + +var TIPXDCXTestnet = big.NewInt(0) +var IsTestnet bool = false +var Enable0xPrefix bool = true +var StoreRewardFolder string +var RollbackHash Hash +var BasePrice = big.NewInt(1000000000000000000) // 1 +var RelayerLockedFund = big.NewInt(20000) // 20000 XDC +var RelayerFee = big.NewInt(1000000000000000) // 0.001 +var XDCXBaseFee = big.NewInt(10000) // 1 / XDCXBaseFee +var RelayerCancelFee = big.NewInt(100000000000000) // 0.0001 +var XDCXBaseCancelFee = new(big.Int).Mul(XDCXBaseFee, big.NewInt(10)) // 1/ (XDCXBaseFee *10) +var RelayerLendingFee = big.NewInt(10000000000000000) // 0.01 +var RelayerLendingCancelFee = big.NewInt(1000000000000000) // 0.001 +var BaseLendingInterest = big.NewInt(100000000) // 1e8 + +var MinGasPrice = big.NewInt(DefaultMinGasPrice) +var RelayerRegistrationSMC = "0x16c63b79f9C8784168103C0b74E6A59EC2de4a02" +var RelayerRegistrationSMCTestnet = "0xA1996F69f47ba14Cb7f661010A7C31974277958c" +var LendingRegistrationSMC = "0x7d761afd7ff65a79e4173897594a194e3c506e57" +var LendingRegistrationSMCTestnet = "0x28d7fC2Cf5c18203aaCD7459EFC6Af0643C97bE8" +var TRC21IssuerSMCTestNet = HexToAddress("0x0E2C88753131CE01c7551B726b28BFD04e44003F") +var TRC21IssuerSMC = HexToAddress("0x8c0faeb5C6bEd2129b8674F262Fd45c4e9468bee") +var XDCXListingSMC = HexToAddress("0xDE34dD0f536170993E8CFF639DdFfCF1A85D3E53") +var XDCXListingSMCTestNet = HexToAddress("0x14B2Bf043b9c31827A472CE4F94294fE9a6277e0") +var TRC21GasPriceBefore = big.NewInt(2500) +var TRC21GasPrice = big.NewInt(250000000) +var RateTopUp = big.NewInt(90) // 90% +var BaseTopUp = big.NewInt(100) +var BaseRecall = big.NewInt(100) +var TIPTRC21Fee = big.NewInt(13523400) +var TIPTRC21FeeTestnet = big.NewInt(225000) +var BlockNumberGas50x = big.NewInt(11818181) +var LimitTimeFinality = uint64(30) // limit in 30 block + +var IgnoreSignerCheckBlockArray = map[uint64]bool{ + uint64(1032300): true, + uint64(1033200): true, + uint64(27307800): true, + uint64(28270800): true, +} +var Blacklist = map[Address]bool{ + HexToAddress("0x5248bfb72fd4f234e062d3e9bb76f08643004fcd"): true, + HexToAddress("0x5ac26105b35ea8935be382863a70281ec7a985e9"): true, + HexToAddress("0x09c4f991a41e7ca0645d7dfbfee160b55e562ea4"): true, + HexToAddress("0xb3157bbc5b401a45d6f60b106728bb82ebaa585b"): true, + HexToAddress("0x741277a8952128d5c2ffe0550f5001e4c8247674"): true, + HexToAddress("0x10ba49c1caa97d74b22b3e74493032b180cebe01"): true, + HexToAddress("0x07048d51d9e6179578a6e3b9ee28cdc183b865e4"): true, + HexToAddress("0x4b899001d73c7b4ec404a771d37d9be13b8983de"): true, + HexToAddress("0x85cb320a9007f26b7652c19a2a65db1da2d0016f"): true, + HexToAddress("0x06869dbd0e3a2ea37ddef832e20fa005c6f0ca39"): true, + HexToAddress("0x82e48bc7e2c93d89125428578fb405947764ad7c"): true, + HexToAddress("0x1f9a78534d61732367cbb43fc6c89266af67c989"): true, + HexToAddress("0x7c3b1fa91df55ff7af0cad9e0399384dc5c6641b"): true, + HexToAddress("0x5888dc1ceb0ff632713486b9418e59743af0fd20"): true, + HexToAddress("0xa512fa1c735fc3cc635624d591dd9ea1ce339ca5"): true, + HexToAddress("0x0832517654c7b7e36b1ef45d76de70326b09e2c7"): true, + HexToAddress("0xca14e3c4c78bafb60819a78ff6e6f0f709d2aea7"): true, + HexToAddress("0x652ce195a23035114849f7642b0e06647d13e57a"): true, + HexToAddress("0x29a79f00f16900999d61b6e171e44596af4fb5ae"): true, + HexToAddress("0xf9fd1c2b0af0d91b0b6754e55639e3f8478dd04a"): true, + HexToAddress("0xb835710c9901d5fe940ef1b99ed918902e293e35"): true, + HexToAddress("0x04dd29ce5c253377a9a3796103ea0d9a9e514153"): true, + HexToAddress("0x2b4b56846eaf05c1fd762b5e1ac802efd0ab871c"): true, + HexToAddress("0x1d1f909f6600b23ce05004f5500ab98564717996"): true, + HexToAddress("0x0dfdcebf80006dc9ab7aae8c216b51c6b6759e86"): true, + HexToAddress("0x2b373890a28e5e46197fbc04f303bbfdd344056f"): true, + HexToAddress("0xa8a3ef3dc5d8e36aee76f3671ec501ec31e28254"): true, + HexToAddress("0x4f3d18136fe2b5665c29bdaf74591fc6625ef427"): true, + HexToAddress("0x175d728b0e0f1facb5822a2e0c03bde93596e324"): true, + HexToAddress("0xd575c2611984fcd79513b80ab94f59dc5bab4916"): true, + HexToAddress("0x0579337873c97c4ba051310236ea847f5be41bc0"): true, + HexToAddress("0xed12a519cc15b286920fc15fd86106b3e6a16218"): true, + HexToAddress("0x492d26d852a0a0a2982bb40ec86fe394488c419e"): true, + HexToAddress("0xce5c7635d02dc4e1d6b46c256cae6323be294a32"): true, + HexToAddress("0x8b94db158b5e78a6c032c7e7c9423dec62c8b11c"): true, + HexToAddress("0x0e7c48c085b6b0aa7ca6e4cbcc8b9a92dc270eb4"): true, + HexToAddress("0x206e6508462033ef8425edc6c10789d241d49acb"): true, + HexToAddress("0x7710e7b7682f26cb5a1202e1cff094fbf7777758"): true, + HexToAddress("0xcb06f949313b46bbf53b8e6b2868a0c260ff9385"): true, + HexToAddress("0xf884e43533f61dc2997c0e19a6eff33481920c00"): true, + HexToAddress("0x8b635ef2e4c8fe21fc2bda027eb5f371d6aa2fc1"): true, + HexToAddress("0x10f01a27cf9b29d02ce53497312b96037357a361"): true, + HexToAddress("0x693dd49b0ed70f162d733cf20b6c43dc2a2b4d95"): true, + HexToAddress("0xe0bec72d1c2a7a7fb0532cdfac44ebab9f6f41ee"): true, + HexToAddress("0xc8793633a537938cb49cdbbffd45428f10e45b64"): true, + HexToAddress("0x0d07a6cbbe9fa5c4f154e5623bfe47fb4d857d8e"): true, + HexToAddress("0xd4080b289da95f70a586610c38268d8d4cf1e4c4"): true, + HexToAddress("0x8bcfb0caf41f0aa1b548cae76dcdd02e33866a1b"): true, + HexToAddress("0xabfef22b92366d3074676e77ea911ccaabfb64c1"): true, + HexToAddress("0xcc4df7a32faf3efba32c9688def5ccf9fefe443d"): true, + HexToAddress("0x7ec1e48a582475f5f2b7448a86c4ea7a26ea36f8"): true, + HexToAddress("0xe3de67289080f63b0c2612844256a25bb99ac0ad"): true, + HexToAddress("0x3ba623300cf9e48729039b3c9e0dee9b785d636e"): true, + HexToAddress("0x402f2cfc9c8942f5e7a12c70c625d07a5d52fe29"): true, + HexToAddress("0xd62358d42afbde095a4ca868581d85f9adcc3d61"): true, + HexToAddress("0x3969f86acb733526cd61e3c6e3b4660589f32bc6"): true, + HexToAddress("0x67615413d7cdadb2c435a946aec713a9a9794d39"): true, + HexToAddress("0xfe685f43acc62f92ab01a8da80d76455d39d3cb3"): true, + HexToAddress("0x3538a544021c07869c16b764424c5987409cba48"): true, + HexToAddress("0xe187cf86c2274b1f16e8225a7da9a75aba4f1f5f"): true, + HexToAddress("0x0000000000000000000000000000000000000011"): true, +} diff --git a/consensus/XDPoS/engines/engine_v2/engine.go b/consensus/XDPoS/engines/engine_v2/engine.go index c2235f5d32a9..e819d75a9e88 100644 --- a/consensus/XDPoS/engines/engine_v2/engine.go +++ b/consensus/XDPoS/engines/engine_v2/engine.go @@ -188,7 +188,10 @@ func (x *XDPoS_v2) initial(chain consensus.ChainReader, header *types.Header) er Signatures: nil, GapNumber: header.Number.Uint64() - x.config.Gap, } - + // prevent overflow + if header.Number.Uint64() < x.config.Gap { + quorumCert.GapNumber = 0 + } // can not call processQC because round is equal to default x.currentRound = 1 x.highestQuorumCert = quorumCert @@ -207,6 +210,10 @@ func (x *XDPoS_v2) initial(chain consensus.ChainReader, header *types.Header) er // Initial first v2 snapshot lastGapNum := x.config.V2.SwitchBlock.Uint64() - x.config.Gap + // prevent overflow + if x.config.V2.SwitchBlock.Uint64() < x.config.Gap { + lastGapNum = 0 + } lastGapHeader := chain.GetHeaderByNumber(lastGapNum) snap, _ := loadSnapshot(x.db, lastGapHeader.Hash()) @@ -839,6 +846,10 @@ func (x *XDPoS_v2) verifyQC(blockChainReader consensus.ChainReader, quorumCert * } epochSwitchNumber := epochInfo.EpochSwitchBlockInfo.Number.Uint64() gapNumber := epochSwitchNumber - epochSwitchNumber%x.config.Epoch - x.config.Gap + // prevent overflow + if epochSwitchNumber-epochSwitchNumber%x.config.Epoch < x.config.Gap { + gapNumber = 0 + } if gapNumber != quorumCert.GapNumber { log.Error("[verifyQC] QC gap number mismatch", "epochSwitchNumber", epochSwitchNumber, "BlockNum", quorumCert.ProposedBlockInfo.Number, "BlockInfoHash", quorumCert.ProposedBlockInfo.Hash, "Gap", quorumCert.GapNumber, "GapShouldBe", gapNumber) return fmt.Errorf("gap number mismatch QC Gap %d, shouldBe %d", quorumCert.GapNumber, gapNumber) diff --git a/consensus/XDPoS/engines/engine_v2/epochSwitch.go b/consensus/XDPoS/engines/engine_v2/epochSwitch.go index 981c46ff9a06..092d847dd510 100644 --- a/consensus/XDPoS/engines/engine_v2/epochSwitch.go +++ b/consensus/XDPoS/engines/engine_v2/epochSwitch.go @@ -51,6 +51,26 @@ func (x *XDPoS_v2) getEpochSwitchInfo(chain consensus.ChainReader, header *types } if isEpochSwitch { log.Debug("[getEpochSwitchInfo] header is epoch switch", "hash", hash.Hex(), "number", h.Number.Uint64()) + if h.Number.Uint64() == 0 { + log.Warn("[getEpochSwitchInfo] block 0, init epoch differently") + // handle genesis block differently as follows + masternodes := common.ExtractAddressFromBytes(h.Extra[32 : len(h.Extra)-65]) + penalties := []common.Address{} + standbynodes := []common.Address{} + epochSwitchInfo := &types.EpochSwitchInfo{ + Penalties: penalties, + Standbynodes: standbynodes, + Masternodes: masternodes, + MasternodesLen: len(masternodes), + EpochSwitchBlockInfo: &types.BlockInfo{ + Hash: hash, + Number: h.Number, + Round: 0, + }, + } + x.epochSwitches.Add(hash, epochSwitchInfo) + return epochSwitchInfo, nil + } quorumCert, round, masternodes, err := x.getExtraFields(h) if err != nil { log.Error("[getEpochSwitchInfo] get extra field", "err", err, "number", h.Number.Uint64()) diff --git a/consensus/XDPoS/engines/engine_v2/snapshot.go b/consensus/XDPoS/engines/engine_v2/snapshot.go index ccae59841278..773f7727ac2f 100644 --- a/consensus/XDPoS/engines/engine_v2/snapshot.go +++ b/consensus/XDPoS/engines/engine_v2/snapshot.go @@ -78,6 +78,10 @@ func (x *XDPoS_v2) getSnapshot(chain consensus.ChainReader, number uint64, isGap gapBlockNum = number } else { gapBlockNum = number - number%x.config.Epoch - x.config.Gap + //prevent overflow + if number-number%x.config.Epoch < x.config.Gap { + gapBlockNum = 0 + } } gapBlockHash := chain.GetHeaderByNumber(gapBlockNum).Hash() diff --git a/consensus/XDPoS/engines/engine_v2/timeout.go b/consensus/XDPoS/engines/engine_v2/timeout.go index 2f077b2a0018..8fcf9d6b7bd3 100644 --- a/consensus/XDPoS/engines/engine_v2/timeout.go +++ b/consensus/XDPoS/engines/engine_v2/timeout.go @@ -195,6 +195,10 @@ func (x *XDPoS_v2) sendTimeout(chain consensus.ChainReader) error { // Notice this +1 is because we expect a block whos is the child of currentHeader currentNumber := currentBlockHeader.Number.Uint64() + 1 gapNumber = currentNumber - currentNumber%x.config.Epoch - x.config.Gap + // prevent overflow + if currentNumber-currentNumber%x.config.Epoch < x.config.Gap { + gapNumber = 0 + } log.Debug("[sendTimeout] is epoch switch when sending out timeout message", "currentNumber", currentNumber, "gapNumber", gapNumber) } else { epochSwitchInfo, err := x.getEpochSwitchInfo(chain, currentBlockHeader, currentBlockHeader.Hash()) @@ -203,6 +207,10 @@ func (x *XDPoS_v2) sendTimeout(chain consensus.ChainReader) error { return err } gapNumber = epochSwitchInfo.EpochSwitchBlockInfo.Number.Uint64() - epochSwitchInfo.EpochSwitchBlockInfo.Number.Uint64()%x.config.Epoch - x.config.Gap + // prevent overflow + if epochSwitchInfo.EpochSwitchBlockInfo.Number.Uint64()-epochSwitchInfo.EpochSwitchBlockInfo.Number.Uint64()%x.config.Epoch < x.config.Gap { + gapNumber = 0 + } log.Debug("[sendTimeout] non-epoch-switch block found its epoch block and calculated the gapNumber", "epochSwitchInfo.EpochSwitchBlockInfo.Number", epochSwitchInfo.EpochSwitchBlockInfo.Number.Uint64(), "gapNumber", gapNumber) } diff --git a/consensus/XDPoS/engines/engine_v2/vote.go b/consensus/XDPoS/engines/engine_v2/vote.go index de79585297af..957abe9182bf 100644 --- a/consensus/XDPoS/engines/engine_v2/vote.go +++ b/consensus/XDPoS/engines/engine_v2/vote.go @@ -30,6 +30,10 @@ func (x *XDPoS_v2) sendVote(chainReader consensus.ChainReader, blockInfo *types. } epochSwitchNumber := epochSwitchInfo.EpochSwitchBlockInfo.Number.Uint64() gapNumber := epochSwitchNumber - epochSwitchNumber%x.config.Epoch - x.config.Gap + // prevent overflow + if epochSwitchNumber-epochSwitchNumber%x.config.Epoch < x.config.Gap { + gapNumber = 0 + } signedHash, err := x.signSignature(types.VoteSigHash(&types.VoteForSign{ ProposedBlockInfo: blockInfo, GapNumber: gapNumber, diff --git a/eth/hooks/engine_v2_hooks.go b/eth/hooks/engine_v2_hooks.go index f356af27a74c..7882cbba3e4a 100644 --- a/eth/hooks/engine_v2_hooks.go +++ b/eth/hooks/engine_v2_hooks.go @@ -224,6 +224,11 @@ func GetSigningTxCount(c *XDPoS.XDPoS, chain consensus.ChainReader, header *type signers := make(map[common.Address]*contracts.RewardLog) mapBlkHash := map[uint64]common.Hash{} + // prevent overflow + if number == 0 { + return signers, nil + } + data := make(map[common.Hash][]common.Address) epochCount := 0 var masternodes []common.Address @@ -259,6 +264,10 @@ func GetSigningTxCount(c *XDPoS.XDPoS, chain consensus.ChainReader, header *type from := *tx.From() data[blkHash] = append(data[blkHash], from) } + // prevent overflow + if i == 0 { + return signers, nil + } } for i := startBlockNumber; i <= endBlockNumber; i++ {