我在 CordaService 中有一个oracle,其中 fun signfun checkFactIsCorrect() 在签名之前检查现实世界中的事实,即证券交易所/ offledger数据库 .

  • 所有节点都有启动器 RequestOracleSig flow .

  • Oracle有响应者 RespondWithOracleSig

在使用mockNetwork的JUnit中,如何模拟 InitiatedBy 流中 CordaServicefun checkFactIsCorrect() ?我不想模拟整个响应者流程,而只是模拟从某个任意源检查事实的特定函数 .

下面是一些示例代码:

@IniatedBy(RequestOracleSig::class.java)
class RespondWithOracleSig {
    some code........
    val signature = try {
            serviceHub.cordaService(OracleService.FactValidator::class.java).sign(ftx)
            } catch (e: Exception) {
                throw FlowException(e)
            }
}

在Oracle服务中:

fun sign(ftx: FilteredTransaction): TransactionSignature {
            // Is the partial Merkle tree valid?
            ftx.verify()

            fun commandValidator(elem: Any) = when {
                elem is Command<*> && elem.value is DummyContract.Commands.Buy -> {
                    myKey in elem.signers
                            && checkFactIsCorrect(elem.value.fact)
                }
                else -> false
            }

            // Is it a Merkle tree we are willing to sign over?
            val isValidMerkleTree = Try.on {
                require(ftx.checkWithFun(::commandValidator))
                ftx.checkCommandVisibility(services.myInfo.legalIdentities.first().owningKey)
            }


            return if (isValidMerkleTree.isSuccess) {
                services.createSignature(ftx, myKey)
            } else {
                throw IllegalArgumentException("Oracle signature requested over invalid transaction.")
            }
        }