2020
2121import blockchains .iaas .uni .stuttgart .de .adaptation .AdapterManager ;
2222import blockchains .iaas .uni .stuttgart .de .api .exceptions .*;
23+ import blockchains .iaas .uni .stuttgart .de .api .model .*;
2324import blockchains .iaas .uni .stuttgart .de .management .callback .CallbackManager ;
2425import blockchains .iaas .uni .stuttgart .de .management .callback .CamundaMessageTranslator ;
2526import blockchains .iaas .uni .stuttgart .de .management .callback .ScipMessageTranslator ;
3132import blockchains .iaas .uni .stuttgart .de .management .model .Subscription ;
3233import blockchains .iaas .uni .stuttgart .de .management .model .SubscriptionKey ;
3334import blockchains .iaas .uni .stuttgart .de .management .model .SubscriptionType ;
34- import blockchains .iaas .uni .stuttgart .de .api .model .Parameter ;
35- import blockchains .iaas .uni .stuttgart .de .api .model .QueryResult ;
36- import blockchains .iaas .uni .stuttgart .de .api .model .TimeFrame ;
37- import blockchains .iaas .uni .stuttgart .de .api .model .Transaction ;
38- import blockchains .iaas .uni .stuttgart .de .api .model .TransactionState ;
3935import com .google .common .base .Strings ;
36+ import io .reactivex .Observable ;
4037import io .reactivex .disposables .Disposable ;
4138import org .slf4j .Logger ;
4239import org .slf4j .LoggerFactory ;
@@ -366,42 +363,33 @@ public void invokeSmartContractFunction(
366363 final String correlationId ,
367364 final String signature ) throws BalException {
368365
369- // Validate scip parameters!
370- if (Strings .isNullOrEmpty (blockchainIdentifier )
371- || Strings .isNullOrEmpty (smartContractPath )
372- || Strings .isNullOrEmpty (functionIdentifier )
373- || timeoutMillis < 0
374- || MathUtils .doubleCompare (requiredConfidence , 0.0 ) < 0
375- || MathUtils .doubleCompare (requiredConfidence , 100.0 ) > 0 ) {
376- throw new InvalidScipParameterException ();
377- }
378-
379- final AdapterManager adapterManager = AdapterManager .getInstance ();
380- final double minimumConfidenceAsProbability = requiredConfidence / 100.0 ;
381- final BlockchainAdapter adapter = adapterManager .getAdapter (blockchainIdentifier );
382- final CompletableFuture <Transaction > future = adapter .invokeSmartContract (smartContractPath ,
383- functionIdentifier , inputs , outputs , minimumConfidenceAsProbability , timeoutMillis );
366+ final CompletableFuture <Transaction > future = this .invokeSmartContractFunction (blockchainIdentifier , smartContractPath ,
367+ functionIdentifier , inputs , outputs , requiredConfidence , timeoutMillis , signature );
384368
385369 future .
386370 thenAccept (tx -> {
387371 if (tx != null ) {
388- if (tx .getState () == TransactionState .CONFIRMED || tx .getState () == TransactionState .RETURN_VALUE ) {
389- CallbackManager .getInstance ().sendCallback (callbackUrl ,
390- ScipMessageTranslator .getInvocationResponseMessage (
391- correlationId ,
392- tx .getReturnValues ()));
393- } else {// it is NOT_FOUND (it was dropped from the system due to invalidation) or ERRORED
394- if (tx .getState () == TransactionState .NOT_FOUND ) {
395- CallbackManager .getInstance ().sendCallback (callbackUrl ,
396- ScipMessageTranslator .getAsynchronousErrorResponseMessage (
397- correlationId ,
398- new TransactionNotFoundException ("The transaction associated with a function invocation is invalidated after it was mined." )));
399- } else {
372+ if (callbackUrl != null ) {
373+ if (tx .getState () == TransactionState .CONFIRMED || tx .getState () == TransactionState .RETURN_VALUE ) {
400374 CallbackManager .getInstance ().sendCallback (callbackUrl ,
401- ScipMessageTranslator .getAsynchronousErrorResponseMessage (
375+ ScipMessageTranslator .getInvocationResponseMessage (
402376 correlationId ,
403- new InvokeSmartContractFunctionFailure ("The smart contract function invocation reported an error." )));
377+ tx .getReturnValues ()));
378+ } else {// it is NOT_FOUND (it was dropped from the system due to invalidation) or ERRORED
379+ if (tx .getState () == TransactionState .NOT_FOUND ) {
380+ CallbackManager .getInstance ().sendCallback (callbackUrl ,
381+ ScipMessageTranslator .getAsynchronousErrorResponseMessage (
382+ correlationId ,
383+ new TransactionNotFoundException ("The transaction associated with a function invocation is invalidated after it was mined." )));
384+ } else {
385+ CallbackManager .getInstance ().sendCallback (callbackUrl ,
386+ ScipMessageTranslator .getAsynchronousErrorResponseMessage (
387+ correlationId ,
388+ new InvokeSmartContractFunctionFailure ("The smart contract function invocation reported an error." )));
389+ }
404390 }
391+ } else {
392+ log .info ("callbackUrl is null" );
405393 }
406394 } else {
407395 log .info ("resulting transaction is null" );
@@ -414,6 +402,9 @@ public void invokeSmartContractFunction(
414402 CallbackManager .getInstance ().sendCallback (callbackUrl ,
415403 ScipMessageTranslator .getAsynchronousErrorResponseMessage (correlationId , (BalException ) e .getCause ()));
416404
405+ if (e instanceof ManualUnsubscriptionException || e .getCause () instanceof ManualUnsubscriptionException ) {
406+ log .info ("Manual unsubscription of SC invocation!" );
407+ }
417408 // ManualUnsubscriptionException is also captured here
418409 return null ;
419410 }).
@@ -427,6 +418,33 @@ public void invokeSmartContractFunction(
427418 SubscriptionManager .getInstance ().createSubscription (correlationId , blockchainIdentifier , smartContractPath , subscription );
428419 }
429420
421+ public CompletableFuture <Transaction > invokeSmartContractFunction (
422+ final String blockchainIdentifier ,
423+ final String smartContractPath ,
424+ final String functionIdentifier ,
425+ final List <Parameter > inputs ,
426+ final List <Parameter > outputs ,
427+ final double requiredConfidence ,
428+ final long timeoutMillis ,
429+ final String signature ) throws BalException {
430+
431+ // Validate scip parameters!
432+ if (Strings .isNullOrEmpty (blockchainIdentifier )
433+ || Strings .isNullOrEmpty (smartContractPath )
434+ || Strings .isNullOrEmpty (functionIdentifier )
435+ || timeoutMillis < 0
436+ || MathUtils .doubleCompare (requiredConfidence , 0.0 ) < 0
437+ || MathUtils .doubleCompare (requiredConfidence , 100.0 ) > 0 ) {
438+ throw new InvalidScipParameterException ();
439+ }
440+
441+ final AdapterManager adapterManager = AdapterManager .getInstance ();
442+ final double minimumConfidenceAsProbability = requiredConfidence / 100.0 ;
443+ final BlockchainAdapter adapter = adapterManager .getAdapter (blockchainIdentifier );
444+ return adapter .invokeSmartContract (smartContractPath ,
445+ functionIdentifier , inputs , outputs , minimumConfidenceAsProbability , timeoutMillis );
446+ }
447+
430448 public void subscribeToEvent (
431449 final String blockchainIdentifier ,
432450 final String smartContractPath ,
@@ -437,21 +455,13 @@ public void subscribeToEvent(
437455 final String callbackUrl ,
438456 final String correlationIdentifier ) {
439457
440- // Validate scip parameters!
441- if (Strings .isNullOrEmpty (blockchainIdentifier )
442- || Strings .isNullOrEmpty (smartContractPath )
443- || Strings .isNullOrEmpty (eventIdentifier )
444- || MathUtils .doubleCompare (degreeOfConfidence , 0.0 ) < 0
445- || MathUtils .doubleCompare (degreeOfConfidence , 100.0 ) > 0 ) {
446- throw new InvalidScipParameterException ();
447- }
448458
449- final double minimumConfidenceAsProbability = degreeOfConfidence / 100.0 ;
450459
451460 // first, we cancel previous identical subscriptions.
452461 this .cancelEventSubscriptions (blockchainIdentifier , smartContractPath , correlationIdentifier , eventIdentifier , outputParameters );
453- Disposable result = AdapterManager .getInstance ().getAdapter (blockchainIdentifier )
454- .subscribeToEvent (smartContractPath , eventIdentifier , outputParameters , minimumConfidenceAsProbability , filter )
462+
463+
464+ Disposable result = this .subscribeToEvent (blockchainIdentifier , smartContractPath , eventIdentifier , outputParameters , degreeOfConfidence , filter )
455465 .doFinally (() -> {
456466 // remove subscription from subscription list
457467 SubscriptionManager .getInstance ().removeSubscription (correlationIdentifier , blockchainIdentifier , smartContractPath );
@@ -471,6 +481,28 @@ public void subscribeToEvent(
471481 SubscriptionManager .getInstance ().createSubscription (correlationIdentifier , blockchainIdentifier , smartContractPath , subscription );
472482 }
473483
484+ public Observable <Occurrence > subscribeToEvent (String blockchainIdentifier ,
485+ final String smartContractPath ,
486+ final String eventIdentifier ,
487+ final List <Parameter > outputParameters ,
488+ final double degreeOfConfidence ,
489+ final String filter ) {
490+ // Validate scip parameters!
491+ if (Strings .isNullOrEmpty (blockchainIdentifier )
492+ || Strings .isNullOrEmpty (smartContractPath )
493+ || Strings .isNullOrEmpty (eventIdentifier )
494+ || MathUtils .doubleCompare (degreeOfConfidence , 0.0 ) < 0
495+ || MathUtils .doubleCompare (degreeOfConfidence , 100.0 ) > 0 ) {
496+ throw new InvalidScipParameterException ();
497+ }
498+
499+ final double minimumConfidenceAsProbability = degreeOfConfidence / 100.0 ;
500+
501+ return AdapterManager .getInstance ().getAdapter (blockchainIdentifier )
502+ .subscribeToEvent (smartContractPath , eventIdentifier , outputParameters , minimumConfidenceAsProbability , filter );
503+ }
504+
505+
474506 public void cancelEventSubscriptions (String blockchainId , String smartContractId , String correlationId , String eventIdentifier , List <Parameter > parameters ) {
475507 // Validate scip parameters!
476508 if (Strings .isNullOrEmpty (blockchainId ) || Strings .isNullOrEmpty (smartContractId )) {
0 commit comments