@@ -35,7 +35,7 @@ library Forest {
35
35
* @param from The spender of the transaction.
36
36
*/
37
37
event TransactionCreated (bytes32 indexed root , bytes32 id , address indexed from );
38
-
38
+
39
39
/**
40
40
* @notice Event emitted when a transaction is spent.
41
41
* @param id The identifier of the transaction.
@@ -58,6 +58,11 @@ library Forest {
58
58
*/
59
59
error TransactionZeroValue ();
60
60
61
+ /**
62
+ * @notice Error thrown when trying to merge over 255 transactions.
63
+ */
64
+ error TransactionMergeSizeExceed ();
65
+
61
66
/**
62
67
* @notice Error thrown when the spending value exceeds the transaction value.
63
68
* @param value The value of the transaction.
@@ -141,7 +146,27 @@ library Forest {
141
146
/**
142
147
* @notice not suitable for use in production.
143
148
*/
144
- function mergeTx (DAG storage self , bytes32 [] memory ids ) internal {
145
- // @TODO
149
+ function mergeTx (DAG storage self , bytes32 [] memory ids ) internal {
150
+ uint256 length = ids.length ;
151
+ if (length >= type (uint8 ).max) revert TransactionMergeSizeExceed ();
152
+ Tx memory ptr = getTx (self, ids[0 ]);
153
+ if (msg .sender != ptr.owner) revert TransactionUnauthorized ();
154
+ Tx memory txn;
155
+ unchecked {
156
+ for (uint8 index = 1 ; index < ids.length ; index++ ) {
157
+ txn = getTx (self, ids[index]);
158
+ if (ptr.root == txn.root && ptr.owner == txn.owner) {
159
+ self.txs[ids[index]].value = 0 ;
160
+ ptr.value += txn.value;
161
+ if (ptr.level < txn.level) {
162
+ ptr.level = txn.level;
163
+ }
164
+ }
165
+ }
166
+ createTx (self, ptr, ptr.owner);
167
+ if (ptr.level > self.hierarchy[ids[0 ]]) {
168
+ self.hierarchy[ids[0 ]] = ptr.level;
169
+ }
170
+ }
146
171
}
147
172
}
0 commit comments