Ejemplo n.º 1
0
        private static void LoadGenesis(ChainSpecJson chainSpecJson, ChainSpec chainSpec)
        {
            var nonce       = ToULong(chainSpecJson.Genesis.Seal.Ethereum.Nonce);
            var mixHash     = HexToKeccak(chainSpecJson.Genesis.Seal.Ethereum.MixHash);
            var parentHash  = HexToKeccak(chainSpecJson.Genesis.ParentHash);
            var timestamp   = HexToUInt256(chainSpecJson.Genesis.Timestamp);
            var difficulty  = HexToUInt256(chainSpecJson.Genesis.Difficulty);
            var extraData   = Bytes.FromHexString(chainSpecJson.Genesis.ExtraData);
            var gasLimit    = HexToLong(chainSpecJson.Genesis.GasLimit);
            var beneficiary = new Address(chainSpecJson.Genesis.Author);

            BlockHeader genesisHeader = new BlockHeader(
                parentHash,
                Keccak.OfAnEmptySequenceRlp,
                beneficiary,
                difficulty,
                0,
                gasLimit,
                timestamp,
                extraData);

            genesisHeader.Hash = Keccak.Zero; // need to run the block to know the actual hash

            genesisHeader.Bloom            = new Bloom();
            genesisHeader.GasUsed          = 0;
            genesisHeader.MixHash          = mixHash;
            genesisHeader.Nonce            = nonce;
            genesisHeader.ReceiptsRoot     = Keccak.EmptyTreeHash;
            genesisHeader.StateRoot        = Keccak.EmptyTreeHash;
            genesisHeader.TransactionsRoot = Keccak.EmptyTreeHash;

            chainSpec.Genesis = new Block(genesisHeader);
        }
Ejemplo n.º 2
0
 private void LoadEngine(ChainSpecJson chainSpecJson, ChainSpec chainSpec)
 {
     if (chainSpecJson.Engine?.AuthorityRound != null)
     {
         chainSpec.SealEngineType = SealEngineType.AuRa;
     }
     else if (chainSpecJson.Engine?.Clique != null)
     {
         chainSpec.SealEngineType = SealEngineType.Clique;
         chainSpec.CliqueEpoch    = chainSpecJson.Engine.Clique.Epoch;
         chainSpec.CliquePeriod   = chainSpecJson.Engine.Clique.Period;
         chainSpec.CliqueReward   = chainSpecJson.Engine.Clique.BlockReward;
     }
     else if (chainSpecJson.Engine?.Ethash != null)
     {
         chainSpec.SealEngineType = SealEngineType.Ethash;
     }
     else if (chainSpecJson.Engine?.NethDev != null)
     {
         chainSpec.SealEngineType = SealEngineType.NethDev;
     }
     else
     {
         throw new NotSupportedException("unknown seal engine in chainspec");
     }
 }
Ejemplo n.º 3
0
        private static void LoadAllocations(ChainSpec chainSpec, ChainSpecJson chainSpecJson)
        {
            if (chainSpecJson.Accounts == null)
            {
                return;
            }

            chainSpec.Allocations = new Dictionary <Address, UInt256>();
            foreach (KeyValuePair <string, ChainSpecAccountJson> account in chainSpecJson.Accounts)
            {
                if (account.Value.Balance != null)
                {
                    bool result = UInt256.TryParse(account.Value.Balance, out UInt256 allocationValue);
                    if (!result)
                    {
                        result = UInt256.TryParse(account.Value.Balance.Replace("0x", string.Empty), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out allocationValue);
                    }

                    if (!result)
                    {
                        throw new InvalidDataException($"Cannot recognize allocation value format in {account.Value.Balance}");
                    }

                    chainSpec.Allocations[new Address(account.Key)] = allocationValue;
                }
            }
        }
Ejemplo n.º 4
0
        private static void LoadGenesis(GenesisFileJson genesisJson, ChainSpec chainSpec)
        {
            var nonce       = genesisJson.Nonce;
            var mixHash     = genesisJson.MixHash;
            var parentHash  = genesisJson.ParentHash ?? Keccak.Zero;
            var timestamp   = genesisJson.Timestamp;
            var difficulty  = genesisJson.Difficulty;
            var extraData   = genesisJson.ExtraData;
            var gasLimit    = genesisJson.GasLimit;
            var beneficiary = genesisJson.Author ?? Address.Zero;

            BlockHeader genesisHeader = new BlockHeader(
                parentHash,
                Keccak.OfAnEmptySequenceRlp,
                beneficiary,
                difficulty,
                0,
                (long)gasLimit,
                timestamp,
                extraData);

            genesisHeader.Author           = beneficiary;
            genesisHeader.Hash             = Keccak.Zero; // need to run the block to know the actual hash
            genesisHeader.Bloom            = new Bloom();
            genesisHeader.GasUsed          = 0;
            genesisHeader.MixHash          = mixHash;
            genesisHeader.Nonce            = (ulong)nonce;
            genesisHeader.ReceiptsRoot     = Keccak.EmptyTreeHash;
            genesisHeader.StateRoot        = Keccak.EmptyTreeHash;
            genesisHeader.TransactionsRoot = Keccak.EmptyTreeHash;

            chainSpec.Genesis = new Block(genesisHeader);
        }
