Browse Source

prettified some stuff in chain

main
gryffyn 5 months ago
parent
commit
ac54fc684e
Signed by: gryffyn GPG Key ID: 6948DD6514D02BEF
  1. 21
      chain/chain.go

21
chain/chain.go

@ -17,26 +17,25 @@ func calcBlockHash(block utils.Block) string {
return utils.Hash(record)
}
func calcHash(index int, timestamp time.Time, data, oldhash string) string {
record := string(rune(index)) + timestamp.String() + data + oldhash
func calcHash(index int, timestamp time.Time, data utils.PcData, oldhash string) string {
js, _ := json.Marshal(data)
record := string(rune(index)) + timestamp.String() + string(js) + oldhash
return utils.Hash(record)
}
func calcPOWHash(index int, timestamp time.Time, data, oldhash string) (string, int) {
var nonce int = 0
hash := calcHash(index, timestamp, string(rune(nonce))+data, oldhash)
func calcPOWHash(index int, timestamp time.Time, data utils.PcData, oldhash string) string {
hash := calcHash(index, timestamp, data, oldhash)
for !strings.HasPrefix(hash, _DIFFICULTY) {
nonce++
hash = calcHash(index, timestamp, string(rune(nonce))+data, oldhash)
data.Nonce++
hash = calcHash(index, timestamp, data, oldhash)
}
return hash, nonce
return hash
}
func GenerateBlock(oldBlock utils.Block, data utils.PcData) (utils.Block, error) {
t := time.Now()
js, _ := json.Marshal(data)
var newBlock utils.Block
hash := calcHash(oldBlock.Index+1, t, string(js), oldBlock.Hash)
hash := calcHash(oldBlock.Index+1, t, data, oldBlock.Hash)
newBlock = utils.Block{PrevHash: oldBlock.Hash, Index: oldBlock.Index + 1, Timestamp: t, Data: data, Hash: hash}
return newBlock, nil
}
@ -59,7 +58,7 @@ func GenesisBlock() (utils.Block, error) {
Nonce: 0,
}
var newBlock utils.Block
hash := calcHash(0, t, "Genesis", "0000000000000000000000000000000000000000000000000000000000000000")
hash := calcHash(0, t, gendata, "0000000000000000000000000000000000000000000000000000000000000000")
newBlock = utils.Block{PrevHash: "0000000000000000000000000000000000000000000000000000000000000000", Timestamp: t, Data: gendata, Hash: hash}
return newBlock, nil
}

Loading…
Cancel
Save