예제 #1
0
 private void CheckNull(KhaosBlock block)
 {
     if (block == null)
     {
         throw new NonCommonBlockException();
     }
 }
예제 #2
0
        public KeyValuePair <List <KhaosBlock>, List <KhaosBlock> > GetBranch(BlockId block1, BlockId block2)
        {
            List <KhaosBlock> list1   = new List <KhaosBlock>();
            List <KhaosBlock> list2   = new List <KhaosBlock>();
            KhaosBlock        kblock1 = this.mini_store.GetBlockByHash(block1);
            KhaosBlock        kblock2 = this.mini_store.GetBlockByHash(block2);

            if (kblock1 != null && kblock2 != null)
            {
                while (!object.Equals(kblock1, kblock2))
                {
                    if (kblock1.Num > kblock2.Num)
                    {
                        list1.Add(kblock1);
                        kblock1 = kblock1.Parent;
                    }
                    else if (kblock1.Num < kblock2.Num)
                    {
                        list2.Add(kblock2);
                        kblock2 = kblock2.Parent;
                    }
                    else
                    {
                        list1.Add(kblock1);
                        list2.Add(kblock2);
                        kblock1 = kblock1.Parent;
                        kblock2 = kblock2.Parent;
                    }
                }
            }

            return(new KeyValuePair <List <KhaosBlock>, List <KhaosBlock> >(list1, list2));
        }
예제 #3
0
        public BlockCapsule Push(BlockCapsule block)
        {
            KhaosBlock kblock = new KhaosBlock(block);

            if (this.head != null && kblock.ParentHash != SHA256Hash.ZERO_HASH)
            {
                KhaosBlock oblock = this.mini_store.GetBlockByHash(kblock.ParentHash);
                if (oblock != null)
                {
                    if (block.Num != oblock.Num + 1)
                    {
                        throw new ArgumentException("parent number : " + oblock.Num + ", block number : " + block.Num);
                    }
                    kblock.Parent = oblock;
                }
                else
                {
                    this.mini_unlinked_store.Insert(kblock);
                    throw new UnLinkedBlockException();
                }
            }

            this.mini_store.Insert(kblock);

            if (this.head == null || block.Num > head.Num)
            {
                SetHead(kblock);
            }

            return(this.head.Block);
        }
예제 #4
0
        public BlockCapsule GetBlock(SHA256Hash hash)
        {
            KhaosBlock block = this.mini_store.GetBlockByHash(hash);

            if (block == null)
            {
                block = this.mini_unlinked_store.GetBlockByHash(hash);
            }

            return(block?.Block);
        }
예제 #5
0
        public bool Pop()
        {
            KhaosBlock parent = this.head.Parent;

            if (parent != null)
            {
                SetHead(parent);
                return(true);
            }

            return(false);
        }
예제 #6
0
        public override bool Equals(object obj)
        {
            if (this == obj)
            {
                return(true);
            }

            if (obj == null || GetType() != obj.GetType())
            {
                return(false);
            }

            KhaosBlock blk = (KhaosBlock)obj;

            return(object.Equals(this.id, blk.id));
        }
예제 #7
0
        public BlockCapsule GetParent(SHA256Hash hash)
        {
            BlockCapsule result = null;
            KhaosBlock   block  = this.mini_store.GetBlockByHash(hash);

            if (block == null)
            {
                block = this.mini_unlinked_store.GetBlockByHash(hash);
            }

            if (block != null && block.Block != null && Contain(block.Id))
            {
                result = block.Block;
            }

            return(result);
        }
예제 #8
0
        public KeyValuePair <List <KhaosBlock>, List <KhaosBlock> > GetBranch(SHA256Hash block1, SHA256Hash block2)
        {
            List <KhaosBlock> list1   = new List <KhaosBlock>();
            List <KhaosBlock> list2   = new List <KhaosBlock>();
            KhaosBlock        kblock1 = this.mini_store.GetBlockByHash(block1);
            KhaosBlock        kblock2 = this.mini_store.GetBlockByHash(block2);

            CheckNull(kblock1);
            CheckNull(kblock2);

            while (kblock1.Num > kblock2.Num)
            {
                list1.Add(kblock1);
                kblock1 = kblock1.Parent;
                CheckNull(kblock1);
                CheckNull(this.mini_store.GetBlockByHash(kblock1.Id));
            }

            while (kblock1.Num < kblock2.Num)
            {
                list2.Add(kblock2);
                kblock2 = kblock2.Parent;
                CheckNull(kblock2);
                CheckNull(this.mini_store.GetBlockByHash(kblock2.Id));
            }

            while (!object.Equals(kblock1, kblock2))
            {
                list1.Add(kblock1);
                list2.Add(kblock2);
                kblock1 = kblock1.Parent;
                CheckNull(kblock1);
                CheckNull(this.mini_store.GetBlockByHash(kblock1.Id));
                kblock2 = kblock2.Parent;
                CheckNull(kblock2);
                CheckNull(this.mini_store.GetBlockByHash(kblock2.Id));
            }

            return(new KeyValuePair <List <KhaosBlock>, List <KhaosBlock> >(list1, list2));
        }
예제 #9
0
        public void Insert(KhaosBlock block)
        {
            // TODO 중복으로 들어올떄는 어떻게 해야할지
            if (!this.khaosblock_hashes.ContainsKey(block.Id))
            {
                this.khaosblock_hashes.TryAdd(block.Id, block);
            }

            long min = Math.Max(0, head.Num - max_capacity);

            foreach (KeyValuePair <long, List <KhaosBlock> > pair in this.khaosblock_numbers.Where(x => x.Key < min))
            {
                this.khaosblock_numbers.TryRemove(pair.Key, out _);
                foreach (KhaosBlock b in pair.Value)
                {
                    this.khaosblock_hashes.TryRemove(b.Id, out _);
                }
            }

            if (this.khaosblock_numbers.ContainsKey(block.Num))
            {
                List <KhaosBlock> blocks = this.khaosblock_numbers[block.Num];
                if (blocks == null)
                {
                    blocks = new List <KhaosBlock>();
                }

                blocks.Add(block);
                this.khaosblock_numbers[block.Num] = blocks;
            }
            else
            {
                this.khaosblock_numbers.TryAdd(block.Num, new List <KhaosBlock>()
                {
                    block
                });
            }
        }
예제 #10
0
 public void SetHead(KhaosBlock block)
 {
     this.head                     = block;
     this.mini_store.Head          = this.head;
     this.mini_unlinked_store.Head = this.head;
 }