Esempio n. 1
0
        public void Constructor()
        {
            Address address    = new PrivateKey().PublicKey.ToAddress();
            var     evaluation = new ActionEvaluation <DumbAction>(
                new DumbAction(address, "item"),
                new ActionContext(
                    address,
                    address,
                    1,
                    new AccountStateDeltaImpl(_ => null),
                    123,
                    false
                    ),
                new AccountStateDeltaImpl(
                    a => a.Equals(address) ? "item" : null
                    )
                );

            Assert.Equal(address, evaluation.Action.TargetAddress);
            Assert.Equal("item", evaluation.Action.Item);
            Assert.Equal(address, evaluation.InputContext.Signer);
            Assert.Equal(address, evaluation.InputContext.Miner);
            Assert.Equal(1, evaluation.InputContext.BlockIndex);
            Assert.Null(
                evaluation.InputContext.PreviousStates.GetState(address)
                );
            Assert.Equal(
                "item",
                evaluation.OutputStates.GetState(address)
                );
        }
Esempio n. 2
0
        internal ActionEvaluation EvaluateBlockAction(
            Block <T> block,
            IReadOnlyList <ActionEvaluation> txActionEvaluations,
            StateCompleterSet <T> stateCompleters,
            ITrie?previousBlockStatesTrie
            )
        {
            if (block == null)
            {
                throw new ArgumentNullException(nameof(block));
            }

            if (_blockAction is null)
            {
                var message = "To evaluate block action, Policy.BlockAction must not be null.";
                throw new InvalidOperationException(message);
            }

            _logger.Debug(
                "Evaluating block action in block {blockIndex}: {block}", block?.Index, block);

            IAccountStateDelta?lastStates = null;

            if (!(txActionEvaluations is null) && txActionEvaluations.Count > 0)
            {
                lastStates = txActionEvaluations[txActionEvaluations.Count - 1].OutputStates;
            }

            Address miner = block !.Miner.GetValueOrDefault();

            if (lastStates is null)
            {
                IValue?GetState(Address a) =>
                _stateGetter(a, block.PreviousHash, stateCompleters.StateCompleter);

                FungibleAssetValue GetBalance(Address address, Currency currency) =>
                _balanceGetter(
                    address,
                    currency,
                    block.PreviousHash,
                    stateCompleters.FungibleAssetStateCompleter
                    );

                lastStates = block.ProtocolVersion > 0
                    ? new AccountStateDeltaImpl(GetState, GetBalance, miner)
                    : new AccountStateDeltaImplV0(GetState, GetBalance, miner);
            }

            return(ActionEvaluation.EvaluateActionsGradually(
                       block.PreEvaluationHash,
                       block.Index,
                       null,
                       lastStates,
                       miner,
                       miner,
                       Array.Empty <byte>(),
                       new[] { _blockAction }.ToImmutableList(),
                       previousBlockStatesTrie: previousBlockStatesTrie,
                       blockAction: true).First());
        }
Esempio n. 3
0
        internal ActionEvaluation EvaluateBlockAction(
            Block <T> block,
            IReadOnlyList <ActionEvaluation> txActionEvaluations)
        {
            if (Policy.BlockAction is null)
            {
                var message = "To evaluate block action, Policy.BlockAction must not be null.";
                throw new InvalidOperationException(message);
            }

            IAccountStateDelta lastStates = null;

            if (!(txActionEvaluations is null) && txActionEvaluations.Count > 0)
            {
                lastStates = txActionEvaluations[txActionEvaluations.Count - 1].OutputStates;
            }

            Address miner = block.Miner.GetValueOrDefault();

            if (lastStates is null)
            {
                lastStates = new AccountStateDeltaImpl(
                    a => GetStates(new[] { a }, block.PreviousHash).GetValueOrDefault(a));
            }

            return(ActionEvaluation.EvaluateActionsGradually(
                       block.Hash,
                       block.Index,
                       null,
                       lastStates,
                       miner,
                       miner,
                       Array.Empty <byte>(),
                       new[] { Policy.BlockAction }.ToImmutableList()).First());
        }
