Esempio n. 1
0
        internal async Task <long> GetParentChainHeight()
        {
            var result = await SideChainTester.CallContractMethodAsync(SideCrossChainContractAddress,
                                                                       nameof(CrossChainContractContainer.CrossChainContractStub
                                                                              .GetParentChainHeight), new Empty());

            var height = SInt64Value.Parser.ParseFrom(result);

            return(height.Value);
        }
Esempio n. 2
0
        public async Task SetBlockchainAgeTest()
        {
            const long age           = 100L;
            var        starter       = new ContractTester <DPoSContractTestAElfModule>();
            var        initialMiners = Enumerable.Range(0, 3).Select(_ => CryptoHelpers.GenerateKeyPair())
                                       .ToList();
            await starter.InitialChainAndTokenAsync(initialMiners);

            var initialMiner = starter.CreateNewContractTester(initialMiners[0]);
            await initialMiner.ExecuteConsensusContractMethodWithMiningAsync(
                nameof(ConsensusContract.SetBlockchainAge),
                new SInt64Value()
            {
                Value = age
            });

            // Starter can set blockchain age.
            {
                var blockchainAge = SInt64Value.Parser.ParseFrom(await starter.CallContractMethodAsync(
                                                                     starter.GetConsensusContractAddress(),
                                                                     nameof(ConsensusContract.GetBlockchainAge),
                                                                     new Empty())).Value;

                blockchainAge.ShouldBe(age);
            }

            var user = starter.CreateNewContractTester(CryptoHelpers.GenerateKeyPair());
            await user.SetBlockchainAgeAsync(age + 100);
        }
Esempio n. 3
0
        public async Task CallContract_Test()
        {
            var callerKeyPair = CryptoHelper.GenerateKeyPair();
            var tester        = new ContractTester <ContractTestAElfModule>(ChainId, callerKeyPair);
            await tester.InitialChainAsync(list =>
            {
                list.AddGenesisSmartContract(
                    ConsensusContractCode,
                    ConsensusSmartContractAddressNameProvider.Name);
                list.AddGenesisSmartContract(
                    TokenContractCode,
                    TokenSmartContractAddressNameProvider.Name);
            });

            var tokenContractAddress = tester.GetContractAddress(TokenSmartContractAddressNameProvider.Name);

            var bytes = await tester.CallContractMethodAsync(tokenContractAddress,
                                                             nameof(TokenContractContainer.TokenContractStub.GetBalance),
                                                             new GetBalanceInput
            {
                Symbol = "ELF",
                Owner  = tester.GetCallOwnerAddress(),
            });

            var balanceOutput = GetBalanceOutput.Parser.ParseFrom(bytes);

            Assert.Equal(0L, balanceOutput.Balance);
        }
Esempio n. 4
0
        internal async Task <AuthorityInfo> GetMethodFeeController <T>(
            ContractTester <T> tester, Address genesisContractAddress) where T : ContractTestAElfModule
        {
            var methodFeeControllerByteString = await tester.CallContractMethodAsync(genesisContractAddress,
                                                                                     nameof(BasicContractZeroContainer.BasicContractZeroStub.GetMethodFeeController), new Empty());

            return(AuthorityInfo.Parser.ParseFrom(methodFeeControllerByteString));
        }
        public static async Task <VotingRecord> GetVotingRecord(
            this ContractTester <DPoSContractTestAElfModule> contractTester, Hash txId)
        {
            var bytes = await contractTester.CallContractMethodAsync(contractTester.GetConsensusContractAddress(),
                                                                     nameof(ConsensusContract.GetVotingRecord), txId);

            return(VotingRecord.Parser.ParseFrom(bytes));
        }
