Example #1
0
        public async Task ActivateAccount()
        {
            var nonce      = new byte[] { 0x00, 0x01, 0x02, 0x03 };
            var privateKey = new PrivateKey();

            (ActivationKey activationKey, PendingActivationState pendingActivation) =
                ActivationKey.Create(privateKey, nonce);
            PolymorphicAction <ActionBase> action = new CreatePendingActivation(pendingActivation);

            BlockChain.MakeTransaction(AdminPrivateKey, new[] { action });
            await BlockChain.MineBlock(AdminAddress);

            var encodedActivationKey = activationKey.Encode();
            var queryResult          = await ExecuteQueryAsync(
                $"mutation {{ activationStatus {{ activateAccount(encodedActivationKey: \"{encodedActivationKey}\") }} }}");

            await BlockChain.MineBlock(AdminAddress);

            var result = (bool)queryResult.Data
                         .As <Dictionary <string, object> >()["activationStatus"]
                         .As <Dictionary <string, object> >()["activateAccount"];

            Assert.True(result);

            var state = (Bencodex.Types.Dictionary)BlockChain.GetState(
                ActivatedAccountsState.Address);
            var     activatedAccountsState = new ActivatedAccountsState(state);
            Address userAddress            = StandaloneContextFx.NineChroniclesNodeService !.MinerPrivateKey !.ToAddress();

            Assert.True(activatedAccountsState.Accounts.Contains(userAddress));
        }
        public void CheckPermission()
        {
            var nonce  = new byte[] { 0x00, 0x01, 0x02, 0x03 };
            var pubKey = new PublicKey(
                ByteUtil.ParseHex("02ed49dbe0f2c34d9dff8335d6dd9097f7a3ef17dfb5f048382eebc7f451a50aa1")
                );
            var pendingActivation = new PendingActivationState(nonce, pubKey);
            var action            = new CreatePendingActivation(pendingActivation);
            var adminAddress      = new Address("399bddF9F7B6d902ea27037B907B2486C9910730");
            var adminState        = new AdminState(adminAddress, 100);
            var state             = new State(ImmutableDictionary <Address, IValue> .Empty
                                              .Add(AdminState.Address, adminState.Serialize())
                                              );

            Assert.Throws <PolicyExpiredException>(
                () => action.Execute(new ActionContext()
            {
                BlockIndex     = 101,
                PreviousStates = state,
                Signer         = adminAddress,
            })
                );

            Assert.Throws <PermissionDeniedException>(
                () => action.Execute(new ActionContext()
            {
                BlockIndex     = 1,
                PreviousStates = state,
                Signer         = default,
        public void Execute()
        {
            var nonce  = new byte[] { 0x00, 0x01, 0x02, 0x03 };
            var pubKey = new PublicKey(
                ByteUtil.ParseHex("02ed49dbe0f2c34d9dff8335d6dd9097f7a3ef17dfb5f048382eebc7f451a50aa1")
                );
            var pendingActivation = new PendingActivationState(nonce, pubKey);
            var action            = new CreatePendingActivation(pendingActivation);
            var adminAddress      = new Address("399bddF9F7B6d902ea27037B907B2486C9910730");
            var adminState        = new AdminState(adminAddress, 100);
            var state             = new State(ImmutableDictionary <Address, IValue> .Empty
                                              .Add(AdminState.Address, adminState.Serialize())
                                              );
            var actionContext = new ActionContext()
            {
                BlockIndex     = 1,
                PreviousStates = state,
                Signer         = adminAddress,
            };

            var nextState = action.Execute(actionContext);

            Assert.Equal(
                pendingActivation.Serialize(),
                nextState.GetState(pendingActivation.address)
                );
        }
        public async Task ActivateAccount()
        {
            var adminPrivateKey = new PrivateKey();
            var adminAddress = adminPrivateKey.ToAddress();
            var activateAccounts = new[] { adminAddress }.ToImmutableHashSet();

            Block <PolymorphicAction <ActionBase> > genesis =
                MakeGenesisBlock(adminAddress, new Currency("NCG", 2, minters: null), activateAccounts);
            NineChroniclesNodeService service = ServiceBuilder.CreateNineChroniclesNodeService(genesis);

            StandaloneContextFx.NineChroniclesNodeService = service;
            StandaloneContextFx.BlockChain = service.Swarm.BlockChain;

            var blockChain = StandaloneContextFx.BlockChain;

            var nonce      = new byte[] { 0x00, 0x01, 0x02, 0x03 };
            var privateKey = new PrivateKey();

            (ActivationKey activationKey, PendingActivationState pendingActivation) =
                ActivationKey.Create(privateKey, nonce);
            PolymorphicAction <ActionBase> action = new CreatePendingActivation(pendingActivation);

            blockChain.MakeTransaction(adminPrivateKey, new[] { action });
            await blockChain.MineBlock(adminAddress);

            var encodedActivationKey = activationKey.Encode();
            var queryResult          = await ExecuteQueryAsync(
                $"mutation {{ activationStatus {{ activateAccount(encodedActivationKey: \"{encodedActivationKey}\") }} }}");

            await blockChain.MineBlock(adminAddress);

            var result = (bool)queryResult.Data
                         .As <Dictionary <string, object> >()["activationStatus"]
                         .As <Dictionary <string, object> >()["activateAccount"];

            Assert.True(result);

            var state = (Bencodex.Types.Dictionary)blockChain.GetState(
                ActivatedAccountsState.Address);
            var     activatedAccountsState = new ActivatedAccountsState(state);
            Address userAddress            = service.PrivateKey.ToAddress();

            Assert.True(activatedAccountsState.Accounts.Contains(userAddress));
        }
        public async Task ActivationStatus(bool existsActivatedAccounts)
        {
            var adminPrivateKey   = new PrivateKey();
            var adminAddress      = adminPrivateKey.ToAddress();
            var activatedAccounts = ImmutableHashSet <Address> .Empty;

            if (existsActivatedAccounts)
            {
                activatedAccounts = new[] { adminAddress }.ToImmutableHashSet();
            }

            Block <PolymorphicAction <ActionBase> > genesis =
                BlockChain <PolymorphicAction <ActionBase> > .MakeGenesisBlock(
                    new PolymorphicAction <ActionBase>[]
            {
                new InitializeStates(
                    rankingState: new RankingState(),
                    shopState: new ShopState(),
                    gameConfigState: new GameConfigState(),
                    redeemCodeState: new RedeemCodeState(Bencodex.Types.Dictionary.Empty
                                                         .Add("address", RedeemCodeState.Address.Serialize())
                                                         .Add("map", Bencodex.Types.Dictionary.Empty)
                                                         ),
                    adminAddressState: new AdminState(adminAddress, 1500000),
                    activatedAccountsState: new ActivatedAccountsState(activatedAccounts),
                    goldCurrencyState: new GoldCurrencyState(new Currency("NCG", 2, minter: null)),
                    goldDistributions: new GoldDistribution[0],
                    tableSheets: _sheets,
                    pendingActivationStates: new PendingActivationState[] { }
                    ),
            }
                    );

            var apvPrivateKey  = new PrivateKey();
            var apv            = AppProtocolVersion.Sign(apvPrivateKey, 0);
            var userPrivateKey = new PrivateKey();
            var properties     = new LibplanetNodeServiceProperties <PolymorphicAction <ActionBase> >
            {
                Host = System.Net.IPAddress.Loopback.ToString(),
                AppProtocolVersion   = apv,
                GenesisBlock         = genesis,
                StorePath            = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()),
                StoreStatesCacheSize = 2,
                SwarmPrivateKey      = new PrivateKey(),
                Port = null,
                MinimumDifficulty = 4096,
                NoMiner           = true,
                Render            = false,
                Peers             = ImmutableHashSet <Peer> .Empty,
                TrustedAppProtocolVersionSigners = null,
                StaticPeers = ImmutableHashSet <Peer> .Empty
            };

            var service = new NineChroniclesNodeService(userPrivateKey, properties, null);

            StandaloneContextFx.NineChroniclesNodeService = service;
            StandaloneContextFx.BlockChain = service.Swarm?.BlockChain;

            var blockChain = StandaloneContextFx.BlockChain !;

            var queryResult = await ExecuteQueryAsync("query { activationStatus { activated } }");

            var result = (bool)queryResult.Data
                         .As <Dictionary <string, object> >()["activationStatus"]
                         .As <Dictionary <string, object> >()["activated"];

            // ActivatedAccounts가 비어있을때는 true이고 하나라도 있을경우 false
            Assert.Equal(!existsActivatedAccounts, result);

            var nonce      = new byte[] { 0x00, 0x01, 0x02, 0x03 };
            var privateKey = new PrivateKey();

            (ActivationKey activationKey, PendingActivationState pendingActivation) =
                ActivationKey.Create(privateKey, nonce);
            PolymorphicAction <ActionBase> action = new CreatePendingActivation(pendingActivation);

            blockChain.MakeTransaction(adminPrivateKey, new[] { action });
            await blockChain.MineBlock(adminAddress);

            action = activationKey.CreateActivateAccount(nonce);
            blockChain.MakeTransaction(userPrivateKey, new[] { action });
            await blockChain.MineBlock(adminAddress);

            queryResult = await ExecuteQueryAsync("query { activationStatus { activated } }");

            result = (bool)queryResult.Data
                     .As <Dictionary <string, object> >()["activationStatus"]
                     .As <Dictionary <string, object> >()["activated"];

            // ActivatedAccounts에 Address가 추가 되었기 때문에 true
            Assert.True(result);
        }
        public async Task ActivateAccount()
        {
            var adminPrivateKey = new PrivateKey();
            var adminAddress = adminPrivateKey.ToAddress();
            var activateAccounts = new[] { adminAddress }.ToImmutableHashSet();

            Block <PolymorphicAction <ActionBase> > genesis =
                BlockChain <PolymorphicAction <ActionBase> > .MakeGenesisBlock(
                    new PolymorphicAction <ActionBase>[]
            {
                new InitializeStates()
                {
                    RankingState     = new RankingState(),
                    ShopState        = new ShopState(),
                    TableSheetsState = new TableSheetsState(),
                    GameConfigState  = new GameConfigState(),
                    RedeemCodeState  = new RedeemCodeState(Bencodex.Types.Dictionary.Empty
                                                           .Add("address", RedeemCodeState.Address.Serialize())
                                                           .Add("map", Bencodex.Types.Dictionary.Empty)
                                                           ),
                    AdminAddressState      = new AdminState(adminAddress, 1500000),
                    ActivatedAccountsState = new ActivatedAccountsState(activateAccounts),
                    GoldCurrencyState      = new GoldCurrencyState(new Currency("NCG", minter: null)),
                    GoldDistributions      = new GoldDistribution[0],
                },
            }
                    );

            var apvPrivateKey = new PrivateKey();
            var apv            = AppProtocolVersion.Sign(apvPrivateKey, 0);
            var userPrivateKey = new PrivateKey();
            var properties     = new LibplanetNodeServiceProperties <PolymorphicAction <ActionBase> >
            {
                Host = System.Net.IPAddress.Loopback.ToString(),
                AppProtocolVersion   = apv,
                GenesisBlock         = genesis,
                StorePath            = null,
                StoreStatesCacheSize = 2,
                PrivateKey           = userPrivateKey,
                Port = null,
                MinimumDifficulty = 4096,
                NoMiner           = true,
                Render            = false,
                Peers             = ImmutableHashSet <Peer> .Empty,
                TrustedAppProtocolVersionSigners = null,
            };

            var service = new NineChroniclesNodeService(properties, null);

            service.PrivateKey = userPrivateKey;
            StandaloneContextFx.NineChroniclesNodeService = service;
            StandaloneContextFx.BlockChain = service.Swarm.BlockChain;

            var blockChain = StandaloneContextFx.BlockChain;

            var nonce      = new byte[] { 0x00, 0x01, 0x02, 0x03 };
            var privateKey = new PrivateKey();

            (ActivationKey activationKey, PendingActivationState pendingActivation) =
                ActivationKey.Create(privateKey, nonce);
            PolymorphicAction <ActionBase> action = new CreatePendingActivation(pendingActivation);

            blockChain.MakeTransaction(adminPrivateKey, new[] { action });
            await blockChain.MineBlock(adminAddress);

            var encodedActivationKey = activationKey.Encode();
            var queryResult          = await ExecuteQueryAsync(
                $"mutation {{ activationStatus {{ activateAccount(encodedActivationKey: \"{encodedActivationKey}\") }} }}");

            await blockChain.MineBlock(adminAddress);

            var result = (bool)queryResult.Data
                         .As <Dictionary <string, object> >()["activationStatus"]
                         .As <Dictionary <string, object> >()["activateAccount"];

            Assert.True(result);

            var state = (Bencodex.Types.Dictionary)blockChain.GetState(
                ActivatedAccountsState.Address);
            var activatedAccountsState = new ActivatedAccountsState(state);
            var userAddress            = userPrivateKey.ToAddress();

            Assert.True(activatedAccountsState.Accounts.Contains(userAddress));
        }