Пример #1
0
        public override ChainReorganization TryAddNewBlock(string previousHash)
        {
            lock (this)
            {
                var result =
                    NativeStringToString(try_add_new_block(_blockchain, previousHash.StringToUtf8()))
                    .Split(Space)
                    .ToArray();

                var  ret           = new ChainReorganization();
                long offset        = 0;
                int  blockRequired = Convert.ToInt32(result[offset++]);
                if (blockRequired != 0)
                {
                    ret.BlockRequired = result[offset++];
                }
                var blocksToRevert = Convert.ToInt32(result[offset++]);
                ret.BlocksToRevert = new List <ChainReorganizationBlock>(blocksToRevert);
                for (int i = 0; i < blocksToRevert; i++)
                {
                    var crb = new ChainReorganizationBlock();
                    crb.Hash   = result[offset++];
                    crb.DbId   = Convert.ToInt64(result[offset++]);
                    crb.Height = Convert.ToInt64(result[offset++]);
                    ret.BlocksToRevert.Add(crb);
                }
                ret.BlocksToRevert = ret.BlocksToRevert.OrderByDescending(x => x.Height).ToList();
                return(ret);
            }
        }
Пример #2
0
        public override ChainReorganization TryAddNewBlock(string previousHash)
        {
            var ret = new ChainReorganization();

            if (_blockchain.Count == 0)
            {
                return(ret);
            }
            int height;

            if (!_blockMap.TryGetValue(previousHash, out height))
            {
                ret.BlockRequired = previousHash;
                return(ret);
            }
            var firstReverted = height + 1;

            ret.BlocksToRevert = new List <ChainReorganizationBlock>(_blockchain.Count - firstReverted);
            for (var i = firstReverted; i < _blockchain.Count; i++)
            {
                var block = _blockchain[i];
                var crb   = new ChainReorganizationBlock
                {
                    Hash   = block.Hash,
                    Height = block.Height,
                    DbId   = block.DbId
                };
                ret.BlocksToRevert.Add(crb);
            }
            return(ret);
        }