In the 'printchain' command, print transactions as well

This commit is contained in:
Ivan Kuznetsov 2017-09-08 10:06:19 +07:00
parent 2b0619e103
commit 6b400109e9
2 changed files with 26 additions and 0 deletions

3
cli.go
View File

@ -64,6 +64,9 @@ func (cli *CLI) printChain() {
fmt.Printf("Hash: %x\n", block.Hash)
pow := NewProofOfWork(block)
fmt.Printf("PoW: %s\n", strconv.FormatBool(pow.Validate()))
for _, tx := range block.Transactions {
fmt.Println(tx)
}
fmt.Println()
if len(block.PrevBlockHash) == 0 {

View File

@ -7,6 +7,7 @@ import (
"encoding/hex"
"fmt"
"log"
"strings"
)
const subsidy = 10
@ -23,6 +24,28 @@ func (tx Transaction) IsCoinbase() bool {
return len(tx.Vin) == 1 && len(tx.Vin[0].Txid) == 0 && tx.Vin[0].Vout == -1
}
// String returns a human-readable representation of a transaction
func (tx Transaction) String() string {
var lines []string
lines = append(lines, fmt.Sprintf("Transaction %x:", tx.ID))
for i, input := range tx.Vin {
lines = append(lines, fmt.Sprintf(" Input %d:", i))
lines = append(lines, fmt.Sprintf(" TXID: %x", input.Txid))
lines = append(lines, fmt.Sprintf(" Out: %d", input.Vout))
lines = append(lines, fmt.Sprintf(" Script: %s", input.ScriptSig))
}
for i, output := range tx.Vout {
lines = append(lines, fmt.Sprintf(" Output %d:", i))
lines = append(lines, fmt.Sprintf(" Value: %d", output.Value))
lines = append(lines, fmt.Sprintf(" Script: %s", output.ScriptPubKey))
}
return strings.Join(lines, "\n")
}
// SetID sets ID of a transaction
func (tx Transaction) SetID() {
var encoded bytes.Buffer