コード例 #1
0
        public Chain(Block forkBlock, Block firstBlock) //creates generic chain
        {
            var forkBlockHash = forkBlock.GetHash();

            string name;
            var    randomValueBuffer = new byte[8];

            new Random().NextBytes(randomValueBuffer);

            name = forkBlock.Height.ToString() + "-" + HexConverter.ToPrefixString(randomValueBuffer);

            coder = new SerialDictionaryEncoder($"chains\\{name}", $"tables\\{name}", 32, 3); //blockhash length, max data length = 2^(3*8) bytes

            if (coder.Count != 0)
            {
                throw new Exception("Chain already exists");
            }

            ForkBlockHeight = forkBlock.Height;
            ForkBlockHash   = forkBlockHash;

            AddBlock(forkBlock);

            topBlockOffset = 0;

            if (firstBlock != null)
            {
                AddBlock(firstBlock);
                Height = firstBlock.Height;
            }
            else
            {
                Height = forkBlock.Height;
            }
        }
コード例 #2
0
        public Chain(string name) //loads chain
        {
            coder          = new SerialDictionaryEncoder($"chains\\{name}", $"tables\\{name}", 32, 3);
            topBlockOffset = coder.Lookup.GetIndexOffset(coder.Count - 1);

            var topBlock    = new Block(coder.Lookup.ReadByOffset(topBlockOffset));
            var bottomBlock = new Block(coder.Lookup.ReadByOffset(0));

            Height          = (int)topBlock.Height;
            ForkBlockHeight = (int)bottomBlock.Height;
            ForkBlockHash   = bottomBlock.GetHash();
        }
コード例 #3
0
        public void SerialDictionaryCoder()
        {
            var testDataPath          = "serialDictionaryUnitTestData";
            var testLookupPath        = "serialDictionaryUnitTestLookup";
            var testReverseLookupPath = "serialDictionaryUnitTestLookup_reverse";

            if (File.Exists(testDataPath))
            {
                File.Delete(testDataPath);
            }
            if (File.Exists(testLookupPath))
            {
                File.Delete(testLookupPath);
            }
            if (File.Exists(testReverseLookupPath))
            {
                File.Delete(testReverseLookupPath);
            }

            var serialDictionaryCoder = new SerialDictionaryEncoder(testDataPath, testLookupPath, 32, 2);

            var expectedDictionary = new Dictionary <byte[], byte[]>();

            for (int i = 0; i < 50; i++)
            {
                var value = new byte[i + 1];

                for (int j = 0; j < value.Length; j++)
                {
                    value[j] = (byte)(40 + i);
                }

                var key = CryptographyHelper.Sha3256(value);

                serialDictionaryCoder.Add(key, value);
                expectedDictionary.Add(key, value);
            }
        }
コード例 #4
0
        //Filename: main or [hex fork block height]-[hex block hash]
        //block hash of fork block height + 1

        public Chain() //creates main chain
        {
            coder           = new SerialDictionaryEncoder($"chains\\main", $"tables\\main", 32, 3);
            ForkBlockHash   = Block.Genesis.GetHash();
            ForkBlockHeight = 0;
        }