1
1
import tape from 'tape'
2
2
import { Server as RPCServer , HttpServer } from 'jayson/promise'
3
- const request = require ( 'supertest' )
4
3
import Common from '@ethereumjs/common'
5
4
import { RPCManager as Manager } from '../../lib/rpc'
6
- import * as Logger from '../../lib/logging'
7
- import { blockChain } from './blockChainStub '
5
+ import { getLogger } from '../../lib/logging'
6
+ import { mockBlockchain } from './mockBlockchain '
8
7
import { Chain } from '../../lib/blockchain/chain'
9
8
import { RlpxServer } from '../../lib/net/server/rlpxserver'
10
- import Blockchain from '@ethereumjs/blockchain'
11
9
import { Config } from '../../lib/config'
10
+ import type Blockchain from '@ethereumjs/blockchain'
11
+ import type EthereumClient from '../../lib/client'
12
+ const request = require ( 'supertest' )
12
13
13
14
const config : any = { loglevel : 'error' }
14
- config . logger = Logger . getLogger ( config )
15
+ config . logger = getLogger ( config )
15
16
16
17
export function startRPC ( methods : any , port : number = 3000 ) {
17
18
const server = new RPCServer ( methods )
@@ -24,59 +25,63 @@ export function closeRPC(server: HttpServer) {
24
25
server . close ( )
25
26
}
26
27
27
- export function createManager ( node : any ) {
28
- return new Manager ( node , config )
28
+ export function createManager ( client : EthereumClient ) {
29
+ return new Manager ( client , config )
29
30
}
30
31
31
- export function createNode ( nodeConfig ? : any ) {
32
- const common = nodeConfig ? .commonChain ?? new Common ( { chain : 'mainnet' } )
32
+ export function createClient ( clientOpts : any = { } ) {
33
+ const common = clientOpts . commonChain ?? new Common ( { chain : 'mainnet' } )
33
34
const config = new Config ( { transports : [ ] , common } )
34
- const chain = new Chain ( {
35
- config,
36
- blockchain : ( < unknown > blockChain ( { } ) ) as Blockchain ,
37
- } )
35
+ const blockchain = ( < any > mockBlockchain ( ) ) as Blockchain
36
+
37
+ const chain = new Chain ( { config, blockchain } )
38
38
chain . opened = true
39
- const defaultNodeConfig = {
39
+
40
+ const defaultClientConfig = {
40
41
blockchain : chain ,
41
42
opened : true ,
42
43
ethProtocolVersions : [ 63 ] ,
43
44
}
44
- const trueNodeConfig = { ...defaultNodeConfig , ...nodeConfig }
45
+ const clientConfig = { ...defaultClientConfig , ...clientOpts }
46
+
45
47
const servers = [
46
48
new RlpxServer ( {
47
49
config,
48
50
bootnodes : '10.0.0.1:1234,10.0.0.2:1234' ,
49
51
} ) ,
50
52
]
51
- return {
53
+
54
+ const client : any = {
52
55
services : [
53
56
{
54
57
name : 'eth' ,
55
- chain : trueNodeConfig . blockchain ,
58
+ chain : clientConfig . blockchain ,
56
59
pool : { peers : [ 1 , 2 , 3 ] } ,
57
60
protocols : [
58
61
{
59
62
name : 'eth' ,
60
- versions : trueNodeConfig . ethProtocolVersions ,
63
+ versions : clientConfig . ethProtocolVersions ,
61
64
} ,
62
65
] ,
63
66
} ,
64
67
] ,
65
68
servers,
66
- opened : trueNodeConfig . opened ,
69
+ opened : clientConfig . opened ,
67
70
server : ( name : string ) => {
68
71
return servers . find ( ( s ) => s . name === name )
69
72
} ,
70
73
}
74
+
75
+ return client as EthereumClient
71
76
}
72
77
73
78
export function baseSetup ( ) {
74
- const manager = createManager ( createNode ( ) )
79
+ const manager = createManager ( createClient ( ) )
75
80
const server = startRPC ( manager . getMethods ( ) )
76
81
return server
77
82
}
78
83
79
- export function params ( method : any , params : any [ ] = [ ] ) {
84
+ export function params ( method : string , params : Array < any > = [ ] ) {
80
85
const req = {
81
86
jsonrpc : '2.0' ,
82
87
method,
@@ -86,14 +91,20 @@ export function params(method: any, params: any[] = []) {
86
91
return req
87
92
}
88
93
89
- export function baseRequest ( t : tape . Test , server : any , req : any , expect : any , expectRes : any ) {
94
+ export function baseRequest (
95
+ t : tape . Test ,
96
+ server : HttpServer ,
97
+ req : Object ,
98
+ expect : number ,
99
+ expectRes : Function
100
+ ) {
90
101
request ( server )
91
102
. post ( '/' )
92
103
. set ( 'Content-Type' , 'application/json' )
93
104
. send ( req )
94
105
. expect ( expect )
95
106
. expect ( expectRes )
96
- . end ( ( err : any ) => {
107
+ . end ( ( err ?: Error ) => {
97
108
closeRPC ( server )
98
109
t . end ( err )
99
110
} )
0 commit comments