diff --git a/contracts/interfaces/IReward.sol b/contracts/interfaces/IReward.sol index 1f66bee..453bcea 100644 --- a/contracts/interfaces/IReward.sol +++ b/contracts/interfaces/IReward.sol @@ -3,7 +3,12 @@ pragma solidity >=0.8.0 <0.9.0; interface IReward { - event DistributeReward(uint indexed pricingIndex, address indexed beneficiary, uint amount); + event DistributeReward( + uint indexed pricingIndex, + address indexed beneficiary, + bytes32 indexed minerId, + uint amount + ); function fillReward(uint beforeLength, uint rewardSectors) external payable; diff --git a/contracts/miner/Mine.sol b/contracts/miner/Mine.sol index 8f1d5f9..ae71e66 100644 --- a/contracts/miner/Mine.sol +++ b/contracts/miner/Mine.sol @@ -224,7 +224,7 @@ contract PoraMine is ZgInitializable, AccessControlEnumerable { uint scaledTarget = poraTarget >> 16; scaledExpected = Math.mulDiv(scaledTarget, targetMineBlocks, currentSubmissions); } else { - scaledExpected = type(uint).max >> 16; + scaledExpected = type(uint).max >> 16; } _adjustDifficultyInner(scaledExpected); @@ -235,7 +235,7 @@ contract PoraMine is ZgInitializable, AccessControlEnumerable { } function _adjustDifficultyInner(uint scaledExpected) internal { - if(fixedDifficulty) { + if (fixedDifficulty) { return; } uint scaledTarget = poraTarget >> 16; @@ -327,13 +327,13 @@ contract PoraMine is ZgInitializable, AccessControlEnumerable { if (block.number <= subtaskMineStart || block.number - subtaskMineStart > targetMineBlocks) { return answer; } - + answer.subtaskDigest = keccak256(abi.encode(answer.context.digest, blockhash(subtaskMineStart))); if (answer.context.epoch > lastMinedEpoch) { _updateMineEpochWhenNeeded(answer.context); } - + if (currentSubmissions < targetSubmissions * 2) { answer.poraTarget = poraTarget; } diff --git a/contracts/miner/MineLib.sol b/contracts/miner/MineLib.sol index 7748d6e..1396d23 100644 --- a/contracts/miner/MineLib.sol +++ b/contracts/miner/MineLib.sol @@ -45,7 +45,7 @@ library MineLib { bytes32[UNITS_PER_SEAL] memory sealedData, uint skipSeals, bytes32[2] memory padDigest - ) internal view returns (bytes32[2] memory recallSeed, bytes32[UNITS_PER_SEAL] memory mixedData) { + ) internal pure returns (bytes32[2] memory recallSeed, bytes32[UNITS_PER_SEAL] memory mixedData) { bytes32[2] memory currentDigest = padDigest.deepCopy(); scratchPadHash(currentDigest, skipSeals * BHASHES_PER_SEAL); @@ -81,6 +81,7 @@ library MineLib { bytes32[8] memory slots; uint offset = 128; uint finalizeOffset = 128 + UNITS_PER_SEAL * 32; + bool blake2bError = false; assembly { let argPtr := add(slots, 0x1c) @@ -100,7 +101,7 @@ library MineLib { for { - } lt(offset, finalizeOffset) { + } and(lt(offset, finalizeOffset), not(blake2bError)) { } { offset := add(offset, 0x80) @@ -118,10 +119,11 @@ library MineLib { } if iszero(staticcall(not(0), 0x09, argPtr, 0xd5, hPtr, 0x40)) { - revert(0, 0) + blake2bError := true } } } + require(!blake2bError, "blake2b internal error at PoRA hash"); // The blake2b hash locates at slots[1] and slots[2]. // Here we only return the first 32 bytes of the blake2b hash. return slots[1]; diff --git a/contracts/miner/WorkerContext.sol b/contracts/miner/WorkerContext.sol index ee47528..b973f9c 100644 --- a/contracts/miner/WorkerContext.sol +++ b/contracts/miner/WorkerContext.sol @@ -8,4 +8,4 @@ struct WorkerContext { uint poraTarget; bytes32 subtaskDigest; uint64 maxShards; -} \ No newline at end of file +} diff --git a/contracts/reward/ChunkRewardBase.sol b/contracts/reward/ChunkRewardBase.sol index e9c2585..158ce9b 100644 --- a/contracts/reward/ChunkRewardBase.sol +++ b/contracts/reward/ChunkRewardBase.sol @@ -78,7 +78,7 @@ abstract contract ChunkRewardBase is IReward, PullPayment, ZgInitializable, Acce } } - function claimMineReward(uint pricingIndex, address payable beneficiary, bytes32) external { + function claimMineReward(uint pricingIndex, address payable beneficiary, bytes32 minerID) external { require(_msgSender() == mine, "Sender does not have permission"); Reward memory reward = rewards[pricingIndex]; @@ -95,7 +95,7 @@ abstract contract ChunkRewardBase is IReward, PullPayment, ZgInitializable, Acce if (rewardAmount > 0) { _asyncTransfer(beneficiary, rewardAmount); - emit DistributeReward(pricingIndex, beneficiary, rewardAmount); + emit DistributeReward(pricingIndex, beneficiary, minerID, rewardAmount); } } diff --git a/contracts/reward/OnePoolReward.sol b/contracts/reward/OnePoolReward.sol index 2e4554f..1194e18 100644 --- a/contracts/reward/OnePoolReward.sol +++ b/contracts/reward/OnePoolReward.sol @@ -119,7 +119,7 @@ contract OnePoolReward is IReward, Context, ZgInitializable { } } - function claimMineReward(uint pricingIndex, address payable beneficiary, bytes32) external { + function claimMineReward(uint pricingIndex, address payable beneficiary, bytes32 minerID) external { require(_msgSender() == mine, "Sender does not have permission"); if (pricingIndex < firstValidChunk) { @@ -136,7 +136,7 @@ contract OnePoolReward is IReward, Context, ZgInitializable { if (claimable > 0) { beneficiary.transfer(claimable); - emit DistributeReward(pricingIndex, beneficiary, claimable); + emit DistributeReward(pricingIndex, beneficiary, minerID, claimable); } } diff --git a/contracts/utils/Blake2b.sol b/contracts/utils/Blake2b.sol index 0cf815c..a51f294 100644 --- a/contracts/utils/Blake2b.sol +++ b/contracts/utils/Blake2b.sol @@ -85,11 +85,13 @@ library Blake2b { bytes8[2] memory t = blake2bLength(offset); bytes memory args = abi.encodePacked(rounds, h[0], h[1], m0, m1, m2, m3, t[0], t[1], finalize); + uint blake2bOut; + assembly { - if iszero(staticcall(not(0), 0x09, add(args, 32), 0xd5, output, 0x40)) { - revert(0, 0) - } + blake2bOut := staticcall(not(0), 0x09, add(args, 32), 0xd5, output, 0x40) } + + require(blake2bOut != 0, "blake2b internal error at blake2bF"); } function blake2bLength(uint length) internal pure returns (bytes8[2] memory t) {