-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
72 lines (61 loc) · 2.04 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"../../pkg/dledger"
)
const (
defaultPort = "8080" // Use this default port if it isn't specified via command line arguments.
)
func main() {
port := defaultPort
if len(os.Args) > 1 {
port = os.Args[1]
}
distributedLedger := dledger.NewDLedger(port, "peers.txt")
distributedLedger.WaitForPeers()
fmt.Printf("I am online at %s and all peers are available.\n", distributedLedger.MyAddress)
distributedLedger.Start()
// Routine for user transaction inputs
var input int
var amount float64
scanner := bufio.NewScanner(os.Stdin)
fmt.Println()
for {
// note: peerAddressMap contains me, but peerAddresses does not
fmt.Printf("\nDear %s, please choose a client for your new transaction.\n", distributedLedger.PeerAddressMap[distributedLedger.MyAddress])
for i, addr := range distributedLedger.PeerAddresses {
fmt.Printf("\t%d) %s\n", i+1, distributedLedger.PeerAddressMap[addr])
}
fmt.Printf("Enter a number: > ")
errForInput := true
for errForInput {
var err error
errForInput = false
scanner.Scan()
input, err = strconv.Atoi(scanner.Text())
if err != nil || input <= 0 || input > len(distributedLedger.PeerAddresses) {
errForInput = true
fmt.Printf("\nBad input, try again: > ")
}
}
chosenAddr := distributedLedger.PeerAddresses[input-1]
fmt.Printf("\nDear %s, please enter how much credits would you like transfer to %s:\n\t> ",
distributedLedger.PeerAddressMap[distributedLedger.MyAddress], distributedLedger.PeerAddressMap[chosenAddr])
errForInput = true
for errForInput {
var err error
errForInput = false
scanner.Scan()
amount, err = strconv.ParseFloat(scanner.Text(), 64)
if err != nil || amount <= 0 {
errForInput = true
fmt.Printf("Bad input, try again: > ")
}
}
distributedLedger.PerformTransaction(chosenAddr, amount)
fmt.Printf("\nSuccessfully added transaction:\n\t'%s sends %f to %s'\n", distributedLedger.PeerAddressMap[distributedLedger.MyAddress], amount, distributedLedger.PeerAddressMap[chosenAddr])
}
}