transactions
This commit is contained in:
5 files changed
+356
-79
No files matched your search
+21
-8
@@ -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 {
|
||||
|
||||
Reference in new issue
Block a user