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

116 lines
2.2 KiB

7 years ago
package main
import (
7 years ago
"fmt"
"math/rand"
7 years ago
)
7 years ago
7 years ago
type Node struct {
ip int
leader bool
reciveBlock chan string
sendBlock chan string
7 years ago
}
7 years ago
type Nodes struct {
7 years ago
Nodes []*Node
}
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
func (n Node) send(cin <-chan string, id int) {
for msg := range cin {
fmt.Printf("Leader has sent message %s to %d\n", msg, id)
}
7 years ago
}
7 years ago
func (n Node) receive() {
fmt.Printf("Node: %d received message\n", n.ip)
7 years ago
}
7 years ago
func createNode(ip int, isLeader bool) Node {
7 years ago
n := Node{ip: ip, leader: isLeader}
7 years ago
return n
}
func pickLeader(i int) Node {
7 years ago
if i == 0 {
return createNode(i, true)
7 years ago
} else {
return createNode(i, false)
7 years ago
}
}
func BufferedTxnQueueWithFanOut(ch <-chan string, size int) []chan string { // This needs
cs := make([]chan string, size)
for i, _ := range cs {
// The size of the channels buffer controls how far behind the recievers
// of the fanOut channels can lag the other channels.
cs[i] = make(chan string)
}
go func() {
for txs := range ch {
for _, c := range cs {
c <- txs
}
}
for _, c := range cs {
// close all our fanOut channels when the input channel is exhausted.
close(c)
}
}()
return cs
}
7 years ago
func TxnGenerator(numOfTxns int, lenOfRandomString int) <-chan string {
out := make(chan string)
go func() {
for i := 0; i < numOfTxns; i++ {
out <- randomString(lenOfRandomString)
}
close(out)
}()
return out
}
7 years ago
func main() {
var (
isLeader Node
numOfTxns = 1000
numOfTxnsInBlock = 10
N = make([]Node, 10)
lenOfRandomString = 10
node_ips = []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
)
for i := range node_ips {
m := pickLeader(i)
7 years ago
N[i] = m
if m.leader {
isLeader = m
7 years ago
}
}
txnqueue := TxnGenerator(numOfTxns, lenOfRandomString)
blockFullOfTxns := BufferedTxnQueueWithFanOut(txnqueue, numOfTxnsInBlock)
for num := range blockFullOfTxns {
fmt.Println(num)
txn := blockFullOfTxns[num]
for i, m := range N {
if !m.leader {
go isLeader.send(txn, i)
}
}
}
7 years ago
}