modify logic for block.go

pull/2/head
Minh Doan 7 years ago
parent 800cdb575d
commit 4cd5370f9c
  1. 7
      .vscode/settings.json
  2. 35
      block.go
  3. 1
      transaction.go

@ -1,10 +1,5 @@
{
"workbench.colorTheme": "Solarized Light",
"npm.enableScriptExplorer": true,
"window.zoomLevel": 3,
"editor.tabSize": 4,
"editor.insertSpaces": true,
"editor.detectIndentation": false,
"editor.tabCompletion": true,
"window.zoomLevel": 1,
}

@ -26,18 +26,37 @@ func (b *Block) HashTransactions() []byte {
return txHash[:]
}
// NewBlock creates and returns Block.
func NewBlock(utxoPool []UTXOPool, prevBlockHash []byte) *Block {
// Serialize serializes the block
func (b *Block) Serialize() []byte {
var result bytes.Buffer
encoder := gob.NewEncoder(&result)
err := encoder.Encode(b)
if err != nil {
log.Panic(err)
}
return result.Bytes()
}
block := &Block{time.Now().Unix(), utxoPool, prevBlockHash, []byte{}}
block.SetHash()
// NewBlock creates and returns Block.
func NewBlock(transactions []*Transaction, prevBlockHash []byte) *Block {
block := &Block{time.Now().Unix(), transactions, prevBlockHash, []byte{}, 0}
block.Hash = block.HashTransactions()
return block
}
// NewGenesisBlock creates and returns genesis Block.
func NewGenesisBlock() *Block {
genesisUTXOPool := UTXOPool{}
genesisUTXOPool.utxos["genesis"] = TOTAL_COINS
func NewGenesisBlock(coinbase *Transaction) *Block {
return NewBlock([]*Transaction{coinbase}, []byte{})
}
return NewBlock(genesisUTXOPool, []byte{})
// DeserializeBlock deserializes a block
func DeserializeBlock(d []byte) *Block {
var block Block
decoder := gob.NewDecoder(bytes.NewReader(d))
err := decoder.Decode(&block)
if err != nil {
log.Panic(err)
}
return &block
}

@ -38,3 +38,4 @@ func (tx *Transaction) SetId() {
hash = sha256.Sum256(encoded.Bytes())
tx.ID = hash[:]
}

Loading…
Cancel
Save