Skip to content

Instantly share code, notes, and snippets.

@alanshaw
Created September 19, 2023 18:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alanshaw/3ad4295ae7288cc767b8b32870540aa4 to your computer and use it in GitHub Desktop.
Save alanshaw/3ad4295ae7288cc767b8b32870540aa4 to your computer and use it in GitHub Desktop.
How fx invocations work

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment