Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add minerID to Distribute event && add revert reason to blake2b call #49

Merged
merged 1 commit into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion contracts/interfaces/IReward.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
8 changes: 4 additions & 4 deletions contracts/miner/Mine.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -235,7 +235,7 @@ contract PoraMine is ZgInitializable, AccessControlEnumerable {
}

function _adjustDifficultyInner(uint scaledExpected) internal {
if(fixedDifficulty) {
if (fixedDifficulty) {
return;
}
uint scaledTarget = poraTarget >> 16;
Expand Down Expand Up @@ -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;
}
Expand Down
8 changes: 5 additions & 3 deletions contracts/miner/MineLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand All @@ -100,7 +101,7 @@ library MineLib {

for {

} lt(offset, finalizeOffset) {
} and(lt(offset, finalizeOffset), not(blake2bError)) {

} {
offset := add(offset, 0x80)
Expand All @@ -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];
Expand Down
2 changes: 1 addition & 1 deletion contracts/miner/WorkerContext.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ struct WorkerContext {
uint poraTarget;
bytes32 subtaskDigest;
uint64 maxShards;
}
}
4 changes: 2 additions & 2 deletions contracts/reward/ChunkRewardBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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);
}
}

Expand Down
4 changes: 2 additions & 2 deletions contracts/reward/OnePoolReward.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}
}

Expand Down
8 changes: 5 additions & 3 deletions contracts/utils/Blake2b.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading