/// <summary> /// Creates CAN IPNS record object to point to a CAN object of the given hash with the given sequence number. /// </summary> /// <param name="Hash">Hash of the CAN object to point IPNS record to.</param> /// <param name="SequenceNumber">Sequence number of the IPNS record.</param> public CanIpnsEntry CreateIpnsRecord(byte[] Hash, UInt64 SequenceNumber) { log.Trace("(Hash:'{0}',SequenceNumber:{1})", Hash.ToBase58(), SequenceNumber); string validityString = DateTime.UtcNow.AddMonths(1).ToString(Rfc3339DateTimeFormat, DateTimeFormatInfo.InvariantInfo); byte[] validityBytes = Encoding.UTF8.GetBytes(validityString); UInt64 ttlNanoSec = (UInt64)(TimeSpan.FromSeconds(IpnsRecordExpirationTimeSeconds).TotalMilliseconds) * (UInt64)1000000; string valueString = CanApi.CreateIpfsPathFromHash(Hash); byte[] valueBytes = Encoding.UTF8.GetBytes(valueString); CanIpnsEntry res = new CanIpnsEntry() { Sequence = SequenceNumber, ValidityType = CanIpnsEntry.Types.ValidityType.Eol, Ttl = ttlNanoSec, Validity = ProtocolHelper.ByteArrayToByteString(validityBytes), Value = ProtocolHelper.ByteArrayToByteString(valueBytes) }; res.Signature = ProtocolHelper.ByteArrayToByteString(CreateIpnsRecordSignature(res)); log.Trace("(-):{0}", res); return(res); }
/// <summary> /// Thread that is initializes CAN objects during the profile server startup. /// </summary> private async void InitThread() { log.Info("()"); initThreadFinished.Reset(); if (Base.Configuration.CanProfileServerContactInformationHash != null) { log.Debug("Old CAN object hash is '{0}', object {1} change.", Base.Configuration.CanProfileServerContactInformationHash.ToBase58(), Base.Configuration.CanProfileServerContactInformationChanged ? "DID" : "did NOT"); } else { log.Debug("No CAN object found."); } bool deleteOldObject = Base.Configuration.CanProfileServerContactInformationChanged && (Base.Configuration.CanProfileServerContactInformationHash != null); byte[] canObject = canContactInformation.ToByteArray(); log.Trace("CAN object: {0}", canObject.ToHex()); while (!ShutdownSignaling.IsShutdown) { // First delete old CAN object if there is any. bool error = false; if (deleteOldObject) { string objectPath = CanApi.CreateIpfsPathFromHash(Base.Configuration.CanProfileServerContactInformationHash); CanDeleteResult cres = await canApi.CanDeleteObject(objectPath); if (cres.Success) { log.Info("Old CAN object hash '{0}' deleted.", Base.Configuration.CanProfileServerContactInformationHash.ToBase58()); } else { log.Warn("Failed to delete old CAN object hash '{0}', error message '{1}', will retry.", Base.Configuration.CanProfileServerContactInformationHash.ToBase58(), cres.Message); error = true; } } else { log.Trace("No old object to delete."); } if (ShutdownSignaling.IsShutdown) { break; } if (!error) { if (Base.Configuration.CanProfileServerContactInformationChanged) { // Now upload the new object. CanUploadResult cres = await canApi.CanUploadObject(canObject); if (cres.Success) { canContactInformationHash = cres.Hash; log.Info("New CAN object hash '{0}' added.", canContactInformationHash.ToBase58()); break; } log.Warn("Unable to add new object to CAN, error message: '{0}'", cres.Message); } else { canContactInformationHash = Base.Configuration.CanProfileServerContactInformationHash; log.Info("CAN object unchanged since last time, hash is '{0}'.", canContactInformationHash.ToBase58()); break; } } // Retry in 10 seconds. try { await Task.Delay(10000, ShutdownSignaling.ShutdownCancellationTokenSource.Token); } catch { // Catch cancellation exception. } } if (canContactInformationHash != null) { if (Base.Configuration.CanProfileServerContactInformationChanged) { // Save the new data to the database. if (!await SaveProfileServerContactInformation()) { log.Error("Failed to save new profile server contact information values to database."); } } // Finally, start IPNS record refreshing timer. await IpnsRecordRefresh(); } initThreadFinished.Set(); log.Info("(-)"); }