transactions
This commit is contained in:
5 files changed
+356
-79
No files matched your search
@@ -10,14 +10,14 @@ import (
|
||||
"github.com/SowinskiBraeden/golang-blockchain/blockchain"
|
||||
)
|
||||
|
||||
type CommandLine struct {
|
||||
blockchain *blockchain.BlockChain
|
||||
}
|
||||
type CommandLine struct{}
|
||||
|
||||
func (cli *CommandLine) printUsage() {
|
||||
fmt.Println("Usage:")
|
||||
fmt.Println(" add -block BLOCK_DATA - add a block to the chain")
|
||||
fmt.Println(" print - Prints the blocks in the chain")
|
||||
fmt.Println(" getbalance -address ADDRESS - get the balance for an address")
|
||||
fmt.Println(" createblockchain -address ADDRESS create a blockchain")
|
||||
fmt.Println(" printchain - Prints the blocks in the chain")
|
||||
fmt.Println(" send -from FROM -to TO -amount AMOUNT - Send amount to an address")
|
||||
}
|
||||
|
||||
func (cli *CommandLine) validateArgs() {
|
||||
@@ -27,54 +27,15 @@ func (cli *CommandLine) validateArgs() {
|
||||
}
|
||||
}
|
||||
|
||||
func (cli *CommandLine) addBlock(data string) {
|
||||
cli.blockchain.AddBlock(data)
|
||||
fmt.Println("Added Block!")
|
||||
}
|
||||
|
||||
func (cli *CommandLine) run() {
|
||||
cli.validateArgs()
|
||||
|
||||
addBlockCmd := flag.NewFlagSet("add", flag.ExitOnError)
|
||||
printChainCmd := flag.NewFlagSet("print", flag.ExitOnError)
|
||||
addBlockData := addBlockCmd.String("block", "", "Block data")
|
||||
|
||||
switch os.Args[1] {
|
||||
case "add":
|
||||
err := addBlockCmd.Parse(os.Args[2:])
|
||||
blockchain.Handle(err)
|
||||
|
||||
case "print":
|
||||
err := printChainCmd.Parse(os.Args[2:])
|
||||
blockchain.Handle(err)
|
||||
|
||||
default:
|
||||
cli.printUsage()
|
||||
runtime.Goexit()
|
||||
}
|
||||
|
||||
if addBlockCmd.Parsed() {
|
||||
if *addBlockData == "" {
|
||||
addBlockCmd.Usage()
|
||||
runtime.Goexit()
|
||||
}
|
||||
|
||||
cli.addBlock(*addBlockData)
|
||||
}
|
||||
|
||||
if printChainCmd.Parsed() {
|
||||
cli.printChain()
|
||||
}
|
||||
}
|
||||
|
||||
func (cli *CommandLine) printChain() {
|
||||
iter := cli.blockchain.Iterator()
|
||||
chain := blockchain.ContinueBlockChain("")
|
||||
defer chain.Database.Close()
|
||||
iter := chain.Iterator()
|
||||
|
||||
for {
|
||||
block := iter.Next()
|
||||
|
||||
fmt.Printf("Prev. Hash: %x\n", block.PrevHash)
|
||||
fmt.Printf("Data: %s\n", block.Data)
|
||||
fmt.Printf("Hash: %x\n", block.Hash)
|
||||
pow := blockchain.NewProof(block)
|
||||
fmt.Printf("PoW: %s\n", strconv.FormatBool(pow.Validate()))
|
||||
@@ -86,11 +47,102 @@ func (cli *CommandLine) printChain() {
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
defer os.Exit(0)
|
||||
chain := blockchain.InitBlockChain()
|
||||
func (cli *CommandLine) createBlockChain(address string) {
|
||||
chain := blockchain.InitBlockChain(address)
|
||||
chain.Database.Close()
|
||||
fmt.Println("Finished!")
|
||||
}
|
||||
|
||||
func (cli *CommandLine) getBalance(address string) {
|
||||
chain := blockchain.ContinueBlockChain(address)
|
||||
defer chain.Database.Close()
|
||||
|
||||
cli := CommandLine{chain}
|
||||
balance := 0
|
||||
UTXOs := chain.FindUTXO(address)
|
||||
|
||||
for _, out := range UTXOs {
|
||||
balance += out.Value
|
||||
}
|
||||
|
||||
fmt.Printf("Balance of %s: %d\n", address, balance)
|
||||
}
|
||||
|
||||
func (cli *CommandLine) send(from, to string, amount int) {
|
||||
chain := blockchain.ContinueBlockChain(from)
|
||||
defer chain.Database.Close()
|
||||
|
||||
tx := blockchain.NewTransaction(from, to, amount, chain)
|
||||
chain.AddBlock([]*blockchain.Transaction{tx})
|
||||
fmt.Println("Success!")
|
||||
}
|
||||
|
||||
func (cli *CommandLine) run() {
|
||||
cli.validateArgs()
|
||||
|
||||
getBalanceCmd := flag.NewFlagSet("getbalance", flag.ExitOnError)
|
||||
createBlockchainCmd := flag.NewFlagSet("createblockchain", flag.ExitOnError)
|
||||
sendCmd := flag.NewFlagSet("send", flag.ExitOnError)
|
||||
printChainCmd := flag.NewFlagSet("printchain", flag.ExitOnError)
|
||||
|
||||
getBalanceAddress := getBalanceCmd.String("address", "", "The address to get balance of")
|
||||
createBlockchainAddress := createBlockchainCmd.String("address", "", "The address to create a blockchain for")
|
||||
sendFrom := sendCmd.String("from", "", "Source wallet address")
|
||||
sendTo := sendCmd.String("to", "", "Destination wallet address")
|
||||
sendAmount := sendCmd.Int("amount", 0, "Amount to send")
|
||||
|
||||
switch os.Args[1] {
|
||||
case "getbalance":
|
||||
err := getBalanceCmd.Parse(os.Args[2:])
|
||||
blockchain.Handle(err)
|
||||
|
||||
case "createblockchain":
|
||||
err := createBlockchainCmd.Parse(os.Args[2:])
|
||||
blockchain.Handle(err)
|
||||
|
||||
case "printchain":
|
||||
err := printChainCmd.Parse(os.Args[2:])
|
||||
blockchain.Handle(err)
|
||||
|
||||
case "send":
|
||||
err := sendCmd.Parse(os.Args[4:])
|
||||
blockchain.Handle(err)
|
||||
|
||||
default:
|
||||
cli.printUsage()
|
||||
runtime.Goexit()
|
||||
}
|
||||
|
||||
if getBalanceCmd.Parsed() {
|
||||
if *getBalanceAddress == "" {
|
||||
getBalanceCmd.Usage()
|
||||
runtime.Goexit()
|
||||
}
|
||||
cli.getBalance(*getBalanceAddress)
|
||||
}
|
||||
|
||||
if createBlockchainCmd.Parsed() {
|
||||
if *createBlockchainAddress == "" {
|
||||
createBlockchainCmd.Usage()
|
||||
runtime.Goexit()
|
||||
}
|
||||
cli.createBlockChain(*createBlockchainAddress)
|
||||
}
|
||||
|
||||
if printChainCmd.Parsed() {
|
||||
cli.printChain()
|
||||
}
|
||||
|
||||
if sendCmd.Parsed() {
|
||||
if *sendFrom == "" || *sendTo == "" || *sendAmount == 0 {
|
||||
sendCmd.Usage()
|
||||
runtime.Goexit()
|
||||
}
|
||||
cli.send(*sendFrom, *sendTo, *sendAmount)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
defer os.Exit(0)
|
||||
cli := CommandLine{}
|
||||
cli.run()
|
||||
}
|
||||
Reference in new issue
Block a user