Skip to content

Add block time to new block events #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NBXplorer.Client/Models/NewBlockEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public uint256 PreviousBlockHash

public long Confirmations { get; set; }

public DateTimeOffset BlockTime { get; set; }

[JsonIgnore]
public override string EventType => "newblock";

Expand Down
4 changes: 3 additions & 1 deletion NBXplorer.Tests/UnitTest1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2066,8 +2066,9 @@ public async Task CanUseWebSockets(bool legacyAPI)
Assert.True(blockEvent.EventId != 0);
Assert.Equal(expectedBlockId, blockEvent.Hash);
Assert.NotEqual(0, blockEvent.Height);

Assert.NotEqual(default, blockEvent.BlockTime);
Assert.Equal(1, blockEvent.Confirmations);
Assert.NotEqual(default, blockEvent.BlockTime);

legacy?.ListenDerivationSchemes(new[] { pubkey });
await tester.SendToAddressAsync(tester.AddressOf(pubkey, "0/1"), Money.Coins(1.0m));
Expand Down Expand Up @@ -2470,6 +2471,7 @@ public async Task CanUseWebSocketsOnAddress(bool legacyAPI)
var blockEvent = await WaitBlock(connected, expectedBlockId, Cancel);
Assert.Equal(expectedBlockId, blockEvent.Hash);
Assert.NotEqual(0, blockEvent.Height);
Assert.NotEqual(default, blockEvent.BlockTime);

legacy?.ListenTrackedSources(new[] { pubkey });
tester.SendToAddress(pubkey.Address, Money.Coins(1.0m));
Expand Down
7 changes: 4 additions & 3 deletions NBXplorer/Backend/Indexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -448,14 +448,14 @@ private async Task SaveMatches(DbConnectionHelper conn, Block block, SlimChained
var seenAt = State == BitcoinDWaiterState.NBXplorerSynching
? block.Header.BlockTime
: DateTimeOffset.UtcNow;
await SaveMatches(conn, block.Transactions, slimChainedBlock, true, seenAt);
await SaveMatches(conn, block.Transactions, slimChainedBlock, true, seenAt, block.Header.BlockTime);
EventAggregator.Publish(new RawBlockEvent(block, this.Network), true);
lastIndexedBlock = slimChainedBlock;
}

SlimChainedBlock _NodeTip;

private async Task SaveMatches(DbConnectionHelper conn, List<Transaction> transactions, SlimChainedBlock slimChainedBlock, bool fireEvents, DateTimeOffset? seenAt = null)
private async Task SaveMatches(DbConnectionHelper conn, List<Transaction> transactions, SlimChainedBlock slimChainedBlock, bool fireEvents, DateTimeOffset? seenAt = null, DateTimeOffset? blockTime = null)
{
foreach (var tx in transactions)
tx.PrecomputeHash(false, true);
Expand All @@ -480,7 +480,8 @@ private async Task SaveMatches(DbConnectionHelper conn, List<Transaction> transa
Hash = slimChainedBlock.Hash,
Height = slimChainedBlock.Height,
PreviousBlockHash = slimChainedBlock.Previous,
Confirmations = confirmations
Confirmations = confirmations,
BlockTime = blockTime ?? now
};
await Repository.SaveEvent(conn, blockEvent);
EventAggregator.Publish(blockEvent);
Expand Down
5 changes: 5 additions & 0 deletions NBXplorer/wwwroot/api.json
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,11 @@
"type": "integer",
"description": "Number of confirmations (typically 1 for a new block).",
"example": 1
},
"blockTime": {
"type": "string",
"format": "date-time",
"description": "Timestamp of the block as reported by the block header."
}
}
},
Expand Down