/// <inheritdoc /> public async Task <Peer> FindPeerAsync(MultiHash id, CancellationToken cancel = default(CancellationToken)) { // Can always find self. if (Swarm.LocalPeer.Id == id) { return(Swarm.LocalPeer); } // Maybe the swarm knows about it. var found = Swarm.KnownPeers.FirstOrDefault(p => p.Id == id); if (found != null && found.Addresses.Count() > 0) { return(found); } // Ask our peers for information on the requested peer. var dquery = new DistributedQuery <Peer> { QueryType = MessageType.FindNode, QueryKey = id, Dht = this, AnswersNeeded = 1 }; await dquery.RunAsync(cancel); if (dquery.Answers.Count == 0) { throw new KeyNotFoundException($"Cannot locate peer '{id}'."); } return(dquery.Answers.First()); }
public async Task Put_Informs_Bitswap() { _dfs = TestDfs.GetTestDfs(null, "sha2-256"); await _dfs.StartAsync(); try { var data = Guid.NewGuid().ToByteArray(); var cid = new Cid { Hash = MultiHash.ComputeHash(data) }; var cts = new CancellationTokenSource(); cts.CancelAfter(20000); var wantTask = _dfs.BitSwapApi.GetAsync(cid, cts.Token); var cid1 = await _dfs.BlockApi.PutAsync(data, cancel : cts.Token); Assert.AreEqual(cid, cid1); Assert.AreEqual(cid, wantTask.Result.Id); Assert.AreEqual(data.Length, wantTask.Result.Size); Assert.AreEqual(data, wantTask.Result.DataBytes); } finally { await _dfs.StopAsync(); } }
protected override async Task <int> OnExecute(CommandLineApplication app) { MultiHash id = PeerId == null ? null : new MultiHash(PeerId); var peer = await Parent.CoreApi.Generic.IdAsync(id); using (JsonWriter writer = new JsonTextWriter(app.Out)) { writer.Formatting = Formatting.Indented; writer.WriteStartObject(); writer.WritePropertyName("ID"); writer.WriteValue(peer.Id.ToBase58()); writer.WritePropertyName("PublicKey"); writer.WriteValue(peer.PublicKey); writer.WritePropertyName("Adddresses"); writer.WriteStartArray(); foreach (var a in peer.Addresses) { if (a != null) { writer.WriteValue(a.ToString()); } } writer.WriteEndArray(); writer.WritePropertyName("AgentVersion"); writer.WriteValue(peer.AgentVersion); writer.WritePropertyName("ProtocolVersion"); writer.WriteValue(peer.ProtocolVersion); writer.WriteEndObject(); } return(0); }
public bool TryGetFavouriteDelta(MultiHash previousDeltaDfsHash, out FavouriteDeltaBroadcast favourite) { Guard.Argument(previousDeltaDfsHash, nameof(previousDeltaDfsHash)).NotNull(); _logger.Debug("Retrieving favourite candidate delta for the successor of delta {0}", previousDeltaDfsHash); var cacheKey = GetCandidateListCacheKey(previousDeltaDfsHash); if (!_candidatesCache.TryGetValue(cacheKey, out ConcurrentBag <string> candidates)) { _logger.Debug("Failed to retrieve any scored candidate with previous delta {0}", previousDeltaDfsHash.ToBase32()); favourite = default; return(false); } var bestCandidate = candidates.Select(c => _candidatesCache.Get(c) as IScoredCandidateDelta) .Where(c => c != null) .OrderByDescending(c => c.Score) .ThenBy(c => c.Candidate.Hash.ToByteArray(), ByteUtil.ByteListMinSizeComparer.Default) .First().Candidate; favourite = new FavouriteDeltaBroadcast { Candidate = bestCandidate, VoterId = _localPeerIdentifier }; _logger.Debug("Retrieved favourite candidate delta {candidate} for the successor of delta {previousDelta}", bestCandidate.Hash.ToByteArray().ToBase32(), previousDeltaDfsHash.ToBase32()); return(true); }
/// <summary> /// Read the identify message and update the peer information. /// </summary> /// <param name="remote"></param> /// <param name="stream"></param> /// <param name="cancel"></param> /// <returns></returns> public async Task UpdateRemotePeerAsync(Peer remote, Stream stream, CancellationToken cancel) { var info = await ProtoBufHelper.ReadMessageAsync <Identify>(stream, cancel).ConfigureAwait(false); remote.AgentVersion = info.AgentVersion; remote.ProtocolVersion = info.ProtocolVersion; if (info.PublicKey == null || info.PublicKey.Length == 0) { throw new InvalidDataException("Public key is missing."); } remote.PublicKey = Convert.ToBase64String(info.PublicKey); if (remote.Id == null) { remote.Id = MultiHash.ComputeHash(info.PublicKey); } if (info.ListenAddresses != null) { remote.Addresses = info.ListenAddresses .Select(b => MultiAddress.TryCreate(b)) .Where(a => a != null) .Select(a => a.WithPeerId(remote.Id)) .ToList(); } if (remote.Addresses.Count() == 0) { log.Warn($"No listen address for {remote}"); } if (!remote.IsValid()) { throw new InvalidDataException($"Invalid peer {remote}."); } }
/// <inheritdoc /> public void Update(MultiHash deltaHash) { try { lock (_synchronisationLock) { var chainedDeltaHashes = _synchroniser .CacheDeltasBetween(LatestKnownDelta, deltaHash, CancellationToken.None) .Reverse() .ToList(); if (!Equals(chainedDeltaHashes.First(), LatestKnownDelta)) { _logger.Warning( "Failed to walk back the delta chain to {LatestKnownDelta}, giving up ledger update.", LatestKnownDelta); return; } foreach (var chainedDeltaHash in chainedDeltaHashes) { UpdateLedgerFromDelta(chainedDeltaHash); } } //https://github.com/catalyst-network/Catalyst.Node/issues/871 FlushTransactionsFromDelta(); } catch (Exception exception) { _logger.Error(exception, "Failed to update the ledger using the delta with hash {deltaHash}", deltaHash); } }
/// <inheritdoc /> public CandidateDeltaBroadcast GetMostPopularCandidateDelta(MultiHash previousDeltaDfsHash) { var candidateListCacheKey = GetCandidateListCacheKey(previousDeltaDfsHash); if (!_candidatesCache.TryGetValue(candidateListCacheKey, out ConcurrentDictionary <FavouriteDeltaBroadcast, bool> retrieved)) { _logger.Debug("Failed to retrieve any favourite candidate with previous delta {0}", previousDeltaDfsHash.ToBase32()); return(null); } var votesThreshold = _deltaProducersProvider.GetDeltaProducersFromPreviousDelta(previousDeltaDfsHash).Count / 3; var favourites = retrieved.Keys.GroupBy(k => k.Candidate.Hash) .Select(g => new { Favourite = g.First(), TotalVotes = g.Count() }) .Where(f => f.TotalVotes >= votesThreshold) .OrderByDescending(h => h.TotalVotes) .ThenByDescending(h => h.Favourite.Candidate.Hash.ToByteArray(), ByteUtil.ByteListMinSizeComparer.Default) .ToList(); _logger.Debug("Found {candidates} popular candidates suitable for confirmation.", favourites.Count); return(favourites.FirstOrDefault()?.Favourite.Candidate); }
/// <inheritdoc /> public IEnumerable <MultiHash> CacheDeltasBetween(MultiHash latestKnownDeltaHash, MultiHash targetDeltaHash, CancellationToken cancellationToken) { var thisHash = targetDeltaHash; do { if (!DeltaCache.TryGetOrAddConfirmedDelta(thisHash, out var retrievedDelta, cancellationToken)) { yield break; } var previousDfsHash = _hashProvider.Cast(retrievedDelta.PreviousDeltaDfsHash.ToByteArray()); _logger.Debug("Retrieved delta {previous} as predecessor of {current}", previousDfsHash, thisHash); yield return(thisHash); thisHash = previousDfsHash; } while (!thisHash.Equals(latestKnownDeltaHash) && !cancellationToken.IsCancellationRequested); yield return(thisHash); }
public async Task Can_Process_DeltaHeightRequest_Correctly() { var deltaHeightRequestMessage = new LatestDeltaHashRequest(); var fakeContext = Substitute.For <IChannelHandlerContext>(); var channeledAny = new ObserverDto(fakeContext, deltaHeightRequestMessage.ToProtocolMessage(PeerIdHelper.GetPeerId(), CorrelationId.GenerateCorrelationId())); var observableStream = new[] { channeledAny }.ToObservable(_testScheduler); _deltaHeightRequestObserver.StartObserving(observableStream); _testScheduler.Start(); var hash = MultiHash.ComputeHash(new byte[32]); var cid = new Cid { Hash = hash }; await fakeContext.Channel.ReceivedWithAnyArgs(1) .WriteAndFlushAsync(new LatestDeltaHashResponse { DeltaIndex = new DeltaIndex { Cid = cid.ToArray().ToByteString(), Height = 100 } }.ToProtocolMessage(PeerIdHelper.GetPeerId(), CorrelationId.GenerateCorrelationId())) .ConfigureAwait(false); _subbedLogger.ReceivedWithAnyArgs(1); }
public ResponseModel addPost(String msg) { var list = wavesApi.getTweetByAddressAsync(privateKeyAccount.Address); byte[] hash = null; if (list.Count > 0) { hash = list[list.Count - 1].hash; hash = SHA256.ComputeHash(hash, 0, hash.Length); } else { var adressByte = Encoding.UTF8.GetBytes(privateKeyAccount.Address); hash = SHA256.ComputeHash(adressByte, 0, adressByte.Length); } var hashModel = ipfs.FileSystem.AddTextAsync(msg).Result; Dictionary <string, object> listValue = new Dictionary <string, object>(); MultiHash s = hashModel.Id.Hash; listValue.Add(BitConverter.ToString(hash).Replace("-", "").Replace("-", ""), Encoding.UTF8.GetBytes(s.ToBase58())); node.PutData(privateKeyAccount, listValue, 1000000); return(new ResponseModel() { }); }
public async Task FindPeer_Closest() { var unknownPeer = new MultiHash("QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCxxx"); var swarm = new Swarm { LocalPeer = self }; await swarm.StartAsync(); var dht = new Dht1 { Swarm = swarm }; await dht.StartAsync(); dht.RoutingTable.Add(other); try { var peer = await dht.FindPeerAsync(unknownPeer); Assert.AreEqual(other, peer); } finally { await swarm.StopAsync(); await dht.StopAsync(); } }
public DeltaBuilderTests() { var hashingAlgorithm = HashingAlgorithm.GetAlgorithmMetadata("blake2b-256"); _hashProvider = new HashProvider(hashingAlgorithm); _random = new Random(); _randomFactory = Substitute.For <IDeterministicRandomFactory>(); _randomFactory.GetDeterministicRandomFromSeed(Arg.Any <byte[]>()) .Returns(ci => new IsaacRandom(((byte[])ci[0]).ToHex())); _producerId = PeerIdHelper.GetPeerId("producer"); _peerSettings = _producerId.ToSubstitutedPeerSettings(); _previousDeltaHash = _hashProvider.ComputeUtf8MultiHash("previousDelta"); _zeroCoinbaseEntry = new CoinbaseEntry { Amount = UInt256.Zero.ToUint256ByteString(), ReceiverPublicKey = _producerId.PublicKey.ToByteString() }; _logger = Substitute.For <ILogger>(); _cache = Substitute.For <IDeltaCache>(); _dateTimeProvider = new DateTimeProvider(); }
public void To_String_Is_Base58_Representation() { var hash = "QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4"; var mh = new MultiHash(hash); Assert.Equal(hash, mh.ToString()); }
public void Parsing_Unknown_Hash_Number() { HashingAlgorithm unknown = null; EventHandler <UnknownHashingAlgorithmEventArgs> unknownHandler = (s, e) => { unknown = e.Algorithm; }; var ms = new MemoryStream(new byte[] { 0x01, 0x02, 0x0a, 0x0b }); MultiHash.UnknownHashingAlgorithm += unknownHandler; try { var mh = new MultiHash(ms); Assert.Equal("ipfs-1", mh.Algorithm.Name); Assert.Equal("ipfs-1", mh.Algorithm.ToString()); Assert.Equal(1, mh.Algorithm.Code); Assert.Equal(2, mh.Algorithm.DigestSize); Assert.Equal(0xa, mh.Digest[0]); Assert.Equal(0xb, mh.Digest[1]); Assert.NotNull(unknown); Assert.Equal("ipfs-1", unknown.Name); Assert.Equal(1, unknown.Code); Assert.Equal(2, unknown.DigestSize); } finally { MultiHash.UnknownHashingAlgorithm -= unknownHandler; } }
public void Parsing_Unknown_Hash_Number() { HashingAlgorithm unknown = null; EventHandler <UnknownHashingAlgorithmEventArgs> unknownHandler = (s, e) => { unknown = e.Algorithm; }; var ms = new MemoryStream(new byte[] { 0x01, 0x02, 0x0a, 0x0b }); MultiHash.UnknownHashingAlgorithm += unknownHandler; try { var mh = new MultiHash(ms); Assert.AreEqual("ipfs-1", mh.Algorithm.Name); Assert.AreEqual("ipfs-1", mh.Algorithm.ToString()); Assert.AreEqual(1, mh.Algorithm.Code); Assert.AreEqual(2, mh.Algorithm.DigestSize); Assert.AreEqual(0xa, mh.Digest[0]); Assert.AreEqual(0xb, mh.Digest[1]); Assert.IsNotNull(unknown, "unknown handler not called"); Assert.AreEqual("ipfs-1", unknown.Name); Assert.AreEqual(1, unknown.Code); Assert.AreEqual(2, unknown.DigestSize); } finally { // ReSharper disable once DelegateSubtraction MultiHash.UnknownHashingAlgorithm -= unknownHandler; } }
/// <summary> /// Adds the <see cref="Cid"/> and <see cref="Peer"/> to the content /// routing system at the specified <see cref="DateTime"/>. /// </summary> /// <param name="cid"> /// The ID of some content that the <paramref name="provider"/> contains. /// </param> /// <param name="provider"> /// The peer ID that contains the <paramref name="cid"/>. /// </param> /// <param name="now"> /// The local time that the <paramref name="provider"/> started to provide /// the <paramref name="cid"/>. /// </param> public void Add(Cid cid, MultiHash provider, DateTime now) { var pi = new ProviderInfo { Expiry = now + ProviderTTL, PeerId = provider }; content.AddOrUpdate( Key(cid), (key) => new List <ProviderInfo> { pi }, (key, providers) => { var existing = providers .Where(p => p.PeerId == provider) .FirstOrDefault(); if (existing != null) { existing.Expiry = pi.Expiry; } else { providers.Add(pi); } return(providers); }); }
/// <inheritdoc /> public async Task <Peer> FindPeerAsync(MultiHash id, CancellationToken cancel = default) { // Can always find self. if (SwarmService.LocalPeer.Id == id) { return(SwarmService.LocalPeer); } // Maybe the swarm knows about it. var found = SwarmService.KnownPeers.FirstOrDefault(p => p.Id == id); if (found != null && found.Addresses.Any()) { return(found); } // Ask our peers for information on the requested peer. var dQuery = new DistributedQuery <Peer> { QueryType = MessageType.FindNode, QueryKey = id, Dht = this, AnswersNeeded = 1 }; await dQuery.RunAsync(cancel).ConfigureAwait(false); // If not found, return the closest peer. return(!dQuery.Answers.Any() ? RoutingTable.NearestPeers(id).FirstOrDefault() : dQuery.Answers.First()); }
/// <summary> /// Create a key ID for the key. /// </summary> /// <param name="key"></param> /// <returns></returns> /// <remarks> /// The key id is the SHA-256 multihash of its public key. The public key is /// a protobuf encoding containing a type and /// the DER encoding of the PKCS SubjectPublicKeyInfo. /// </remarks> MultiHash CreateKeyId(AsymmetricKeyParameter key) { var spki = SubjectPublicKeyInfoFactory .CreateSubjectPublicKeyInfo(key) .GetDerEncoded(); // Add protobuf cruft. var publicKey = new Proto.PublicKey { Data = spki }; if (key is RsaKeyParameters) { publicKey.Type = Proto.KeyType.RSA; } else if (key is ECPublicKeyParameters) { publicKey.Type = Proto.KeyType.Secp256k1; } else { throw new NotSupportedException($"The key type {key.GetType().Name} is not supported."); } using (var ms = new MemoryStream()) { ProtoBuf.Serializer.Serialize(ms, publicKey); ms.Position = 0; return(MultiHash.ComputeHash(ms, "sha2-256")); } }
/// <summary> /// Gets the identity information of the remote peer. /// </summary> /// <param name="connection"> /// The currenty connection to the remote peer. /// </param> /// <param name="cancel"></param> /// <returns></returns> public async Task <Peer> GetRemotePeer(PeerConnection connection, CancellationToken cancel) { var muxer = await connection.MuxerEstablished.Task; log.Debug("Get remote identity"); Peer remote = connection.RemotePeer; using (var stream = await muxer.CreateStreamAsync("id", cancel)) { await connection.EstablishProtocolAsync("/multistream/", stream, cancel); await connection.EstablishProtocolAsync("/ipfs/id/", stream, cancel); var info = await ProtoBufHelper.ReadMessageAsync <Identify>(stream, cancel); if (remote == null) { remote = new Peer(); connection.RemotePeer = remote; } remote.AgentVersion = info.AgentVersion; remote.ProtocolVersion = info.ProtocolVersion; if (info.PublicKey == null || info.PublicKey.Length == 0) { throw new InvalidDataException("Public key is missing."); } remote.PublicKey = Convert.ToBase64String(info.PublicKey); if (remote.Id == null) { remote.Id = MultiHash.ComputeHash(info.PublicKey); } if (info.ListenAddresses != null) { remote.Addresses = info.ListenAddresses .Select(b => { try { return(new MultiAddress(b)); } catch { return(null); } }) .Where(a => a != null) .ToList(); } } // TODO: Verify the Peer ID connection.IdentityEstablished.TrySetResult(remote); log.Debug($"Peer id '{remote}' of {connection.RemoteAddress}"); return(remote); }
public void HashNames() { var mh = new MultiHash("sha1", new byte[20]); mh = new MultiHash("sha2-256", new byte[32]); mh = new MultiHash("sha2-512", new byte[64]); mh = new MultiHash("keccak-512", new byte[64]); }
public void Compute_Hash_Array() { var hello = Encoding.UTF8.GetBytes("Hello, world."); var mh = MultiHash.ComputeHash(hello); Assert.Equal(MultiHash.DefaultAlgorithmName, mh.Algorithm.Name); Assert.NotNull(mh.Digest); }
public void Base58_Encode_Decode() { var mh = new MultiHash("QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB"); Assert.Equal("sha2-256", mh.Algorithm.Name); Assert.Equal(32, mh.Digest.Length); Assert.Equal("QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB", mh.ToBase58()); }
public void Base32_Encode() { var mh = new MultiHash("QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB"); Assert.Equal("sha2-256", mh.Algorithm.Name); Assert.Equal(32, mh.Digest.Length); Assert.Equal("ciqbed3k6ya5i3qqwljochwxdrk5exzqilbckapedujenz5b5hj5r3a", mh.ToBase32()); }
public void ChangeHasherGoodOrderTest() { MultiHash hasher = new MultiHash(new MD5(), new Sha1()); hasher.TransformBlock(TestVectors.s("1234567890"), 0, 10, null, 0); hasher.HashAlgorithms.Reverse(); hasher.TransformFinalBlock(new byte[1], 0, 0); }
//Give the same TransactionId everytime. public MultiHash GetId(string algorithmName) { var publicEntryClone = new PublicEntry(this); publicEntryClone.Amount = ByteString.CopyFrom(TrimEnd(publicEntryClone.Amount.ToByteArray())); publicEntryClone.GasPrice = ByteString.CopyFrom(TrimEnd(publicEntryClone.GasPrice.ToByteArray())); return(MultiHash.ComputeHash(publicEntryClone.ToByteArray(), algorithmName)); }
public void ChangeHasherBadTest() { MultiHash hasher = new MultiHash(new MD5(), new Sha1()); hasher.TransformBlock(TestVectors.s("1234567890"), 0, 10, null, 0); hasher.HashAlgorithms.Add(new RipeMD128()); hasher.TransformFinalBlock(new byte[1], 0, 0); }
public async Task <IEnumerable <Cid> > WantsAsync(MultiHash peer = null, CancellationToken cancel = default(CancellationToken)) { if (peer == null) { peer = (await ipfs.LocalPeer).Id; } return((await ipfs.BitswapService).PeerWants(peer)); }
/// <inheritdoc /> public async Task <IEnumerable <PingResult> > PingAsync(MultiHash peer, int count = 10, CancellationToken cancel = default(CancellationToken)) { var stream = await DownloadAsync("ping", cancel, peer.ToString(), $"count={count.ToString(CultureInfo.InvariantCulture)}"); return(PingResultFromStream(stream)); }
/// <summary> /// Send echo requests to a peer. /// </summary> /// <param name="peerId"> /// The peer ID to receive the echo requests. /// </param> /// <param name="count"> /// The number of echo requests to send. Defaults to 10. /// </param> /// <param name="cancel"> /// Is used to stop the task. When cancelled, the <see cref="TaskCanceledException"/> is raised. /// </param> /// <returns> /// A task that represents the asynchronous operation. The task's value is /// the sequence of <see cref="PingResult"/>. /// </returns> public async Task <IEnumerable <PingResult> > PingAsync(MultiHash peerId, int count = 10, CancellationToken cancel = default(CancellationToken)) { var peer = new Peer { Id = peerId }; return(await PingAsync(peer, count, cancel).ConfigureAwait(false)); }
public async Task <Peer> IdAsync(MultiHash peer = null, CancellationToken cancel = default(CancellationToken)) { if (peer == null) { return(await ipfs.LocalPeer.ConfigureAwait(false)); } return(await ipfs.Dht.FindPeerAsync(peer, cancel).ConfigureAwait(false)); }