-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
52 lines (42 loc) · 1.1 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
package main
import (
"bytes"
"math/rand"
"strconv"
"time"
"github.com/chalfel/complete-blockchain/core"
"github.com/chalfel/complete-blockchain/crypto"
"github.com/chalfel/complete-blockchain/network"
"github.com/sirupsen/logrus"
)
func main() {
trLocal := network.NewLocalTransport("LOCAL")
trRemote := network.NewLocalTransport("REMOTE")
trLocal.Connect(trRemote)
trRemote.Connect(trLocal)
go func() {
for {
if err := sendTransaction(trRemote, trLocal.Addr()); err != nil {
logrus.Error(err)
}
time.Sleep(1 * time.Second)
}
}()
opts := network.ServerOpts{
Transports: []network.Transport{trLocal},
}
s := network.NewServer(opts)
s.Start()
}
func sendTransaction(tr network.Transport, to network.NetAddr) error {
privKey := crypto.GeneratePrivateKey()
data := []byte(strconv.FormatInt(int64(rand.Intn(1000000000)), 10))
tx := core.NewTransaction(data)
tx.Sign(privKey)
buf := &bytes.Buffer{}
if err := tx.Encode(core.NewGobTxEncoder(buf)); err != nil {
return err
}
msg := network.NewMessage(network.MessageTypeTx, buf.Bytes())
return tr.SendMessage(to, msg.Bytes())
}