The core protocol of WoopChain
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
woop/node.go

75 lines
1.2 KiB

7 years ago
package main
import (
7 years ago
"fmt"
"time"
"math/rand"
7 years ago
)
7 years ago
7 years ago
var node_ips = []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
type Node struct {
7 years ago
ip int
7 years ago
leader bool
}
7 years ago
type Nodes struct {
7 years ago
Nodes []*Node
}
type txn struct {
tx string
}
func randomInt(min, max int) int {
return min + rand.Intn(max-min)
}
// Generate a random string of A-Z chars with len = l
func randomString(len int) string {
bytes := make([]byte, len)
for i := 0; i < len; i++ {
bytes[i] = byte(randomInt(97, 122))
}
return string(bytes)
}
7 years ago
7 years ago
func (n Node) send() {
fmt.Printf("Leader with id: %d has sent message\n", n.ip)
7 years ago
}
7 years ago
func (n Node) receive() {
fmt.Printf("Node: %d received message\n", n.ip)
7 years ago
}
func createNode(ip int, isLeader bool) Node {
7 years ago
n := Node{ip: ip, leader: isLeader}
7 years ago
return n
}
7 years ago
func pickLeader(i int) bool {
7 years ago
if i == 0 {
return true
} else {
7 years ago
return false
7 years ago
}
}
7 years ago
var N = make([]Node, 10)
7 years ago
func main() {
7 years ago
for i, id := range node_ips {
isLeader := pickLeader(i)
m := createNode(id, isLeader)
N[i] = m
}
for i, _ := range N {
m := N[i]
if m.leader {
m.send()
} else {
m.receive()
}
}
for i := 0; i < 100; i++ {
rand.Seed(time.Now().UnixNano())
fmt.Println(randomString(10)) // print 10 chars
}
7 years ago
}