Esempio n. 4
0
        public bool DoAction()
        {
            LinkedList <ActionType> actions = new LinkedList <ActionType>();

            foreach (var position in Body.CurrentPositions)
            {
                if (position == null)
                {
                    continue;
                }
                actions.AddRange(FindPossibleActions(position));
            }

            var actionsToExecute = actions.Where(a => ActionEvaluation.Evaluate(a) >= 0);

            actionsToExecute = actionsToExecute.OrderByDescending(a => ActionEvaluation.Evaluate(a)).Take(Math.Min(actionsToExecute.Count(), Size));

            if (!actions.Any())
            {
                return(false);
            }

            Body.ExecuteActions(actionsToExecute);
            return(true);
        }
Esempio n. 5
0
        public bool DoAction()
        {
            var actions = FindPossibleActions(Body.CurrentPosition);

            if (!actions.Any())
            {
                return(false);
            }
            var actionToExecute = Utilities.MaxEntry(actions, a => ActionEvaluation.Evaluate(a));

            if (ActionEvaluation.Evaluate(actionToExecute) < 0)
            {
                return(false);
            }
            Body.ExecuteAction(actionToExecute);
            return(true);
        }
Esempio n. 6
0
 EvaluateActionsGradually(
     HashDigest <SHA256> blockHash,
     long blockIndex,
     IAccountStateDelta previousStates,
     Address minerAddress,
     bool rehearsal = false
     )
 {
     return(ActionEvaluation.EvaluateActionsGradually(
                blockHash,
                blockIndex,
                previousStates,
                minerAddress,
                Signer,
                Signature,
                Actions.Cast <IAction>().ToImmutableList(),
                rehearsal));
 }
Esempio n. 7
0
        public void Constructor()
        {
            var     txid       = new System.Random().NextTxId();
            Address address    = new PrivateKey().ToAddress();
            var     evaluation = new ActionEvaluation(
                new DumbAction(address, "item"),
                new ActionContext(
                    address,
                    txid,
                    address,
                    1,
                    new AccountStateDeltaImpl(
                        addrs => new IValue[addrs.Count],
                        (_, c) => new FungibleAssetValue(c),
                        address
                        ),
                    123,
                    false
                    ),
                new AccountStateDeltaImpl(
                    addrs => addrs
                    .Select(a => a.Equals(address) ? (Text)"item" : (IValue)null)
                    .ToArray(),
                    (_, c) => new FungibleAssetValue(c),
                    address
                    )
                );
            var action = (DumbAction)evaluation.Action;

            Assert.Equal(address, action.TargetAddress);
            Assert.Equal("item", action.Item);
            Assert.Equal(address, evaluation.InputContext.Signer);
            Assert.Equal(txid, evaluation.InputContext.TxId);
            Assert.Equal(address, evaluation.InputContext.Miner);
            Assert.Equal(1, evaluation.InputContext.BlockIndex);
            Assert.Null(
                evaluation.InputContext.PreviousStates.GetState(address)
                );
            Assert.Equal(
                (Text)"item",
                evaluation.OutputStates.GetState(address)
                );
        }
Esempio n. 8
0
        public void Constructor()
        {
            Address address    = new PrivateKey().ToAddress();
            var     evaluation = new ActionEvaluation(
                new DumbAction(address, "item"),
                new ActionContext(
                    address,
                    address,
                    1,
                    new AccountStateDeltaImpl(
                        _ => null,
                        (_, c) => new FungibleAssetValue(c),
                        address
                        ),
                    123,
                    false
                    ),
                new AccountStateDeltaImpl(
                    a => a.Equals(address) ? (Text)"item" : null,
                    (_, c) => new FungibleAssetValue(c),
                    address
                    )
                );
            var action = (DumbAction)evaluation.Action;

            Assert.Equal(address, action.TargetAddress);
            Assert.Equal("item", action.Item);
            Assert.Equal(address, evaluation.InputContext.Signer);
            Assert.Equal(address, evaluation.InputContext.Miner);
            Assert.Equal(1, evaluation.InputContext.BlockIndex);
            Assert.Null(
                evaluation.InputContext.PreviousStates.GetState(address)
                );
            Assert.Equal(
                (Text)"item",
                evaluation.OutputStates.GetState(address)
                );
        }