Skip to content

Commit

Permalink
Temporarily fix batch indentifier
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielbosio committed Sep 13, 2024
1 parent 741a15f commit 710a9e8
Showing 1 changed file with 43 additions and 121 deletions.
164 changes: 43 additions & 121 deletions contracts/src/core/AlignedLayerServiceManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {IRewardsCoordinator} from "eigenlayer-contracts/src/contracts/interfaces
import {AlignedLayerServiceManagerStorage} from "./AlignedLayerServiceManagerStorage.sol";
import {IAlignedLayerServiceManager} from "./IAlignedLayerServiceManager.sol";

import "forge-std/console.sol";

/**
* @title Primary entrypoint for procuring services from Aligned.
*/
Expand All @@ -26,17 +28,10 @@ contract AlignedLayerServiceManager is
uint8 internal constant QUORUM_THRESHOLD_PERCENTAGE = 67;

//old NewBatch event, for smooth Operator upgradeability
event NewBatch(
bytes32 indexed batchMerkleRoot,
uint32 taskCreatedBlock,
string batchDataPointer
);
event NewBatch(bytes32 indexed batchMerkleRoot, uint32 taskCreatedBlock, string batchDataPointer);
// EVENTS
event NewBatchV2(
bytes32 indexed batchMerkleRoot,
address senderAddress,
uint32 taskCreatedBlock,
string batchDataPointer
bytes32 indexed batchMerkleRoot, address senderAddress, uint32 taskCreatedBlock, string batchDataPointer
);

event BatchVerified(bytes32 indexed batchMerkleRoot, address senderAddress);
Expand All @@ -49,45 +44,32 @@ contract AlignedLayerServiceManager is
IStakeRegistry __stakeRegistry
)
BLSSignatureChecker(__registryCoordinator)
ServiceManagerBase(
__avsDirectory,
__rewardsCoordinator,
__registryCoordinator,
__stakeRegistry
)
ServiceManagerBase(__avsDirectory, __rewardsCoordinator, __registryCoordinator, __stakeRegistry)
{
_disableInitializers();
}

