123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- // Copyright © 2021 Ettore Di Giacinto <[email protected]>
- //
- // This program is free software; you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation; either version 2 of the License, or
- // (at your option) any later version.
- //
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- //
- // You should have received a copy of the GNU General Public License along
- // with this program; if not, see <http://www.gnu.org/licenses/>.
- package blockchain
- import (
- "crypto/sha256"
- "encoding/hex"
- "fmt"
- "time"
- )
- type DataString string
- // Block represents each 'item' in the blockchain
- type Block struct {
- Index int
- Timestamp string
- Storage map[string]map[string]Data
- Hash string
- PrevHash string
- }
- // Blockchain is a series of validated Blocks
- type Blockchain []Block
- // make sure block is valid by checking index, and comparing the hash of the previous block
- func (newBlock Block) IsValid(oldBlock Block) bool {
- if oldBlock.Index+1 != newBlock.Index {
- return false
- }
- if oldBlock.Hash != newBlock.PrevHash {
- return false
- }
- if newBlock.Checksum() != newBlock.Hash {
- return false
- }
- return true
- }
- // Checksum does SHA256 hashing of the block
- func (b Block) Checksum() string {
- record := fmt.Sprint(b.Index, b.Timestamp, b.Storage, b.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 (oldBlock Block) NewBlock(s map[string]map[string]Data) Block {
- var newBlock Block
- t := time.Now().UTC()
- newBlock.Index = oldBlock.Index + 1
- newBlock.Timestamp = t.String()
- newBlock.Storage = s
- newBlock.PrevHash = oldBlock.Hash
- newBlock.Hash = newBlock.Checksum()
- return newBlock
- }
|