Skip to content

Commit f9f3c46

Browse files
Seulgi Kimsgkim126
authored andcommitted
Add an option to decide whether Miner allows CreateShard transaction
1 parent 9e0dfe9 commit f9f3c46

File tree

6 files changed

+83
-2
lines changed

6 files changed

+83
-2
lines changed

codechain/codechain.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,10 @@ args:
168168
value_name: LIMIT
169169
help: Maximum amount of transactions in the queue (waiting to be included in next block).
170170
takes_value: true
171+
- allow-create-shard:
172+
long: allow-create-shard
173+
help: Make the miner allow CreateShard transactions
174+
takes_value: false
171175
- notify-work:
172176
long: notify-work
173177
value_name: URLS

codechain/config/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ impl Config {
7979
mem_size => Some(mem_size * 1024 * 1024),
8080
},
8181
mem_pool_fee_bump_shift: self.mining.mem_pool_fee_bump_shift.unwrap(),
82+
allow_create_shard: self.mining.allow_create_shard.unwrap_or(false),
8283
new_work_notify: self.mining.notify_work.clone().unwrap(),
8384
force_sealing: self.mining.force_sealing.unwrap(),
8485
reseal_on_own_transaction,
@@ -216,6 +217,7 @@ pub struct Mining {
216217
pub mem_pool_size: Option<usize>,
217218
pub mem_pool_mem_limit: Option<usize>,
218219
pub mem_pool_fee_bump_shift: Option<usize>,
220+
pub allow_create_shard: Option<bool>,
219221
pub notify_work: Option<Vec<String>>,
220222
pub force_sealing: Option<bool>,
221223
pub reseal_on_txs: Option<String>,
@@ -366,6 +368,9 @@ impl Mining {
366368
if other.mem_pool_mem_limit.is_some() {
367369
self.mem_pool_mem_limit = other.mem_pool_mem_limit;
368370
}
371+
if other.allow_create_shard.is_some() {
372+
self.allow_create_shard = other.allow_create_shard;
373+
}
369374
if other.notify_work.is_some() {
370375
self.notify_work = other.notify_work.clone();
371376
}
@@ -410,6 +415,9 @@ impl Mining {
410415
if let Some(mem_pool_size) = matches.value_of("mem-pool-size") {
411416
self.mem_pool_size = Some(mem_pool_size.parse().map_err(|_| "Invalid size")?);
412417
}
418+
if matches.is_present("allow-create-shard") {
419+
self.allow_create_shard = Some(true)
420+
}
413421
if let Some(notify_work) = matches.values_of("notify-work") {
414422
self.notify_work = Some(notify_work.map(|a| a.into()).collect());
415423
}

codechain/config/presets/config.dev.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ disable = false
99
mem_pool_mem_limit = 4 # MB
1010
mem_pool_size = 32768
1111
mem_pool_fee_bump_shift = 3 # 12.5%
12+
allow_create_shard = false
1213
notify_work = []
1314
force_sealing = false
1415
reseal_on_txs = "all"

codechain/config/presets/config.prod.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ disable = false
88
mem_pool_mem_limit = 512 # MB
99
mem_pool_size = 524288
1010
mem_pool_fee_bump_shift = 3 # 12.5%
11+
allow_create_shard = false
1112
notify_work = []
1213
force_sealing = true
1314
reseal_on_txs = "all"

core/src/miner/miner.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ pub struct MinerOptions {
7575
/// then `new_fee > old_fee + old_fee >> mem_pool_fee_bump_shift` should be satisfied to replace.
7676
/// Local transactions ignore this option.
7777
pub mem_pool_fee_bump_shift: usize,
78+
pub allow_create_shard: bool,
7879
/// How many historical work packages can we store before running out?
7980
pub work_queue_size: usize,
8081
}
@@ -92,6 +93,7 @@ impl Default for MinerOptions {
9293
mem_pool_size: 8192,
9394
mem_pool_memory_limit: Some(2 * 1024 * 1024),
9495
mem_pool_fee_bump_shift: 3,
96+
allow_create_shard: false,
9597
work_queue_size: 20,
9698
}
9799
}
@@ -270,6 +272,9 @@ impl Miner {
270272
cdebug!(MINER, "Rejected transaction {:?}: already in the blockchain", hash);
271273
return Err(HistoryError::TransactionAlreadyImported.into())
272274
}
275+
if !self.is_allowed_transaction(&tx.action) {
276+
cdebug!(MINER, "Rejected transaction {:?}: {:?} is not allowed transaction", hash, tx.action);
277+
}
273278
match self
274279
.engine
275280
.verify_transaction_basic(&tx, &best_block_header)
@@ -471,6 +476,12 @@ impl Miner {
471476
// The previous transaction has failed
472477
continue
473478
}
479+
if !self.is_allowed_transaction(&tx.action) {
480+
invald_tx_users.insert(signer_public);
481+
invalid_transactions.push(tx.hash());
482+
continue
483+
}
484+
474485
let hash = tx.hash();
475486
let start = Instant::now();
476487
// Check whether transaction type is allowed for sender
@@ -597,6 +608,18 @@ impl Miner {
597608
}
598609
})
599610
}
611+
612+
fn is_allowed_transaction(&self, action: &Action) -> bool {
613+
if let Action::CreateShard {
614+
..
615+
} = action
616+
{
617+
if !self.options.allow_create_shard {
618+
return false
619+
}
620+
}
621+
true
622+
}
600623
}
601624