Esempio n. 6
0
        public static async Task <ValidationResult> ValidateConsensusAfterExecutionAsync(
            this ContractTester <DPoSSideChainTestAElfModule> tester, DPoSHeaderInformation information)
        {
            var bytes = await tester.CallContractMethodAsync(tester.GetConsensusContractAddress(),
                                                             ConsensusConsts.ValidateConsensusAfterExecution, information);

            return(ValidationResult.Parser.ParseFrom(bytes));
        }
 public static async Task <long> GetLIBOffset(this ContractTester <DPoSContractTestAElfModule> miner)
 {
     return(SInt64Value.Parser.ParseFrom((await miner.CallContractMethodAsync(
                                              miner.GetConsensusContractAddress(),
                                              nameof(ConsensusContract.GetLIBOffset),
                                              new Empty()
                                              ))).Value);
 }
        public static async Task <Candidates> GetCandidatesAsync(
            this ContractTester <DPoSContractTestAElfModule> contractTester)
        {
            var bytes = await contractTester.CallContractMethodAsync(contractTester.GetConsensusContractAddress(),
                                                                     nameof(ConsensusContract.GetCandidates), new Empty());

            return(Candidates.Parser.ParseFrom(bytes));
        }
        public static async Task <DPoSHeaderInformation> GetInformationToUpdateConsensusAsync(
            this ContractTester <DPoSContractTestAElfModule> tester,
            DPoSTriggerInformation triggerInformation, DateTime dateTime)
        {
            var bytes = await tester.CallContractMethodAsync(tester.GetConsensusContractAddress(),
                                                             ConsensusConsts.GetInformationToUpdateConsensus, triggerInformation, dateTime);

            return(DPoSHeaderInformation.Parser.ParseFrom(bytes));
        }
Esempio n. 10
0
        protected async Task <Address> GetGenesisAddressAsync(ContractTester <BasicContractZeroTestAElfModule> tester,
                                                              Address parliamentContract)
        {
            var organizationAddress = Address.Parser.ParseFrom(await tester.CallContractMethodAsync(
                                                                   parliamentContract,
                                                                   nameof(ParliamentContractContainer.ParliamentContractStub.GetDefaultOrganizationAddress),
                                                                   new Empty()));

            return(organizationAddress);
        }
Esempio n. 11
0
        public static async Task <LongList> CheckDividendsOfPreviousTerm(
            this ContractTester <DPoSContractTestAElfModule> contractTester)
        {
            var bytes = await contractTester.CallContractMethodAsync(
                contractTester.GetDividendsContractAddress(),
                nameof(DividendContract.CheckDividendsOfPreviousTerm),
                new Empty());

            return(LongList.Parser.ParseFrom(bytes));
        }
Esempio n. 12
0
        public static async Task <Tickets> GetTicketsInformationAsync(
            this ContractTester <DPoSContractTestAElfModule> contractTester)
        {
            var bytes = await contractTester.CallContractMethodAsync(contractTester.GetConsensusContractAddress(),
                                                                     nameof(ConsensusContract.GetTicketsInformation), new PublicKey()
            {
                Hex = contractTester.PublicKey
            });

            return(Tickets.Parser.ParseFrom(bytes));
        }
        internal async Task <long> GetParentChainHeight(
            ContractTester <MultiTokenContractCrossChainTestAElfModule> tester, Address sideCrossChainContract)
        {
            var result = await tester.CallContractMethodAsync(sideCrossChainContract,
                                                              nameof(CrossChainContractContainer.CrossChainContractStub
                                                                     .GetParentChainHeight), new Empty());

            var height = Int64Value.Parser.ParseFrom(result);

            return(height.Value);
        }
        internal async Task <long> GetSideChainHeight(int chainId)
        {
            var result = await MainChainTester.CallContractMethodAsync(CrossChainContractAddress,
                                                                       nameof(CrossChainContractContainer.CrossChainContractStub
                                                                              .GetSideChainHeight), new Int32Value
            {
                Value = chainId
            });

            var height = Int64Value.Parser.ParseFrom(result);

            return(height.Value);
        }
Esempio n. 15
0
        public static async Task <long> GetBalanceAsync(this ContractTester <MultiTokenContractTestAElfModule> contractTester,
                                                        Address targetAddress)
        {
            var balanceOutput = GetBalanceOutput.Parser.ParseFrom(
                await contractTester.CallContractMethodAsync(contractTester.GetTokenContractAddress(),
                                                             nameof(TokenContract.GetBalance), new GetBalanceInput
            {
                Owner  = targetAddress,
                Symbol = "ELF"
            }));

            return(balanceOutput.Balance);
        }
Esempio n. 16
0
        protected async Task ApproveBalanceAsync(long amount)
        {
            var callOwner = Address.FromPublicKey(MainChainTester.KeyPair.PublicKey);

            var approveResult = await MainChainTester.ExecuteContractWithMiningAsync(TokenContractAddress,
                                                                                     nameof(TokenContractContainer.TokenContractStub.Approve), new ApproveInput
            {
                Spender = CrossChainContractAddress,
                Symbol  = "ELF",
                Amount  = amount
            });

            approveResult.Status.ShouldBe(TransactionResultStatus.Mined);
            await MainChainTester.CallContractMethodAsync(TokenContractAddress,
                                                          nameof(TokenContractContainer.TokenContractStub.GetAllowance),
                                                          new GetAllowanceInput
            {
                Symbol  = "ELF",
                Owner   = callOwner,
                Spender = CrossChainContractAddress
            });
        }
