From 823536b78ec4e5c404a0e304d33171fc358f63bc Mon Sep 17 00:00:00 2001 From: wuwei1972 Date: Tue, 2 Apr 2019 20:18:21 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=8C=BA=E5=9D=97=E5=85=AC?= =?UTF-8?q?=E5=85=B1=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../rep/network/cache/TransactionPool.scala | 10 +- .../network/consensus/util/BlockVerify.scala | 195 ++++++++++++++++++ .../rep/network/persistence/Storager.scala | 2 +- .../scala/rep/network/util/NodeHelp.scala | 32 +++ .../scala/rep/storage/ImpDataAccess.scala | 6 +- 5 files changed, 236 insertions(+), 9 deletions(-) create mode 100644 src/main/scala/rep/network/consensus/util/BlockVerify.scala create mode 100644 src/main/scala/rep/network/util/NodeHelp.scala diff --git a/src/main/scala/rep/network/cache/TransactionPool.scala b/src/main/scala/rep/network/cache/TransactionPool.scala index e7dcf76c..71b4d85b 100644 --- a/src/main/scala/rep/network/cache/TransactionPool.scala +++ b/src/main/scala/rep/network/cache/TransactionPool.scala @@ -141,12 +141,12 @@ class TransactionPool(moduleName: String) extends ModuleBase(moduleName) { case false => //ignore case true => //签名验证成功 - if(pe.getTransLength() < 100) - logMsg(LogType.INFO,s"<<<<<<<<<<<<<>>>>>>>>>transaction=${pe.getTransLength()}" ) - if (SystemProfile.getMaxCacheTransNum == 0 || pe.getTransLength() < SystemProfile.getMaxCacheTransNum) { - pe.putTran(t) + if(pe.getTransPoolMgr.getTransLength() < 100) + logMsg(LogType.INFO,s"<<<<<<<<<<<<<>>>>>>>>>transaction=${pe.getTransPoolMgr.getTransLength()}" ) + if (SystemProfile.getMaxCacheTransNum == 0 || pe.getTransPoolMgr.getTransLength() < SystemProfile.getMaxCacheTransNum) { + pe.getTransPoolMgr.putTran(t) //广播接收交易事件 - if (pe.getTransLength() <= SystemProfile.getMinBlockTransNum) + if (pe.getTransPoolMgr.getTransLength() <= SystemProfile.getMinBlockTransNum) getActorRef(pe.getSysTag, GlobalUtils.ActorType.VOTER_MODULE) ! VoteRecover } diff --git a/src/main/scala/rep/network/consensus/util/BlockVerify.scala b/src/main/scala/rep/network/consensus/util/BlockVerify.scala new file mode 100644 index 00000000..6bb2c4f5 --- /dev/null +++ b/src/main/scala/rep/network/consensus/util/BlockVerify.scala @@ -0,0 +1,195 @@ +package rep.network.consensus.util + +import rep.protos.peer._ +import rep.crypto.cert.SignTool +import scala.util.control.Breaks._ +import rep.crypto.Sha256 +import com.google.protobuf.ByteString +import rep.utils.IdTool + +object BlockVerify { +/****************************交易验证签名相关的操作开始**********************************************************/ + def VerifyOneSignOfTrans(t: Transaction, sysName: String): (Boolean, String) = { + var result = false + var resultMsg = "" + + try { + val sig = t.signature + val tOutSig = t.clearSignature + result = SignTool.verify(sig.get.signature.toByteArray(), tOutSig.toByteArray, sig.get.getCertId, sysName) + } catch { + case e: RuntimeException => + result = false + resultMsg = s"The transaction(${t.id}) is not trusted${e.getMessage}" + } + (result, resultMsg) + } + + def VerifySignOfMultiTrans(trans: Seq[Transaction], sysName: String): (Boolean, String) = { + var result = true + var resultMsg = "" + try { + breakable( + trans.foreach(f => { + val r = VerifyOneSignOfTrans(f, sysName) + if (!r._1) { + result = false + resultMsg = r._2 + break + } + })) + } catch { + case e: RuntimeException => + result = false + resultMsg = s"The VerifySignOfMultiTrans error,info=${e.getMessage}" + } + (result, resultMsg) + } + + def VerifyAllTransSignOfBlock(block: Block, sysName: String): (Boolean, String) = { + try { + VerifySignOfMultiTrans(block.transactions.toArray[Transaction], sysName) + } catch { + case e: RuntimeException => (false, s"Transaction's sign Error,info=${e.getMessage}") + } + } +/****************************交易验证签名相关的操作结束**********************************************************/ + +/****************************区块验证签名相关的操作开始**********************************************************/ + def VerifyOneEndorseOfBlock(endor: Signature, NonSignDataOfBlock: Array[Byte], sysName: String): (Boolean, String) = { + var result = false + var resultMsg = "" + try { + val certid = endor.getCertId + result = SignTool.verify(endor.signature.toByteArray, NonSignDataOfBlock, certid, sysName) + } catch { + case e: RuntimeException => + result = false + resultMsg = s"The endorsement is not trusted${e.getMessage}" + } + (result, resultMsg) + } + + def VerifyMutliEndorseOfBlock(endorses: Seq[Signature], NonSignDataOfBlock: Array[Byte], sysName: String): (Boolean, String) = { + var result = true + var resultMsg = "" + try { + breakable( + endorses.foreach(f => { + val r = VerifyOneEndorseOfBlock(f, NonSignDataOfBlock, sysName) + if (!r._1) { + result = false + resultMsg = r._2 + break + } + })) + } catch { + case e: RuntimeException => + result = false + resultMsg = s"The VerifySignOfMultiTrans error,info=${e.getMessage}" + } + (result, resultMsg) + } + + def VerifyAllEndorseOfBlock(block: Block, sysName: String): (Boolean, String) = { + try { + val endors = block.endorsements + val blkOutEndorse = block.clearEndorsements + VerifyMutliEndorseOfBlock(endors, blkOutEndorse.toByteArray, sysName) + } catch { + case e: RuntimeException => (false, s"Endorsement's sign Error,info=${e.getMessage}") + } + } +/****************************区块验证签名相关的操作结束**********************************************************/ + +/****************************验证区块hash相关的操作开始**********************************************************/ + def VerifyHashOfBlock(block: Block): Boolean = { + var result = false + try { + val oldhash = block.hashOfBlock.toStringUtf8() + val blkOutEndorse = block.clearEndorsements + val blkOutBlockHash = blkOutEndorse.withHashOfBlock(ByteString.EMPTY) + val hash = Sha256.hashstr(blkOutBlockHash.toByteArray) + if (oldhash.equals(hash)) { + result = true + } + } catch { + case e: RuntimeException => + result = false + } + result + } +/****************************验证区块hash相关的操作结束**********************************************************/ + +/****************************检查背书是否完成开始**********************************************************/ + def EndorsementIsFinishOfBlock(block: Block, NodeNumber: Int): Boolean = { + var result = false + try { + val endorseNumber = block.endorsements.size + if ((endorseNumber - 1) >= Math.floor(((NodeNumber) * 1.0) / 2)) { + result = true + } + } catch { + case e: RuntimeException => + result = false + } + result + } +/****************************检查背书是否完成结束**********************************************************/ + +/****************************验证背书信息是否排序的操作开始**********************************************************/ + def VerifyEndorserSorted(srclist: Array[Signature]): Int = { + var b: Int = 0 + if (srclist == null || srclist.length < 2) { + b + } else { + if (IdTool.getSigner4String(srclist(0).getCertId) < IdTool.getSigner4String(srclist(1).getCertId)) { //升序 + b = 1 + } else { //降序 + b = -1 + } + + breakable( + for (i <- 1 to srclist.length - 1) { + if (b == 1 && IdTool.getSigner4String(srclist(i).getCertId) < IdTool.getSigner4String(srclist(i - 1).getCertId)) { + b = 0 + break + } + + if (b == -1 && IdTool.getSigner4String(srclist(i).getCertId) > IdTool.getSigner4String(srclist(i - 1).getCertId)) { + b = 0 + break + } + }) + } + b + } + + def VerifyTransactionSorted(srclist: Array[Transaction]): Int = { + var b: Int = 0 + if (srclist == null || srclist.length < 2) { + b + } else { + if (srclist(0).id < srclist(1).id) { //升序 + b = 1 + } else { //降序 + b = -1 + } + + breakable( + for (i <- 1 to srclist.length - 1) { + if (b == 1 && srclist(i).id < srclist(i - 1).id) { + b = 0 + break + } + + if (b == -1 && srclist(i).id > srclist(i - 1).id) { + b = 0 + break + } + }) + } + b + } +/****************************验证背书信息是否排序的操作结束**********************************************************/ +} \ No newline at end of file diff --git a/src/main/scala/rep/network/persistence/Storager.scala b/src/main/scala/rep/network/persistence/Storager.scala index 3c4b546b..33caf1a2 100644 --- a/src/main/scala/rep/network/persistence/Storager.scala +++ b/src/main/scala/rep/network/persistence/Storager.scala @@ -10,7 +10,7 @@ import rep.protos.peer._ import rep.storage.ImpDataAccess import rep.network.consensus.vote.CRFDVoterModule.NextVote import scala.collection.mutable -import rep.utils.GlobalUtils.{ActorType, BlockEvent, EventType,BlockChainStatus} +import rep.utils.GlobalUtils.{ActorType, BlockEvent, EventType} import rep.network.sync.SyncModule.{ChainDataReqSingleBlk} import scala.collection.immutable import rep.network.cluster.ClusterHelper diff --git a/src/main/scala/rep/network/util/NodeHelp.scala b/src/main/scala/rep/network/util/NodeHelp.scala new file mode 100644 index 00000000..645697cf --- /dev/null +++ b/src/main/scala/rep/network/util/NodeHelp.scala @@ -0,0 +1,32 @@ +package rep.network.util + +import akka.actor.{ ActorRef, Props } + +object NodeHelp { + def isSameNodeForRef(srcRef: ActorRef, destRef: ActorRef): Boolean = { + if (srcRef == null) false + if (destRef == null) false + val srcStr = getNodePath(srcRef) + val destStr = getNodePath(destRef) + isSameNodeForString(srcStr, destStr) + } + + def isSameNodeForString(srcStr: String, destStr: String): Boolean = { + var b: Boolean = false + if (srcStr.indexOf("/user") > 0) { + val addr = srcStr.substring(0, srcStr.indexOf("/user")) + b = destStr.indexOf(addr) != -1 + } + b + } + + def getNodePath(actref: ActorRef):String={ + if(actref == null) "" + akka.serialization.Serialization.serializedActorPath(actref) + } + + def ConsensusConditionChecked(inputNumber: Int, nodeNumber: Int): Boolean = { + if ((inputNumber - 1) >= Math.floor(((nodeNumber)*1.0) / 2)) true else false + } + +} \ No newline at end of file diff --git a/src/main/scala/rep/storage/ImpDataAccess.scala b/src/main/scala/rep/storage/ImpDataAccess.scala index b6fb2e71..9dc28c71 100644 --- a/src/main/scala/rep/storage/ImpDataAccess.scala +++ b/src/main/scala/rep/storage/ImpDataAccess.scala @@ -361,9 +361,9 @@ class ImpDataAccess private(SystemName:String) extends IDataAccess(SystemName){ } }else{ - val prve = newblock.previousBlockHash.toString("UTF-8") - val cur = Sha256.hashstr(lastblock.toByteArray) - if(prve.equals(cur)){ + val prve = newblock.previousBlockHash.toStringUtf8() + val cur = lastblock.hashOfBlock.toStringUtf8()//Sha256.hashstr(lastblock.toByteArray) + if(prve.equals(cur) && (lastblock.height+1) == newblock.height){ b = true }else{ b = false