Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: show timestamp of the block #3

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
c0accf3
cli: show timestamp of the block
RichardWeiYang Oct 11, 2017
5e43c77
merkle tree: fix the implementation and add a test case
RichardWeiYang Oct 16, 2017
cee6700
base58: fix Base58Decode on calculating the zeroBytes
RichardWeiYang Oct 16, 2017
f9935da
base58_test: add test case for Base58
RichardWeiYang Oct 16, 2017
782db48
cli: add three cli command for exploring
RichardWeiYang Oct 16, 2017
42b9dbb
version: display the addree when a new node is connected
RichardWeiYang Oct 18, 2017
8ea4bc8
Use View instead of Update in NewBlockchain()
RichardWeiYang Oct 18, 2017
29099f0
check wallet before sending
RichardWeiYang Oct 18, 2017
009fda2
display the number of tx mined
RichardWeiYang Oct 18, 2017
2974ee8
cli: print PubKeyHash in getAddress
RichardWeiYang Oct 18, 2017
5f4f2f5
cli: add command getPubKeyHash
RichardWeiYang Oct 18, 2017
36cfffc
cli: add getBlock command
RichardWeiYang Oct 19, 2017
01d18ce
add Address in TXOut
RichardWeiYang Oct 19, 2017
5c88b24
display the addresse in TXIn and optmize getAddress command
RichardWeiYang Oct 19, 2017
c410eb2
display the private key
RichardWeiYang Oct 20, 2017
9f6ac35
update gitignore
RichardWeiYang Oct 21, 2017
64cf474
fix private key display
RichardWeiYang Oct 21, 2017
04318d7
cli: generatePrivKey
RichardWeiYang Oct 21, 2017
b415f08
make it a package bc
RichardWeiYang Oct 22, 2017
c72ff7f
Change README for bc package
RichardWeiYang Oct 22, 2017
cb4ae18
export DB in blockchain
RichardWeiYang Oct 22, 2017
9b2f73b
add PrintHTML method to chain
RichardWeiYang Oct 22, 2017
56c9e3f
add GetBalance to chain
RichardWeiYang Oct 22, 2017
db3fe41
more detail explanation
RichardWeiYang Oct 31, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to do this via 'html/template', and this should be a separate package, because it's not directly related to the logic of the blockchain.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, I just learn go, so...

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, "")
}