Esempio n. 17
0
        public static async Task <List <Transaction> > GenerateConsensusTransactionsAsync(
            this ContractTester <DPoSContractTestAElfModule> tester,
            DPoSTriggerInformation triggerInformation)
        {
            var bytes = await tester.CallContractMethodAsync(tester.GetConsensusContractAddress(),
                                                             ConsensusConsts.GenerateConsensusTransactions, triggerInformation);

            var txs = TransactionList.Parser.ParseFrom(bytes).Transactions.ToList();

            tester.SignTransaction(ref txs, tester.KeyPair);
            tester.SupplyTransactionParameters(ref txs);

            return(txs);
        }
Esempio n. 18
0
        public static async Task <ConsensusCommand> GetConsensusCommandAsync(
            this ContractTester <DPoSContractTestAElfModule> tester,
            Timestamp timestamp)
        {
            var commandInput = new CommandInput
            {
                PublicKey = ByteString.CopyFrom(tester.KeyPair.PublicKey),
            };
            var bytes = await tester.CallContractMethodAsync(
                tester.GetConsensusContractAddress(), // Usually the second contract is consensus contract.
                ConsensusConsts.GetConsensusCommand,
                commandInput, timestamp.ToDateTime());

            return(ConsensusCommand.Parser.ParseFrom(bytes));
        }
Esempio n. 19
0
        public static async Task <ConsensusCommand> GetConsensusCommandAsync(
            this ContractTester <DPoSSideChainTestAElfModule> tester,
            Timestamp timestamp = null)
        {
            var triggerInformation = new DPoSTriggerInformation
            {
                PublicKey = ByteString.CopyFrom(tester.KeyPair.PublicKey),
            };
            var bytes = await tester.CallContractMethodAsync(
                tester.GetConsensusContractAddress(), // Usually the second contract is consensus contract.
                ConsensusConsts.GetConsensusCommand,
                triggerInformation);

            return(ConsensusCommand.Parser.ParseFrom(bytes));
        }
Esempio n. 20
0
        public static async Task <Tickets> GetPageableNotWithdrawnTicketsInfo(
            this ContractTester <DPoSContractTestAElfModule> contractTester, string publicKey, int startIndex,
            int length)
        {
            var bytes = await contractTester.CallContractMethodAsync(contractTester.GetConsensusContractAddress(),
                                                                     nameof(ConsensusContract.GetPageableNotWithdrawnTicketsInfo),
                                                                     new PageableTicketsInfoInput()
            {
                PublicKey = publicKey,
                Start     = startIndex,
                Length    = length
            });

            return(Tickets.Parser.ParseFrom(bytes));
        }
Esempio n. 21
0
        public static async Task <Block> GenerateConsensusTransactionsAndMineABlockAsync(
            this ContractTester <DPoSContractTestAElfModule> tester,
            DPoSTriggerInformation triggerInformation,
            params ContractTester <DPoSContractTestAElfModule>[] testersGonnaExecuteThisBlock)
        {
            var bytes = await tester.CallContractMethodAsync(tester.GetConsensusContractAddress(),
                                                             ConsensusConsts.GenerateConsensusTransactions,
                                                             triggerInformation);

            var txs = TransactionList.Parser.ParseFrom(bytes).Transactions.ToList();

            tester.SignTransaction(ref txs, tester.KeyPair);
            tester.SupplyTransactionParameters(ref txs);

            var block = await tester.MineAsync(txs);

            foreach (var contractTester in testersGonnaExecuteThisBlock)
            {
                await contractTester.ExecuteBlock(block, txs);
            }

            return(block);
        }
Esempio n. 22
0
 public static async Task <ByteString> CallConsensusContractMethodAsync(
     this ContractTester <DPoSSideChainTestAElfModule> contractTester, string methodName, IMessage input)
 {
     return(await contractTester.CallContractMethodAsync(contractTester.GetConsensusContractAddress(),
                                                         methodName, input));
 }