602625
const SEALING_TIMEOUT_IN_BLOCKS: u64 = 5;

test/src/e2e/shard.test.ts

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
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-
import { expect } from "chai";
17+
import * as chai from "chai";
18+
import * as chaiAsPromised from "chai-as-promised";
19+
chai.use(chaiAsPromised);
1820
import "mocha";
1921
import {
2022
aliceAddress,
@@ -26,10 +28,12 @@ import {
2628
} from "../helper/constants";
2729
import CodeChain from "../helper/spawn";
2830

31+
const expect = chai.expect;
32+
2933
describe("CreateShard", function() {
3034
let node: CodeChain;
3135
before(async function() {
32-
node = new CodeChain();
36+
node = new CodeChain({ argv: ["--allow-create-shard"] });
3337
await node.start();
3438
});
3539

@@ -413,3 +417,43 @@ describe("CreateShard", function() {
413417
await node.clean();
414418
});
415419
});
420+
421+
describe("Cannot create shard without allow-create-shard flag", function() {
422+
let node: CodeChain;
423+
before(async function() {
424+
node = new CodeChain();
425+
await node.start();
426+
});
427+
428+
it("Create 1 shard", async function() {
429+
const seq: number = (await node.sdk.rpc.chain.getSeq(faucetAddress))!;
430+
431+
await node.sdk.rpc.chain.sendSignedTransaction(
432+
node.sdk.core
433+
.createPayTransaction({ recipient: aliceAddress, quantity: 1 })
434+
.sign({ secret: faucetSecret, seq, fee: 10 })
435+
);
436+
437+
const tx = node.sdk.core
438+
.createCreateShardTransaction({ users: [aliceAddress] })
439+
.sign({ secret: faucetSecret, seq: seq + 1, fee: 10 });
440+
expect(
441+
await node.sdk.rpc.sendRpcRequest("chain_getShardIdByHash", [
442+
tx.hash(),
443+
null
444+
])
445+
).be.null;
446+
expect(node.sdk.rpc.chain.sendSignedTransaction(tx)).be.rejected;
447+
expect(await node.sdk.rpc.chain.containTransaction(tx.hash())).be.false;
448+
expect(await node.sdk.rpc.chain.getTransaction(tx.hash())).be.null;
449+
const afterShardId = await node.sdk.rpc.sendRpcRequest(
450+
"chain_getShardIdByHash",
451+
[tx.hash(), null]
452+
);
453+
expect(afterShardId).be.null;
454+
});
455+
456+
after(async function() {
457+
await node.clean();
458+
});
459+
});

0 commit comments

Comments
 (0)