mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-27 23:26:44 +00:00
* initial * fixed tests * eip155 tests * progress * fix * fix TestFetchStateSyncEvents* * debug * fixed with updated list of validators * fix * a bit more stable * optimize allocations * linters * fix vals copy * a bit of refactoring * update TestGetTransactionReceiptsByBlock * remove logs from TestGetTransactionReceiptsByBlock in favour of checks * comments * Linters * linters * fixes after merge * fixes after merge * fixes after merge * fixes after merge * fail fast Co-authored-by: Evgeny Danienko <6655321@bk.ru>
82 lines
1.5 KiB
Go
82 lines
1.5 KiB
Go
package server
|
|
|
|
import (
|
|
"fmt"
|
|
"math/rand"
|
|
"net"
|
|
"os"
|
|
"sync/atomic"
|
|
"time"
|
|
)
|
|
|
|
var maxPortCheck int32 = 100
|
|
|
|
// findAvailablePort returns the next available port starting from `from`
|
|
func findAvailablePort(from int32, count int32) (int32, error) {
|
|
if count == maxPortCheck {
|
|
return 0, fmt.Errorf("no available port found")
|
|
}
|
|
|
|
port := atomic.AddInt32(&from, 1)
|
|
addr := fmt.Sprintf("localhost:%d", port)
|
|
|
|
count++
|
|
|
|
lis, err := net.Listen("tcp", addr)
|
|
if err == nil {
|
|
lis.Close()
|
|
return port, nil
|
|
} else {
|
|
return findAvailablePort(from, count)
|
|
}
|
|
}
|
|
|
|
func CreateMockServer(config *Config) (*Server, error) {
|
|
if config == nil {
|
|
config = DefaultConfig()
|
|
}
|
|
|
|
// find available port for grpc server
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
var (
|
|
from int32 = 60000 // the min port to start checking from
|
|
to int32 = 61000 // the max port to start checking from
|
|
)
|
|
|
|
//nolint: gosec
|
|
port, err := findAvailablePort(rand.Int31n(to-from+1)+from, 0)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// grpc port
|
|
config.GRPC.Addr = fmt.Sprintf(":%d", port)
|
|
|
|
// datadir
|
|
datadir, _ := os.MkdirTemp("/tmp", "bor-cli-test")
|
|
config.DataDir = datadir
|
|
|
|
// find available port for http server
|
|
from = 8545
|
|
to = 9545
|
|
|
|
//nolint: gosec
|
|
port, err = findAvailablePort(rand.Int31n(to-from+1)+from, 0)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
config.JsonRPC.Http.Port = uint64(port)
|
|
|
|
// start the server
|
|
return NewServer(config)
|
|
}
|
|
|
|
func CloseMockServer(server *Server) {
|
|
// remove the contents of temp data dir
|
|
os.RemoveAll(server.config.DataDir)
|
|
|
|
// close the server
|
|
server.Stop()
|
|
}
|