Skip to content

Commit c849334

Browse files
uri-99MauroToscano
andauthored
refactor: move calculation of priceperproof to batcher (#530)
Co-authored-by: Mauro Toscano <12560266+MauroToscano@users.noreply.github.com>
1 parent fc446bf commit c849334

9 files changed

Lines changed: 29 additions & 544 deletions

File tree

batcher/aligned-batcher/src/eth/abi/BatcherPaymentService.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

batcher/aligned-batcher/src/eth/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,15 @@ pub async fn create_new_task(
6666
batch_merkle_root: [u8; 32],
6767
batch_data_pointer: String,
6868
proof_submitters: Vec<Address>,
69-
respond_to_task_cost: U256,
69+
gas_for_aggregator: U256,
70+
gas_per_proof: U256,
7071
) -> Result<TransactionReceipt, anyhow::Error> {
7172
let call = payment_service.create_new_task(
7273
batch_merkle_root,
7374
batch_data_pointer,
7475
proof_submitters,
75-
respond_to_task_cost,
76+
gas_for_aggregator,
77+
gas_per_proof,
7678
);
7779
let pending_tx = call.send().await?;
7880

batcher/aligned-batcher/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,8 @@ impl Batcher {
451451
*batch_merkle_root,
452452
batch_data_pointer,
453453
submitter_addresses,
454-
U256::from(400000000000000u64),
454+
U256::from(350000u64), // FIXME(uri): This value should be read from /Users/urix/aligned_layer/contracts/script/deploy/config/devnet/batcher-payment-service.devnet.config.json
455+
U256::from(21000u64), //FIXME(uri): This value should be read from /Users/urix/aligned_layer/contracts/script/deploy/config/devnet/batcher-payment-service.devnet.config.json
455456
)
456457
.await
457458
{

contracts/script/deploy/BatcherPaymentServiceDeployer.s.sol

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,6 @@ contract BatcherPaymentServiceDeployer is Script {
2828
".permissions.owner"
2929
);
3030

31-
uint256 paymentServiceCreateTaskGasCost = stdJson.readUint(
32-
config_data,
33-
".amounts.paymentServiceCreateTaskGasCost"
34-
);
35-
36-
uint256 serviceManagerCreateTaskGasCost = stdJson.readUint(
37-
config_data,
38-
".amounts.serviceManagerCreateTaskGasCost"
39-
);
40-
41-
uint256 extraUserTxGasCost = stdJson.readUint(
42-
config_data,
43-
".amounts.extraUserTxGasCost"
44-
);
45-
4631
vm.startBroadcast();
4732

4833
BatcherPaymentService batcherPaymentService = new BatcherPaymentService();
@@ -53,10 +38,7 @@ contract BatcherPaymentServiceDeployer is Script {
5338
BatcherPaymentService(payable(address(proxy))).initialize(
5439
alignedLayerServiceManager,
5540
batcherPaymentServiceOwner,
56-
batcherWallet,
57-
paymentServiceCreateTaskGasCost,
58-
serviceManagerCreateTaskGasCost,
59-
extraUserTxGasCost
41+
batcherWallet
6042
);
6143

6244
vm.stopBroadcast();

contracts/script/deploy/config/devnet/batcher-payment-service.devnet.config.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
"alignedLayerServiceManager": "0x1613beB3B2C4f22Ee086B2b38C1476A3cE7f78E8"
66
},
77
"amounts": {
8-
"paymentServiceCreateTaskGasCost": 42000,
9-
"serviceManagerCreateTaskGasCost": 60000,
10-
"extraUserTxGasCost": 6500
8+
"gasForAggregator": "300000",
9+
"gasPerProof": "21000"
1110
},
1211
"permissions": {
1312
"owner": "0x14dC79964da2C08b23698B3D3cc7Ca32193d9955"

contracts/script/deploy/config/holesky/batcher-payment-service.holesky.config.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
"alignedLayerServiceManager": "<aligned_layer_service_manager_address>"
55
},
66
"amounts": {
7-
"paymentServiceCreateTaskGasCost": 42000,
8-
"serviceManagerCreateTaskGasCost": 60000,
9-
"extraUserTxGasCost": 6500
7+
"gasForAggregator": "300000",
8+
"gasPerProof": "21000"
109
},
1110
"permissions": {
1211
"owner": "<owner_address>"

contracts/scripts/anvil/state/alignedlayer-deployed-anvil-state.json

Lines changed: 1 addition & 468 deletions
Large diffs are not rendered by default.

contracts/src/core/AlignedLayerServiceManager.sol

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ contract AlignedLayerServiceManager is
133133
// and send transaction cost to aggregator.
134134
uint256 finalGasLeft = gasleft();
135135

136-
// FIXME: should we add 21000 gas from the transfer + some additional for the other steps (~40k gas)?
137-
uint256 txCost = (initialGasLeft - finalGasLeft + 21000) * tx.gasprice;
136+
// 70k was measured by trial and error until the aggregator got paid a bit over what it needed
137+
uint256 txCost = (initialGasLeft - finalGasLeft + 70000) * tx.gasprice;
138138

139139
require(
140140
batchersBalances[batchesState[batchMerkleRoot].batcherAddress] >=

contracts/src/core/BatcherPaymentService.sol

Lines changed: 14 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@ contract BatcherPaymentService is
2121

2222
mapping(address => uint256) public UserBalances;
2323

24-
uint256 public PAYMENT_SERVICE_CREATE_TASK_GAS_COST; // Base gas cost of executing createNewTask of this contract
25-
uint256 public SERVICE_MANAGER_CREATE_TASK_GAS_COST; // Gas cost of calling createNewTask in AlignedLayerServiceManager
26-
uint256 public EXTRA_USER_TX_GAS_COST; // As we must iterate over the proofSubmitters, there is an extra gas cost per extra user
27-
2824
// storage gap for upgradeability
2925
uint256[25] private __GAP;
3026

@@ -36,20 +32,14 @@ contract BatcherPaymentService is
3632
function initialize(
3733
address _AlignedLayerServiceManager,
3834
address _BatcherPaymentServiceOwner,
39-
address _BatcherWallet,
40-
uint256 _PaymentServiceCreateTaskGasCost,
41-
uint256 _ServiceManagerCreateTaskGasCost,
42-
uint256 _ExtraUserTxGasCost
35+
address _BatcherWallet
4336
) public initializer {
4437
__Ownable_init(); // default is msg.sender
4538
__UUPSUpgradeable_init();
4639
_transferOwnership(_BatcherPaymentServiceOwner);
4740

4841
AlignedLayerServiceManager = _AlignedLayerServiceManager;
4942
BatcherWallet = _BatcherWallet;
50-
PAYMENT_SERVICE_CREATE_TASK_GAS_COST = _PaymentServiceCreateTaskGasCost;
51-
SERVICE_MANAGER_CREATE_TASK_GAS_COST = _ServiceManagerCreateTaskGasCost;
52-
EXTRA_USER_TX_GAS_COST = _ExtraUserTxGasCost;
5343
}
5444

5545
// PAYABLE FUNCTIONS
@@ -63,38 +53,33 @@ contract BatcherPaymentService is
6353
bytes32 batchMerkleRoot,
6454
string calldata batchDataPointer,
6555
address[] calldata proofSubmitters, // one address for each payer proof, 1 user has 2 proofs? send twice that address
66-
uint256 costOfRespondToTask // TODO hardcode gas cost? It is variable because of signature sdk. could have upper bound and multiply by current gas cost + x%.
56+
uint256 gasForAggregator,
57+
uint256 gasPerProof
6758
) external onlyBatcher whenNotPaused {
59+
uint256 feeForAggregator = gasForAggregator * tx.gasprice;
60+
uint256 feePerProof = gasPerProof * tx.gasprice;
61+
6862
uint256 amountOfSubmitters = proofSubmitters.length;
69-
require(amountOfSubmitters > 0, "No proof submitters");
7063

71-
// each user must pay its fraction of the gas cost of this transaction back to the batcher
72-
// + 10% for increments in gas price
73-
uint256 currentTxCost = ((PAYMENT_SERVICE_CREATE_TASK_GAS_COST +
74-
SERVICE_MANAGER_CREATE_TASK_GAS_COST +
75-
(EXTRA_USER_TX_GAS_COST * amountOfSubmitters)) *
76-
tx.gasprice *
77-
11) / 10;
64+
require(amountOfSubmitters > 0, "No proof submitters");
7865

79-
// divide the price by the amount of submitters
80-
uint256 totalCostPerProof = (costOfRespondToTask + currentTxCost) /
81-
amountOfSubmitters;
66+
require(feePerProof * amountOfSubmitters > feeForAggregator, "Not enough gas to pay the batcher");
8267

8368
// discount from each payer
8469
// will revert if one of them has insufficient balance
8570
for (uint256 i = 0; i < amountOfSubmitters; i++) {
8671
address payer = proofSubmitters[i];
8772
require(
88-
UserBalances[payer] >= totalCostPerProof,
73+
UserBalances[payer] >= feePerProof,
8974
"Payer has insufficient balance"
9075
);
91-
UserBalances[payer] -= totalCostPerProof;
76+
UserBalances[payer] -= feePerProof;
9277
}
9378

9479
// call alignedLayerServiceManager
9580
// with value to fund the task's response
9681
(bool success, ) = AlignedLayerServiceManager.call{
97-
value: costOfRespondToTask
82+
value: feeForAggregator
9883
}(
9984
abi.encodeWithSignature(
10085
"createNewTask(bytes32,string)",
@@ -105,7 +90,9 @@ contract BatcherPaymentService is
10590

10691
require(success, "createNewTask call failed");
10792

108-
payable(BatcherWallet).transfer(currentTxCost);
93+
uint256 feeForBatcher = (feePerProof * amountOfSubmitters) - feeForAggregator;
94+
95+
payable(BatcherWallet).transfer(feeForBatcher);
10996
}
11097

11198
function withdraw(uint256 amount) external whenNotPaused {
@@ -118,24 +105,6 @@ contract BatcherPaymentService is
118105
emit FundsWithdrawn(msg.sender, amount);
119106
}
120107

121-
function setPaymentServiceCreateTaskGasCost(
122-
uint256 amount
123-
) external onlyOwner whenNotPaused {
124-
PAYMENT_SERVICE_CREATE_TASK_GAS_COST = amount;
125-
}
126-
127-
function setServiceManagerCreateTaskGasCost(
128-
uint256 amount
129-
) external onlyOwner whenNotPaused {
130-
SERVICE_MANAGER_CREATE_TASK_GAS_COST = amount;
131-
}
132-
133-
function setExtraUserTxGasCost(
134-
uint256 amount
135-
) external onlyOwner whenNotPaused {
136-
EXTRA_USER_TX_GAS_COST = amount;
137-
}
138-
139108
function pause() public onlyOwner {
140109
_pause();
141110
}

0 commit comments

Comments
 (0)