Skip to content

Commit 5308345

Browse files
committed
refactor: some optimize
Signed-off-by: MASDXI <sirawitt42@gmail.com>
1 parent f3b1119 commit 5308345

File tree

3 files changed

+10
-13
lines changed

3 files changed

+10
-13
lines changed

contracts/abstracts/ForestToken.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
1313
*/
1414
abstract contract ForestToken is ERC20, IForest {
1515
using Forest for Forest.DAG;
16-
16+
1717
Forest.DAG private _dag;
1818

1919
/**

contracts/abstracts/UTXOToken.sol

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,11 @@ abstract contract UTXOToken is ERC20, IUTXO {
5454
revert UTXOTransferOverTransactionValue(txvalue, value);
5555
}
5656
_UTXO.spendTx(UTXO.TxInput(tokenId, signature), from);
57-
uint256 change = txvalue - value;
58-
if (change != 0) {
57+
txvalue = txvalue - value;
58+
if (txvalue != 0) {
5959
_UTXO.createTx(
6060
UTXO.TxOutput(value, to),
6161
tokenId,
62-
UTXO.calcTxHash(from, _UTXO.getTxCount(from)),
6362
from,
6463
extraData
6564
);
@@ -77,7 +76,6 @@ abstract contract UTXOToken is ERC20, IUTXO {
7776
_UTXO.createTx(
7877
UTXO.TxOutput(value, account),
7978
bytes32(0),
80-
UTXO.calcTxHash(address(0), _UTXO.getTxCount(address(0))),
8179
address(0),
8280
extraData
8381
);
@@ -95,11 +93,11 @@ abstract contract UTXOToken is ERC20, IUTXO {
9593
if (value == _UTXO.getTxValue(tokenId)) {
9694
_UTXO.consumeTx(tokenId);
9795
} else {
96+
// @TODO handling not allow value greater than tx value.
9897
_UTXO.consumeTx(tokenId);
9998
_UTXO.createTx(
10099
UTXO.TxOutput(value, account),
101100
tokenId,
102-
UTXO.calcTxHash(account, _UTXO.getTxCount(account)),
103101
account,
104102
extraData
105103
);

contracts/libraries/UTXO.sol

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,25 +119,24 @@ library UnspentTransactionOutput {
119119
* @param self The UTXO storage.
120120
* @param txOutput The transaction output details.
121121
* @param input The input identifier of the transaction.
122-
* @param id The identifier of the transaction.
123122
* @param creator The creator of the transaction.
124123
*/
125124
function createTx(
126125
UTXO storage self,
127126
TxOutput memory txOutput,
128127
bytes32 input,
129-
bytes32 id,
130128
address creator,
131129
bytes32 extraData
132130
) internal {
133131
if (txOutput.value == 0) {
134132
revert TransactionZeroValue();
135133
}
136-
if (_transactionExist(self, id)) {
137-
revert TransactionExist();
138-
}
134+
uint256 nonce = self.nonces[creator];
135+
bytes32 id = calcTxHash(creator, self.nonces[creator]);
139136
self.txs[id] = Tx(input, txOutput.value, txOutput.account, false, extraData);
140-
self.nonces[creator]++;
137+
unchecked {
138+
self.nonces[creator] = nonce++;
139+
}
141140

142141
emit TransactionCreated(id, creator, txOutput.account);
143142
}
@@ -167,7 +166,7 @@ library UnspentTransactionOutput {
167166
}
168167

169168
/**
170-
* @notice Consumes (marks as spent) a transaction in the UTXO.
169+
* @notice Consumes (marks as spent without require signature) a transaction in the UTXO.
171170
* @param self The UTXO storage.
172171
* @param id The identifier of the transaction to consume.
173172
*/

0 commit comments

Comments
 (0)