Created
January 12, 2024 08:06
-
-
Save Oxygenesis/121a7bf5967f580e086b7d19dc8fa955 to your computer and use it in GitHub Desktop.
go simple blockchain logics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"crypto/sha256" | |
"fmt" | |
"strconv" | |
"time" | |
) | |
type Block struct { | |
Index int | |
Timestamp string | |
Transactions []Transaction | |
Data string | |
PreviousHash string | |
Hash string | |
} | |
func calculateHash(block Block) string { | |
record := strconv.Itoa(block.Index) + block.Timestamp + block.Data + block.PreviousHash | |
h := sha256.New() | |
h.Write([]byte(record)) | |
hashed := h.Sum(nil) | |
return fmt.Sprintf("%x", hashed) | |
} | |
func createBlock(prevBlock Block, data string) Block { | |
block := Block{ | |
Index: prevBlock.Index + 1, Timestamp: time.Now().String(), Data: data, PreviousHash: prevBlock.Hash, | |
} | |
block.Hash = calculateHash(block) | |
return block | |
} | |
func createGenesisBlock() Block { | |
return Block{Index: 0, Timestamp: time.Now().String(), Data: "Genesis Block", PreviousHash: "", Hash: ""} | |
} | |
func main() { | |
// Create the blockchain and add the genesis block | |
var blockchain []Block | |
genesisBlock := createGenesisBlock() | |
blockchain = append(blockchain, genesisBlock) | |
previousBlock := genesisBlock | |
// Add blocks to the blockchain | |
numBlocks := 10 | |
for i := 1; i <= numBlocks; i++ { | |
newData := fmt.Sprintf("Block #%d data", i) | |
newBlock := createBlock(previousBlock, newData) | |
blockchain = append(blockchain, newBlock) | |
previousBlock = newBlock | |
fmt.Printf("Block #%d has been added to the blockchain!\n", newBlock.Index) | |
fmt.Printf("Hash: %s\n\n", newBlock.Hash) | |
} | |
// Create the blockchain and add the genesis block | |
//var blockchain []Block | |
//genesisBlock := createGenesisBlock() | |
//blockchain = append(blockchain, genesisBlock) | |
//previousBlock := genesisBlock | |
// Add blocks with transactions to the blockchain | |
transactions1 := []Transaction{ | |
{Sender: "Alice", Recipient: "Bob", Amount: 50}, | |
{Sender: "Bob", Recipient: "Charlie", Amount: 25}, | |
} | |
block1 := createBlockLedger(previousBlock, transactions1) | |
blockchain = append(blockchain, block1) | |
previousBlock = block1 | |
transactions2 := []Transaction{ | |
{Sender: "Charlie", Recipient: "Alice", Amount: 10}, | |
{Sender: "Alice", Recipient: "Bob", Amount: 5}, | |
} | |
block2 := createBlockLedger(previousBlock, transactions2) | |
blockchain = append(blockchain, block2) | |
// Print the blockchain ledger | |
for _, block := range blockchain { | |
fmt.Printf("Block #%d \n", block.Index) | |
for _, tx := range block.Transactions { | |
fmt.Printf("Sender: %s, Recipient: %s, Amount: %f\n", tx.Sender, tx.Recipient, tx.Amount) | |
} | |
fmt.Printf("Hash: %s\n\n", block.Hash) | |
} | |
} | |
type Transaction struct { | |
Sender string | |
Recipient string | |
Amount float64 | |
} | |
func createBlockLedger(prevBlock Block, transactions []Transaction) Block { | |
block := Block{ | |
Index: prevBlock.Index + 1, | |
Timestamp: time.Now().String(), | |
Transactions: transactions, | |
PreviousHash: prevBlock.Hash, | |
} | |
block.Hash = calculateHash(block) | |
return block | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment