Esempio n. 1
0
        /**
         * used asn1 and get hash
         *
         * @param blockNumber
         * @param previousHash
         * @param dataHash
         * @return byte[]
         * @throws IOException
         * @throws InvalidIllegalArgumentException
         */
        public static byte[] CalculateBlockHash(HFClient client, long blockNumber, byte[] previousHash, byte[] dataHash)
        {
            if (previousHash == null)
            {
                throw new ArgumentException("previousHash parameter is null.");
            }

            if (dataHash == null)
            {
                throw new ArgumentException("dataHash parameter is null.");
            }

            if (null == client)
            {
                throw new ArgumentException("client parameter is null.");
            }

            ICryptoSuite cryptoSuite = client.CryptoSuite;

            if (null == cryptoSuite)
            {
                throw new ArgumentException("Client crypto suite has not  been set.");
            }

            MemoryStream         s   = new MemoryStream();
            DerSequenceGenerator seq = new DerSequenceGenerator(s);

            seq.AddObject(new DerInteger((int)blockNumber));
            seq.AddObject(new DerOctetString(previousHash));
            seq.AddObject(new DerOctetString(dataHash));
            seq.Close();
            s.Flush();
            return(cryptoSuite.Hash(s.ToArray()));
        }
Esempio n. 2
0
        /**
         * Returns a channel configured using the details in the Network Configuration file
         *
         * @param client      The associated client
         * @param channelName The name of the channel
         * @return A configured Channel instance
         */
        public async Task <Channel> LoadChannelAsync(HFClient client, string channelName, CancellationToken token = default(CancellationToken))
        {
            if (logger.IsTraceEnabled())
            {
                logger.Trace($"NetworkConfig.loadChannel: {channelName}");
            }

            Channel channel;

            JObject channels = jsonConfig["channels"] as JObject;

            if (channels != null)
            {
                JToken jsonChannel = channels[channelName];
                if (jsonChannel != null)
                {
                    channel = client.GetChannel(channelName);
                    if (channel != null)
                    {
                        // The channel already exists in the client!
                        // Note that by rights this should never happen as HFClient.loadChannelFromConfig should have already checked for this!
                        throw new NetworkConfigurationException($"Channel {channelName} is already configured in the client!");
                    }

                    channel = await ReconstructChannelAsync(client, channelName, jsonChannel, token).ConfigureAwait(false);
                }
                else
                {
                    List <string> channelNames = GetChannelNames();
                    if (channelNames.Count == 0)
                    {
                        throw new NetworkConfigurationException("Channel configuration has no channels defined.");
                    }

                    StringBuilder sb = new StringBuilder(1000);

                    channelNames.ForEach(s =>
                    {
                        if (sb.Length != 0)
                        {
                            sb.Append(", ");
                        }
                        sb.Append(s);
                    });
                    throw new NetworkConfigurationException($"Channel {channelName} not found in configuration file. Found channel names: {sb}");
                }
            }
            else
            {
                throw new NetworkConfigurationException("Channel configuration has no channels defined.");
            }

            return(channel);
        }
Esempio n. 3
0
        // Returns a new Orderer instance for the specified orderer name
        private Orderer GetOrderer(HFClient client, string ordererName)
        {
            Orderer orderer = null;
            Node    o       = orderers.GetOrNull(ordererName);

            if (o != null)
            {
                orderer = client.NewOrderer(o.Name, o.Url, o.Properties);
            }

            return(orderer);
        }
Esempio n. 4
0
        // Returns a new EventHub instance for the specified name
        private EventHub GetEventHub(HFClient client, string name)
        {
            EventHub ehub = null;
            Node     e    = eventHubs.GetOrNull(name);

            if (e != null)
            {
                ehub = client.NewEventHub(e.Name, e.Url, e.Properties);
            }

            return(ehub);
        }
Esempio n. 5
0
        // Returns a new Peer instance for the specified peer name
        private Peer GetPeer(HFClient client, string peerName)
        {
            Peer peer = null;
            Node p    = peers.GetOrNull(peerName);

            if (p != null)
            {
                peer = client.NewPeer(p.Name, p.Url, p.Properties);
            }

            return(peer);
        }
Esempio n. 6
0
        // Reconstructs an existing channel
        private async Task <Channel> ReconstructChannelAsync(HFClient client, string channelName, JToken jsonChannel, CancellationToken token = default(CancellationToken))
        {
            Channel channel;


            channel = client.NewChannel(channelName);

            // orderers is an array of orderer name strings
            JArray ordererNames = jsonChannel["orderers"] as JArray;

            //out("Orderer names: " + (ordererNames == null ? "null" : ordererNames.toString()));
            if (ordererNames != null)
            {
                foreach (JToken jsonVal in ordererNames)
                {
                    string  ordererName = jsonVal.Value <string>();
                    Orderer orderer     = GetOrderer(client, ordererName);
                    if (orderer == null)
                    {
                        throw new NetworkConfigurationException($"Error constructing channel {channelName}. Orderer {ordererName} not defined in configuration");
                    }

                    channel.AddOrderer(orderer);
                }
            }

            // peers is an object containing a nested object for each peer
            JToken jsonPeers = jsonChannel["peers"];
            bool   foundPeer = false;

            //out("Peers: " + (peers == null ? "null" : peers.toString()));
            if (jsonPeers != null)
            {
                foreach (JProperty prop in jsonPeers.Children <JProperty>())
                {
                    string peerName = prop.Name;

                    if (logger.IsTraceEnabled())
                    {
                        logger.Trace($"NetworkConfig.reconstructChannel: Processing peer {peerName}");
                    }

                    JToken jsonPeer = prop.Value;
                    if (jsonPeer == null)
                    {
                        throw new NetworkConfigurationException($"Error constructing channel {channelName}. Invalid peer entry: {peerName}");
                    }

                    Peer peer = GetPeer(client, peerName);
                    if (peer == null)
                    {
                        throw new NetworkConfigurationException($"Error constructing channel {channelName}. Peer {peerName} not defined in configuration");
                    }

                    // Set the various roles
                    PeerOptions peerOptions = PeerOptions.CreatePeerOptions();
                    foreach (PeerRole peerRole in Enum.GetValues(typeof(PeerRole)))
                    {
                        SetPeerRole(peerOptions, jsonPeer, peerRole);
                    }

                    foundPeer = true;

                    // Add the event hub associated with this peer
                    EventHub eventHub = GetEventHub(client, peerName);
                    if (eventHub != null)
                    {
                        await channel.AddEventHubAsync(eventHub, token).ConfigureAwait(false);

                        if (!peerOptions.HasPeerRoles())
                        {
                            // means no roles were found but there is an event hub so define all roles but eventing.
                            peerOptions.SetPeerRoles(new List <PeerRole> {
                                PeerRole.ENDORSING_PEER, PeerRole.CHAINCODE_QUERY, PeerRole.LEDGER_QUERY
                            });
                        }
                    }

                    await channel.AddPeerAsync(peer, peerOptions, token).ConfigureAwait(false);
                }
            }

            if (!foundPeer)
            {
                // peers is a required field
                throw new NetworkConfigurationException($"Error constructing channel {channelName}. At least one peer must be specified");
            }

            return(channel);
        }