planetarium / libplanet

Blockchain in C#/.NET for on-chain, decentralized gaming
https://docs.libplanet.io/
GNU Lesser General Public License v2.1
506 stars 142 forks source link

`BlockChain<T>.Store.GetBlockCommit()` fails #2953

Closed tkiapril closed 1 year ago

tkiapril commented 1 year ago

Unsure if this is a bug, but the following code returns null when chain.Store.GetBlockCommit(block.Hash) is called:

using System;
using System.Collections.Immutable;
using Bencodex.Types;
using Libplanet.Action;
using Libplanet.Action.Sys;
using Libplanet.Blockchain;
using Libplanet.Blockchain.Policies;
using Libplanet.Blocks;
using Libplanet.Consensus;
using Libplanet.Crypto;
using Libplanet.Store;
using Libplanet.Store.Trie;
using Xunit;

namespace Libplanet.Tests
{
    public class BlockCommitTest
    {
        [Fact]
        public void CannotGetBlockCommitFromStore()
        {
            var pk = new PrivateKey();
            var chain = BlockChain<Dum>.Create(
                new NullBlockPolicy<Dum>(),
                new VolatileStagePolicy<Dum>(),
                new MemoryStore(),
                new TrieStateStore(new MemoryKeyValueStore()),
                BlockChain<Dum>.ProposeGenesisBlock(
                    privateKey: pk,
                    systemActions: new[]
                    {
                        new SetValidator(new Validator(pk.PublicKey, 1)),
                    }));

            var block = chain.ProposeBlock(
                pk, lastCommit: chain.GetBlockCommit(chain.Tip.Index));
            var blockCommit = new BlockCommit(
                chain.Tip.Index + 1,
                0,
                block.Hash,
                ImmutableArray<Vote>.Empty
                    .Add(
                        new VoteMetadata(
                                chain.Tip.Index + 1,
                                0,
                                block.Hash,
                                DateTimeOffset.UtcNow,
                                pk.PublicKey,
                                VoteFlag.PreCommit)
                            .Sign(pk)));
            chain.Append(block, blockCommit);
            Assert.Equal(blockCommit, chain.GetBlockCommit(block.Hash));
            Assert.Equal(blockCommit, chain.Store.GetBlockCommit(block.Hash));
        }

        private class Dum : IAction
        {
            public IValue PlainValue => null;

            public void LoadPlainValue(IValue plainValue)
            {
            }

            public IAccountStateDelta Execute(IActionContext context) => context.PreviousStates;
        }
    }
}
tkiapril commented 1 year ago

It seems like this behavior is intentional, see #2894. Closed as wontfix.