Ejemplo n.º 1
0
        // To decrease granularity of timestamp
        // Supposed to be 2^n-1

        private bool SignBlock(List <StakeTx> stakeTxes, Block block, ChainedBlock pindexBest, long fees)
        {
            // if we are trying to sign
            //    something except proof-of-stake block template
            if (!block.Transactions[0].Outputs[0].IsEmpty)
            {
                return(false);
            }

            // if we are trying to sign
            //    a complete proof-of-stake block
            if (BlockStake.IsProofOfStake(block))
            {
                return(true);
            }

            Key         key         = null;
            Transaction txCoinStake = new Transaction();

            txCoinStake.Time &= ~PosConsensusValidator.STAKE_TIMESTAMP_MASK;

            long searchTime = txCoinStake.Time;             // search to current time


            if (searchTime > this.lastCoinStakeSearchTime)
            {
                long searchInterval = searchTime - this.lastCoinStakeSearchTime;
                if (this.CreateCoinStake(stakeTxes, pindexBest, block, searchInterval, fees, ref txCoinStake, ref key))
                {
                    if (txCoinStake.Time >= BlockValidator.GetPastTimeLimit(pindexBest) + 1)
                    {
                        // make sure coinstake would meet timestamp protocol
                        //    as it would be the same as the block timestamp
                        block.Transactions[0].Time = block.Header.Time = txCoinStake.Time;

                        // we have to make sure that we have no future timestamps in
                        //    our transactions set
                        foreach (var transaction in block.Transactions)
                        {
                            if (transaction.Time > block.Header.Time)
                            {
                                block.Transactions.Remove(transaction);
                            }
                        }

                        block.Transactions.Insert(1, txCoinStake);
                        block.UpdateMerkleRoot();

                        // append a signature to our block
                        var signature = key.Sign(block.GetHash());

                        block.BlockSignatur = new BlockSignature {
                            Signature = signature.ToDER()
                        };
                        return(true);
                    }
                }

                this.LastCoinStakeSearchInterval = searchTime - this.LastCoinStakeSearchTime;
                this.LastCoinStakeSearchTime     = searchTime;
            }

            return(false);
        }
Ejemplo n.º 2
0
        // To decrease granularity of timestamp
        // Supposed to be 2^n-1

        private bool SignBlock(Block block, ChainedBlock pindexBest, long fees)
        {
            // if we are trying to sign
            //    something except proof-of-stake block template
            if (!block.Transactions[0].Outputs[0].IsEmpty)
            {
                return(false);
            }

            // if we are trying to sign
            //    a complete proof-of-stake block
            if (block.IsProofOfStake())
            {
                return(true);
            }

            //this.LastCoinStakeSearchTime = DateTime.UtcNow.ToUnixTimestamp();// GetAdjustedTime(); // startup timestamp

            Key         key         = null;
            Transaction txCoinStake = new Transaction();

            //if (BlockValidator.IsProtocolV2(bestHeight + 1)) // we are never in V2
            int tt = (int)txCoinStake.Time;

            tt &= ~BlockValidator.STAKE_TIMESTAMP_MASK;
            txCoinStake.Time = (uint)tt;

            long searchTime = txCoinStake.Time;             // search to current time

            //Console.WriteLine($"mask = {txCoinStake.Time & BlockValidator.STAKE_TIMESTAMP_MASK} searchInterval = {searchTime - this.LastCoinStakeSearchTime} searchTime = {searchTime} lastCoinStakeSearchTime = {this.LastCoinStakeSearchTime}");

            if (searchTime > this.LastCoinStakeSearchTime)
            //if (DateTime.UtcNow.Second % 16 == 0) // every 16 sedons
            {
                long searchInterval = searchTime - this.LastCoinStakeSearchTime;
                if (this.walletService.CreateCoinStake(pindexBest, block.Header.Bits, searchInterval, fees, ref txCoinStake, ref key))
                {
                    if (txCoinStake.Time >= BlockValidator.GetPastTimeLimit(pindexBest) + 1)
                    {
                        // make sure coinstake would meet timestamp protocol
                        //    as it would be the same as the block timestamp
                        block.Transactions[0].Time = block.Header.Time = txCoinStake.Time;

                        // we have to make sure that we have no future timestamps in
                        //    our transactions set
                        foreach (var transaction in block.Transactions)
                        {
                            if (transaction.Time > block.Header.Time)
                            {
                                block.Transactions.Remove(transaction);
                            }
                        }

                        block.Transactions.Insert(1, txCoinStake);
                        block.UpdateMerkleRoot();

                        // append a signature to our block
                        var signature = key.Sign(block.GetHash());

                        block.BlockSignatur = new BlockSignature {
                            Signature = signature.ToDER()
                        };
                        return(true);
                    }
                }
                this.LastCoinStakeSearchInterval = searchTime - this.LastCoinStakeSearchTime;
                this.LastCoinStakeSearchTime     = searchTime;
            }

            return(false);
        }