Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // make sure block is valid by checking index, and comparing the hash of the previous block
- func isBlockValid(newBlock, oldBlock Block) bool {
- if oldBlock.Index+1 != newBlock.Index {
- return false
- }
- if oldBlock.Hash != newBlock.PrevHash {
- return false
- }
- if calculateHash(newBlock) != newBlock.Hash {
- return false
- }
- return true
- }
- // SHA256 hashing
- func calculateHash(block Block) string {
- record := strconv.Itoa(block.Index) + block.Timestamp + strconv.Itoa(block.BPM) + block.PrevHash
- h := sha256.New()
- h.Write([]byte(record))
- hashed := h.Sum(nil)
- return hex.EncodeToString(hashed)
- }
- // create a new block using previous block's hash
- func generateBlock(oldBlock Block, BPM int) Block {
- var newBlock Block
- t := time.Now()
- newBlock.Index = oldBlock.Index + 1
- newBlock.Timestamp = t.String()
- newBlock.BPM = BPM
- newBlock.PrevHash = oldBlock.Hash
- newBlock.Hash = calculateHash(newBlock)
- return newBlock
- }
Add Comment
Please, Sign In to add comment