Esempio n. 1
0
        /// <summary>
        ///
        /// After running an instance of the Wonka engine, this method will:
        ///
        /// 1.) Persist all data to files (metadata, rules, current data, report), zip the files, and then upload the .ZIP to IPFS
        /// 2.) Create the hash of the Zip file
        /// 3.) Create an entry on the ChronoLog contract (including the IPFS URL and hash of the Zip file)
        ///
        /// <param name="poEngine">The instance of the Wonka engine that has just run a RuleTree</param>
        /// <param name="poEngineInitProps">Various properties of this instance of the Wonka engine</param>
        /// <param name="poRecord">This record holds the current snapshot of the data being addressed by the metadata (mentioned in WonkaRefEnvironment)</param>
        /// <param name="poReport">This report holds the verbose results of running the RuleTree</param>
        /// <param name="psIpfsUrl">This URL points to the IPFS node that acts as the read-only gateway</param>
        /// <param name="psWriteIpfsUrl">This URL points to the IPFS node that will be used to upload the Zip file of the results</param>
        /// <param name="psChronoLogContractAddr">This address points to an instance of the ChronoLog contract</param>
        /// <returns>Unique name for entry in the ChronoLog contract</returns>
        /// </summary>
        public static async Task <string> StoreWonkaResultsAsync(this WonkaBizRulesEngine poEngine,
                                                                 Wonka.Eth.Init.WonkaEthEngineInitialization poEngineInitProps,
                                                                 WonkaProduct poRecord,
                                                                 Wonka.Eth.Extensions.RuleTreeReport poReport,
                                                                 string psIpfsUrl,
                                                                 string psWriteIpfsUrl,
                                                                 string psChronoLogContractAddr)
        {
            var RefEnv = WonkaRefEnvironment.GetInstance();

            var signer     = new EthereumMessageSigner();
            var prodMsgXml = new WonkaProductMsgWriter().WriteWonkaMsg(new WonkaProductMessage(poRecord, true));

            var inputSignature
                = signer.EncodeUTF8AndSign(prodMsgXml, new EthECKey(poEngineInitProps.EthPassword));

            string sUniqueChronoLogName = "WI-" + DateTime.Now.ToUniversalTime().ToEpochTime();

            string sZipIpfsUrl =
                await poEngine.UploadInvocationAsync(poEngineInitProps, poRecord, poReport, psIpfsUrl, psWriteIpfsUrl, sUniqueChronoLogName).ConfigureAwait(false);

            var addChronoLogEventFunction =
                new Wonka.Eth.Autogen.ChronoLog.AddChronoLogEventFunction()
            {
                UniqueName = sUniqueChronoLogName,
                EType      = "WONKA_INVOKE",
                Desc       = "", // Empty for now
                Data       = "", // Empty for now
                Hash       = inputSignature,
                Url        = sZipIpfsUrl
            };

            string sTrxHash =
                await poReport.WriteToChronoLog(poEngineInitProps, psChronoLogContractAddr, addChronoLogEventFunction).ConfigureAwait(false);

            return(sUniqueChronoLogName);
        }
Esempio n. 2
0
        /// <summary>
        ///
        /// This method will persist all data to files (metadata, rules, current data, report), zip the files, and then upload the .ZIP to IPFS
        ///
        /// <param name="poEngine">The instance of the Wonka engine that has just run</param>
        /// <param name="poRecord">This record holds the current snapshot of the data being addressed by the metadata (mentioned in WonkaRefEnvironment)</param>
        /// <param name="poReport">This report holds the verbose results of running the RuleTree</param>
        /// <param name="psUniqueChronoLogName">This will be the unique name of the ChronoLog entry</param>
        /// <param name="psTmpDirectory">The directory which will hold the files to be zipped and uploaded</param>
        /// <returns>IPFS Url of the Zip file</returns>
        /// </summary>
        public static string ZipInvocation(this WonkaBizRulesEngine poEngine, WonkaProduct poRecord, IRuleTreeReport poReport, string psUniqueChronoLogName, string psTmpDirectory = @"./tmp")
        {
            string sZipFile = "";

            DirectoryInfo tmpDirInfo = new DirectoryInfo(psTmpDirectory);

            if (!tmpDirInfo.Exists)
            {
                tmpDirInfo.Create();
            }

            var snapshotDirInfo = Directory.CreateDirectory(psTmpDirectory + "/snapshot-" + psUniqueChronoLogName);

            var rulesWriter = new WonkaBizRulesXmlWriter(poEngine);

            File.WriteAllText(snapshotDirInfo.FullName + "/" + psUniqueChronoLogName + ".rules.xml", rulesWriter.ExportXmlString());

            var prodMsgXml = new WonkaProductMsgWriter().WriteWonkaMsg(new WonkaProductMessage(poRecord, true));

            File.WriteAllText(snapshotDirInfo.FullName + "/" + psUniqueChronoLogName + ".prdmsg.xml", prodMsgXml);

            var reportXml = new WonkaRuleTreeReportWriter(poReport).ExportXmlString();

            File.WriteAllText(snapshotDirInfo.FullName + "/" + psUniqueChronoLogName + ".report.xml", reportXml);

            sZipFile = psTmpDirectory + "/" + psUniqueChronoLogName + ".zip";
            using (var zipOutputStream = new ZipOutputStream(File.Create(sZipFile)))
            {
                try
                {
                    zipOutputStream.SetLevel(5);

                    var snapshotFileList = snapshotDirInfo.GetFiles();
                    foreach (var tmpFile in snapshotFileList)
                    {
                        Int32 nSize = 0;

                        using (FileStream tmpFileStream = File.OpenRead(tmpFile.FullName))
                        {
                            ZipEntry tmpEntry = new ZipEntry(tmpFile.Name);

                            zipOutputStream.PutNextEntry(tmpEntry);
                            while (tmpFileStream.Position < tmpFileStream.Length)
                            {
                                byte[] buffer = new byte[16384];

                                nSize = tmpFileStream.Read(buffer, 0, buffer.Length);

                                zipOutputStream.Write(buffer, 0, nSize);
                                zipOutputStream.Flush();
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    // NOTE: What should be done here?
                    throw e;
                }
            }

            return(sZipFile);
        }