public static SmartTransaction FromLine(string line, Network expectedNetwork) { line = Guard.NotNullOrEmptyOrWhitespace(nameof(line), line, trim: true); expectedNetwork = Guard.NotNull(nameof(expectedNetwork), expectedNetwork); var parts = line.Split(':', StringSplitOptions.None).Select(x => x.Trim()).ToArray(); var transactionString = parts[1]; Transaction transaction = Transaction.Parse(transactionString, expectedNetwork); try { // First is redundand txhash serialization. var heightString = parts[2]; var blockHashString = parts[3]; var blockIndexString = parts[4]; var labelString = parts[5]; var firstSeenString = parts[6]; var isReplacementString = parts[7]; if (!Height.TryParse(heightString, out Height height)) { height = Height.Unknown; } if (!uint256.TryParse(blockHashString, out uint256 blockHash)) { blockHash = null; } if (!int.TryParse(blockIndexString, out int blockIndex)) { blockIndex = 0; } var label = new SmartLabel(labelString); DateTimeOffset firstSeen = default; if (long.TryParse(firstSeenString, out long unixSeconds)) { firstSeen = DateTimeOffset.FromUnixTimeSeconds(unixSeconds); } if (!bool.TryParse(isReplacementString, out bool isReplacement)) { isReplacement = false; } return(new SmartTransaction(transaction, height, blockHash, blockIndex, label, isReplacement, firstSeen)); } catch (Exception ex) { Logger.LogDebug(ex); return(new SmartTransaction(transaction, Height.Unknown)); } }
/// <summary> /// Update the transaction with the data acquired from another transaction. (For example merge their labels.) /// </summary> public bool TryUpdate(SmartTransaction tx) { var updated = false; // If this is not the same tx, then don't update. if (this != tx) { throw new InvalidOperationException($"{GetHash()} != {tx.GetHash()}"); } // Set the height related properties, only if confirmed. if (tx.Confirmed) { if (Height != tx.Height) { Height = tx.Height; updated = true; } if (tx.BlockHash != null && BlockHash != tx.BlockHash) { BlockHash = tx.BlockHash; BlockIndex = tx.BlockIndex; updated = true; } } // Always the earlier seen is the firstSeen. if (tx.FirstSeen < FirstSeen) { FirstSeen = tx.FirstSeen; updated = true; } // Merge labels. if (Label != tx.Label) { Label = SmartLabel.Merge(Label, tx.Label); updated = true; } return(updated); }
public SmartTransaction(Transaction transaction, Height height, uint256 blockHash = null, int blockIndex = 0, SmartLabel label = null, bool isReplacement = false, DateTimeOffset firstSeen = default) { Transaction = transaction; Label = label ?? SmartLabel.Empty; SetHeight(height, blockHash, blockIndex); FirstSeen = firstSeen == default ? DateTimeOffset.UtcNow : firstSeen; IsReplacement = isReplacement; }