Skip to content

Commit 208f79b

Browse files
error optimization for validator sc (#89)
error optimization for validator sc
1 parent 7209dbd commit 208f79b

File tree

8 files changed

+28
-37
lines changed

8 files changed

+28
-37
lines changed

consensus/tests/engine_v2_tests/adaptor_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func TestAdaptorShouldGetAuthorForDifferentConsensusVersion(t *testing.T) {
3030
// Insert one more block to make it above 10, which means now we are on v2 of consensus engine
3131
// Insert block 901
3232
33-
merkleRoot := "4de5b7decb41a4aa648140d0fecb169bd22f07e92be20cf63397111f48653bdc"
33+
merkleRoot := "b3e34cf1d3d80bcd2c5add880842892733e45979ddaf16e531f660fdf7ca5787"
3434
header := &types.Header{
3535
Root: common.HexToHash(merkleRoot),
3636
Number: big.NewInt(int64(901)),

consensus/tests/engine_v2_tests/helper.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ func PrepareXDCTestBlockChainWith128Candidates(t *testing.T, numOfBlocks int, ch
567567
blockCoinBase = signer.Hex()
568568
}
569569
roundNumber := int64(i) - chainConfig.XDPoS.V2.SwitchBlock.Int64()
570-
block := CreateBlock(blockchain, chainConfig, currentBlock, i, roundNumber, blockCoinBase, signer, signFn, nil, nil, "10f2c00fef7093f265fde39c806f17aaea50a2093ca68ab795462021c38fd4b0")
570+
block := CreateBlock(blockchain, chainConfig, currentBlock, i, roundNumber, blockCoinBase, signer, signFn, nil, nil, "c2bf7b59be5184fc1148be5db14692b2dc89a1b345895d3e8d0ee7b8a7607450")
571571
err = blockchain.InsertBlock(block)
572572
if err != nil {
573573
t.Fatal(err)
@@ -609,7 +609,7 @@ func PrepareQCandProcess(t *testing.T, blockchain *BlockChain, currentBlock *typ
609609
func CreateBlock(blockchain *BlockChain, chainConfig *params.ChainConfig, startingBlock *types.Block, blockNumber int, roundNumber int64, blockCoinBase string, signer common.Address, signFn func(account accounts.Account, hash []byte) ([]byte, error), penalties []common.Address, signersKey []*ecdsa.PrivateKey, merkleRoot string) *types.Block {
610610
currentBlock := startingBlock
611611
if len(merkleRoot) == 0 {
612-
merkleRoot = "4de5b7decb41a4aa648140d0fecb169bd22f07e92be20cf63397111f48653bdc"
612+
merkleRoot = "b3e34cf1d3d80bcd2c5add880842892733e45979ddaf16e531f660fdf7ca5787"
613613
}
614614
var header *types.Header
615615
statedb, err := blockchain.State()

consensus/tests/engine_v2_tests/mine_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func TestYourTurnInitialV2(t *testing.T) {
2828
t.Logf("Inserting block with propose at 11...")
2929
blockCoinbaseA := "0xaaa0000000000000000000000000000000000011"
3030
//Get from block validator error message
31-
merkleRoot := "4de5b7decb41a4aa648140d0fecb169bd22f07e92be20cf63397111f48653bdc"
31+
merkleRoot := "b3e34cf1d3d80bcd2c5add880842892733e45979ddaf16e531f660fdf7ca5787"
3232
extraInBytes := generateV2Extra(11, parentBlock, signer, signFn, nil)
3333

3434
header := &types.Header{
@@ -79,7 +79,7 @@ func TestShouldMineOncePerRound(t *testing.T) {
7979
_, err := adaptor.Seal(blockchain, block910, nil)
8080
assert.Nil(t, err)
8181
time.Sleep(time.Duration(minePeriod) * time.Second)
82-
merkleRoot := "4de5b7decb41a4aa648140d0fecb169bd22f07e92be20cf63397111f48653bdc"
82+
merkleRoot := "b3e34cf1d3d80bcd2c5add880842892733e45979ddaf16e531f660fdf7ca5787"
8383

8484
header := &types.Header{
8585
Root: common.HexToHash(merkleRoot),
@@ -128,7 +128,7 @@ func TestUpdateMasterNodes(t *testing.T) {
128128
t.Fatal(err)
129129
}
130130
//Get from block validator error message
131-
merkleRoot := "238cda75adb6630d1b7ac209012cc95f258fb84fa2b7a14e1c02a092955fc73d"
131+
merkleRoot := "42970e0f2265db7d85d0d9cd7328d58bd53bdb54d85d7997e95a1c3f60845602"
132132
header := &types.Header{
133133
Root: common.HexToHash(merkleRoot),
134134
Number: big.NewInt(int64(1350)),
@@ -270,7 +270,7 @@ func TestUpdateMultipleMasterNodes(t *testing.T) {
270270
t.Logf("Inserting block with propose at 1350...")
271271
blockCoinbaseA := "0xaaa0000000000000000000000000000000001350"
272272
//Get from block validator error message
273-
merkleRoot := "10f2c00fef7093f265fde39c806f17aaea50a2093ca68ab795462021c38fd4b0"
273+
merkleRoot := "c2bf7b59be5184fc1148be5db14692b2dc89a1b345895d3e8d0ee7b8a7607450"
274274
parentBlock := CreateBlock(blockchain, config, currentBlock, 1350, 450, blockCoinbaseA, signer, signFn, nil, nil, merkleRoot)
275275
err := blockchain.InsertBlock(parentBlock)
276276
assert.Nil(t, err)

consensus/tests/engine_v2_tests/penalty_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func TestHookPenaltyV2TwoEpoch(t *testing.T) {
3838
header1335 := blockchain.GetHeaderByNumber(config.XDPoS.Epoch + config.XDPoS.Gap - uint64(common.MergeSignRange))
3939
tx, err := signingTxWithKey(header1335, 0, voterKey)
4040
assert.Nil(t, err)
41-
block1349 := CreateBlock(blockchain, conf, block1348, 1349, 1358, signer.Hex(), signer, signFn, nil, nil, "10f2c00fef7093f265fde39c806f17aaea50a2093ca68ab795462021c38fd4b0")
41+
block1349 := CreateBlock(blockchain, conf, block1348, 1349, 1358, signer.Hex(), signer, signFn, nil, nil, "c2bf7b59be5184fc1148be5db14692b2dc89a1b345895d3e8d0ee7b8a7607450")
4242
err = blockchain.InsertBlock(block1349)
4343
assert.Nil(t, err)
4444
adaptor.CacheSigningTxs(block1349.Hash(), []*types.Transaction{tx})

consensus/tests/engine_v2_tests/verify_header_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func TestShouldVerifyBlock(t *testing.T) {
7777
err = adaptor.VerifyHeader(blockchain, invalidPenaltiesExistBlock, true)
7878
assert.Equal(t, utils.ErrInvalidFieldInNonGapPlusOneSwitch, err)
7979

80-
merkleRoot := "4de5b7decb41a4aa648140d0fecb169bd22f07e92be20cf63397111f48653bdc"
80+
merkleRoot := "b3e34cf1d3d80bcd2c5add880842892733e45979ddaf16e531f660fdf7ca5787"
8181
parentNotExistBlock := blockchain.GetBlockByNumber(901).Header()
8282
parentNotExistBlock.ParentHash = common.HexToHash(merkleRoot)
8383
err = adaptor.VerifyHeader(blockchain, parentNotExistBlock, true)

contracts/validator/contract/validator.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

contracts/validator/src/bytecode

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

contracts/validator/src/contracts/XDCValidator.sol

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -52,81 +52,75 @@ contract XDCValidator {
5252

5353
modifier onlyValidCandidateCap() {
5454
// anyone can deposit X XDC to become a candidate
55-
require(msg.value >= minCandidateCap, "onlyValidCandidateCap");
55+
require(msg.value >= minCandidateCap, "Low Candidate Cap");
5656
_;
5757
}
5858

5959
modifier onlyValidVoterCap() {
60-
require(msg.value >= minVoterCap, "onlyValidVoterCap");
60+
require(msg.value >= minVoterCap, "Low Voter Cap");
6161
_;
6262
}
6363

6464
modifier onlyKYCWhitelisted() {
6565
require(
6666
kycString[msg.sender].length != 0 ||
6767
ownerToCandidate[msg.sender].length > 0,
68-
"onlyKYCWhitelisted"
68+
"Not KYC Whitelisted"
6969
);
7070
_;
7171
}
7272

7373
modifier onlyOwner(address _candidate) {
74-
require(validatorsState[_candidate].owner == msg.sender, "onlyOwner");
74+
require(validatorsState[_candidate].owner == msg.sender, "Not Owner");
7575
_;
7676
}
7777

7878
modifier onlyCandidate(address _candidate) {
79-
require(validatorsState[_candidate].isCandidate, "onlyCandidate");
79+
require(validatorsState[_candidate].isCandidate, "Not Candidate");
8080
_;
8181
}
8282

8383
modifier onlyValidCandidate(address _candidate) {
84-
require(validatorsState[_candidate].isCandidate, "onlyValidCandidate");
84+
require(validatorsState[_candidate].isCandidate, "Not Valid Candidate");
8585
_;
8686
}
8787

8888
modifier onlyNotCandidate(address _candidate) {
89-
require(!validatorsState[_candidate].isCandidate, "onlyNotCandidate");
89+
require(!validatorsState[_candidate].isCandidate, "Already Candidate");
9090
_;
9191
}
9292

9393
modifier onlyValidVote(address _candidate, uint256 _cap) {
9494
require(
9595
validatorsState[_candidate].voters[msg.sender] >= _cap,
96-
"onlyValidVote validatorsState[_candidate].voters[msg.sender] >= _cap is false"
96+
"Insufficient Vote Cap"
9797
);
9898
if (validatorsState[_candidate].owner == msg.sender) {
9999
require(
100100
validatorsState[_candidate].voters[msg.sender].sub(_cap) >=
101101
minCandidateCap,
102-
"onlyValidVote validatorsState[_candidate].voters[msg.sender] - (_cap) >= minCandidateCap is false"
102+
"Below Min Cap"
103103
);
104104
}
105105
_;
106106
}
107107

108108
modifier onlyValidWithdraw(uint256 _blockNumber, uint256 _index) {
109-
require(
110-
_blockNumber > 0,
111-
"onlyValidWithdraw _blockNumber > 0 is false"
112-
);
113-
require(
114-
block.number >= _blockNumber,
115-
"onlyValidWithdraw block.number >= _blockNumber is false"
116-
);
109+
require(_blockNumber > 0, "Invalid Block Number");
110+
require(block.number >= _blockNumber, "Block Number Too High");
117111
require(
118112
withdrawsState[msg.sender].caps[_blockNumber] > 0,
119-
"onlyValidWithdraw withdrawsState[msg.sender].caps[_blockNumber] > 0 is false"
113+
"Invalid Withdraw Cap"
120114
);
121115
require(
122116
withdrawsState[msg.sender].blockNumbers[_index] == _blockNumber,
123-
"onlyValidWithdraw withdrawsState[msg.sender].blockNumbers[_index] == _blockNumber is false"
117+
"Mismatch Block Number"
124118
);
125119
_;
126120
}
127121

128122
modifier onlyGrandMaster() {
129-
require(grandMasterMap[msg.sender] == true, "onlyGrandMaster");
123+
require(grandMasterMap[msg.sender] == true, "Not Grand Master");
130124
_;
131125
}
132126

@@ -313,10 +307,7 @@ contract XDCValidator {
313307
}
314308

315309
function checkMinCandidateNum() private view {
316-
require(
317-
candidates.length >= minCandidateNum,
318-
"cadidates must greater than minCandidateNum"
319-
);
310+
require(candidates.length >= minCandidateNum, "Low Candidate Count");
320311
}
321312

322313
// voteInvalidKYC : any candidate can vote for invalid KYC i.e. a particular candidate's owner has uploaded a bad KYC.
@@ -332,7 +323,7 @@ contract XDCValidator {
332323
address _invalidMasternode = getCandidateOwner(_invalidCandidate);
333324
require(
334325
!hasVotedInvalid[candidateOwner][_invalidMasternode],
335-
"!hasVotedInvalid[candidateOwner][_invalidMasternode]"
326+
"Already Voted Invalid"
336327
);
337328
hasVotedInvalid[candidateOwner][_invalidMasternode] = true;
338329
invalidKYCCount[_invalidMasternode]++;

0 commit comments

Comments
 (0)