Skip to content

Commit 825cee6

Browse files
committed
permission checking for service transaction for announceAvailability for free.
1 parent 0bc2fd0 commit 825cee6

File tree

5 files changed

+50
-9
lines changed

5 files changed

+50
-9
lines changed

.solhint.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"extends": "solhint:default",
33
"plugins": [],
44
"rules": {
5-
"code-complexity": ["warn", 19],
6-
"function-max-lines": ["warn", 135]
5+
"code-complexity": ["warn", 20],
6+
"function-max-lines": ["warn", 137]
77
}
88
}

contracts/TxPermissionHbbft.sol

+8-3
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,11 @@ contract TxPermissionHbbft is UpgradeableOwned, ITxPermission {
249249
(bool callable,) = validatorSetContract.reportMaliciousCallable(
250250
_sender, maliciousMiningAddress, blockNumber
251251
);
252-
253252
return (callable ? CALL : NONE, false);
254-
} else if (_gasPrice > 0) {
253+
} else if (signature == ANNOUNCE_AVAILABILITY_SIGNATURE) {
254+
return (validatorSetContract.canCallAnnounceAvailability(_sender) ? CALL : NONE, false);
255+
}
256+
else if (_gasPrice > 0) {
255257
// The other functions of ValidatorSet contract can be called
256258
// by anyone except validators' mining addresses if gasPrice is not zero
257259
return (validatorSetContract.isValidator(_sender) ? NONE : CALL, false);
@@ -368,7 +370,10 @@ contract TxPermissionHbbft is UpgradeableOwned, ITxPermission {
368370

369371
// bytes4(keccak256("writeAcks(uint256,bytes[])"))
370372
bytes4 public constant WRITE_ACKS_SIGNATURE = 0xc56aef48;
371-
373+
374+
// bytes4(keccak256("announceAvailability()"))
375+
bytes4 public constant ANNOUNCE_AVAILABILITY_SIGNATURE = 0x292525af;
376+
372377
/// @dev An internal function used by the `addAllowedSender` and `initialize` functions.
373378
/// @param _sender The address for which transactions of any type must be allowed.
374379
function _addAllowedSender(address _sender)

contracts/ValidatorSetHbbft.sol

+4-2
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,6 @@ contract ValidatorSetHbbft is UpgradeabilityAdmin, IValidatorSetHbbft {
624624

625625
if (stakingByMiningAddress[_miningAddress] == address(0)) {
626626
// not a validator node.
627-
// revert('this address is not a validator');
628627
return false;
629628
}
630629

@@ -718,13 +717,16 @@ contract ValidatorSetHbbft is UpgradeabilityAdmin, IValidatorSetHbbft {
718717

719718
// if (stakingByMiningAddress[_miningAddress] == address(0)) {
720719
// // not a validator node.
720+
// // revert('this address is not a validator');
721721
// return false;
722722
// }
723723

724-
// if (validatorAvailableSince[_miningAddress] == 0) {
724+
// if (validatorAvailableSince[_miningAddress] != 0) {
725725
// // "Validator was not marked as unavailable."
726726
// return false;
727727
// }
728+
729+
// return true;
728730
// }
729731

730732
/// @dev Updates the total reporting counter (see the `reportingCounterTotal` public mapping) for the current

contracts/interfaces/IValidatorSetHbbft.sol

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ interface IValidatorSetHbbft {
2121
function handleFailedKeyGeneration() external;
2222
function areDelegatorsBanned(address) external view returns(bool);
2323
function blockRewardContract() external view returns(address);
24-
//function canCallAnnounceAvailability() external view returns(bool);
24+
function canCallAnnounceAvailability(address _miningAddress) external view returns(bool);
2525
function getPendingValidators() external view returns(address[] memory);
2626
function getPreviousValidators() external view returns(address[] memory);
2727
function getValidators() external view returns(address[] memory);

test/KeyGenHistory.js

+35-1
Original file line numberDiff line numberDiff line change
@@ -246,9 +246,11 @@ contract('InitializerHbbft', async accounts => {
246246
// they are gone :-o
247247
//(await validatorSetHbbft.getPendingValidators.call()).should.be.deep.equal([]);
248248

249+
249250
// announcing availability.
250251
// this should place us back on the list of active and available pools.
251-
(await validatorSetHbbft.announceAvailability({ from: newPoolMiningAddress }));
252+
await announceAvailability(newPoolMiningAddress);
253+
252254

253255
await printValidatorState('after announceAvailability:');
254256

@@ -456,6 +458,38 @@ async function call2ParaFunction(functionName, from, upcommingEpochNumber, parts
456458
await call.send({ from, gas: '7000000' });
457459
}
458460

461+
async function announceAvailability( pool ) {
462+
463+
const call = validatorSetHbbft.contract.methods.announceAvailability();
464+
465+
const asEncoded = call.encodeABI();
466+
467+
if (logOutput) {
468+
console.log('calling: announceAvailability');
469+
console.log('pool: ', pool)
470+
console.log('ecodedCall: ', asEncoded);
471+
}
472+
473+
const allowedTxType = await txPermission.allowedTxTypes(pool, validatorSetHbbft.address, '0x0' /* value */, '0x0' /* gas price */, asEncoded);
474+
475+
//console.log(allowedTxType.typesMask.toString());
476+
// don't ask to cache this result.
477+
allowedTxType.cache.should.be.equal(false);
478+
479+
/// 0x01 - basic transaction (e.g. ether transferring to user wallet);
480+
/// 0x02 - contract call;
481+
/// 0x04 - contract creation;
482+
/// 0x08 - private transaction.
483+
484+
allowedTxType.typesMask.should.be.bignumber.equal(new BN('2'), 'Transaction should be allowed according to TxPermission Contract.');
485+
486+
// we know now, that this call is allowed.
487+
// so we can execute it.
488+
await call.send({ from: pool, gas: '7000000' });
489+
490+
}
491+
492+
459493
async function callReward(isEpochEndBlock) {
460494
// console.log('getting validators...');
461495
// note: this call used to crash because of a internal problem with a previous call of evm_mine and evm_increase_time https://github.com/DMDcoin/hbbft-posdao-contracts/issues/13

0 commit comments

Comments
 (0)