C++实现区块链的源码
看了上面的算法,相信大家基本可以猜到,相对于比特币的限量的性质,对于本算法来说,难解程度的根本原因即为向量环路的迭代次数。迭代次数越多,则算法越难解,从而导致解题需要花费更多的时候,再基于这点,在数学上,当解题次数足够大时,效率会无限小,从而导致了解题时间无限长最后导致加密货币的发放无限小。
创世区块创建(部分大妈在前面有实现,而区块这一部分将会详细解答)
voidMake_First_Block() { Getpublickey(); blo.data=circle; blo.pre_hash=0; blo.this_hash=(blo.pre_hash+public_Key)*(a+b); Block.push_back(blo); }
由于在区块链中,本区快的数字签名是基于上一区块的数字签名和区块本身的DATA决定,所以,在这里我们采用了上一区块的数字签名加上难解的PublicKey乘上长轴和短轴的和实现本区块的数字签名的算法。
添加区块(当当前区块被算出时,添加新区块,检查签名正确性。)
voidAppend_Block() { pre_blo=blo; boolflag=true; autotemp=public_Key; circle=circle+1; Getpublickey(); blo.data=circle; blo.pre_hash=blo.this_hash; blo.this_hash=(blo.pre_hash+public_Key)*(a+b); for(list::iteratoritor=Block.begin();itor!=Block.end();itor++) { if((*itor).this_hash!=(*itor).pre_hash+temp*(a+b)) { flag=false; break; } } if(flag){Block.push_back(blo);}; }
这个迭代其实可以不用的,因为我在外部还定义了一个block类型的全局变量Pre_block和blo。Pre_block存储了上一个区块的信息。而本区块的信息则存储在Blo中。只有当用户解出当前区块后,才可以得到新区块。而data参数,为了方便仅存储了当前区块所在的位置。
区块的计算(用类实现)
classGet_Block:Create_Block{ public: intdiffcult; intnumber=1; Get_Block():Create_Block(“OK”){ } voidcalc() { doublestart=clock(); while(true){ for(unsignedlonglongz=1;zGetpublickey(); blockbloc; bloc.data=circle; bloc.pre_hash=pre_blo.this_hash; bloc.this_hash=(blo.pre_hash+person->public_Key)*(i+j); if(blo.data==bloc.data&&blo.pre_hash==bloc.pre_hash&&blo.this_hash==bloc.this_hash) { doubleend=clock(); cout<<“历时”< Append_Block(); start=clock(); } delete[]person; } } } } } };
完整代码:
#include #include#include #include #include #include #include #include usingnamespacestd; structMoving_Point{ unsignedlonglongx; unsignedlonglongy; }; intcircle=1; classMartix{ public: staticconstintcircle_s=1;//假定向量环路为1; staticconstintKEY=Martix::circle_s*8; private: unsignedlonglongmartix_4_2[Martix::KEY/2][2];//存储向量矩阵 unsignedlonglongmartix_8_8[Martix::KEY][Martix::KEY];//存储由向量矩阵得到的转置矩阵 unsignedlonglongmartix_complete[KEY*2];//存储操作完成后的矩阵(一维) public: Martix(stringa){}; Martix(inta,intb,intcircle) { intkey=8; intcir=circle; while(cir–) { martix_4_2[key/2-4][0]=(-1)*b;martix_4_2[key/2-4][1]=(-1)*a; martix_4_2[key/2-3][0]=b;martix_4_2[key/2-3][1]=(-1)*a; martix_4_2[key/2-2][0]=b;martix_4_2[key/2-2][1]=a; martix_4_2[key/2-1][0]=(-1)*b;martix_4_2[key/2-1][1]=a; key+=8; } } voidChange_New_Martix(){ for(inti=0;i<2;i++) { for(intj=0;j<2;j++) { martix_8_8[i][j]=0; } } for(intj=2;j a=in_a; this->b=in_b; p.x=0; p.y=0; public_Key=Getpublickey(); } unsignedlonglongGetpublickey() { Get_Public_Key(); returnpublic_Key; } Moving_PointGetPoint() { Get_Point(); returnp; } public: voidPrintPrivateKey(){ cout<<“#############私钥:#############”< a<<“\t\t”; cout<<“短轴:”<<2this->b< Change_New_Martix(); this->Save_Martix(); this->public_Key=this->GetPublicKey(); } voidGet_Moving_Point_P()//得到一个随机的在椭圆上的点的坐标 { for(inti=0;i a;i++) { for(intj=0;j b;j++) { p.x=i; p.y=j; } } } boolIs_Moving_Point(){ if(pow(b,2)*pow(p.y,2)+pow(a,2)*pow(p.x,2)==pow(a,2)*pow(b,2)&&p.y<=a&&p.x<=b) returntrue; else returnfalse; } }; structblock{ unsignedlonglongthis_hash; unsignedlonglongpre_hash; unsignedlonglongdata; }; blockblo; blockpre_blo={0,0,0}; classCreate_Block:publicCryptography{ public: listBlock; public: Create_Block(stringa):Cryptography(“OK”){}; Create_Block(intx=rand()*2,inty=rand(),intdiffcult=1):Cryptography(x,y,diffcult){ } voidMake_First_Block() { Getpublickey(); blo.data=circle; blo.pre_hash=0; blo.this_hash=(blo.pre_hash+public_Key)*(a+b); Block.push_back(blo); } voidAppend_Block() { pre_blo=blo; boolflag=true; autotemp=public_Key; circle=circle+1; Getpublickey(); blo.data=circle; blo.pre_hash=blo.this_hash; blo.this_hash=(blo.pre_hash+public_Key)*(a+b); for(list::iteratoritor=Block.begin();itor!=Block.end();itor++) { if((*itor).this_hash!=(*itor).pre_hash+temp*(a+b)) { flag=false; break; } } if(flag){Block.push_back(blo);}; } }; classGet_Block:Create_Block{ public: intdiffcult; intnumber=1; Get_Block():Create_Block(“OK”){ } voidcalc() { doublestart=clock(); while(true){ for(unsignedlonglongz=1;z Getpublickey(); blockbloc; bloc.data=circle; bloc.pre_hash=pre_blo.this_hash; bloc.this_hash=(blo.pre_hash+person->public_Key)*(i+j); if(blo.data==bloc.data&&blo.pre_hash==bloc.pre_hash&&blo.this_hash==bloc.this_hash) { doubleend=clock(); cout<<“历时”< Append_Block(); start=clock(); } delete[]person; } } } } } }; intmain() { Create_Block*one=newCreate_Block(); one->Make_First_Block(); Get_Block*two=newGet_Block(); two->calc(); return0; }
不得不说第一个区块的挖掘永远是最快的。第二个区块确实要等好久才可以得出。以上即为C/C++实现区块链的全部源码。仅用于学习交流,不得用于商业用途,转载必究。
作者:程序小黑
来源:CSDN
原文:https://blog.csdn.net/qq_27180763/article/details/82588305
版权声明:本文为博主原创文章,转载请附上博文链接!
到此这篇关于C++实现区块链的源码的文章就介绍到这了,更多相关c++区块链内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!