This repository has been archived by the owner on Oct 31, 2024. It is now read-only.
forked from 0xPolygon/polygon-edge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbroadcast_test.go
149 lines (122 loc) · 3.51 KB
/
broadcast_test.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package e2e
import (
"context"
"fmt"
"math/big"
"sync"
"testing"
"time"
"github.com/0xPolygon/polygon-edge/crypto"
"github.com/0xPolygon/polygon-edge/e2e/framework"
"github.com/0xPolygon/polygon-edge/helper/tests"
"github.com/0xPolygon/polygon-edge/types"
"github.com/stretchr/testify/assert"
)
func TestBroadcast(t *testing.T) {
// This test is not stable
// Opened the ticket to check + fix it
t.Skip()
testCases := []struct {
name string
numNodes int
// Number of nodes that connects to left node
numConnectedNodes int
}{
{
name: "tx should not reach to last node",
numNodes: 10,
numConnectedNodes: 5,
},
{
name: "tx should reach to last node",
numNodes: 10,
numConnectedNodes: 10,
},
}
signer := &crypto.FrontierSigner{}
senderKey, senderAddr := tests.GenerateKeyAndAddr(t)
_, receiverAddr := tests.GenerateKeyAndAddr(t)
conf := func(config *framework.TestServerConfig) {
config.SetConsensus(framework.ConsensusDummy)
config.Premine(senderAddr, framework.EthToWei(10))
}
for _, tt := range testCases {
tt := tt
t.Run(tt.name, func(t *testing.T) {
srvs := framework.NewTestServers(t, tt.numNodes, conf)
framework.MultiJoinSerial(t, srvs[0:tt.numConnectedNodes])
// Check the connections
connectionErrors := framework.NewAtomicErrors(len(srvs))
var wgForConnections sync.WaitGroup
for i, srv := range srvs {
srv := srv
// Required number of connections
numRequiredConnections := 0
if i < tt.numConnectedNodes {
if i == 0 || i == tt.numConnectedNodes-1 {
numRequiredConnections = 1
} else {
numRequiredConnections = 2
}
}
wgForConnections.Add(1)
go func() {
defer wgForConnections.Done()
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
_, err := framework.WaitUntilPeerConnects(ctx, srv, numRequiredConnections)
if err != nil {
connectionErrors.Append(err)
}
}()
}
wgForConnections.Wait()
for _, err := range connectionErrors.Errors() {
t.Error(err)
}
if len(connectionErrors.Errors()) > 0 {
t.Fail()
}
// wait until gossip protocol build mesh network
// (https://github.com/libp2p/specs/blob/master/pubsub/gossipsub/gossipsub-v1.0.md)
time.Sleep(time.Second * 2)
tx, err := signer.SignTx(&types.Transaction{
Nonce: 0,
From: senderAddr,
To: &receiverAddr,
Value: framework.EthToWei(1),
Gas: 1000000,
GasPrice: big.NewInt(10000),
Input: []byte{},
}, senderKey)
if err != nil {
t.Fatalf("failed to sign transaction, err=%+v", err)
}
_, err = srvs[0].JSONRPC().Eth().SendRawTransaction(tx.MarshalRLP())
if err != nil {
t.Fatalf("failed to send transaction, err=%+v", err)
}
for i, srv := range srvs {
srv := srv
shouldHaveTxPool := false
subTestName := fmt.Sprintf("node %d shouldn't have tx in txpool", i)
if i < tt.numConnectedNodes {
shouldHaveTxPool = true
subTestName = fmt.Sprintf("node %d should have tx in txpool", i)
}
t.Run(subTestName, func(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithTimeout(context.Background(), framework.DefaultTimeout)
defer cancel()
res, err := framework.WaitUntilTxPoolFilled(ctx, srv, 1)
if shouldHaveTxPool {
assert.NoError(t, err)
assert.Equal(t, uint64(1), res.Length)
} else {
assert.ErrorIs(t, err, tests.ErrTimeout)
}
})
}
})
}
}