-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
70 lines (63 loc) · 1.58 KB
/
main.go
File metadata and controls
70 lines (63 loc) · 1.58 KB
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
package main
import (
"context"
"log"
"time"
"github.com/CaiqueRibeiro/blocker/crypto"
"github.com/CaiqueRibeiro/blocker/node"
"github.com/CaiqueRibeiro/blocker/proto"
"github.com/CaiqueRibeiro/blocker/util"
"google.golang.org/grpc"
)
func main() {
makeNode(":3000", []string{}, true) // creates a genesis node
time.Sleep(time.Second)
makeNode(":4000", []string{":3000"}, false) // creates a node that connects to the genesis node
time.Sleep(time.Second)
makeNode(":6000", []string{":4000"}, false) // creates a node that connects to the genesis node
for {
time.Sleep(time.Second)
makeTransaction()
}
}
func makeNode(listenAddr string, bootstrapNodes []string, isValidator bool) *node.Node {
cfg := node.ServerConfig{
Version: "Blocker-1",
ListenAddr: listenAddr,
}
if isValidator {
cfg.PrivateKey = crypto.GeneratePrivateKey()
}
n := node.NewNode(cfg) // creates a new node
go n.Start(listenAddr, bootstrapNodes) // starts the node
return n
}
// temporary: just to test gRPC calls
func makeTransaction() {
client, err := grpc.Dial(":3000", grpc.WithInsecure())
if err != nil {
log.Fatal(err)
}
c := proto.NewNodeClient(client)
privKey := crypto.GeneratePrivateKey()
tx := &proto.Transaction{
Version: 1,
Inputs: []*proto.TxInput{
{
PrevTxHash: util.RandomHash(),
PrevOutIndex: 0,
PublicKey: privKey.Public().Bytes(),
},
},
Outputs: []*proto.TxOutput{
{
Amount: 99,
Address: privKey.Public().Address().Bytes(),
},
},
}
_, err = c.HandleTransaction(context.TODO(), tx)
if err != nil {
log.Fatal(err)
}
}