Skip to content
This repository was archived by the owner on Jun 24, 2025. It is now read-only.

Commit 083bb20

Browse files
authored
Gmpv3 (#60)
1 parent c375eae commit 083bb20

File tree

11 files changed

+311
-778
lines changed

11 files changed

+311
-778
lines changed

src/GasUtils.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ library GasUtils {
1212
using PrimitiveUtils for uint256;
1313

1414
function calldataSize(uint16 messageSize) internal pure returns (uint256) {
15-
return uint256(messageSize).align32() + 708; // selector + Signature + Batch
15+
return uint256(messageSize).align32() + 676; // selector + Signature + Batch
1616
}
1717

1818
/**

src/Gateway.sol

Lines changed: 20 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,6 @@ contract Gateway is IGateway, UUPSUpgradeable, OwnableUpgradeable {
5656
bytes32 indexed id, bytes32 indexed source, address indexed dest, GmpStatus status, bytes32 result
5757
);
5858

59-
/**
60-
* @dev Emitted when shards are registered.
61-
* @param keys registered shard's keys
62-
*/
63-
event ShardsRegistered(TssKey[] keys);
64-
65-
/**
66-
* @dev Emitted when shards are unregistered.
67-
* @param keys unregistered shard's keys
68-
*/
69-
event ShardsUnregistered(TssKey[] keys);
70-
7159
// number of signing sessions per batch
7260
mapping(uint64 => uint16) internal _signingSessions;
7361

@@ -112,6 +100,8 @@ contract Gateway is IGateway, UUPSUpgradeable, OwnableUpgradeable {
112100
transferOwnership(newAdmin);
113101
}
114102

103+
function _include_gmp_message(GmpMessage memory) external {}
104+
115105
/**
116106
* Withdraw funds from the gateway contract
117107
* @param amount The amount to withdraw
@@ -142,36 +132,34 @@ contract Gateway is IGateway, UUPSUpgradeable, OwnableUpgradeable {
142132
* @dev List all shards.
143133
*/
144134
function shards() external view returns (TssKey[] memory) {
145-
return ShardStore.getMainStorage().listShards();
135+
return ShardStore.getMainStorage().list();
146136
}
147137

148138
/**
149139
* @dev Register Shards in batch.
150140
*/
151-
function setShards(TssKey[] calldata publicKeys) external onlyOwner {
152-
(TssKey[] memory created, TssKey[] memory revoked) = ShardStore.getMainStorage().replaceTssKeys(publicKeys);
153-
154-
if (created.length > 0) {
155-
emit ShardsRegistered(created);
141+
function setShards(TssKey[] calldata register, TssKey[] calldata revoke) external onlyOwner {
142+
ShardStore.MainStorage storage store = ShardStore.getMainStorage();
143+
for (uint256 i = 0; i < register.length; i++) {
144+
store.register(register[i]);
156145
}
157-
158-
if (revoked.length > 0) {
159-
emit ShardsUnregistered(revoked);
146+
for (uint256 i = 0; i < revoke.length; i++) {
147+
store.revoke(revoke[i]);
160148
}
161149
}
162150

163151
/**
164152
* @dev List all routes.
165153
*/
166154
function routes() external view returns (Route[] memory) {
167-
return RouteStore.getMainStorage().listRoutes();
155+
return RouteStore.getMainStorage().list();
168156
}
169157

170158
/**
171159
* @dev Create or update a single route
172160
*/
173161
function setRoute(Route calldata info) external onlyOwner {
174-
RouteStore.getMainStorage().createOrUpdateRoute(info);
162+
RouteStore.getMainStorage().insert(info);
175163
}
176164

177165
// IGateway implementation
@@ -344,12 +332,7 @@ contract Gateway is IGateway, UUPSUpgradeable, OwnableUpgradeable {
344332
return operationHash;
345333
}
346334

347-
bool isSuccess = ShardStore.getMainStorage().register(publicKey);
348-
if (isSuccess) {
349-
TssKey[] memory keys = new TssKey[](1);
350-
keys[0] = publicKey;
351-
emit ShardsRegistered(keys);
352-
}
335+
ShardStore.getMainStorage().register(publicKey);
353336
}
354337

355338
/**
@@ -367,12 +350,7 @@ contract Gateway is IGateway, UUPSUpgradeable, OwnableUpgradeable {
367350
return operationHash;
368351
}
369352

370-
bool isSuccess = ShardStore.getMainStorage().revoke(publicKey);
371-
if (isSuccess) {
372-
TssKey[] memory keys = new TssKey[](1);
373-
keys[0] = publicKey;
374-
emit ShardsUnregistered(keys);
375-
}
353+
ShardStore.getMainStorage().revoke(publicKey);
376354
}
377355

378356
/**
@@ -478,10 +456,14 @@ contract Gateway is IGateway, UUPSUpgradeable, OwnableUpgradeable {
478456
function execute(Signature calldata signature, Batch calldata batch) external {
479457
uint256 initialGas = gasleft();
480458

459+
// Load shard from storage
460+
ShardStore.ShardInfo storage signer = ShardStore.getMainStorage().get(signature.xCoord);
461+
481462
uint16 numSigningSessions = _signingSessions[batch.batchId];
482463
bool dry;
483464
if (numSigningSessions == 0) {
484-
numSigningSessions = batch.numSigningSessions;
465+
numSigningSessions = signer.numSessions;
466+
emit BatchExecuted(batch.batchId);
485467
} else if (numSigningSessions == 1) {
486468
revert("batch already executed");
487469
} else if (numSigningSessions > 1) {
@@ -492,17 +474,13 @@ contract Gateway is IGateway, UUPSUpgradeable, OwnableUpgradeable {
492474

493475
// Execute the commands and compute the operations root hash
494476
(, bytes32 rootHash) = _executeCommands(batch.ops, dry);
495-
emit BatchExecuted(batch.batchId);
496477

497478
// Compute the Batch signing hash
498-
rootHash = PrimitiveUtils.hash(batch.version, batch.batchId, batch.numSigningSessions, uint256(rootHash));
479+
rootHash = PrimitiveUtils.hash(batch.version, batch.batchId, uint256(rootHash));
499480
bytes32 signingHash = keccak256(
500481
abi.encodePacked("Analog GMP v2", networkId(), bytes32(uint256(uint160(address(this)))), rootHash)
501482
);
502483

503-
// Load shard from storage
504-
ShardStore.ShardInfo storage signer = ShardStore.getMainStorage().get(signature);
505-
506484
// Verify Signature
507485
require(
508486
Schnorr.verify(signer.yParity, signature.xCoord, uint256(signingHash), signature.e, signature.s),
@@ -512,7 +490,7 @@ contract Gateway is IGateway, UUPSUpgradeable, OwnableUpgradeable {
512490
// Refund the chronicle gas
513491
unchecked {
514492
// Extra gas overhead used to execute the refund logic + selector overhead
515-
uint256 gasUsed = 2890;
493+
uint256 gasUsed = 2968;
516494

517495
// Compute the gas used + base cost + proxy overhead
518496
gasUsed += GasUtils.txBaseGas();

src/Primitives.sol

Lines changed: 5 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
pragma solidity >=0.8.0;
55

6-
uint8 constant GMP_VERSION = 1;
6+
uint8 constant GMP_VERSION = 0;
77

88
/**
99
* @dev Maximum size of the GMP payload
@@ -16,8 +16,9 @@ uint256 constant MAX_PAYLOAD_SIZE = 0x6000;
1616
* @param xCoord affine x-coordinate
1717
*/
1818
struct TssKey {
19-
uint8 yParity;
2019
uint256 xCoord;
20+
uint8 yParity;
21+
uint16 numSessions;
2122
}
2223

2324
/**
@@ -88,8 +89,6 @@ struct Batch {
8889
uint8 version;
8990
/// @dev The batch identifier
9091
uint64 batchId;
91-
/// @dev The number of signing sessions
92-
uint16 numSigningSessions;
9392
/// @dev The ops to execute
9493
GatewayOp[] ops;
9594
}
@@ -109,8 +108,8 @@ struct Route {
109108
bytes32 gateway;
110109
uint256 relativeGasPriceNumerator;
111110
uint256 relativeGasPriceDenominator;
112-
uint256 gasCoef0;
113-
uint256 gasCoef1;
111+
uint64 gasCoef0;
112+
uint64 gasCoef1;
114113
}
115114

116115
/**
@@ -156,12 +155,6 @@ library PrimitiveUtils {
156155
*/
157156
uint256 internal constant ALLOCATED_MEMORY = 0x40;
158157

159-
/**
160-
* @dev Solidity's reserved location for the scratch memory.
161-
* Reference: https://docs.soliditylang.org/en/v0.8.28/internals/layout_in_memory.html
162-
*/
163-
uint256 internal constant SCRATCH_MEMORY = 0x60;
164-
165158
/**
166159
* @dev Read the current allocated size (a.k.a free memory pointer).
167160
*/
@@ -216,36 +209,6 @@ library PrimitiveUtils {
216209
}
217210
}
218211

219-
/**
220-
* @dev Hashes four 256-bit words without memory allocation, uses the memory between 0x00~0x80.
221-
*
222-
* The reserverd memory region `0x40~0x80` is restored to its previous state after execution.
223-
* See https://docs.soliditylang.org/en/v0.8.28/internals/layout_in_memory.html for more details.
224-
*/
225-
function hash(uint256 a, uint256 b, uint256 c, uint256 d) internal pure returns (bytes32 h) {
226-
assembly ("memory-safe") {
227-
mstore(0x00, a)
228-
mstore(0x20, b)
229-
230-
// Backup the free memory pointer
231-
let freeMemBackup := mload(ALLOCATED_MEMORY)
232-
mstore(ALLOCATED_MEMORY, c)
233-
{
234-
// Backup the scratch space 0x60
235-
let backup := mload(0x60)
236-
237-
// Compute the hash
238-
mstore(SCRATCH_MEMORY, d)
239-
h := keccak256(0x00, 0x80)
240-
241-
// Restore the scratch space 0x60
242-
mstore(SCRATCH_MEMORY, backup)
243-
}
244-
// Restore the free memory pointer
245-
mstore(ALLOCATED_MEMORY, freeMemBackup)
246-
}
247-
}
248-
249212
/**
250213
* @dev Returns the smallest of two numbers.
251214
*/

0 commit comments

Comments
 (0)