From 962d1ac8fa24cbc80404e758b5df9015d3726794 Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Mon, 4 Dec 2017 12:52:14 +0100 Subject: [PATCH] Preparing for blockchain fork code upgrade --- Basic/Basic.csproj | 1 + Basic/Types/Common/BlockchainHash.cs | 48 +++++++++ Basic/Types/Structure/BasicDocument.cs | 97 +++++++++++++++++++ Logic/Financial/BitcoinBlockchainUpgrade.cs | 42 ++++++++ Logic/Financial/BitcoinUtility.cs | 17 ++++ Logic/Logic.csproj | 1 + Logic/Support/SystemSettings.cs | 23 +++++ .../v5/Financial/BlockchainCodeUpgrading.aspx | 13 +++ .../Financial/BlockchainCodeUpgrading.aspx.cs | 12 +++ .../BlockchainCodeUpgrading.aspx.designer.cs | 13 +++ Site/Site.csproj | 8 ++ 11 files changed, 275 insertions(+) create mode 100644 Basic/Types/Common/BlockchainHash.cs create mode 100644 Basic/Types/Structure/BasicDocument.cs create mode 100644 Logic/Financial/BitcoinBlockchainUpgrade.cs create mode 100644 Site/Pages/v5/Financial/BlockchainCodeUpgrading.aspx create mode 100644 Site/Pages/v5/Financial/BlockchainCodeUpgrading.aspx.cs create mode 100644 Site/Pages/v5/Financial/BlockchainCodeUpgrading.aspx.designer.cs diff --git a/Basic/Basic.csproj b/Basic/Basic.csproj index e80b5477..8cd79a9e 100644 --- a/Basic/Basic.csproj +++ b/Basic/Basic.csproj @@ -57,6 +57,7 @@ + diff --git a/Basic/Types/Common/BlockchainHash.cs b/Basic/Types/Common/BlockchainHash.cs new file mode 100644 index 00000000..4388d911 --- /dev/null +++ b/Basic/Types/Common/BlockchainHash.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Swarmops.Common.Enums; + +namespace Swarmops.Basic.Types.Common +{ + [Serializable] + public class BlockchainHash + { + [Obsolete("Do not call the parameterless ctor", true)] + public BlockchainHash() + { + // do not call the parameterless ctor; it is there for serialization to work + } + + public BlockchainHash(BitcoinChain chain, string hash) + { + this.Chain = chain; + this.Hash = hash; + } + + public BitcoinChain Chain { get; private set; } + public string Hash { get; private set; } + + public static BlockchainHash Parse(string input) + { + int indexOfDash = input.IndexOf('-'); + if (indexOfDash < 0) + { + throw new ArgumentException("Invalid chain hash"); + } + + string chainPart = input.Substring(0, indexOfDash); + string hashPart = input.Substring(indexOfDash + 1); + BitcoinChain chain = (BitcoinChain) Enum.Parse(typeof (BitcoinChain), chainPart); + + return new BlockchainHash(chain, hashPart); + } + + public new string ToString() + { + return this.Chain.ToString() + "-" + this.Hash; + } + } +} diff --git a/Basic/Types/Structure/BasicDocument.cs b/Basic/Types/Structure/BasicDocument.cs new file mode 100644 index 00000000..2bd3a7cd --- /dev/null +++ b/Basic/Types/Structure/BasicDocument.cs @@ -0,0 +1,97 @@ +using System; +using Swarmops.Common.Enums; +using Swarmops.Common.Interfaces; + +namespace Swarmops.Basic.Types.Structure +{ + public class BasicDocument : IHasIdentity + { + private readonly string clientFileName; + private readonly int documentId; + private readonly DocumentType documentType; + private readonly Int64 fileSize; + private readonly int foreignId; + private readonly int uploadedByPersonId; + private readonly DateTime uploadedDateTime; + private string description; + private string serverFileName; + + #region IHasIdentity Members + + public int Identity + { + get { return this.DocumentId; } + } + + #endregion + + public BasicDocument (int documentId, string serverFileName, string clientFileName, string description, + DocumentType documentType, int foreignId, Int64 fileSize, int uploadedByPersonId, DateTime uploadedDateTime) + { + this.documentId = documentId; + this.serverFileName = serverFileName; + this.clientFileName = clientFileName; + this.description = description; + this.documentType = documentType; + this.foreignId = foreignId; + this.fileSize = fileSize; + this.uploadedByPersonId = uploadedByPersonId; + this.uploadedDateTime = uploadedDateTime; + } + + public BasicDocument (BasicDocument original) : + this (original.documentId, original.serverFileName, original.clientFileName, original.description, + original.documentType, original.foreignId, original.fileSize, original.uploadedByPersonId, + original.uploadedDateTime) + { + // empty ctor apart from calling the other one + } + + public int DocumentId + { + get { return this.documentId; } + } + + public string ServerFileName + { + get { return this.serverFileName; } + protected set { this.serverFileName = value; } + } + + public string ClientFileName + { + get { return this.clientFileName; } + } + + public string Description + { + get { return this.description; } + protected set { this.description = value; } + } + + public DocumentType DocumentType + { + get { return this.documentType; } + } + + public int ForeignId + { + get { return this.foreignId; } + } + + public Int64 FileSize + { + get { return this.fileSize; } + } + + public int UploadedByPersonId + { + get { return this.uploadedByPersonId; } + } + + public DateTime UploadedDateTime + { + get { return this.uploadedDateTime; } + } + } +} \ No newline at end of file diff --git a/Logic/Financial/BitcoinBlockchainUpgrade.cs b/Logic/Financial/BitcoinBlockchainUpgrade.cs new file mode 100644 index 00000000..5e72b373 --- /dev/null +++ b/Logic/Financial/BitcoinBlockchainUpgrade.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Swarmops.Logic.Support; + +namespace Swarmops.Logic.Financial +{ + internal class BitcoinBlockchainUpgrade + { + static internal void Upgrade(int fromVersion) + { + fromVersion -= fromVersion%2; + int toVersion = BitcoinUtility.ExpectedBlockchainCodeVersion; + + while (fromVersion < toVersion) + { + SystemSettings.BlockchainCodeVersion = fromVersion + 1; // Indicates upgrade in progress + + switch (fromVersion) + { + case 0: + SplitCashFromCore(); + break; + default: + throw new NotImplementedException("Unimplemented upgrade path"); + } + + + fromVersion += 2; + } + + SystemSettings.BlockchainCodeVersion = fromVersion; + } + + static internal void SplitCashFromCore() + { + + } + } +} diff --git a/Logic/Financial/BitcoinUtility.cs b/Logic/Financial/BitcoinUtility.cs index 42588a6d..d983dd2f 100644 --- a/Logic/Financial/BitcoinUtility.cs +++ b/Logic/Financial/BitcoinUtility.cs @@ -37,6 +37,23 @@ namespace Swarmops.Logic.Financial } + /// + /// Always an even number. If an odd number, the upgrade to the next even number + /// is currently in progress, and all blockchain operations are halted. + /// + public static int ExpectedBlockchainCodeVersion => 2; + + public static bool CurrentlyUpgrading + { + get { return SystemSettings.BlockchainCodeVersion%2 == 1 ? true : false; } + } + + public static void Upgrade(int fromVersion) + { + BitcoinBlockchainUpgrade.Upgrade(fromVersion); + } + + public static void VerifyBitcoinHotWallet() { // This must only be run from the backend diff --git a/Logic/Logic.csproj b/Logic/Logic.csproj index 5e263cc1..ab4ae7d3 100644 --- a/Logic/Logic.csproj +++ b/Logic/Logic.csproj @@ -131,6 +131,7 @@ + diff --git a/Logic/Support/SystemSettings.cs b/Logic/Support/SystemSettings.cs index dada9ba2..2e30d7f1 100644 --- a/Logic/Support/SystemSettings.cs +++ b/Logic/Support/SystemSettings.cs @@ -282,6 +282,29 @@ namespace Swarmops.Logic.Support } + public static int BlockchainCodeVersion + { + get + { + string result = Persistence.Key["BlockchainCodeVersion"]; + if (!string.IsNullOrEmpty(result)) + { + try + { + return Int32.Parse(result); + } + catch (Exception) + { + return 0; + } + } + + return 0; + } + set { Persistence.Key["BlockchainCodeVersion"] = value.ToString(CultureInfo.InvariantCulture); } + } + + public static bool DatabaseInitialized { get { return !String.IsNullOrEmpty(InstallationId); } diff --git a/Site/Pages/v5/Financial/BlockchainCodeUpgrading.aspx b/Site/Pages/v5/Financial/BlockchainCodeUpgrading.aspx new file mode 100644 index 00000000..3c716319 --- /dev/null +++ b/Site/Pages/v5/Financial/BlockchainCodeUpgrading.aspx @@ -0,0 +1,13 @@ +<%@ Page Title="" Language="C#" MasterPageFile="~/Master-v5.master" AutoEventWireup="true" Inherits="Pages_v5_Finance_BlockchainCodeUpgrading" Codebehind="BlockchainCodeUpgrading.aspx.cs" %> + + + + +

A blockchain upgrade is in progress

+ + Fail symbolA blockchain upgrade is currently in progress, either within Swarmops or on the blockchain itself. Financial operations are suspended until the upgrade is completed. This could be in minutes or in hours; check the appropriate bulletins. +
+
+ + + diff --git a/Site/Pages/v5/Financial/BlockchainCodeUpgrading.aspx.cs b/Site/Pages/v5/Financial/BlockchainCodeUpgrading.aspx.cs new file mode 100644 index 00000000..5b76958c --- /dev/null +++ b/Site/Pages/v5/Financial/BlockchainCodeUpgrading.aspx.cs @@ -0,0 +1,12 @@ +using System; +using Swarmops.Frontend; + +public partial class Pages_v5_Finance_BlockchainCodeUpgrading : PageV5Base +{ + protected void Page_Load (object sender, EventArgs e) + { + PageTitle = "Upgrade in progress"; + PageIcon = "iconshock-coins-cross"; + InfoBoxLiteral = "An upgrade of the blockchain is in progress. Until the upgrade has verifiably completed, all financial operations are suspended."; + } +} \ No newline at end of file diff --git a/Site/Pages/v5/Financial/BlockchainCodeUpgrading.aspx.designer.cs b/Site/Pages/v5/Financial/BlockchainCodeUpgrading.aspx.designer.cs new file mode 100644 index 00000000..076d49aa --- /dev/null +++ b/Site/Pages/v5/Financial/BlockchainCodeUpgrading.aspx.designer.cs @@ -0,0 +1,13 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + + + +public partial class Pages_v5_Finance_BlockchainCodeUpgrading { +} diff --git a/Site/Site.csproj b/Site/Site.csproj index bcf5bafe..fc3910a8 100644 --- a/Site/Site.csproj +++ b/Site/Site.csproj @@ -216,6 +216,13 @@ SendMassMessage.aspx + + BlockchainCodeUpgrading.aspx + ASPXCodeBehind + + + BlockchainCodeUpgrading.aspx + Json-ListInvoicesOutbound.aspx ASPXCodeBehind @@ -1110,6 +1117,7 @@ +