From 9702626e0e723461c77c2642d4bd7b71d324375c Mon Sep 17 00:00:00 2001 From: Ladd Hoffman Date: Sun, 28 Apr 2024 18:02:39 -0500 Subject: [PATCH] add batch size check in rollup contract --- ethereum/contracts/Rollup.sol | 52 +++++++++++++++++++++++------------ 1 file changed, 35 insertions(+), 17 deletions(-) diff --git a/ethereum/contracts/Rollup.sol b/ethereum/contracts/Rollup.sol index 39abe37..366a73a 100644 --- a/ethereum/contracts/Rollup.sol +++ b/ethereum/contracts/Rollup.sol @@ -8,10 +8,10 @@ contract Rollup is Availability { constructor(DAO dao) Availability(dao) {} struct BatchItem { - address worker; + address author; uint stakeAmount; uint fee; - string evidenceContentId; + string postId; } mapping(uint => BatchItem) items; @@ -19,22 +19,29 @@ contract Rollup is Availability { address batchWorker; uint batchWorkerStakeIndex; + /// Instead of initiating a validation pool, call this method to include + /// the stakes and fee in the next batch validation pool function addItem( - address worker, + address author, uint stakeAmount, - string calldata evidenceContentId + string calldata postId ) public payable { BatchItem storage item = items[itemCount++]; - item.worker = worker; + item.author = author; item.stakeAmount = stakeAmount; item.fee = msg.value; - item.evidenceContentId = evidenceContentId; + item.postId = postId; } + /// To be called by the currently assigned batch worker, + /// If no batch worker has been assigned this may be called by anybody, + /// but it will only succeed if it is able to assign a new worker. function submitBatch( string calldata batchPostId, + uint batchSize, uint poolDuration - ) public { + ) public returns (uint poolIndex) { + require(batchSize <= itemCount, "Batch size too large"); if (batchWorker != address(0)) { require( msg.sender == batchWorker, @@ -46,7 +53,7 @@ contract Rollup is Availability { for (uint i = 0; i < itemCount; i++) { fee += items[i].fee; } - uint poolIndex = dao.initiateValidationPool{value: fee}( + poolIndex = dao.initiateValidationPool{value: fee}( batchPostId, poolDuration, [uint256(1), uint256(3)], @@ -60,20 +67,31 @@ contract Rollup is Availability { for (uint i = 0; i < itemCount; i++) { dao.delegatedStakeOnValidationPool( poolIndex, - items[i].worker, + items[i].author, items[i].stakeAmount, true ); } // Include availability stakes from the batch worker - dao.delegatedStakeOnValidationPool( - poolIndex, - batchWorker, - stakes[batchWorkerStakeIndex].amount, - true - ); - // Reset item count so we can start the next batch - itemCount = 0; + if (batchWorker != address(0)) { + dao.delegatedStakeOnValidationPool( + poolIndex, + batchWorker, + stakes[batchWorkerStakeIndex].amount, + true + ); + } + if (batchSize < itemCount) { + // Some items were added after this batch was computed. + // Keep them in the queue to be included in the next batch. + for (uint i = 0; i < itemCount - batchSize; i++) { + items[i] = items[batchSize + i]; + } + itemCount = itemCount - batchSize; + } else { + // Reset item count so we can start the next batch + itemCount = 0; + } // Select the next worker batchWorkerStakeIndex = assignWork(); batchWorker = stakes[batchWorkerStakeIndex].worker;