1313 * See the License for the specific language governing permissions and
1414 * limitations under the License.
1515 */
16- import { combineLatest } from 'rxjs' ;
16+ import * as fs from 'fs' ;
17+ import * as path from 'path' ;
1718import { map } from 'rxjs/operators' ;
19+ import { BootstrapService , BootstrapUtils , ConfigPreset , Preset , StartParams } from 'symbol-bootstrap' ;
1820import { IListener } from '../../src/infrastructure/IListener' ;
1921import { RepositoryFactory } from '../../src/infrastructure/RepositoryFactory' ;
2022import { RepositoryFactoryHttp } from '../../src/infrastructure/RepositoryFactoryHttp' ;
2123import { Account } from '../../src/model/account/Account' ;
24+ import { NetworkCurrencyLocal } from '../../src/model/mosaic/NetworkCurrencyLocal' ;
25+ import { NetworkCurrencyPublic } from '../../src/model/mosaic/NetworkCurrencyPublic' ;
26+ import { NamespaceId } from '../../src/model/namespace/NamespaceId' ;
2227import { NetworkType } from '../../src/model/network/NetworkType' ;
2328import { SignedTransaction } from '../../src/model/transaction/SignedTransaction' ;
2429import { Transaction } from '../../src/model/transaction/Transaction' ;
2530import { UInt64 } from '../../src/model/UInt64' ;
2631import { TransactionService } from '../../src/service/TransactionService' ;
27- import { NetworkCurrencyPublic } from '../../src/model/mosaic/NetworkCurrencyPublic' ;
28- import { NetworkCurrencyLocal } from '../../src/model/mosaic/NetworkCurrencyLocal' ;
29- import { NamespaceId } from '../../src/model/namespace/NamespaceId' ;
30- import * as yaml from 'js-yaml' ;
31- import * as path from 'path' ;
32- import * as fs from 'fs' ;
3332
3433export class IntegrationTestHelper {
3534 public apiUrl : string ;
@@ -50,83 +49,95 @@ export class IntegrationTestHelper {
5049 public transactionService : TransactionService ;
5150 public networkCurrencyNamespaceId : NamespaceId ;
5251 public networkCurrencyDivisibility : number ;
52+ public service = new BootstrapService ( ) ;
53+ public config : StartParams ;
54+ public startEachTime = true ;
5355
54- start ( ) : Promise < IntegrationTestHelper > {
55- return new Promise < IntegrationTestHelper > ( ( resolve , reject ) => {
56- fs . readFile ( path . resolve ( __dirname , '../conf/network.conf' ) , ( err , jsonData : any ) => {
57- if ( err ) {
58- return reject ( err ) ;
59- }
60- const json = JSON . parse ( jsonData ) ;
61- console . log ( `Running tests against: ${ json . apiUrl } ` ) ;
62- this . apiUrl = json . apiUrl ;
63- this . repositoryFactory = new RepositoryFactoryHttp ( json . apiUrl ) ;
64- this . transactionService = new TransactionService (
65- this . repositoryFactory . createTransactionRepository ( ) ,
66- this . repositoryFactory . createReceiptRepository ( ) ,
67- ) ;
68- combineLatest ( this . repositoryFactory . getGenerationHash ( ) , this . repositoryFactory . getNetworkType ( ) ) . subscribe (
69- ( [ generationHash , networkType ] ) => {
70- this . networkType = networkType ;
71- this . generationHash = generationHash ;
72- this . account = this . createAccount ( json . testAccount ) ;
73- this . account2 = this . createAccount ( json . testAccount2 ) ;
74- this . account3 = this . createAccount ( json . testAccount3 ) ;
75- this . multisigAccount = this . createAccount ( json . multisigAccount ) ;
76- this . cosignAccount1 = this . createAccount ( json . cosignatoryAccount ) ;
77- this . cosignAccount2 = this . createAccount ( json . cosignatory2Account ) ;
78- this . cosignAccount3 = this . createAccount ( json . cosignatory3Account ) ;
79- this . cosignAccount4 = this . createAccount ( json . cosignatory4Account ) ;
80- this . harvestingAccount = this . createAccount ( json . harvestingAccount ) ;
81- this . listener = this . repositoryFactory . createListener ( ) ;
56+ private async startBootstrapServer ( ) : Promise < { accounts : string [ ] ; apiUrl : string } > {
57+ this . config = {
58+ preset : Preset . bootstrap ,
59+ reset : this . startEachTime ,
60+ customPreset : './e2e/e2e-preset.yml' ,
61+ timeout : 60000 * 3 ,
62+ target : 'target/bootstrap-test' ,
63+ daemon : false ,
64+ user : 'current' ,
65+ } ;
8266
83- // What would be the best maxFee? In the future we will load the fee multiplier from rest.
84- this . maxFee = UInt64 . fromUint ( 1000000 ) ;
67+ console . log ( 'Starting bootstrap server' ) ;
68+ const configResult = await this . service . start ( { ...this . config , daemon : true } ) ;
69+ const accounts = configResult . addresses ?. mosaics ?. [ 'currency' ] . map ( ( n ) => n . privateKey ) ;
70+ if ( ! accounts ) {
71+ throw new Error ( 'Nemesis accounts could not be loaded!' ) ;
72+ }
73+ return { accounts, apiUrl : 'http://localhost:3000' } ;
74+ }
8575
86- // network Currency
87- this . networkCurrencyNamespaceId = this . apiUrl . toLowerCase ( ) . includes ( 'localhost' )
88- ? NetworkCurrencyLocal . NAMESPACE_ID
89- : NetworkCurrencyPublic . NAMESPACE_ID ;
90- this . networkCurrencyDivisibility = this . apiUrl . toLowerCase ( ) . includes ( 'localhost' )
91- ? NetworkCurrencyLocal . DIVISIBILITY
92- : NetworkCurrencyPublic . DIVISIBILITY ;
76+ async close ( ) : Promise < void > {
77+ if ( this . listener && this . listener . isOpen ( ) ) await this . listener . close ( ) ;
78+ if ( this . config && this . startEachTime ) {
79+ console . log ( 'Stopping bootstrap server....' ) ;
80+ await this . service . stop ( this . config ) ;
81+ await BootstrapUtils . sleep ( 2000 ) ;
82+ }
83+ }
9384
94- const bootstrapRoot =
95- process . env . CATAPULT_SERVICE_BOOTSTRAP || path . resolve ( __dirname , '../../../../catapult-service-bootstrap' ) ;
96- const bootstrapPath = `${ bootstrapRoot } /build/generated-addresses/addresses.yaml` ;
97- fs . readFile ( bootstrapPath , ( error : any , yamlData : any ) => {
98- if ( error ) {
99- console . log (
100- `catapult-service-bootstrap generated address could not be loaded from path ${ bootstrapPath } . Ignoring and using accounts from network.conf.` ,
101- ) ;
102- return resolve ( this ) ;
103- } else {
104- console . log ( `catapult-service-bootstrap generated address loaded from path ${ bootstrapPath } .` ) ;
105- const parsedYaml = yaml . safeLoad ( yamlData ) ;
106- this . account = this . createAccount ( parsedYaml . nemesis_addresses [ 0 ] ) ;
107- this . account2 = this . createAccount ( parsedYaml . nemesis_addresses [ 1 ] ) ;
108- this . account3 = this . createAccount ( parsedYaml . nemesis_addresses [ 2 ] ) ;
109- this . multisigAccount = this . createAccount ( parsedYaml . nemesis_addresses [ 3 ] ) ;
110- this . cosignAccount1 = this . createAccount ( parsedYaml . nemesis_addresses [ 4 ] ) ;
111- this . cosignAccount2 = this . createAccount ( parsedYaml . nemesis_addresses [ 5 ] ) ;
112- this . cosignAccount3 = this . createAccount ( parsedYaml . nemesis_addresses [ 6 ] ) ;
113- this . cosignAccount4 = this . createAccount ( parsedYaml . nemesis_addresses [ 7 ] ) ;
114- this . harvestingAccount = this . createAccount ( parsedYaml . nemesis_addresses_harvesting [ 0 ] ) ;
115- return resolve ( this ) ;
116- }
117- } ) ;
118- } ,
119- ( error ) => {
120- console . log ( 'There has been an error loading the configuration. ' , error ) ;
121- return reject ( error ) ;
122- } ,
123- ) ;
124- } ) ;
125- } ) ;
85+ private async connectToExternalServer ( ) : Promise < { accounts : string [ ] ; apiUrl : string } > {
86+ const json = JSON . parse ( fs . readFileSync ( path . resolve ( __dirname , '../conf/network.conf' ) , 'utf8' ) ) ;
87+ const accounts = [
88+ json . testAccount . privateKey ,
89+ json . testAccount2 . privateKey ,
90+ json . testAccount3 . privateKey ,
91+ json . multisigAccount . privateKey ,
92+ json . cosignatoryAccount . privateKey ,
93+ json . cosignatory2Account . privateKey ,
94+ json . cosignatory3Account . privateKey ,
95+ json . cosignatory4Account . privateKey ,
96+ json . harvestingAccount . privateKey ,
97+ ] ;
98+ return { accounts, apiUrl : json . apiUrl } ;
12699 }
127100
128- createAccount ( data ) : Account {
129- return Account . createFromPrivateKey ( data . privateKey ? data . privateKey : data . private , this . networkType ) ;
101+ async start ( { openListener } : { openListener : boolean } ) : Promise < IntegrationTestHelper > {
102+ // await this.service.stop(this.config);
103+ const config = await this . startBootstrapServer ( ) ;
104+ const accounts = config . accounts ;
105+ this . apiUrl = config . apiUrl ;
106+ this . repositoryFactory = new RepositoryFactoryHttp ( this . apiUrl ) ;
107+ this . transactionService = new TransactionService (
108+ this . repositoryFactory . createTransactionRepository ( ) ,
109+ this . repositoryFactory . createReceiptRepository ( ) ,
110+ ) ;
111+
112+ this . networkType = await this . repositoryFactory . getNetworkType ( ) . toPromise ( ) ;
113+ this . generationHash = await this . repositoryFactory . getGenerationHash ( ) . toPromise ( ) ;
114+
115+ let index = 0 ;
116+ this . account = Account . createFromPrivateKey ( accounts [ index ++ ] , this . networkType ) ;
117+ this . account2 = Account . createFromPrivateKey ( accounts [ index ++ ] , this . networkType ) ;
118+ this . account3 = Account . createFromPrivateKey ( accounts [ index ++ ] , this . networkType ) ;
119+ this . multisigAccount = Account . createFromPrivateKey ( accounts [ index ++ ] , this . networkType ) ;
120+ this . cosignAccount1 = Account . createFromPrivateKey ( accounts [ index ++ ] , this . networkType ) ;
121+ this . cosignAccount2 = Account . createFromPrivateKey ( accounts [ index ++ ] , this . networkType ) ;
122+ this . cosignAccount3 = Account . createFromPrivateKey ( accounts [ index ++ ] , this . networkType ) ;
123+ this . cosignAccount4 = Account . createFromPrivateKey ( accounts [ index ++ ] , this . networkType ) ;
124+ this . harvestingAccount = Account . createFromPrivateKey ( accounts [ index ++ ] , this . networkType ) ;
125+
126+ this . listener = this . repositoryFactory . createListener ( ) ;
127+
128+ // What would be the best maxFee? In the future we will load the fee multiplier from rest.
129+ this . maxFee = UInt64 . fromUint ( 1000000 ) ;
130+ this . networkCurrencyNamespaceId = this . apiUrl . toLowerCase ( ) . includes ( 'localhost' )
131+ ? NetworkCurrencyLocal . NAMESPACE_ID
132+ : NetworkCurrencyPublic . NAMESPACE_ID ;
133+ this . networkCurrencyDivisibility = this . apiUrl . toLowerCase ( ) . includes ( 'localhost' )
134+ ? NetworkCurrencyLocal . DIVISIBILITY
135+ : NetworkCurrencyPublic . DIVISIBILITY ;
136+
137+ if ( openListener ) {
138+ await this . listener . open ( ) ;
139+ }
140+ return this ;
130141 }
131142
132143 createNetworkCurrency ( amount : number , isRelative = true ) : NetworkCurrencyPublic | NetworkCurrencyLocal {
0 commit comments