-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock.ts
45 lines (45 loc) · 1.69 KB
/
block.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { GENESIS, mine_rate } from "./config";
import hexToBinary from "hex-to-binary";
import { cryptoHash } from "./hash";
export class Block{
timestamp:string;
prevHash:string;
hash:string;
data:string[];
nonce:number;
difficulty:number;
constructor(timestamp:string,prevHash:string,hash:string,data:string[],nonce:number,difficulty:number){
this.timestamp=timestamp;
this.hash=hash;
this.prevHash=prevHash;
this.data=data;
this.nonce=nonce;
this.difficulty=difficulty;
}
static genesis(){
return new this(GENESIS.timestamp,GENESIS.prevHash,GENESIS.hash,GENESIS.data,GENESIS.nonce,GENESIS.difficulty)
}
static mineBlock(prevBlock:Block,data:string[]){
let hash,timestamp;
let {difficulty}=prevBlock
const prevHash:string=prevBlock.hash.toString();
let nonce=0;
do{
nonce++;
timestamp=Date.now().toString();
difficulty=Block.adjustDifficulty(prevBlock,timestamp)
hash=cryptoHash(timestamp,prevHash,...data,nonce,difficulty)
}while(hexToBinary(hash).substring(0,difficulty)!=='0'.repeat(difficulty))
return new this(timestamp,prevHash,hash,data,nonce,difficulty)
}
static adjustDifficulty(originalBlock:Block,timestamp:string):number{
const {difficulty}=originalBlock;
const difference=Number(timestamp)- Number(originalBlock.timestamp);
if(difficulty<1) return difficulty+1;
if(difference>mine_rate) return difficulty-1;
return difficulty+1;
}
}
// const genesisBlock:Block=Block.genesis();
// const result:Block=Block.mineBlock(genesisBlock,['a','c'])
// console.log(result)