Ejemplo n.º 1
0
        private TransactionReceipt FinishCycleTxReceipt(ulong blockIndex)
        {
            var tx = _transactionBuilder.InvokeTransactionWithGasPrice(
                UInt160Utils.Zero,
                ContractRegisterer.GovernanceContract,
                Utility.Money.Zero,
                GovernanceInterface.MethodFinishCycle,
                0,
                UInt256Utils.ToUInt256(GovernanceContract.GetCycleByBlockNumber(_blockManager.GetHeight()))
                );

            return(HardforkHeights.IsHardfork_9Active(blockIndex) ?
                   new TransactionReceipt
            {
                Hash = tx.FullHash(SignatureUtils.ZeroNew, true),
                Status = TransactionStatus.Pool,
                Transaction = tx,
                Signature = SignatureUtils.ZeroNew,
            }
                :
                   new TransactionReceipt
            {
                Hash = tx.FullHash(SignatureUtils.ZeroOld, false),
                Status = TransactionStatus.Pool,
                Transaction = tx,
                Signature = SignatureUtils.ZeroOld,
            });
        }
Ejemplo n.º 2
0
        public IEnumerable <Block> GetBlocksByHeightRange(ulong height, ulong count)
        {
            var result = new List <Block>();

            for (var i = height; i < height + count; i++)
            {
                if (i > GetTotalBlockHeight())
                {
                    break;
                }
                var block = GetBlockByHeight(i);
                if (block is null)
                {
                    Logger.LogWarning($"No block for requested height {i}, current height is {GetTotalBlockHeight()}");
                    continue;
                }
                if (UInt256Utils.IsZero(block.Header.StateHash))
                {
                    Logger.LogError($"Requested block for height {i} has zero state hash, current height is {GetTotalBlockHeight()}");
                    continue;
                }
                result.Add(block);
            }

            return(result);
        }
Ejemplo n.º 3
0
 public BlockchainEventFilterParams(BlockchainEvent eventType, UInt256[] txHashes)
 {
     EventType = eventType;
     // sorting the tx hashes for further optimization
     Array.Sort(txHashes, (x, y) => UInt256Utils.Compare(x, y));
     PendingTransactionList = new List <UInt256>(txHashes.ToList());
     PollingTime            = TimeUtils.CurrentTimeMillis();
 }
Ejemplo n.º 4
0
        private bool IsFinishCycleTx(Block block, Transaction tx)
        {
            var indexInCycle = block.Header.Index % StakingContract.CycleDuration;
            var cycle        = block.Header.Index / StakingContract.CycleDuration;

            if (cycle > 0 && indexInCycle == 0 && tx.To.Equals(ContractRegisterer.GovernanceContract))
            {
                var expectedTx = BuildSystemContractTx(ContractRegisterer.GovernanceContract, GovernanceInterface.MethodFinishCycle,
                                                       UInt256Utils.ToUInt256(GovernanceContract.GetCycleByBlockNumber(block.Header.Index - 1)));
                return(CheckSystemTxEquality(expectedTx, tx));
            }
            return(false);
        }
Ejemplo n.º 5
0
        private bool IsDistributeCycleRewardsAndPenaltiesTx(Block block, Transaction tx)
        {
            var indexInCycle = block.Header.Index % StakingContract.CycleDuration;
            var cycle        = block.Header.Index / StakingContract.CycleDuration;

            if (cycle > 0 && indexInCycle == StakingContract.AttendanceDetectionDuration &&
                tx.To.Equals(ContractRegisterer.GovernanceContract))
            {
                var expectedTx = BuildSystemContractTx(ContractRegisterer.GovernanceContract, GovernanceInterface.MethodDistributeCycleRewardsAndPenalties,
                                                       UInt256Utils.ToUInt256(GovernanceContract.GetCycleByBlockNumber(block.Header.Index - 1)));
                return(CheckSystemTxEquality(expectedTx, tx));
            }
            return(false);
        }
Ejemplo n.º 6
0
        private TransactionReceipt BuildSystemContractTxReceipt(UInt160 contractAddress, string mehodSignature)
        {
            var transaction = _transactionBuilder.InvokeTransactionWithGasPrice(
                UInt160Utils.Zero,
                contractAddress,
                Money.Zero,
                mehodSignature,
                0,
                UInt256Utils.ToUInt256(GovernanceContract.GetCycleByBlockNumber(_stateManager.LastApprovedSnapshot.Blocks.GetTotalBlockHeight()))
                );

            return(new TransactionReceipt
            {
                Hash = transaction.FullHash(SignatureUtils.ZeroNew, true),
                Status = TransactionStatus.Pool,
                Transaction = transaction,
                Signature = SignatureUtils.ZeroNew,
            });
        }