Esempio n. 23
0
        public async Task Get_RoundInformation()
        {
            //query return null
            {
                var input = new SInt64Value
                {
                    Value = 2
                };
                var roundInformation = await Starter.CallContractMethodAsync(Starter.GetConsensusContractAddress(),
                                                                             nameof(ConsensusContract.GetRoundInformation), input);

                var round = Round.Parser.ParseFrom(roundInformation);
                round.ShouldBe(new Round());
            }

            //query with result
            {
                var input = new SInt64Value
                {
                    Value = 1
                };
                var roundInformation = await Starter.CallContractMethodAsync(Starter.GetConsensusContractAddress(),
                                                                             nameof(ConsensusContract.GetRoundInformation), input);

                var round = Round.Parser.ParseFrom(roundInformation);
                round.ShouldNotBeNull();
                round.RoundNumber.ShouldBe(1);
                round.RealTimeMinersInformation.Count.ShouldBe(3);
            }
        }
Esempio n. 24
0
        public async Task Withdraw_By_TransactionId()
        {
            const int  lockTime    = 100;
            const long pocketMoney = 10000L;
            var        candidate   = (await Starter.GenerateCandidatesAsync(1))[0];
            var        voters      = await Starter.GenerateVotersAsync(2, pocketMoney);

            var txResult = await voters[0].Vote(candidate.PublicKey, Amount, lockTime);

            txResult.Status.ShouldBe(TransactionResultStatus.Mined);

            var blockAge = SInt64Value.Parser.ParseFrom(await Starter.CallContractMethodAsync(
                                                            Starter.GetConsensusContractAddress(),
                                                            nameof(ConsensusContract.GetBlockchainAge),
                                                            new Empty())).Value;

            var ticketsOfCandidate = await candidate.GetTicketsInformationAsync();

            ticketsOfCandidate.ObtainedTickets.ShouldBe(Amount);
            var ticketsOfVoter = await voters[0].GetTicketsInformationAsync();

            ticketsOfVoter.VotedTickets.ShouldBe(Amount);

            //Change term
            await Miners.ChangeTermAsync(1);

            // UnlockAge > CurrentAge
            var txId            = Hash.Parser.ParseFrom(txResult.ReturnValue);
            var withdrawResult1 = await voters[0]
                                  .ExecuteConsensusContractMethodWithMiningAsync(
                nameof(ConsensusContract.WithdrawByTransactionId), txId);

            withdrawResult1.Status.ShouldBe(TransactionResultStatus.Failed);
            withdrawResult1.Error.Contains(ContractErrorCode.Message[ContractErrorCode.InvalidOperation])
            .ShouldBeTrue();

            await Starter.SetBlockchainAgeAsync(blockAge + lockTime);

            //Withdraw by other voter
            var withdrawResult2 = await voters[1]
                                  .ExecuteConsensusContractMethodWithMiningAsync(nameof(ConsensusContract.WithdrawByTransactionId), txId);

            withdrawResult2.Status.ShouldBe(TransactionResultStatus.Failed);
            withdrawResult2.Error.Contains(ContractErrorCode.Message[ContractErrorCode.NoPermission]).ShouldBeTrue();

            //Withdrawn
            var withdrawResult3 = await voters[0]
                                  .ExecuteConsensusContractMethodWithMiningAsync(nameof(ConsensusContract.WithdrawByTransactionId), txId);

            withdrawResult3.Status.ShouldBe(TransactionResultStatus.Mined);

            //Check withdrawal time and unlock age
            var votingRecord = await voters[0].GetVotingRecord(txResult.TransactionId);

            votingRecord.UnlockAge.ShouldBe(blockAge + lockTime);

            //Check candidate & voter tickets
            ticketsOfCandidate = await candidate.GetTicketsInformationAsync();

            ticketsOfCandidate.ObtainedTickets.ShouldBe(0L);
            ticketsOfVoter = await voters[0].GetTicketsInformationAsync();
            ticketsOfVoter.VotedTickets.ShouldBe(0L);

            //Check voter balance
            var balanceResult = await Starter.GetBalanceAsync(voters[0].GetCallOwnerAddress());

            balanceResult.ShouldBe(10000L);

            //Has already withdrawn
            var withdrawResult4 = await voters[0]
                                  .ExecuteConsensusContractMethodWithMiningAsync(nameof(ConsensusContract.WithdrawByTransactionId), txId);

            withdrawResult4.Status.ShouldBe(TransactionResultStatus.Failed);
            withdrawResult4.Error.Contains(ContractErrorCode.Message[ContractErrorCode.InvalidOperation])
            .ShouldBeTrue();
        }