-
Notifications
You must be signed in to change notification settings - Fork 0
/
solicitar_Ledger.go
113 lines (96 loc) · 2.32 KB
/
solicitar_Ledger.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"bufio"
"encoding/json"
"fmt"
"math/rand"
"net"
"strings"
)
var HOSTS = []string{"10.11.97.199:8000",
"10.11.98.201:8000",
"10.11.98.213:8000",
"10.11.98.211:8000",
"10.11.98.210:8000",
"10.11.98.215:8000",
"10.11.98.214:8000",
"10.11.98.226:8000",
"10.11.97.225:8000",
"10.11.97.218:8000",
"10.11.97.219:8000",
"10.11.98.207:8000",
"10.11.98.205:8000",
"10.11.98.229:8000"}
type Block struct {
Index int `json:"Index"`
Timestamp string `json:"Timestamp"`
Hash string `json:"Hash"`
PrevHash string `json:"PrevHash"`
Payload string `json:"Payload"`
}
type Ledger struct {
Nombre string
}
var blockchain []Block
func Encoder() {
block := Block{
Index: 10,
Timestamp: "11-05-1992",
Hash: "312",
PrevHash: "123",
Payload: "1231",
}
b, _ := json.Marshal(block)
s := string(b)
fmt.Println(s)
}
func Decoder(_msg string) {
bytes := []byte(_msg)
var block_ Block
json.Unmarshal(bytes, &block_)
blockchain = append(blockchain, block_)
for l := range blockchain {
fmt.Printf("-------------------\n")
fmt.Printf("Data File Number %d\n", l)
fmt.Printf("-------------------\n")
fmt.Printf("Index=%d\n", blockchain[l].Index)
fmt.Printf("Timestamp=%s\n", blockchain[l].Timestamp)
fmt.Printf("Hash=%s\n", blockchain[l].Hash)
fmt.Printf("Prevhash=%s\n", blockchain[l].PrevHash)
fmt.Printf("Payload=%s\n", blockchain[l].Payload)
fmt.Printf("-------------------\n")
fmt.Println(" ")
}
}
const (
PROTOCOL = "tcp"
LOCALHOST = "10.11.98.229:8000"
)
func send(n string) {
host := HOSTS[rand.Intn(len(HOSTS))]
resp := fmt.Sprintf("%s", n)
con, _ := net.Dial(PROTOCOL, host)
fmt.Println(n)
fmt.Println(host)
defer con.Close()
fmt.Fprintln(con, resp)
}
func handle(con net.Conn) {
defer con.Close()
r := bufio.NewReader(con)
msg, _ := r.ReadString('\n')
msg = strings.TrimSpace(msg)
send(msg)
}
func start() {
var num string
fmt.Print("Mensaje: ")
fmt.Scanf("%s\n", &num)
send(num)
}
func main() {
Decoder(`{"Index":0,"Timestamp":"11-05-1992","Hash":"312","Prevhash":"123","Payload":"1231"}`)
Decoder(`{"Index":1,"Timestamp":"11-05-1992","Hash":"312","Prevhash":"123","Payload":"1231"}`)
Decoder(`{"Index":2,"Timestamp":"11-05-1992","Hash":"312","Prevhash":"123","Payload":"1231"}`)
fmt.Println("Elements in blockchain: ", len(blockchain))
}