Ejemplo n.º 7
0
        private TransactionReceipt MakeNextValidatorsTxReceipt()
        {
            var sk = Crypto.GeneratePrivateKey();
            var pk = Crypto.ComputePublicKey(sk, false);
            var tx = _transactionBuilder.InvokeTransactionWithGasPrice(
                _wallet.EcdsaKeyPair.PublicKey.GetAddress(),
                ContractRegisterer.GovernanceContract,
                Money.Zero,
                GovernanceInterface.MethodChangeValidators,
                0,
                UInt256Utils.ToUInt256(GovernanceContract.GetCycleByBlockNumber(_stateManager.LastApprovedSnapshot.Blocks.GetTotalBlockHeight())),
                (pk)
                );
            var res = Signer.Sign(tx, _wallet.EcdsaKeyPair, true);

            Assert.False(_eventData.ContainsKey(res.Hash));
            _eventData.Add(res.Hash,
                           ByteString.CopyFrom(ContractEncoder.Encode(GovernanceInterface.EventChangeValidators, (pk))));
            return(res);
        }
Ejemplo n.º 8
0
        private TransactionReceipt MakeKeygenSendValuesTxReceipt()
        {
            var proposer = new BigInteger(0).ToUInt256();
            var value    = new Byte[0];
            var tx       = _transactionBuilder.InvokeTransactionWithGasPrice(
                _wallet.EcdsaKeyPair.PublicKey.GetAddress(),
                ContractRegisterer.GovernanceContract,
                Money.Zero,
                GovernanceInterface.MethodKeygenSendValue,
                0,
                UInt256Utils.ToUInt256(GovernanceContract.GetCycleByBlockNumber(_stateManager.LastApprovedSnapshot.Blocks.GetTotalBlockHeight())),
                proposer, (value)
                );
            var res = Signer.Sign(tx, _wallet.EcdsaKeyPair, true);

            Assert.False(_eventData.ContainsKey(res.Hash));
            _eventData.Add(res.Hash,
                           ByteString.CopyFrom(ContractEncoder.Encode(GovernanceInterface.EventKeygenSendValue,
                                                                      proposer, (value))));
            return(res);
        }
Ejemplo n.º 9
0
        public JArray SyncPendingTransaction(ulong id, BlockchainEventFilterParams filter, bool poll)
        {
            var results = new JArray();

            var pendingTx = filter.PendingTransactionList;
            var poolTx    = _transactionPool.Transactions.Keys.ToArray();

            Array.Sort(poolTx, (x, y) => UInt256Utils.Compare(x, y));

            // comparing two sorted list listA and listB in O(listA.Count + listB.Count)

            int iter = 0;

            foreach (var txHash in poolTx)
            {
                if (txHash is null)
                {
                    continue;
                }

                while (iter < pendingTx.Count && UInt256Utils.Compare(txHash, pendingTx[iter]) > 0)
                {
                    iter++;
                }
                if (iter == pendingTx.Count || UInt256Utils.Compare(txHash, pendingTx[iter]) < 0)
                {
                    results.Add(Web3DataFormatUtils.Web3Data(txHash));
                }
            }

            if (poll)
            {
                // Pending transaction list in filter must be sorted for further optimization
                filter.PendingTransactionList = new List <UInt256>(poolTx.ToList());
                filter.PollingTime            = TimeUtils.CurrentTimeMillis();
                _filters[id] = filter;
            }
            return(results);
        }
Ejemplo n.º 10
0
        private TransactionReceipt MakeCommitTransaction()
        {
            var biVarPoly  = BiVarSymmetricPolynomial.Random(0);
            var commitment = biVarPoly.Commit().ToBytes();
            var row        = new Byte[0];
            var tx         = _transactionBuilder.InvokeTransactionWithGasPrice(
                _wallet.EcdsaKeyPair.PublicKey.GetAddress(),
                ContractRegisterer.GovernanceContract,
                Money.Zero,
                GovernanceInterface.MethodKeygenCommit,
                0,
                UInt256Utils.ToUInt256(GovernanceContract.GetCycleByBlockNumber(_stateManager.LastApprovedSnapshot.Blocks.GetTotalBlockHeight())),
                commitment,
                new byte[][] { row }
                );

            var res = Signer.Sign(tx, _wallet.EcdsaKeyPair, true);

            Assert.False(_eventData.ContainsKey(res.Hash));
            _eventData.Add(res.Hash,
                           ByteString.CopyFrom(ContractEncoder.Encode(GovernanceInterface.EventKeygenCommit,
                                                                      commitment, new byte[][] { row })));
            return(res);
        }