Ejemplo n.º 1
0
        public async Task DoesTransactionFollowsPolicy()
        {
            var adminPrivateKey     = new PrivateKey();
            var adminAddress        = adminPrivateKey.ToAddress();
            var activatedPrivateKey = new PrivateKey();
            var activatedAddress    = activatedPrivateKey.ToAddress();

            IBlockPolicy <PolymorphicAction <ActionBase> > policy   = BlockPolicy.GetPolicy(10000);
            IRenderer <PolymorphicAction <ActionBase> >    renderer = BlockPolicy.GetRenderer();
            Block <PolymorphicAction <ActionBase> >        genesis  = MakeGenesisBlock(
                adminAddress,
                ImmutableHashSet.Create(activatedAddress).Add(adminAddress)
                );

            using var store = new DefaultStore(null);
            var blockChain = new BlockChain <PolymorphicAction <ActionBase> >(
                policy,
                store,
                store,
                genesis,
                renderers: new[] { renderer }
                );
            Transaction <PolymorphicAction <ActionBase> > txByStranger =
                Transaction <PolymorphicAction <ActionBase> > .Create(
                    0,
                    new PrivateKey(),
                    genesis.Hash,
                    new PolymorphicAction <ActionBase>[] { }
                    );

            // 새로 만든 키는 활성화 유저 리스트에 없기 때문에 차단됩니다.
            Assert.False(policy.DoesTransactionFollowsPolicy(txByStranger));

            var newActivatedPrivateKey = new PrivateKey();
            var newActivatedAddress    = newActivatedPrivateKey.ToAddress();

            // 관리자 계정으로 활성화 시킵니다.
            Transaction <PolymorphicAction <ActionBase> > invitationTx = blockChain.MakeTransaction(
                adminPrivateKey,
                new PolymorphicAction <ActionBase>[] { new AddActivatedAccount(newActivatedAddress) }
                );

            blockChain.StageTransaction(invitationTx);
            await blockChain.MineBlock(adminAddress);

            Transaction <PolymorphicAction <ActionBase> > txByNewActivated =
                Transaction <PolymorphicAction <ActionBase> > .Create(
                    0,
                    newActivatedPrivateKey,
                    genesis.Hash,
                    new PolymorphicAction <ActionBase>[] { }
                    );

            // 활성화 된 계정이기 때문에 테스트에 성공합니다.
            Assert.True(policy.DoesTransactionFollowsPolicy(txByNewActivated));
        }
Ejemplo n.º 2
0
        public void DoesTransactionFollowsPolicyWithEmpty()
        {
            var adminPrivateKey = new PrivateKey();
            var adminAddress    = new Address(adminPrivateKey.PublicKey);
            IBlockPolicy <PolymorphicAction <ActionBase> > policy  = BlockPolicy.GetPolicy(10000);
            Block <PolymorphicAction <ActionBase> >        genesis = MakeGenesisBlock(adminAddress, ImmutableHashSet <Address> .Empty);

            using var store = new DefaultStore(null);
            _ = new BlockChain <PolymorphicAction <ActionBase> >(
                policy,
                store,
                genesis
                );
            Transaction <PolymorphicAction <ActionBase> > tx = Transaction <PolymorphicAction <ActionBase> > .Create(
                0,
                new PrivateKey(),
                genesis.Hash,
                new PolymorphicAction <ActionBase>[] { });

            Assert.True(policy.DoesTransactionFollowsPolicy(tx));
        }
        public NineChroniclesNodeService(
            LibplanetNodeServiceProperties <NineChroniclesActionType> properties,
            RpcNodeServiceProperties?rpcNodeServiceProperties,
            Progress <PreloadState> preloadProgress = null,
            bool ignoreBootstrapFailure             = false
            )
        {
            Properties    = properties;
            RpcProperties = rpcNodeServiceProperties;

            try
            {
                Libplanet.Crypto.CryptoConfig.CryptoBackend = new Secp256K1CryptoBackend <SHA256>();
                Log.Debug("Secp256K1CryptoBackend initialized.");
            }
            catch (Exception e)
            {
                Log.Error("Secp256K1CryptoBackend initialize failed. Use default backend. {e}", e);
            }

            // BlockPolicy shared through Lib9c.
            IBlockPolicy <PolymorphicAction <ActionBase> > blockPolicy = BlockPolicy.GetPolicy(
                properties.MinimumDifficulty
                );

            async Task minerLoopAction(
                BlockChain <NineChroniclesActionType> chain,
                Swarm <NineChroniclesActionType> swarm,
                PrivateKey privateKey,
                CancellationToken cancellationToken)
            {
                var miner = new Miner(chain, swarm, privateKey);

                Log.Debug("Miner called.");
                while (!cancellationToken.IsCancellationRequested)
                {
                    try
                    {
                        if (swarm.Running)
                        {
                            Log.Debug("Start mining.");
                            var block = await miner.MineBlockAsync(cancellationToken);

                            const int txCountThreshold = 10;
                            var       txCount          = block?.Transactions.Count() ?? 0;
                            if (!(block is null) && txCount >= txCountThreshold)
                            {
                                Log.Error($"Block {block.Index}({block.Hash}) transaction count is {txCount}.");
                            }
                        }
                        else
                        {
                            await Task.Delay(1000, cancellationToken);
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.Error(ex, "Exception occurred.");
                    }
                }
            }

            NodeService = new LibplanetNodeService <NineChroniclesActionType>(
                Properties,
                blockPolicy,
                minerLoopAction,
                preloadProgress,
                ignoreBootstrapFailure
                );

            // FIXME: Agent.cs와 중복된 코드입니다.
            if (BlockPolicy.ActivatedAccounts is null)
            {
                var rawState = NodeService?.BlockChain?.GetState(ActivatedAccountsState.Address);
                BlockPolicy.UpdateActivationSet(rawState);
            }
        }