// @param _rewardsInitiator The address which is allowed to create AVS rewards submissions.
function initialize(
address _initialOwner,
address _rewardsInitiator
) public initializer {
function initialize(address _initialOwner, address _rewardsInitiator) public initializer {
__ServiceManagerBase_init(_initialOwner, _rewardsInitiator);
}

function createNewTask(
bytes32 batchMerkleRoot,
string calldata batchDataPointer
) external payable {
function createNewTask(bytes32 batchMerkleRoot, string calldata batchDataPointer) external payable {
bytes32 batchIdentifier;
if (block.number < 2_268_375) // TODO set number of blocks
console.log("block.number: %s", block.number);
if (
block.number < 2_268_375 // TODO set number of blocks
) {
batchIdentifier = batchMerkleRoot;
else
} else {
batchIdentifier = keccak256(abi.encodePacked(batchMerkleRoot, msg.sender));
}

require(
batchesState[batchIdentifier].taskCreatedBlock == 0,
"Batch was already submitted"
);
require(batchesState[batchIdentifier].taskCreatedBlock == 0, "Batch was already submitted");

if (msg.value > 0) {
batchersBalances[msg.sender] += msg.value;
emit BatcherBalanceUpdated(
msg.sender,
batchersBalances[msg.sender]
);
emit BatcherBalanceUpdated(msg.sender, batchersBalances[msg.sender]);
}

require(batchersBalances[msg.sender] > 0, "Batcher balance is empty");
Expand All @@ -100,17 +82,8 @@ contract AlignedLayerServiceManager is
batchesState[batchIdentifier] = batchState;

// old event for smooth Operator upgradeability:
emit NewBatch(
batchMerkleRoot,
uint32(block.number),
batchDataPointer
);
emit NewBatchV2(
batchMerkleRoot,
msg.sender,
uint32(block.number),
batchDataPointer
);
emit NewBatch(batchMerkleRoot, uint32(block.number), batchDataPointer);
emit NewBatchV2(batchMerkleRoot, msg.sender, uint32(block.number), batchDataPointer);
}

// previous version of this function, for smooth upgradeability
Expand All @@ -129,40 +102,25 @@ contract AlignedLayerServiceManager is

// Note: This is a hacky solidity way to see that the element exists
// Value 0 would mean that the task is in block 0 so this can't happen.
require(
batchesState[batchMerkleRoot].taskCreatedBlock != 0,
"Batch doesn't exists"
);
require(batchesState[batchMerkleRoot].taskCreatedBlock != 0, "Batch doesn't exists");

// Check task hasn't been responsed yet
require(
batchesState[batchMerkleRoot].responded == false,
"Batch already responded"
);
require(batchesState[batchMerkleRoot].responded == false, "Batch already responded");

require(
batchersBalances[batcherAddress] > 0,
"Batcher has no balance"
);
require(batchersBalances[batcherAddress] > 0, "Batcher has no balance");

batchesState[batchMerkleRoot].responded = true;

/* CHECKING SIGNATURES & WHETHER THRESHOLD IS MET OR NOT */
// check that aggregated BLS signature is valid
(
QuorumStakeTotals memory quorumStakeTotals,
bytes32 _hashOfNonSigners
) = checkSignatures(
batchMerkleRoot,
batchesState[batchMerkleRoot].taskCreatedBlock,
nonSignerStakesAndSignature
);
(QuorumStakeTotals memory quorumStakeTotals, bytes32 _hashOfNonSigners) = checkSignatures(
batchMerkleRoot, batchesState[batchMerkleRoot].taskCreatedBlock, nonSignerStakesAndSignature
);

// check that signatories own at least a threshold percentage of each quourm
require(
quorumStakeTotals.signedStakeForQuorum[0] * THRESHOLD_DENOMINATOR >=
quorumStakeTotals.totalStakeForQuorum[0] *
QUORUM_THRESHOLD_PERCENTAGE,
quorumStakeTotals.signedStakeForQuorum[0] * THRESHOLD_DENOMINATOR
>= quorumStakeTotals.totalStakeForQuorum[0] * QUORUM_THRESHOLD_PERCENTAGE,
"Signatories do not own at least threshold percentage of a quorum"
);

Expand All @@ -175,14 +133,10 @@ contract AlignedLayerServiceManager is
uint256 txCost = (initialGasLeft - finalGasLeft + 70000) * tx.gasprice;

require(
batchersBalances[batcherAddress] >=
txCost,
"Batcher has not sufficient funds for paying this transaction"
batchersBalances[batcherAddress] >= txCost, "Batcher has not sufficient funds for paying this transaction"
);

batchersBalances[
batcherAddress
] -= txCost;
batchersBalances[batcherAddress] -= txCost;
payable(msg.sender).transfer(txCost);
}

Expand All @@ -194,45 +148,31 @@ contract AlignedLayerServiceManager is
) external {
uint256 initialGasLeft = gasleft();

bytes32 batchIdentifierHash = keccak256(
abi.encodePacked(batchMerkleRoot, senderAddress)
);
bytes32 batchIdentifierHash = keccak256(abi.encodePacked(batchMerkleRoot, senderAddress));

/* CHECKING SIGNATURES & WHETHER THRESHOLD IS MET OR NOT */

// Note: This is a hacky solidity way to see that the element exists
// Value 0 would mean that the task is in block 0 so this can't happen.
require(
batchesState[batchIdentifierHash].taskCreatedBlock != 0,
"Batch doesn't exists"
);
require(batchesState[batchIdentifierHash].taskCreatedBlock != 0, "Batch doesn't exists");

// Check task hasn't been responsed yet
require(
batchesState[batchIdentifierHash].responded == false,
"Batch already responded"
);
require(batchesState[batchIdentifierHash].responded == false, "Batch already responded");

require(batchersBalances[senderAddress] > 0, "Batcher has no balance");

batchesState[batchIdentifierHash].responded = true;

/* CHECKING SIGNATURES & WHETHER THRESHOLD IS MET OR NOT */
// check that aggregated BLS signature is valid
(
QuorumStakeTotals memory quorumStakeTotals,
bytes32 _hashOfNonSigners
) = checkSignatures(
batchIdentifierHash,
batchesState[batchIdentifierHash].taskCreatedBlock,
nonSignerStakesAndSignature
);
(QuorumStakeTotals memory quorumStakeTotals, bytes32 _hashOfNonSigners) = checkSignatures(
batchIdentifierHash, batchesState[batchIdentifierHash].taskCreatedBlock, nonSignerStakesAndSignature
);

// check that signatories own at least a threshold percentage of each quourm
require(
quorumStakeTotals.signedStakeForQuorum[0] * THRESHOLD_DENOMINATOR >=
quorumStakeTotals.totalStakeForQuorum[0] *
QUORUM_THRESHOLD_PERCENTAGE,
quorumStakeTotals.signedStakeForQuorum[0] * THRESHOLD_DENOMINATOR
>= quorumStakeTotals.totalStakeForQuorum[0] * QUORUM_THRESHOLD_PERCENTAGE,
"Signatories do not own at least threshold percentage of a quorum"
);

Expand All @@ -246,15 +186,11 @@ contract AlignedLayerServiceManager is
uint256 txCost = (initialGasLeft - finalGasLeft + 70000) * tx.gasprice;

require(
batchersBalances[senderAddress] >= txCost,
"Batcher has not sufficient funds for paying this transaction"
batchersBalances[senderAddress] >= txCost, "Batcher has not sufficient funds for paying this transaction"
);

batchersBalances[senderAddress] -= txCost;
emit BatcherBalanceUpdated(
senderAddress,
batchersBalances[senderAddress]
);
emit BatcherBalanceUpdated(senderAddress, batchersBalances[senderAddress]);
payable(msg.sender).transfer(txCost);
}

