transactions

This commit is contained in:
SowinskiBraeden committed 2022-12-01 16:52:37 -08:00
1 parent a7f50318ed
commit 9e6d5d4def
5 files changed
+356 -79

No files matched your search

+21 -8
View File
@@ -2,19 +2,32 @@ package blockchain
import (
"bytes"
"crypto/sha256"
"encoding/gob"
"log"
)
type Block struct {
Hash []byte
Data []byte
PrevHash []byte
Nonce int
Hash []byte
Transactions []*Transaction
PrevHash []byte
Nonce int
}
func CreateBlock(data string, prevHash []byte) *Block {
block := &Block{[]byte{}, []byte(data), prevHash, 0}
func (b *Block) HashTransactions() []byte {
var txHashes [][]byte
var txHash [32]byte
for _, tx := range b.Transactions {
txHashes = append(txHashes, tx.ID)
}
txHash = sha256.Sum256(bytes.Join(txHashes, []byte{}))
return txHash[:]
}
func CreateBlock(txs []*Transaction, prevHash []byte) *Block {
block := &Block{[]byte{}, txs, prevHash, 0}
pow := NewProof(block)
nonce, hash := pow.Run()
@@ -24,8 +37,8 @@ func CreateBlock(data string, prevHash []byte) *Block {
return block
}
func Genesis() *Block {
return CreateBlock("Genesis", []byte{})
func Genesis(coinbase *Transaction) *Block {
return CreateBlock([]*Transaction{coinbase}, []byte{})
}
func (b *Block) Serialize() []byte {