From db03e279a6a30641b4c4058a6303d0e476d9f627 Mon Sep 17 00:00:00 2001
From: cgeek <cem.moreau@gmail.com>
Date: Wed, 6 May 2020 21:38:35 +0200
Subject: [PATCH] [feat] generator: add `--nocheck-issuer` option

---
 app/lib/blockchain/DuniterBlockchain.ts              |  5 +++--
 app/lib/computation/BlockchainContext.ts             |  7 +++++--
 app/modules/crawler/lib/sync/v2/GlobalIndexStream.ts |  1 +
 app/modules/prover/index.ts                          | 10 +++++++++-
 app/service/BlockchainService.ts                     |  4 ++--
 5 files changed, 20 insertions(+), 7 deletions(-)

diff --git a/app/lib/blockchain/DuniterBlockchain.ts b/app/lib/blockchain/DuniterBlockchain.ts
index faeec03d7..05dc9136b 100644
--- a/app/lib/blockchain/DuniterBlockchain.ts
+++ b/app/lib/blockchain/DuniterBlockchain.ts
@@ -45,6 +45,7 @@ export class DuniterBlockchain {
   static async checkBlock(
     block: BlockDTO,
     withPoWAndSignature: boolean,
+    ignoreIssuer: boolean,
     conf: ConfDTO,
     dal: FileDAL
   ) {
@@ -78,7 +79,7 @@ export class DuniterBlockchain {
     if (Indexer.rulePreviousIssuer(block, HEAD) === false)
       throw Error("rulePreviousIssuer");
     // BR_G101
-    if (Indexer.ruleIssuerIsMember(HEAD) === false)
+    if (!ignoreIssuer && Indexer.ruleIssuerIsMember(HEAD) === false)
       throw Error("ruleIssuerIsMember");
     // BR_G54
     if (Indexer.ruleIssuersCount(block, HEAD) === false)
@@ -215,7 +216,7 @@ export class DuniterBlockchain {
     };
 
     const isMember = await dal.isMember(block.issuer);
-    if (!isMember) {
+    if (!ignoreIssuer && !isMember) {
       if (block.number == 0) {
         if (!matchesList(new RegExp("^" + block.issuer + ":"), block.joiners)) {
           throw Error("Block not signed by the root members");
diff --git a/app/lib/computation/BlockchainContext.ts b/app/lib/computation/BlockchainContext.ts
index 9ca3e66e5..a0589908f 100644
--- a/app/lib/computation/BlockchainContext.ts
+++ b/app/lib/computation/BlockchainContext.ts
@@ -123,11 +123,13 @@ export class BlockchainContext {
 
   async checkBlock(
     block: BlockDTO,
-    withPoWAndSignature: boolean
+    withPoWAndSignature: boolean,
+    ignoreIssuer: boolean
   ): Promise<any> {
     return DuniterBlockchain.checkBlock(
       block,
       withPoWAndSignature,
+      ignoreIssuer,
       this.conf,
       this.dal
     );
@@ -205,7 +207,8 @@ export class BlockchainContext {
   async checkAndAddBlock(block: BlockDTO, trim = true) {
     const { index, HEAD } = await this.checkBlock(
       block,
-      constants.WITH_SIGNATURES_AND_POW
+      constants.WITH_SIGNATURES_AND_POW,
+      false
     );
     return await this.addBlock(block, index, HEAD, trim);
   }
diff --git a/app/modules/crawler/lib/sync/v2/GlobalIndexStream.ts b/app/modules/crawler/lib/sync/v2/GlobalIndexStream.ts
index c28345513..ceb33b67f 100644
--- a/app/modules/crawler/lib/sync/v2/GlobalIndexStream.ts
+++ b/app/modules/crawler/lib/sync/v2/GlobalIndexStream.ts
@@ -591,6 +591,7 @@ export class GlobalIndexStream extends Duplex {
     const { index, HEAD } = await DuniterBlockchain.checkBlock(
       dto,
       constants.WITH_SIGNATURES_AND_POW,
+      false,
       this.conf,
       this.dal
     );
diff --git a/app/modules/prover/index.ts b/app/modules/prover/index.ts
index 4ca5f1a49..8bdca8b55 100644
--- a/app/modules/prover/index.ts
+++ b/app/modules/prover/index.ts
@@ -122,6 +122,10 @@ export const ProverDependency = {
         desc:
           "With gen-* commands: the generated block is submitted to this node only.",
       },
+      {
+        value: "--nocheck-issuer",
+        desc: "With gen-* commands: ignore issuer rule.",
+      },
       {
         value: "--submit-host <host>",
         desc:
@@ -276,7 +280,11 @@ function generateAndSend(
                 const parsed = parsers.parseBlock.syncWrite(
                   block.getRawSigned()
                 );
-                await server.BlockchainService.checkBlock(parsed, false);
+                await server.BlockchainService.checkBlock(
+                  parsed,
+                  false,
+                  program.nocheckIssuer
+                );
                 logger.info("Acceptable block");
                 next();
               } catch (e) {
diff --git a/app/service/BlockchainService.ts b/app/service/BlockchainService.ts
index 75e3172cd..63c4d5c78 100644
--- a/app/service/BlockchainService.ts
+++ b/app/service/BlockchainService.ts
@@ -165,9 +165,9 @@ export class BlockchainService extends FIFOService {
     return bb;
   }
 
-  checkBlock(block: any, withPoWAndSignature = true) {
+  checkBlock(block: any, withPoWAndSignature = true, ignoreIssuer = false) {
     const dto = BlockDTO.fromJSONObject(block);
-    return this.mainContext.checkBlock(dto, withPoWAndSignature);
+    return this.mainContext.checkBlock(dto, withPoWAndSignature, ignoreIssuer);
   }
 
   /**
-- 
GitLab