Skip to content

Commit

Permalink
Initial fixes to compilation (stratisproject#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
dangershony authored Dec 9, 2019
1 parent 2488c9e commit dd0b514
Show file tree
Hide file tree
Showing 16 changed files with 96 additions and 54 deletions.
5 changes: 4 additions & 1 deletion src/FederationSetup/GenesisMiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ public static Block MineGenesisBlock(SmartContractPoAConsensusFactory consensusF

Transaction txNew = consensusFactory.CreateTransaction();
txNew.Version = (uint)version;
txNew.Time = unixTime;

if (txNew is IPosTransactionWithTime posTx)
posTx.Time = unixTime;

txNew.AddInput(new TxIn()
{
ScriptSig = new Script(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1029,6 +1029,7 @@ public void CheckKernel_ValidKernelCheck_DoesNotThrowConsensusError()

var outPoint = new OutPoint(transaction, 1);
var headerbits = Target.Difficulty1.ToCompact();
// TODO: Is this intentionally using the time of the stub coinbase instead of the coinstake? This looks like a bug
var transactionTime = ((PosTransaction)stakableHeader.Block.Transactions[0]).Time;

this.stakeValidator.CheckKernel(new PosRuleContext(), header, headerbits, transactionTime, outPoint);
Expand Down
1 change: 1 addition & 0 deletions src/Stratis.Bitcoin.Features.Wallet/WalletRPCController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ public GetTransactionModel GetTransaction(string txid)
string hex;
if (transactionFromStore != null)
{
// TODO: Use block header time only. The transaction times will need to be uniformly set to a fixed value when an anti-malleability softfork activates
if (transactionFromStore is IPosTransactionWithTime posTrx)
transactionTime = Utils.UnixTimeToDateTime(posTrx.Time);
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ public async Task GetTransactionOnUnconfirmedTransactionAsync()
resultSendingWallet.BlockIndex.Should().BeNull();
resultSendingWallet.BlockTime.Should().BeNull();
resultSendingWallet.TimeReceived.Should().BeGreaterThan((DateTimeOffset.Now - TimeSpan.FromMinutes(1)).ToUnixTimeSeconds());
resultSendingWallet.TransactionTime.Should().Be(((PosTransaction)trx).Time);
resultSendingWallet.Details.Count.Should().Be(1);

GetTransactionDetailsModel detailsSendingWallet = resultSendingWallet.Details.Single();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void RemoveTransactions_When_OverwritingSpendDetails()

// Create a spending transaction that spends transaction A
Transaction transactionB = this.network.CreateTransaction();
transactionB.Time = transactionA.Time + 1;
((PosTransaction)transactionB).Time = ((PosTransaction)transactionA).Time + 1;
transactionB.AddInput(transactionA, 0);
transactionB.AddOutput(new TxOut(Money.Coins(5), this.federationMultiSigAddress));
federationWalletManager.ProcessTransaction(transactionB);
Expand All @@ -74,7 +74,7 @@ public void RemoveTransactions_When_OverwritingSpendDetails()

// Create another spending transaction that also spends transaction A
Transaction transactionC = this.network.CreateTransaction();
transactionC.Time = transactionB.Time + 1;
((PosTransaction)transactionC).Time = ((PosTransaction)transactionB).Time + 1;
transactionC.AddInput(transactionA, 0);
transactionC.AddOutput(new TxOut(Money.Coins(5), this.federationMultiSigAddress));
federationWalletManager.ProcessTransaction(transactionC);
Expand Down
8 changes: 5 additions & 3 deletions src/Stratis.Features.FederatedPeg/TargetChain/SigningUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,12 @@ public static bool TemplatesMatch(Network network, Transaction partialTransactio
{
if (network.Consensus.IsProofOfStake)
{
if (partialTransaction1.Time != partialTransaction2.Time)
{
// As a proof of stake network will be using a PosConsensusFactory, this should never happen, but it is checked for safety.
if (!(partialTransaction1 is IPosTransactionWithTime tx1) || !(partialTransaction2 is IPosTransactionWithTime tx2))
return false;

if (tx1.Time != tx2.Time)
return false;
}
}

if ((partialTransaction1.Inputs.Count != partialTransaction2.Inputs.Count) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ public List<WithdrawalModel> GetHistory(int maximumEntriesToReturn)
var result = new List<WithdrawalModel>();
ICrossChainTransfer[] transfers = this.crossChainTransferStore.GetTransfersByStatus(new[] { CrossChainTransferStatus.SeenInBlock });

foreach (ICrossChainTransfer transfer in transfers.OrderByDescending(t => t.PartialTransaction.Time))
// TODO: Need to check if this is only used for wallet UI purposes and has no consensus implications
foreach (ICrossChainTransfer transfer in transfers.OrderByDescending(t => t.BlockHeight)) // t.PartialTransaction.Time
{
if (maximumEntriesToReturn-- <= 0)
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ private void AddTransactionToWallet(Transaction transaction, TxOut utxo, int? bl
BlockHeight = blockHeight,
BlockHash = blockHash,
Id = transactionHash,
CreationTime = DateTimeOffset.FromUnixTimeSeconds(block?.Header.Time ?? transaction.Time),
CreationTime = DateTimeOffset.FromUnixTimeSeconds(block?.Header.Time ?? this.dateTimeProvider.GetTime()),
Index = index,
ScriptPubKey = script
};
Expand Down Expand Up @@ -843,7 +843,7 @@ private SpendingDetails BuildSpendingDetails(Transaction transaction,
{
TransactionId = transaction.GetHash(),
Payments = payments,
CreationTime = DateTimeOffset.FromUnixTimeSeconds(block?.Header.Time ?? transaction.Time),
CreationTime = DateTimeOffset.FromUnixTimeSeconds(block?.Header.Time ?? this.dateTimeProvider.GetTime()),
BlockHeight = blockHeight,
BlockHash = blockHash,
Transaction = (blockHeight > 0) ? null : transaction,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,22 @@ public bool ProcessTransactions(IEnumerable<Transaction> transactions, HashHeigh
{
TxIn txIn = tx.Inputs[i];

if (transactionsOfInterest.Contains(txIn.PrevOut, out HashSet<AddressIdentifier> addresses))
if (!transactionsOfInterest.Contains(txIn.PrevOut, out HashSet<AddressIdentifier> addresses))
continue;

// Record our outputs that are being spent.
foreach (AddressIdentifier address in addresses)
{
// Record our outputs that are being spent.
foreach (AddressIdentifier address in addresses)
RecordSpend(block, txIn, address.ScriptPubKey, tx.IsCoinBase | tx.IsCoinStake, tx.Time, tx.TotalOut, txId, i);
// TODO: Probably need to derive time explicitly from the block header. How were PoW transactions being handled here?
long time = 0;
if (tx is IPosTransactionWithTime posTx)
time = posTx.Time;

additions = true;
addSpendTx = true;
RecordSpend(block, txIn, address.ScriptPubKey, tx.IsCoinBase | tx.IsCoinStake, time, tx.TotalOut, txId, i);
}

additions = true;
addSpendTx = true;
}

// Build temp.Outputs.
Expand All @@ -119,41 +126,46 @@ public bool ProcessTransactions(IEnumerable<Transaction> transactions, HashHeigh
bool containsAddress = addressesOfInterest.Contains(pubKeyScript, out AddressIdentifier address);

// Paying to one of our addresses?
if (addSpendTx || containsAddress)
if (!addSpendTx && !containsAddress)
continue;

// Check if top-up is required.
if (containsAddress && address != null)
{
// Check if top-up is required.
if (containsAddress && address != null)
// Get the top-up tracker that applies to this account and address type.
ITopUpTracker tracker = this.GetTopUpTracker(address);
if (!tracker.IsWatchOnlyAccount)
{
// Get the top-up tracker that applies to this account and address type.
ITopUpTracker tracker = this.GetTopUpTracker(address);
if (!tracker.IsWatchOnlyAccount)
// If an address inside the address buffer is being used then top-up the buffer.
while (address.AddressIndex >= tracker.NextAddressIndex)
{
// If an address inside the address buffer is being used then top-up the buffer.
while (address.AddressIndex >= tracker.NextAddressIndex)
{
AddressIdentifier newAddress = tracker.CreateAddress();

// Add the new address to our addresses of interest.
addressesOfInterest.AddTentative(Script.FromHex(newAddress.ScriptPubKey),
new AddressIdentifier()
{
WalletId = newAddress.WalletId,
AccountIndex = newAddress.AccountIndex,
AddressType = newAddress.AddressType,
AddressIndex = newAddress.AddressIndex
});
}
AddressIdentifier newAddress = tracker.CreateAddress();

// Add the new address to our addresses of interest.
addressesOfInterest.AddTentative(Script.FromHex(newAddress.ScriptPubKey),
new AddressIdentifier()
{
WalletId = newAddress.WalletId,
AccountIndex = newAddress.AccountIndex,
AddressType = newAddress.AddressType,
AddressIndex = newAddress.AddressIndex
});
}
}
}

// Record outputs received by our wallets.
this.RecordReceipt(block, pubKeyScript, txOut, tx.IsCoinBase | tx.IsCoinStake, tx.Time, txId, i, containsAddress && address.AddressType == 1);
// TODO: Probably need to derive time explicitly from the block header. How were PoW transactions being handled here?
long time = 0;
if (tx is IPosTransactionWithTime posTx)
time = posTx.Time;

additions = true;
// Record outputs received by our wallets.
this.RecordReceipt(block, pubKeyScript, txOut, tx.IsCoinBase | tx.IsCoinStake, time, txId, i, containsAddress && address.AddressType == 1);

if (containsAddress)
transactionsOfInterest.AddTentative(new OutPoint(txId, i), address);
}
additions = true;

if (containsAddress)
transactionsOfInterest.AddTentative(new OutPoint(txId, i), address);
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/Stratis.Sidechains.Networks/CirrusNetwork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ public static NetworksSelector NetworksSelector
public static Block CreateGenesis(SmartContractCollateralPoAConsensusFactory consensusFactory, uint genesisTime, uint nonce, uint bits, int version, Money reward, string coinbaseText)
{
Transaction genesisTransaction = consensusFactory.CreateTransaction();
genesisTransaction.Time = genesisTime;

// TODO: If the PoW/PoA networks do not use a PosConsensusFactory, their transactions will now be missing timestamps that were historically included for hashing.
if (genesisTransaction is IPosTransactionWithTime posTx)
posTx.Time = genesisTime;

genesisTransaction.Version = 1;
genesisTransaction.AddInput(new TxIn()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ public void NoBalance_TxValue1_TransferValue1()
// Condensing tx generated. 1 input from tx and 2 outputs - 1 for each contract and receiver
Transaction internalTransaction = this.transferProcessor.Process(stateMock.Object, contractAddress, txContextMock.Object, transferInfos, false);
Assert.NotNull(internalTransaction);
Assert.Equal(txContextMock.Object.Time, internalTransaction.Time);
if (internalTransaction is IPosTransactionWithTime posTx)
Assert.Equal(txContextMock.Object.Time, posTx.Time);
Assert.Single(internalTransaction.Inputs);
Assert.Equal(2, internalTransaction.Outputs.Count);
Assert.Equal(txContextMock.Object.TransactionHash, internalTransaction.Inputs[0].PrevOut.Hash);
Expand Down Expand Up @@ -242,7 +243,8 @@ public void HasBalance_TxValue1_TransferValue0()
// Condensing tx generated. 2 inputs. Current tx and stored spendable output. 1 output.
Transaction internalTransaction = this.transferProcessor.Process(stateMock.Object, contractAddress, txContextMock.Object, transferInfos, false);
Assert.NotNull(internalTransaction);
Assert.Equal(txContextMock.Object.Time, internalTransaction.Time);
if (internalTransaction is IPosTransactionWithTime posTx)
Assert.Equal(txContextMock.Object.Time, posTx.Time);
Assert.Equal(2, internalTransaction.Inputs.Count);
Assert.Single(internalTransaction.Outputs);
Assert.Equal(txContextMock.Object.TransactionHash, internalTransaction.Inputs[0].PrevOut.Hash);
Expand Down Expand Up @@ -292,7 +294,8 @@ public void HasBalance_TxValue0_TransferValue1()
// Condensing tx generated. 1 input. 2 outputs for each receiver and contract.
Transaction internalTransaction = this.transferProcessor.Process(stateMock.Object, contractAddress, txContextMock.Object, transferInfos, false);
Assert.NotNull(internalTransaction);
Assert.Equal(txContextMock.Object.Time, internalTransaction.Time);
if (internalTransaction is IPosTransactionWithTime posTx)
Assert.Equal(txContextMock.Object.Time, posTx.Time);
Assert.Single(internalTransaction.Inputs);
Assert.Equal(2, internalTransaction.Outputs.Count);
Assert.Equal(new uint256(1), internalTransaction.Inputs[0].PrevOut.Hash);
Expand Down Expand Up @@ -345,7 +348,8 @@ public void HasBalance_TxValue1_TransferValue1()
// Condensing tx generated. 2 inputs from currently stored utxo and current tx. 2 outputs for each receiver and contract.
Transaction internalTransaction = this.transferProcessor.Process(stateMock.Object, contractAddress, txContextMock.Object, transferInfos, false);
Assert.NotNull(internalTransaction);
Assert.Equal(txContextMock.Object.Time, internalTransaction.Time);
if (internalTransaction is IPosTransactionWithTime posTx)
Assert.Equal(txContextMock.Object.Time, posTx.Time);
Assert.Equal(2, internalTransaction.Inputs.Count);
Assert.Equal(2, internalTransaction.Outputs.Count);
Assert.Equal(new uint256(123), internalTransaction.Inputs[0].PrevOut.Hash);
Expand Down Expand Up @@ -425,7 +429,8 @@ public void Transfers_Summed_Correctly()
// Condensing tx generated. 1 input. 3 outputs with consolidated balances.
Transaction internalTransaction = this.transferProcessor.Process(stateMock.Object, contractAddress, txContextMock.Object, transferInfos, false);
Assert.NotNull(internalTransaction);
Assert.Equal(txContextMock.Object.Time, internalTransaction.Time);
if (internalTransaction is IPosTransactionWithTime posTx)
Assert.Equal(txContextMock.Object.Time, posTx.Time);
Assert.Single(internalTransaction.Inputs);
Assert.Equal(3, internalTransaction.Outputs.Count);
Assert.Equal(new uint256(1), internalTransaction.Inputs[0].PrevOut.Hash);
Expand Down Expand Up @@ -463,7 +468,8 @@ public void Execution_Failure_With_Value_No_Transfers_Creates_Refund()
string outputAddress = PayToPubkeyHashTemplate.Instance.ExtractScriptPubKeyParameters(refundTransaction.Outputs[0].ScriptPubKey).GetAddress(this.network).ToString();

Assert.Equal(txContextMock.Object.Sender.ToBase58Address(this.network), outputAddress);
Assert.Equal(txContextMock.Object.Time, refundTransaction.Time);
if (refundTransaction is IPosTransactionWithTime posTx)
Assert.Equal(txContextMock.Object.Time, posTx.Time);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ public Transaction Process(IStateRepository stateSnapshot,
private Transaction CreateRefundTransaction(IContractTransactionContext transactionContext)
{
Transaction tx = this.network.CreateTransaction();
tx.Time = transactionContext.Time;

if (tx is IPosTransactionWithTime posTx)
posTx.Time = transactionContext.Time;

// Input from contract call
var outpoint = new OutPoint(transactionContext.TransactionHash, transactionContext.Nvout);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ public uint Time
{
get
{
return this.transaction.Time;
// TODO: Is a zero fallback value acceptable here?
return (this.transaction is IPosTransactionWithTime posTx) ? posTx.Time : 0;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ public Transaction CreateCondensingTransaction()
private Transaction BuildTransaction()
{
Transaction tx = this.network.CreateTransaction();
tx.Time = this.transactionContext.Time; // set to time of actual transaction.

if (tx is IPosTransactionWithTime posTx)
posTx.Time = this.transactionContext.Time; // set to time of actual transaction.

foreach (ContractUnspentOutput vin in this.unspents)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,9 @@ private Transaction BuildTransferContractTransaction(CoreNode scSender, Transact
Transaction transferContractTransaction = scSender.FullNode.NodeService<IWalletTransactionHandler>().BuildTransaction(txBuildContext);

var updatedTransaction = scSender.FullNode.Network.CreateTransaction();
updatedTransaction.Time = (uint)scSender.FullNode.NodeService<IDateTimeProvider>().GetAdjustedTimeAsUnixTimestamp();

if (updatedTransaction is IPosTransactionWithTime posTx)
posTx.Time = (uint)scSender.FullNode.NodeService<IDateTimeProvider>().GetAdjustedTimeAsUnixTimestamp();

foreach (var txIn in transferContractTransaction.Inputs)
{
Expand Down
Loading

0 comments on commit dd0b514

Please sign in to comment.