add PrintHTML method to chain
This commit is contained in:
parent
cb4ae18f43
commit
9b2f73b11b
20
block.go
20
block.go
|
@ -3,7 +3,10 @@ package bc
|
|||
import (
|
||||
"bytes"
|
||||
"encoding/gob"
|
||||
"fmt"
|
||||
"log"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
@ -71,3 +74,20 @@ func DeserializeBlock(d []byte) *Block {
|
|||
|
||||
return &block
|
||||
}
|
||||
|
||||
func (b *Block) PrintHTML(detail bool) string {
|
||||
var lines []string
|
||||
lines = append(lines, fmt.Sprintf("<h2>Block <a href=\"/block/%x\">%x</a> </h2>", b.Hash, b.Hash))
|
||||
lines = append(lines, fmt.Sprintf("Height: %d</br>", b.Height))
|
||||
lines = append(lines, fmt.Sprintf("Prev. block: <a href=\"/block/%x\">%x</a></br>", b.PrevBlockHash, b.PrevBlockHash))
|
||||
lines = append(lines, fmt.Sprintf("Created at : %s</br>", time.Unix(b.Timestamp, 0)))
|
||||
pow := NewProofOfWork(b)
|
||||
lines = append(lines, fmt.Sprintf("PoW: %s</br></br>", strconv.FormatBool(pow.Validate())))
|
||||
if detail {
|
||||
for _, tx := range b.Transactions {
|
||||
lines = append(lines, tx.PrintHTML())
|
||||
}
|
||||
}
|
||||
lines = append(lines, fmt.Sprintf("</br></br>"))
|
||||
return strings.Join(lines, "\n")
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@ import (
|
|||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/boltdb/bolt"
|
||||
)
|
||||
|
@ -358,3 +359,20 @@ func dbExists(dbFile string) bool {
|
|||
|
||||
return true
|
||||
}
|
||||
|
||||
func (bc *Blockchain) PrintHTML() string {
|
||||
var lines []string
|
||||
bci := bc.Iterator()
|
||||
|
||||
for {
|
||||
block := bci.Next()
|
||||
|
||||
lines = append(lines, block.PrintHTML(false))
|
||||
|
||||
if len(block.PrevBlockHash) == 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
return strings.Join(lines, "\n")
|
||||
|
||||
}
|
||||
|
|
|
@ -249,3 +249,32 @@ func DeserializeTransaction(data []byte) Transaction {
|
|||
|
||||
return transaction
|
||||
}
|
||||
|
||||
func (tx Transaction) PrintHTML() string {
|
||||
var lines []string
|
||||
|
||||
lines = append(lines, fmt.Sprintf("<h4> Transaction %x:</h4>", tx.ID))
|
||||
|
||||
for i, input := range tx.Vin {
|
||||
|
||||
pubKeyHash := HashPubKey(input.PubKey)
|
||||
versionedPayload := append([]byte{version}, pubKeyHash...)
|
||||
fullPayload := append(versionedPayload, checksum(versionedPayload)...)
|
||||
|
||||
lines = append(lines, fmt.Sprintf("<div>Input %d:</div>", i))
|
||||
lines = append(lines, fmt.Sprintf("<div style=\"text-indent:2em;\">TXID: %x</div>", input.Txid))
|
||||
lines = append(lines, fmt.Sprintf("<div style=\"text-indent:2em;\">Out: %d</div>", input.Vout))
|
||||
lines = append(lines, fmt.Sprintf("<div style=\"text-indent:2em;\">Signature: %x</div>", input.Signature))
|
||||
lines = append(lines, fmt.Sprintf("<div style=\"text-indent:2em;\">PubKey: %x</div>", input.PubKey))
|
||||
lines = append(lines, fmt.Sprintf("<div style=\"text-indent:2em;\">Addr : %s</div>", Base58Encode(fullPayload)))
|
||||
}
|
||||
|
||||
for i, output := range tx.Vout {
|
||||
lines = append(lines, fmt.Sprintf("<div>Output %d:</div>", i))
|
||||
lines = append(lines, fmt.Sprintf("<div style=\"text-indent:2em;\">Value: %d</div>", output.Value))
|
||||
lines = append(lines, fmt.Sprintf("<div style=\"text-indent:2em;\">Script: %x</div>", output.PubKeyHash))
|
||||
lines = append(lines, fmt.Sprintf("<div style=\"text-indent:2em;\">Addr : %s</div>", output.Address))
|
||||
}
|
||||
|
||||
return strings.Join(lines, "")
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue