Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Asynchronous Blockchain.MineBlock() #29

Closed
dahlia opened this issue Jan 7, 2019 · 2 comments
Closed

Asynchronous Blockchain.MineBlock() #29

dahlia opened this issue Jan 7, 2019 · 2 comments
Assignees
Labels
bug Something isn't working suggestion Suggestion or feature request

Comments

@dahlia
Copy link
Contributor

dahlia commented Jan 7, 2019

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 of Blockchain.MineBlock().

@dahlia dahlia added bug Something isn't working suggestion Suggestion or feature request labels Jan 7, 2019
@longfin longfin self-assigned this Jan 9, 2019
@longfin
Copy link
Member

longfin commented Jan 10, 2019

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.)

  • In Unity, we can use 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.
    • Nevertheless, if implemented in libplanet.net, we need to use Unity-dependent feature like 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

@dahlia dahlia added this to the 0.3.0 milestone Apr 8, 2019
@dahlia dahlia removed this from the 0.3.0 milestone Jun 1, 2019
@limebell
Copy link
Member

I guess #517 closes this issue.

@dahlia dahlia closed this as completed Sep 27, 2019
dahlia added a commit to dahlia/libplanet that referenced this issue Mar 9, 2021
limebell pushed a commit to limebell/libplanet that referenced this issue Jul 7, 2021
OnedgeLee pushed a commit to OnedgeLee/libplanet that referenced this issue Jan 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working suggestion Suggestion or feature request
Projects
None yet
Development

No branches or pull requests

3 participants