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 SerialDictionaryCoder($"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; } }
public Chain(string name) //loads chain { coder = new SerialDictionaryCoder($"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(); }
//Filename: main or [hex fork block height]-[hex block hash] //note: block hash of fork block height + 1 public Chain() //creates main chain { coder = new SerialDictionaryCoder($"chains\\main", $"tables\\main", 32, 3); ForkBlockHash = Block.Genesis.GetHash(); ForkBlockHeight = 0; }