Skip to content

Commit 2ce612d

Browse files
Juhyung Parksgkim126
authored andcommitted
Make the MemPool's Timelock check condition consistent with the VM
Currently, the VM compares the Timelock value with the best block's information. And the MemPool compares the Timelock value with the next block's data or current time. Even though a transaction has the same Timelock condition in the transaction's input field and its script, the MemPool's test is passed and the script's test is failed. After this commit, the two tests return the same result.
1 parent 9af1075 commit 2ce612d

File tree

3 files changed

+30
-28
lines changed

3 files changed

+30
-28
lines changed

core/src/miner/mem_pool.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
// You should have received a copy of the GNU Affero General Public License
1515
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1616

17-
use std::cmp;
1817
use std::collections::{BTreeSet, HashMap, HashSet};
1918
use std::ops::Range;
2019
use std::sync::Arc;
@@ -26,7 +25,6 @@ use kvdb::{DBTransaction, KeyValueDB};
2625
use primitives::H256;
2726
use rlp;
2827
use table::Table;
29-
use time::get_time;
3028

3129
use super::backup;
3230
use super::mem_pool_types::{
@@ -963,12 +961,12 @@ impl MemPool {
963961
/// Checks the given timelock with the current time/timestamp.
964962
fn should_wait_timelock(timelock: &TxTimelock, best_block_number: BlockNumber, best_block_timestamp: u64) -> bool {
965963
if let Some(block_number) = timelock.block {
966-
if block_number > best_block_number + 1 {
964+
if block_number > best_block_number {
967965
return true
968966
}
969967
}
970968
if let Some(timestamp) = timelock.timestamp {
971-
if timestamp > cmp::max(get_time().sec as u64, best_block_timestamp) {
969+
if timestamp > best_block_timestamp {
972970
return true
973971
}
974972
}

test/src/e2e.long/timelock.test.ts

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -68,26 +68,24 @@ describe("Timelock", function() {
6868
}
6969

7070
describe("Transaction should go into the current queue", async function() {
71-
[1, 2].forEach(function(target) {
72-
it(`Minted at block 1, send transfer with Timelock::Block(${target})`, async function() {
73-
const tracker = await sendTxWithTimelock({
74-
type: "block",
75-
value: target
76-
});
77-
await checkTx(tracker, true);
71+
it(`Minted at block 1, send transfer with Timelock::Block(0)`, async function() {
72+
const tracker = await sendTxWithTimelock({
73+
type: "block",
74+
value: 0
7875
});
76+
await checkTx(tracker, true);
7977
});
8078

81-
[0, 1].forEach(function(target) {
82-
it(`Minted at block 1, send transfer with Timelock::BlockAge(${target})`, async function() {
83-
const tracker = await sendTxWithTimelock({
84-
type: "blockAge",
85-
value: target
86-
});
87-
await checkTx(tracker, true);
79+
80+
it(`Minted at block 1, send transfer with Timelock::BlockAge(0)`, async function() { 
81+
const tracker = await sendTxWithTimelock({
82+
type: "blockAge",
83+
value: 0
8884
});
85+
await checkTx(tracker, true);
8986
});
9087

88+
9189
it("send transfer with Timelock::Time(0)", async function() {
9290
const tracker = await sendTxWithTimelock({
9391
type: "time",
@@ -168,8 +166,10 @@ describe("Timelock", function() {
168166

169167
await node.sdk.rpc.devel.startSealing();
170168
await node.sdk.rpc.devel.startSealing();
169+
await checkTx(tracker, false);
170+
await node.sdk.rpc.devel.startSealing();
171171

172-
expect(await node.getBestBlockNumber()).to.equal(3);
172+
expect(await node.getBestBlockNumber()).to.equal(4);
173173
await checkTx(tracker, true);
174174
});
175175

@@ -180,14 +180,14 @@ describe("Timelock", function() {
180180
value: 3
181181
});
182182

183-
for (let i = 1; i <= 3; i++) {
183+
for (let i = 1; i <= 4; i++) {
184184
expect(await node.getBestBlockNumber()).to.equal(i);
185185
await checkTx(tracker, false);
186186

187187
await node.sdk.rpc.devel.startSealing();
188188
}
189189

190-
expect(await node.getBestBlockNumber()).to.equal(4);
190+
expect(await node.getBestBlockNumber()).to.equal(5);
191191
await checkTx(tracker, true);
192192
});
193193
});
@@ -292,12 +292,13 @@ describe("Timelock", function() {
292292

293293
await node.sdk.rpc.devel.startSealing();
294294
await node.sdk.rpc.devel.startSealing();
295-
expect(await node.getBestBlockNumber()).to.equal(4);
295+
await node.sdk.rpc.devel.startSealing();
296+
expect(await node.getBestBlockNumber()).to.equal(5);
296297
await checkTx(tx.tracker(), false);
297298

298299
await node.sdk.rpc.devel.startSealing();
299300
await node.sdk.rpc.devel.startSealing();
300-
expect(await node.getBestBlockNumber()).to.equal(6);
301+
expect(await node.getBestBlockNumber()).to.equal(7);
301302
await checkTx(tx.tracker(), true);
302303
}).timeout(10_000);
303304

@@ -329,12 +330,13 @@ describe("Timelock", function() {
329330

330331
await node.sdk.rpc.devel.startSealing();
331332
await node.sdk.rpc.devel.startSealing();
332-
expect(await node.getBestBlockNumber()).to.equal(4);
333+
await node.sdk.rpc.devel.startSealing();
334+
expect(await node.getBestBlockNumber()).to.equal(5);
333335
await checkTx(tx.tracker(), false);
334336

335337
await node.sdk.rpc.devel.startSealing();
336338
await node.sdk.rpc.devel.startSealing();
337-
expect(await node.getBestBlockNumber()).to.equal(6);
339+
expect(await node.getBestBlockNumber()).to.equal(7);
338340
await checkTx(tx.tracker(), true);
339341
}).timeout(10_000);
340342

@@ -366,7 +368,8 @@ describe("Timelock", function() {
366368

367369
await node.sdk.rpc.devel.startSealing();
368370
await node.sdk.rpc.devel.startSealing();
369-
expect(await node.getBestBlockNumber()).to.equal(4);
371+
await node.sdk.rpc.devel.startSealing();
372+
expect(await node.getBestBlockNumber()).to.equal(5);
370373
await checkTx(tx.tracker(), true);
371374
}).timeout(10_000);
372375

test/src/e2e/mempool.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,13 @@ describe("Timelock", function() {
143143
await checkTx(txhash2, false);
144144

145145
await node.sdk.rpc.devel.startSealing();
146-
expect(await node.getBestBlockNumber()).to.equal(2);
146+
await node.sdk.rpc.devel.startSealing();
147+
expect(await node.getBestBlockNumber()).to.equal(3);
147148
await checkTx(txhash1, false);
148149
await checkTx(txhash2, false);
149150

150151
await node.sdk.rpc.devel.startSealing();
151-
expect(await node.getBestBlockNumber()).to.equal(3);
152+
expect(await node.getBestBlockNumber()).to.equal(4);
152153
await checkTx(txhash1, false);
153154
await checkTx(txhash2, true);
154155
});

0 commit comments

Comments
 (0)