コード例 #1
0
        // Copied from BlockChain<T>.SetStates().
        private static void SetStates(
            Guid chainId,
            IStateStore stateStore,
            Block <NCAction> block,
            IReadOnlyList <ActionEvaluation> actionEvaluations,
            bool buildStateReferences
            )
        {
            IImmutableSet <Address> stateUpdatedAddresses = actionEvaluations
                                                            .SelectMany(a => a.OutputStates.StateUpdatedAddresses)
                                                            .ToImmutableHashSet();
            IImmutableSet <(Address, Currency)> updatedFungibleAssets = actionEvaluations
                                                                        .SelectMany(a => a.OutputStates.UpdatedFungibleAssets
                                                                                    .SelectMany(kv => kv.Value.Select(c => (kv.Key, c))))
                                                                        .ToImmutableHashSet();

            if (!stateStore.ContainsBlockStates(block.Hash))
            {
                var totalDelta = GetTotalDelta(actionEvaluations, ToStateKey, ToFungibleAssetKey);
                stateStore.SetStates(block, totalDelta);
            }

            if (buildStateReferences && stateStore is IBlockStatesStore blockStatesStore)
            {
                IImmutableSet <string> stateUpdatedKeys = stateUpdatedAddresses
                                                          .Select(ToStateKey)
                                                          .ToImmutableHashSet();
                IImmutableSet <string> assetUpdatedKeys = updatedFungibleAssets
                                                          .Select(ToFungibleAssetKey)
                                                          .ToImmutableHashSet();
                IImmutableSet <string> updatedKeys = stateUpdatedKeys.Union(assetUpdatedKeys);
                blockStatesStore.StoreStateReference(chainId, updatedKeys, block.Hash, block.Index);
            }
        }
コード例 #2
0
 public void SetStates <T>(
     Block <T> blockHash,
     IImmutableDictionary <string, IValue> states)
     where T : IAction, new()
 {
     Log(nameof(SetStates), blockHash, states);
     _stateStore.SetStates(blockHash, states);
 }
コード例 #3
0
ファイル: TestUtils.cs プロジェクト: Chaleeshadow/libplanet
        public static Block <T> AttachStateRootHash <T>(
            this Block <T> block, IStateStore stateStore, IAction blockAction)
            where T : IAction, new()
        {
            IValue StateGetter(
                Address address, HashDigest <SHA256>?blockHash, StateCompleter <T> stateCompleter) =>
            blockHash is null
                    ? null
                    : stateStore.GetState(ToStateKey(address), blockHash.Value);

            FungibleAssetValue FungibleAssetValueGetter(
                Address address,
                Currency currency,
                HashDigest <SHA256>?blockHash,
                FungibleAssetStateCompleter <T> stateCompleter)
            {
                if (blockHash is null)
                {
                    return(FungibleAssetValue.FromRawValue(currency, 0));
                }

                IValue value = stateStore.GetState(
                    ToFungibleAssetKey(address, currency), blockHash.Value);

                return(FungibleAssetValue.FromRawValue(
                           currency,
                           value is Bencodex.Types.Integer i ? i.Value : 0));
            }

            var blockEvaluator = new BlockEvaluator <T>(
                blockAction, StateGetter, FungibleAssetValueGetter, null);
            var actionEvaluationResult = blockEvaluator
                                         .EvaluateActions(block, StateCompleterSet <T> .Reject)
                                         .GetTotalDelta(ToStateKey, ToFungibleAssetKey);

            stateStore.SetStates(block, actionEvaluationResult);
            if (stateStore is TrieStateStore trieStateStore)
            {
                block = new Block <T>(block, trieStateStore.GetRootHash(block.Hash));
                stateStore.SetStates(block, actionEvaluationResult);
            }

            return(block);
        }