creatBlock(data){ var lastBlock=this.getLastBlock(); var newIndex=lastBlock.index+1; var newTime=newDate().getTime()/1000; var newPreHash=lastBlock.hash; var newhash=Block.caHash(newIndex,newPreHash,newTime,data.toString()); returnthis.blocks[this.blocks.length]= new Block.Block( newIndex, newPreHash, newTime, data, newhash ); }
creatBlock(data){ var lastBlock=this.getLastBlock(); var newIndex=lastBlock.index+1; var newTime=newDate().getTime()/1000; var newPreHash=lastBlock.hash; var creator= this.path;//简单的用路径来代替用户,creator是Block新增的一个属性 var newhash=Block.caHash(newIndex,newPreHash,newTime,data.toString(),creator); var newblock= new Block.Block( newIndex, newPreHash, newTime, data, newhash,creator ); fs.writeFile("./blcs/"+this.path+"/block"+this.head.nums+".json",JSON.stringify(newblock,"","\t"),function (err){ if (err!=null) console.log(err); });
//此处注释掉的为后续增加的代码,此时可暂不阅读 // this.head.nums++; // this.head.existsHash.push(newhash); // if (!this.head.creators.includes(newblock.creator)) // this.head.creators.push(newblock.creator);