Want to do an async task of adding two number together? Not problem.
invoke do/async-addition 40, 2
# create a delegation for the result and get the CID
result-cid = cid-for(delegate(do/async-addition-result 40, 2))
# create a receipt for this invocation, including the CID of the result delegation
receipt = generate-receipt-for(do/async-addition 40, 2, fx.join: result-cid)
# return the receipt to the caller
return receipt
So now the invoker has obtained a CID to a delegation for the result of 40 + 2 (result-cid
), it came back in the receipt from the invocation.
Eventually the task runs:
run add_numbers(40, 2)
# compute the result
answer = 40 + 2
# regenerate the result delegation.
# because content addressing, it will have the SAME CID as the result-cid
# that was sent back in the receipt of the original invocation!
result = delegate(do/async-addition-result 40, 2)
# create a receipt for the result, including the answer
receipt = generate-receipt-for(result, answer: answer)
# stash the receipt, keyed by the result delegation CID
store-put(cid-for(result), receipt)
The receipt is stored in the service, by the CID of the result delegation. So the invoker can poll the service, providing the CID of a delegation and obtain the receipt.
We haven't built this bit yet but something like:
invoke receipt/get bafydelegation
data = store-get(bafydelegation)
receipt = generate-receipt-for(receipt/get bafydelegation, receipt: data)
return receipt