Skip to content

Commit 20e990d

Browse files
committed
fix testutils
1 parent dac0cd7 commit 20e990d

15 files changed

+266
-119
lines changed

.github/workflows/go.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ jobs:
1212
runs-on: ubuntu-latest
1313
steps:
1414

15-
- name: Set up Go 1.19
15+
- name: Set up Go 1.21
1616
uses: actions/setup-go@v2
1717
with:
18-
go-version: 1.19
18+
go-version: 1.21
1919
id: go
2020

2121
- name: Check out code into the Go module directory
2222
uses: actions/checkout@v2
2323

24-
- name: Get dependencies
24+
- name: Get qng dependencies
2525
run: |
2626
git clone https://github.com/Qitmeer/qng.git
2727
cd qng && make && pwd && cp ./build/bin/qng /usr/local/bin/

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,4 @@ config.toml
3838
ca.cert
3939
linux-wallet
4040
testutils/mainnet/
41+
testutils/privnet/

go.mod

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/Qitmeer/qitmeer-wallet
22

3-
go 1.18
3+
go 1.19
44

55
require (
66
github.com/BurntSushi/toml v1.2.0
@@ -78,7 +78,7 @@ require (
7878
github.com/kr/pretty v0.3.1 // indirect
7979
github.com/kr/text v0.2.0 // indirect
8080
github.com/libp2p/go-buffer-pool v0.1.0 // indirect
81-
github.com/libp2p/go-libp2p v0.27.8 // indirect
81+
github.com/libp2p/go-libp2p v0.27.1 // indirect
8282
github.com/magiconair/properties v1.8.6 // indirect
8383
github.com/mattn/go-isatty v0.0.18 // indirect
8484
github.com/mattn/go-runewidth v0.0.13 // indirect
@@ -142,4 +142,5 @@ require (
142142
)
143143

144144
replace github.com/ethereum/go-ethereum v1.11.5 => github.com/Qitmeer/go-ethereum v1.11.5-q.3
145+
145146
replace github.com/karalabe/usb v0.0.2 => github.com/dindinw/karalabe-usb v0.0.0-20230613095851-cd69a62c9c30

go.sum

+2-4
Original file line numberDiff line numberDiff line change
@@ -368,8 +368,6 @@ github.com/jung-kurt/gofpdf v1.0.3-0.20190309125859-24315acbbda5/go.mod h1:7Id9E
368368
github.com/jwilder/encoding v0.0.0-20170811194829-b4e1701a28ef/go.mod h1:Ct9fl0F6iIOGgxJ5npU/IUOhOhqlVrGjyIZc8/MagT0=
369369
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88/go.mod h1:3w7q1U84EfirKl04SVQ/s7nPm1ZPhiXd34z40TNz36k=
370370
github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213/go.mod h1:vNUNkEQ1e29fT/6vq2aBdFsgNPmy8qMdSay1npru+Sw=
371-
github.com/karalabe/usb v0.0.2 h1:M6QQBNxF+CQ8OFvxrT90BA0qBOXymndZnk5q235mFc4=
372-
github.com/karalabe/usb v0.0.2/go.mod h1:Od972xHfMJowv7NGVDiWVxk2zxnWgjLlJzE+F4F7AGU=
373371
github.com/kataras/golog v0.0.10/go.mod h1:yJ8YKCmyL+nWjERB90Qwn+bdyBZsaQwU3bTVFgkFIp8=
374372
github.com/kataras/iris/v12 v12.1.8/go.mod h1:LMYy4VlP67TQ3Zgriz8RE2h2kMZV2SgMYbq3UhfoFmE=
375373
github.com/kataras/neffos v0.0.14/go.mod h1:8lqADm8PnbeFfL7CLXh1WHw53dG27MC3pgi2R1rmoTE=
@@ -410,8 +408,8 @@ github.com/labstack/gommon v0.3.0/go.mod h1:MULnywXg0yavhxWKc+lOruYdAhDwPK9wf0OL
410408
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
411409
github.com/libp2p/go-buffer-pool v0.1.0 h1:oK4mSFcQz7cTQIfqbe4MIj9gLW+mnanjyFtc6cdF0Y8=
412410
github.com/libp2p/go-buffer-pool v0.1.0/go.mod h1:N+vh8gMqimBzdKkSMVuydVDq+UV5QTWy5HSiZacSbPg=
413-
github.com/libp2p/go-libp2p v0.27.8 h1:IX5x/4yKwyPQeVS2AXHZ3J4YATM9oHBGH1gBc23jBAI=
414-
github.com/libp2p/go-libp2p v0.27.8/go.mod h1:eCFFtd0s5i/EVKR7+5Ki8bM7qwkNW3TPTTSSW9sz8NE=
411+
github.com/libp2p/go-libp2p v0.27.1 h1:k1u6RHsX3hqKnslDjsSgLNURxJ3O1atIZCY4gpMbbus=
412+
github.com/libp2p/go-libp2p v0.27.1/go.mod h1:FAvvfQa/YOShUYdiSS03IR9OXzkcJXwcNA2FUCh9ImE=
415413
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
416414
github.com/magiconair/properties v1.8.6 h1:5ibWZ6iY0NctNGWo87LalDlEZ6R41TqbbDamhfG/Qzo=
417415
github.com/magiconair/properties v1.8.6/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=

rpc/client/httpclient.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ import (
55
"crypto/x509"
66
"encoding/json"
77
"fmt"
8-
"github.com/Qitmeer/qng/log"
98
"io/ioutil"
109
"net"
1110
"net/http"
1211
"time"
1312

13+
"github.com/Qitmeer/qng/log"
14+
1415
"github.com/samuel/go-socks/socks"
1516
)
1617

@@ -90,15 +91,15 @@ func (e RPCError) Error() string {
9091
return fmt.Sprintf("%d: %s", e.Code, e.Message)
9192
}
9293

93-
//Request json req
94+
// Request json req
9495
type Request struct {
9596
Jsonrpc string `json:"jsonrpc"`
9697
Method string `json:"method"`
9798
Params []json.RawMessage `json:"params"`
9899
ID interface{} `json:"id"`
99100
}
100101

101-
//makeRequestData
102+
// makeRequestData
102103
func MakeRequestData(rpcVersion string, id interface{}, method string, params []interface{}) ([]byte, error) {
103104
// default to JSON-RPC 1.0 if RPC type is not specified
104105
if rpcVersion != "2.0" && rpcVersion != "1.0" {
@@ -135,7 +136,7 @@ func MakeRequestData(rpcVersion string, id interface{}, method string, params []
135136
return reqData, nil
136137
}
137138

138-
//IsValidIDType id string number
139+
// IsValidIDType id string number
139140
func IsValidIDType(id interface{}) bool {
140141
switch id.(type) {
141142
case int, int8, int16, int32, int64,

rpc/walletrpc/methods_test.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/hex"
55
"encoding/json"
66
"fmt"
7+
78
"github.com/Qitmeer/qitmeer-wallet/config"
89
"github.com/Qitmeer/qng/log"
910

@@ -44,8 +45,8 @@ func TestGetSyncHeight(t *testing.T) {
4445
}
4546

4647
func openWallet() (*wallet.Wallet, error) {
47-
dbpath := "/Users/luoshan/Library/Application Support/Qitwallet/testnet"
48-
tomlpath := "/Users/luoshan/GolandProjects/qitmeer-wallet/config.toml"
48+
dbpath := "/Qitwallet/testnet"
49+
tomlpath := "/qitmeer-wallet/config.toml"
4950
pubpass := "public"
5051
dbpass := "123456"
5152
err := config.LoadConfig(tomlpath)
@@ -77,7 +78,7 @@ func openWallet() (*wallet.Wallet, error) {
7778

7879
func test_wallet_createNewAccount(w *wallet.Wallet) error {
7980
cmd := &qitmeerjson.CreateNewAccountCmd{
80-
Account: "luoshan4",
81+
Account: "test1",
8182
}
8283
msg, err := CreateNewAccount(cmd, w)
8384
if err != nil {

testutils/harness.go

+22-14
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,18 @@ package testutils
66

77
import (
88
"fmt"
9-
"github.com/Qitmeer/qng/core/protocol"
10-
"github.com/Qitmeer/qng/params"
11-
"github.com/ethereum/go-ethereum/ethclient"
129
"io/ioutil"
10+
"math/rand"
1311
"net"
1412
"os"
1513
"strconv"
1614
"sync"
1715
"testing"
1816
"time"
17+
18+
"github.com/Qitmeer/qng/core/protocol"
19+
"github.com/Qitmeer/qng/params"
20+
"github.com/ethereum/go-ethereum/ethclient"
1921
)
2022

2123
const DefaultMaxRpcConnRetries = 10
@@ -161,7 +163,7 @@ func NewHarnessWithMnemonic(t *testing.T, mnemonic, path string, usePkAddr bool,
161163
defer harnessStateMutex.Unlock()
162164
id := len(harnessInstances)
163165
// create temporary folder
164-
testDir, err := ioutil.TempDir("", "test-harness-"+strconv.Itoa(id)+"-*")
166+
testDir, err := ioutil.TempDir("", "test-harness-"+strconv.Itoa(int(time.Now().UnixNano())+id)+"-*")
165167
if err != nil {
166168
return nil, err
167169
}
@@ -248,24 +250,30 @@ func AllHarnesses() []*Harness {
248250

249251
const (
250252
// the minimum and maximum p2p and rpc port numbers used by a test harness.
251-
minP2PPort = 28200 // 28200 The min is inclusive
252-
maxP2PPort = minP2PPort + 10000 // 38199 The max is exclusive
253-
minRPCPort = maxP2PPort // 38200
254-
maxRPCPort = minRPCPort + 10000 // 48199
255-
minEVMPort = maxRPCPort // 48200
256-
maxEVMPort = minEVMPort + 2000 // 53199
257-
minEVMWSPort = maxEVMPort // 53200
258-
maxEVMWSPort = minEVMWSPort + 2000 // 58199
253+
minP2PPort = 18200 // 18200 The min is inclusive
254+
maxP2PPort = minP2PPort + 1000 // 19199 The max is exclusive
255+
minRPCPort = maxP2PPort // 19200
256+
maxRPCPort = minRPCPort + 1000 // 30199
257+
minEVMPort = maxRPCPort // 30200
258+
maxEVMPort = minEVMPort + 1000 // 31199
259+
minEVMWSPort = maxEVMPort // 31200
260+
maxEVMWSPort = minEVMWSPort + 1000 // 32199
259261
)
260262

263+
var argsLock sync.Mutex
264+
261265
// GenListenArgs returns auto generated args for p2p listen and rpc listen in the format of
262266
// ["--listen=127.0.0.1:12345", --rpclisten=127.0.0.1:12346"].
263267
// in order to support multiple test node running at the same time.
264268
func genListenArgs() (string, string, string, string) {
269+
argsLock.Lock()
270+
defer argsLock.Unlock()
265271
localhost := "127.0.0.1"
266272
genPort := func(min, max int) string {
267-
port := min + len(harnessInstances) + (42 * harnessMainProcessId % (max - min))
268-
return strconv.Itoa(port)
273+
rand.Seed(time.Now().UnixNano() + int64(len(harnessInstances)))
274+
return fmt.Sprintf("%d", rand.Intn(max-min)+min)
275+
// port := min + len(harnessInstances) + (42 * harnessMainProcessId % (max - min))
276+
// return strconv.Itoa(port)
269277
}
270278
p2p := net.JoinHostPort(localhost, genPort(minP2PPort, maxP2PPort))
271279
rpc := net.JoinHostPort(localhost, genPort(minRPCPort, maxRPCPort))

testutils/harness_test.go

+20-18
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
package testutils
66

77
import (
8+
"encoding/json"
89
"fmt"
9-
"github.com/Qitmeer/qng/core/types"
10-
"github.com/Qitmeer/qng/params"
1110
"sync"
1211
"testing"
1312
"time"
13+
14+
"github.com/Qitmeer/qng/core/types"
15+
"github.com/Qitmeer/qng/params"
1416
)
1517

1618
func TestHarness(t *testing.T) {
@@ -91,6 +93,7 @@ func TestSyncUnConfirmedCoinBase(t *testing.T) {
9193
}
9294

9395
h.WaitWalletInit()
96+
time.Sleep(10 * time.Second)
9497
if info, err := h.Client.NodeInfo(); err != nil {
9598
t.Errorf("test failed : %v", err)
9699
return
@@ -100,7 +103,6 @@ func TestSyncUnConfirmedCoinBase(t *testing.T) {
100103
t.Errorf("test failed, expect %v , but got %v", expect, info.Network)
101104
return
102105
}
103-
104106
}
105107
GenerateBlock(t, h, 10)
106108
time.Sleep(10 * time.Second)
@@ -131,6 +133,7 @@ func TestSyncConfirmedCoinBase(t *testing.T) {
131133
}
132134

133135
h.WaitWalletInit()
136+
time.Sleep(10 * time.Second)
134137
if info, err := h.Client.NodeInfo(); err != nil {
135138
t.Errorf("test failed : %v", err)
136139
return
@@ -142,22 +145,20 @@ func TestSyncConfirmedCoinBase(t *testing.T) {
142145
}
143146

144147
}
145-
GenerateBlock(t, h, 20)
146-
time.Sleep(10 * time.Second)
147-
148-
GenerateBlock(t, h, 1)
149-
time.Sleep(10 * time.Second)
148+
GenerateBlock(t, h, 18)
149+
time.Sleep(15 * time.Second)
150150
b, err := h.wallet.Balance(types.MEERA)
151151
if err != nil {
152152
t.Errorf("test failed : %v", err)
153153
return
154154
}
155-
if b.UnspentAmount.Value != 250000000000 {
156-
t.Errorf("test failed, expect unspent balance %d, but got %d", 250000000000, b.UnspentAmount.Value)
155+
b1, _ := json.Marshal(b)
156+
if b.UnspentAmount.Value != 100000000000 {
157+
t.Errorf("test failed, expect unspent balance %d, but got %d %v", 100000000000, b.UnspentAmount.Value, string(b1))
157158
return
158159
}
159160
if b.UnconfirmedAmount.Value != 800000000000 {
160-
t.Errorf("test failed, expect unconfirmed balance %d, but got %d", 800000000000, b.UnspentAmount.Value)
161+
t.Errorf("test failed, expect unconfirmed balance %d, but got %d %v", 800000000000, b.UnspentAmount.Value, string(b1))
161162
return
162163
}
163164
}
@@ -177,7 +178,7 @@ func TestSpent(t *testing.T) {
177178
return
178179
}
179180
h.WaitWalletInit()
180-
181+
time.Sleep(10 * time.Second)
181182
if info, err := h.Client.NodeInfo(); err != nil {
182183
t.Errorf("test failed : %v", err)
183184
return
@@ -189,26 +190,27 @@ func TestSpent(t *testing.T) {
189190
}
190191

191192
}
192-
GenerateBlock(t, h, 20)
193-
time.Sleep(10 * time.Second)
194-
GenerateBlock(t, h, 1)
193+
GenerateBlock(t, h, 18)
195194
time.Sleep(10 * time.Second)
196195
b, err := h.wallet.Balance(types.MEERA)
197196
if err != nil {
198197
t.Errorf("test failed : %v", err)
199198
return
200199
}
201-
_, err = h.wallet.SendToAddress("RmV7i7JoomcHuQCVMN66SiTYUCkRtzQ6fSf", types.MEERA, 1000)
200+
_, err = h.wallet.SendToAddress("RmV7i7JoomcHuQCVMN66SiTYUCkRtzQ6fSf", types.MEERA, 498)
202201
if err != nil {
203202
t.Errorf("test failed, %v", err)
203+
return
204204
}
205+
GenerateBlock(t, h, 1)
206+
time.Sleep(10 * time.Second)
205207
b, err = h.wallet.Balance(types.MEERA)
206208
if err != nil {
207209
t.Errorf("test failed : %v", err)
208210
return
209211
}
210-
if b.SpendAmount.Value != 150000000000 {
211-
t.Errorf("test failed, expect spent balance %d, but got %d", 200000000000, b.UnspentAmount.Value)
212+
if b.SpendAmount.Value != 50000000000 {
213+
t.Errorf("test failed, expect spent balance %d, but got %d", 50000000000, b.SpendAmount.Value)
212214
return
213215
}
214216
GenerateBlock(t, h, 1)

testutils/node.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ package testutils
77
import (
88
"bufio"
99
"fmt"
10-
"github.com/Qitmeer/qng/params"
11-
"github.com/Qitmeer/qng/rpc"
1210
"io"
1311
"os"
1412
"os/exec"
1513
"path/filepath"
14+
"strings"
1615
"sync"
1716
"testing"
17+
18+
"github.com/Qitmeer/qng/params"
19+
"github.com/Qitmeer/qng/rpc"
1820
)
1921

2022
// the configuration of the node
@@ -59,6 +61,8 @@ func (n *nodeConfig) args() []string {
5961
args := []string{}
6062
if n.listen != "" {
6163
args = append(args, fmt.Sprintf("--listen=%s", n.listen))
64+
port := strings.Split(n.listen, ":")[1]
65+
args = append(args, fmt.Sprintf("--port=%s", port))
6266
}
6367
if n.rpclisten != "" {
6468
args = append(args, fmt.Sprintf("--rpclisten=%s", n.rpclisten))

testutils/node_test.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"os/exec"
1212
"reflect"
1313
"regexp"
14+
"strings"
1415
"testing"
1516
"time"
1617
)
@@ -29,6 +30,7 @@ func TestNewNodeCmdArgs(t *testing.T) {
2930
args := []string{
3031
"qng",
3132
"--listen=127.0.0.1:38130",
33+
"--port=38130",
3234
"--rpclisten=127.0.0.1:38131",
3335
"--rpcuser=testuser",
3436
"--rpcpass=testpass",
@@ -53,7 +55,7 @@ func TestNewNodeCmdArgs(t *testing.T) {
5355
}
5456

5557
func TestNodeStartStop(t *testing.T) {
56-
found, err := exec.LookPath("qitmeer")
58+
found, err := exec.LookPath("qng")
5759
if err != nil {
5860
t.Skip(fmt.Sprintf("skip the test since: %v", err))
5961
} else {
@@ -84,6 +86,7 @@ func TestGenListenArgs(t *testing.T) {
8486
c.listen, c.rpclisten, c.evmlisten, c.evmWSlisten = a1, a2, a3, a4
8587
args := []string{
8688
"--listen=" + a1,
89+
"--port=" + strings.Split(a1, ":")[1],
8790
"--rpclisten=" + a2,
8891
fmt.Sprintf(`--evmenv="--http --http.port=%s --ws --ws.port=%s"`, a3, a4),
8992
"--rpcuser=testuser",

0 commit comments

Comments
 (0)