/// <summary> /// Creates a new chain with the provided ChainId and Smart Contract Zero. /// </summary> /// <returns>The new chain async.</returns> /// <param name="chainId">The new chain id which will be derived from the creator address.</param> /// <param name="smartContractRegistration">The smart contract registration containing the code of the SmartContractZero.</param> public async Task <IChain> CreateNewChainAsync(Hash chainId, List <SmartContractRegistration> smartContractRegistration) { try { // TODO: Centralize this function in Hash class // SmartContractZero address can be derived from ChainId foreach (var reg in smartContractRegistration) { var contractAddress = GenesisContractHash(chainId, (SmartContractType)reg.Type); await _smartContractService.DeployContractAsync(chainId, contractAddress, reg, true); } var builder = new GenesisBlockBuilder(); builder.Build(chainId); // add block to storage var blockchain = _chainService.GetBlockChain(chainId); await blockchain.AddBlocksAsync(new List <IBlock> { builder.Block }); var chain = new Chain { GenesisBlockHash = await blockchain.GetCurrentBlockHashAsync(), Id = chainId }; return(chain); } catch (Exception e) { _logger.Error("CreateNewChainAsync Error: " + e); return(null); } }
public void GenesisBlockBuilder_Test() { var builder = new GenesisBlockBuilder(); builder = builder.Build(0); builder.Block.Header.Height.ShouldBe(KernelConstants.GenesisBlockHeight); builder.Block.Header.PreviousBlockHash.ShouldBe(Hash.Empty); builder.Block.Header.ChainId.ShouldBe(0); builder.Block.Body.BlockHeader.ShouldBe(builder.Block.Header.GetHash()); }