Closed dahlia closed 4 years ago
At first I thought to add the interface like below.
Task<Block<T>> Blockchain.MineBlockAsync(Address rewardBenficiary);
IEnumerator Blockchain.MineBlock(Address rewardBeneficiary, out Block<T> block);
// or
IEnumerator Blockchain.MineBlock(Address rewardBeneficiary, Action<Block<T>> onMined);
But there is some points to think about... (If I know something wrong, please comment.)
async
/ await
. but there is no support like MonoBehaviour.StopCoroutine()
.IEnumerator Blockchain.MineBlock()
fits Unity's coroutine idiom perfectly. but making concurrency in this way seems to be Unity's style, not usual .NET style.
WaitForSeconds
.We can still add an async
/ await
style API, but it does not come up with any good utility right now.
Instead I suggest workaround for Unity like below
public IEnumerator Mine(Address address)
{
while (true)
{
var task = Task.Run(() => blocks.MineBlock(address));
yield return new WaitUntil(() => task.IsCompleted);
Debug.Log($"mined {task.Result.Index}");
}
}
private void Awake()
{
StartCoroutine(Mine(UserAddress));
}
cc
@ipdae @kijun
I guess #517 closes this issue.
In Unity Engine the current block mining API blocks (serializes the flow) so that the event loop cannot switch the context to other cooperative tasks. We should prevent it by providing an
async
interface ofBlockchain.MineBlock()
.