Skip to content

Commit

Permalink
add PrintHTML method to chain
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardWeiYang committed Oct 22, 2017
1 parent cb4ae18 commit 9b2f73b
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
20 changes: 20 additions & 0 deletions block.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package bc
import (
"bytes"
"encoding/gob"
"fmt"
"log"
"strconv"
"strings"
"time"
)

Expand Down Expand Up @@ -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")
}
18 changes: 18 additions & 0 deletions blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"log"
"os"
"strings"

"github.com/boltdb/bolt"
)
Expand Down Expand Up @@ -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")

}
29 changes: 29 additions & 0 deletions transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "")
}

0 comments on commit 9b2f73b

Please sign in to comment.