Ejemplo n.º 5
0
        public ChainSpec Load(byte[] data)
        {
            try
            {
                string jsonData    = System.Text.Encoding.UTF8.GetString(data);
                var    genesisJson = _serializer.Deserialize <GenesisFileJson>(jsonData);
                var    chainSpec   = new ChainSpec();

                chainSpec.ChainId = (int)genesisJson.Config.ChainId;
                LoadGenesis(genesisJson, chainSpec);
                LoadEngine(genesisJson, chainSpec);
                LoadAllocations(genesisJson, chainSpec);

                chainSpec.HomesteadBlockNumber        = genesisJson.Config.HomesteadBlock;
                chainSpec.TangerineWhistleBlockNumber = genesisJson.Config.Eip150Block;
                chainSpec.SpuriousDragonBlockNumber   = genesisJson.Config.Eip158Block;
                chainSpec.ByzantiumBlockNumber        = genesisJson.Config.ByzantiumBlock;
                chainSpec.ConstantinopleBlockNumber   = genesisJson.Config.ConstantinopleBlock;

                return(chainSpec);
            }
            catch (Exception e)
            {
                throw new InvalidDataException($"Error when loading chainspec ({e.Message})", e);
            }
        }
Ejemplo n.º 6
0
        private static void LoadBootnodes(ChainSpecJson chainSpecJson, ChainSpec chainSpec)
        {
            if (chainSpecJson.Nodes == null)
            {
                return;
            }

            chainSpec.NetworkNodes = new NetworkNode[chainSpecJson.Nodes.Length];
            for (int i = 0; i < chainSpecJson.Nodes.Length; i++)
            {
                chainSpec.NetworkNodes[i] = new NetworkNode(chainSpecJson.Nodes[i], $"bootnode{i}");
            }
        }
Ejemplo n.º 7
0
        private void LoadTransitions(ChainSpecJson chainSpecJson, ChainSpec chainSpec)
        {
            if (chainSpecJson.Engine?.Ethash != null)
            {
                chainSpec.HomesteadBlockNumber = (UInt256?)chainSpecJson.Engine.Ethash.HomesteadTransition;
                chainSpec.DaoForkBlockNumber   = (UInt256?)chainSpecJson.Engine.Ethash.DaoHardForkTransition;
            }

            chainSpec.TangerineWhistleBlockNumber = (UInt256?)chainSpecJson.Params.Eip150Transition;
            chainSpec.SpuriousDragonBlockNumber   = (UInt256?)chainSpecJson.Params.Eip160Transition;
            chainSpec.ByzantiumBlockNumber        = (UInt256?)chainSpecJson.Params.Eip140Transition;
            chainSpec.ConstantinopleBlockNumber   = (UInt256?)chainSpecJson.Params.Eip145Transition;
        }
Ejemplo n.º 8
0
        public ChainSpec Load(byte[] data)
        {
            string jsonData      = System.Text.Encoding.UTF8.GetString(data);
            var    chainSpecJson = _serializer.Deserialize <ChainSpecJson>(jsonData);
            var    chainSpec     = new ChainSpec();

            chainSpec.ChainId = ToInt(chainSpecJson.Params.NetworkId);
            chainSpec.Name    = chainSpecJson.Name;
            LoadGenesis(chainSpecJson, chainSpec);
            LoadAllocations(chainSpec, chainSpecJson);
            LoadBootnodes(chainSpecJson, chainSpec);

            return(chainSpec);
        }
Ejemplo n.º 9
0
 private void LoadEngine(GenesisFileJson genesisJson, ChainSpec chainSpec)
 {
     if (genesisJson.Config.Clique != null)
     {
         chainSpec.SealEngineType = SealEngineType.Clique;
         chainSpec.CliquePeriod   = genesisJson.Config.Clique.Period;
         chainSpec.CliqueEpoch    = genesisJson.Config.Clique.Epoch;
         chainSpec.CliqueReward   = 0;
     }
     else
     {
         chainSpec.SealEngineType = SealEngineType.Ethash;
     }
 }
Ejemplo n.º 10
0
        private static void LoadAllocations(ChainSpec chainSpec, ChainSpecJson chainSpecJson)
        {
            if (chainSpecJson.Accounts == null)
            {
                return;
            }

            chainSpec.Allocations = new Dictionary <Address, UInt256>();
            foreach (KeyValuePair <string, ChainSpecAccountJson> account in chainSpecJson.Accounts)
            {
                if (account.Value.Balance != null)
                {
                    chainSpec.Allocations[new Address(account.Key)] = UInt256.Parse(account.Value.Balance);
                }
            }
        }
Ejemplo n.º 11
0
        public ChainSpec Load(byte[] data)
        {
            try
            {
                string jsonData      = System.Text.Encoding.UTF8.GetString(data);
                var    chainSpecJson = _serializer.Deserialize <ChainSpecJson>(jsonData);
                var    chainSpec     = new ChainSpec();

                chainSpec.ChainId = ToInt(chainSpecJson.Params.NetworkId);
                chainSpec.Name    = chainSpecJson.Name;
                LoadGenesis(chainSpecJson, chainSpec);
                LoadAllocations(chainSpec, chainSpecJson);
                LoadBootnodes(chainSpecJson, chainSpec);

                return(chainSpec);
            }
            catch (Exception e)
            {
                throw new InvalidDataException($"Error when loading chainspec ({e.Message})", e);
            }
        }