mirror of
https://gitee.com/BTAJL/repchain.git
synced 2024-12-05 05:08:29 +08:00
添加区块公共处理
This commit is contained in:
parent
775a7a3eaf
commit
823536b78e
@ -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
|
||||
}
|
||||
|
||||
|
195
src/main/scala/rep/network/consensus/util/BlockVerify.scala
Normal file
195
src/main/scala/rep/network/consensus/util/BlockVerify.scala
Normal file
@ -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
|
||||
}
|
||||
/****************************验证背书信息是否排序的操作结束**********************************************************/
|
||||
}
|
@ -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
|
||||
|
32
src/main/scala/rep/network/util/NodeHelp.scala
Normal file
32
src/main/scala/rep/network/util/NodeHelp.scala
Normal file
@ -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
|
||||
}
|
||||
|
||||
}
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user