Expand All @@ -268,9 +204,8 @@ contract AlignedLayerServiceManager is
uint256 verificationDataBatchIndex,
address senderAddress
) external view returns (bool) {
bytes32 batchIdentifierHash = keccak256(
abi.encodePacked(batchMerkleRoot, senderAddress)
);
// bytes32 batchIdentifierHash = keccak256(abi.encodePacked(batchMerkleRoot, senderAddress));
bytes32 batchIdentifierHash = batchMerkleRoot;

if (batchesState[batchIdentifierHash].taskCreatedBlock == 0) {
return false;
Expand All @@ -280,22 +215,12 @@ contract AlignedLayerServiceManager is
return false;
}

bytes memory leaf = abi.encodePacked(
proofCommitment,
pubInputCommitment,
provingSystemAuxDataCommitment,
proofGeneratorAddr
);
bytes memory leaf =
abi.encodePacked(proofCommitment, pubInputCommitment, provingSystemAuxDataCommitment, proofGeneratorAddr);

bytes32 hashedLeaf = keccak256(leaf);

return
Merkle.verifyInclusionKeccak(
merkleProof,
batchIdentifierHash,
hashedLeaf,
verificationDataBatchIndex
);
return Merkle.verifyInclusionKeccak(merkleProof, batchIdentifierHash, hashedLeaf, verificationDataBatchIndex);
}

function balanceOf(address account) public view returns (uint256) {
Expand All @@ -307,10 +232,7 @@ contract AlignedLayerServiceManager is
emit BatcherBalanceUpdated(msg.sender, batchersBalances[msg.sender]);
}

function checkPublicInput(
bytes calldata publicInput,
bytes32 hash
) public pure returns (bool) {
function checkPublicInput(bytes calldata publicInput, bytes32 hash) public pure returns (bool) {
return keccak256(publicInput) == hash;
}
}

0 comments on commit 710a9e8

Please sign in to comment.