Skip to content

Commit e852ac8

Browse files
committed
fix(): add new feature: client.GetTxDataFromPeers and client.BroadcastData
1 parent c1bd5b3 commit e852ac8

5 files changed

+30
-47
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ fmt.Println(id, err) // {{id}}, nil
9191
- [x] GetBlockByHeight
9292
- [x] BatchSendItemToBundler
9393
- [x] GetBundle
94+
- [x] GetTxDataFromPeers
95+
- [x] BroadcastData
9496

9597
Initialize the instance:
9698

client.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func (c *Client) GetPeers() ([]string, error) {
7575
fpeers = append(fpeers, p)
7676
}
7777

78-
return peers[:], nil
78+
return fpeers, nil
7979
}
8080

8181
// GetTransactionByID status: Pending/Invalid hash/overspend
@@ -169,10 +169,13 @@ func (c *Client) GetTransactionData(id string, extension ...string) (body []byte
169169
body, statusCode, err := c.httpGet(urlPath)
170170

171171
// When data is bigger than 12MiB statusCode == 400 NOTE: Data bigger than that has to be downloaded chunk by chunk.
172-
if statusCode == 400 || len(body) == 0 {
172+
if statusCode == 400 {
173173
body, err = c.DownloadChunkData(id)
174174
return
175175
} else if statusCode == 200 {
176+
if len(body) == 0 {
177+
return c.DownloadChunkData(id)
178+
}
176179
return body, nil
177180
} else if statusCode == 202 {
178181
return nil, ErrPendingTx
@@ -189,6 +192,9 @@ func (c *Client) GetTransactionDataByGateway(id string) (body []byte, err error)
189192
body, statusCode, err := c.httpGet(urlPath)
190193
switch statusCode {
191194
case 200:
195+
if len(body) == 0 {
196+
return c.DownloadChunkData(id)
197+
}
192198
return body, nil
193199
case 400:
194200
return c.DownloadChunkData(id)

client_broadcast.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package goar
33
import (
44
"errors"
55
"fmt"
6-
7-
"github.com/everFinance/sandy_log/log"
86
)
97

108
func (c *Client) GetTxDataFromPeers(txId string) ([]byte, error) {
@@ -15,11 +13,12 @@ func (c *Client) GetTxDataFromPeers(txId string) ([]byte, error) {
1513

1614
for _, peer := range peers {
1715
pNode := NewClient("http://" + peer)
18-
data, err := pNode.GetTransactionData(txId)
16+
data, err := pNode.DownloadChunkData(txId)
1917
if err != nil {
20-
log.Error("get tx data failed", "error", err, "peer", peer)
18+
fmt.Printf("get tx data error:%v, peer: %s\n", err, peer)
2119
continue
2220
}
21+
fmt.Printf("success get tx data; peer: %s\n", peer)
2322
return data, nil
2423
}
2524

@@ -34,7 +33,6 @@ func (c *Client) BroadcastData(txId string, data []byte, numOfNodes int64) error
3433

3534
count := int64(0)
3635
for _, peer := range peers {
37-
3836
fmt.Printf("upload peer: %s, count: %d\n", peer, count)
3937
arNode := NewClient("http://" + peer)
4038
uploader, err := CreateUploader(arNode, txId, data)

client_test.go

+2-24
Original file line numberDiff line numberDiff line change
@@ -201,28 +201,6 @@ func TestClient_GetPeers(t *testing.T) {
201201
t.Log(len(peers))
202202
}
203203

204-
// func Test_BroadcastData(t *testing.T) {
205-
// data := make([]byte, 1103732)
206-
// data[0] = byte('z')
207-
// data[1] = byte('y')
208-
// data[2] = byte('j')
209-
// for i := 3; i < len(data); i++ {
210-
// data[i] = byte('a')
211-
// }
212-
213-
// cli := NewClient("https://arweave.net")
214-
// txId := "D3GOny9cItUEc8qAl1oLUtnoLOB3OfSB-wKbw8TUIRc"
215-
// err = cli.BroadcastData(txId, data, 1)
216-
// assert.NoError(t, err)
217-
// }
218-
219-
// func Test_GetTxDataFromPeers(t *testing.T) {
220-
// cli := NewClient("https://arweave.net")
221-
// txId := "D3GOny9cItUEc8qAl1oLUtnoLOB3OfSB-wKbw8TUIRc"
222-
// data, err := cli.GetTxDataFromPeers(txId)
223-
// assert.NoError(t, err)
224-
// assert.Equal(t, 1471643, len(data))
225-
// }
226204
func Test_GetTxDataFromPeers(t *testing.T) {
227205
cli := NewClient("https://arweave.net")
228206
txId := "J5FY1Ovd6JJ49WFHfCf-1wDM1TbaPSdKnGIB_8ePErE"
@@ -241,12 +219,12 @@ func Test_GetTxDataFromPeers(t *testing.T) {
241219
assert.Equal(t, tx.DataRoot, dataRoot)
242220
}
243221

244-
func TestClient_UploadTxDataToPeers(t *testing.T) {
222+
func TestClient_BroadcastData(t *testing.T) {
245223
cli := NewClient("https://arweave.net")
246224
txId := "J5FY1Ovd6JJ49WFHfCf-1wDM1TbaPSdKnGIB_8ePErE"
247225
data, err := cli.GetTransactionData(txId, "json")
248226
assert.NoError(t, err)
249227

250-
err = cli.UploadTxDataToPeers(txId, data)
228+
err = cli.BroadcastData(txId, data, 20)
251229
assert.NoError(t, err)
252230
}

wallet_test.go

+15-16
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package goar
22

33
import (
44
"encoding/base64"
5-
"github.com/everFinance/goar/types"
65
"testing"
76

87
"github.com/stretchr/testify/assert"
@@ -64,21 +63,21 @@ func TestWallet_SendAR(t *testing.T) {
6463

6564
// test send small size file
6665
func TestWallet_SendDataSpeedUp01(t *testing.T) {
67-
arNode := "https://arweave.net"
68-
w, err := NewWalletFromPath("./example/testKey.json", arNode) // your wallet private key
69-
assert.NoError(t, err)
70-
71-
// data := []byte("aaa this is a goar test small size file data") // small file
72-
data := make([]byte, 255*1024)
73-
for i := 0; i < len(data); i++ {
74-
data[i] = byte('b' + i)
75-
}
76-
tags := []types.Tag{
77-
{Name: "GOAR", Value: "SMDT"},
78-
}
79-
id, err := w.SendDataSpeedUp(data, tags, 50)
80-
assert.NoError(t, err)
81-
t.Logf("tx hash: %s", id)
66+
// arNode := "https://arweave.net"
67+
// w, err := NewWalletFromPath("./example/testKey.json", arNode) // your wallet private key
68+
// assert.NoError(t, err)
69+
//
70+
// // data := []byte("aaa this is a goar test small size file data") // small file
71+
// data := make([]byte, 255*1024)
72+
// for i := 0; i < len(data); i++ {
73+
// data[i] = byte('b' + i)
74+
// }
75+
// tags := []types.Tag{
76+
// {Name: "GOAR", Value: "SMDT"},
77+
// }
78+
// id, err := w.SendDataSpeedUp(data, tags, 50)
79+
// assert.NoError(t, err)
80+
// t.Logf("tx hash: %s", id)
8281
}
8382

8483
// test send big size file

0 commit comments

Comments
 (0)