-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_broadcast.go
130 lines (111 loc) · 2.65 KB
/
client_broadcast.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package goar
import (
"errors"
"fmt"
"github.com/permadao/goar/schema"
)
func (c *Client) BroadcastData(txId string, data []byte, numOfNodes int64, peers ...string) error {
var err error
if len(peers) == 0 {
peers, err = c.GetPeers()
if err != nil {
return err
}
}
count := int64(0)
pNode := NewTempConn()
for _, peer := range peers {
pNode.SetTempConnUrl("http://" + peer)
uploader, err := CreateUploader(pNode, txId, data)
if err != nil {
continue
}
if err = uploader.Once(); err != nil {
continue
}
count++
if count >= numOfNodes {
return nil
}
}
return fmt.Errorf("upload tx data to peers failed, txId: %s", txId)
}
func (c *Client) GetTxDataFromPeers(txId string, peers ...string) ([]byte, error) {
var err error
if len(peers) == 0 {
peers, err = c.GetPeers()
if err != nil {
return nil, err
}
}
pNode := NewTempConn()
for _, peer := range peers {
pNode.SetTempConnUrl("http://" + peer)
data, err := pNode.DownloadChunkData(txId)
if err != nil {
continue
}
return data, nil
}
return nil, errors.New("get tx data from peers failed")
}
func (c *Client) GetBlockFromPeers(height int64, peers ...string) (*schema.Block, error) {
var err error
if len(peers) == 0 {
peers, err = c.GetPeers()
if err != nil {
return nil, err
}
}
pNode := NewTempConn()
for _, peer := range peers {
pNode.SetTempConnUrl("http://" + peer)
block, err := pNode.GetBlockByHeight(height)
if err != nil {
continue
}
fmt.Printf("success get block; peer: %s\n", peer)
return block, nil
}
return nil, errors.New("get block from peers failed")
}
func (c *Client) GetTxFromPeers(arId string, peers ...string) (*schema.Transaction, error) {
var err error
if len(peers) == 0 {
peers, err = c.GetPeers()
if err != nil {
return nil, err
}
}
pNode := NewTempConn()
for _, peer := range peers {
pNode.SetTempConnUrl("http://" + peer)
tx, err := pNode.GetTransactionByID(arId)
if err != nil {
continue
}
fmt.Printf("success get tx; peer: %s, arTx: %s\n", peer, arId)
return tx, nil
}
return nil, fmt.Errorf("get tx failed; arId: %s", arId)
}
func (c *Client) GetUnconfirmedTxFromPeers(arId string, peers ...string) (*schema.Transaction, error) {
var err error
if len(peers) == 0 {
peers, err = c.GetPeers()
if err != nil {
return nil, err
}
}
pNode := NewTempConn()
for _, peer := range peers {
pNode.SetTempConnUrl("http://" + peer)
tx, err := pNode.GetUnconfirmedTx(arId)
if err != nil {
continue
}
fmt.Printf("success get unconfirmed tx; peer: %s, arTx: %s\n", peer, arId)
return tx, nil
}
return nil, fmt.Errorf("get unconfirmed tx failed; arId: %s", arId)
}