What is the default nLocktime for a transaction in Bitcoin Core?
Wallet source code
In version 23.0 the wallet\spend.c
includes this
/**
* Return a height-based locktime for new transactions (uses the height of the
* current chain tip unless we are not synced with the current chain
*/
static uint32_t GetLocktimeForNewTransaction(interfaces::Chain& chain, const uint256& block_hash, int block_height)
{
uint32_t locktime;
// Discourage fee sniping.
//
// For a large miner the value of the transactions in the best block and
// the mempool can exceed the cost of deliberately attempting to mine two
// blocks to orphan the current best block. By setting nLockTime such that
// only the next block can include the transaction, we discourage this
// practice as the height restricted and limited blocksize gives miners
// considering fee sniping fewer options for pulling off this attack.
//
// A simple way to think about this is from the wallet's point of view we
// always want the blockchain to move forward. By setting nLockTime this
// way we're basically making the statement that we only want this
// transaction to appear in the next block; we don't want to potentially
// encourage reorgs by allowing transactions to appear at lower heights
// than the next block in forks of the best chain.
//
// Of course, the subsidy is high enough, and transaction volume low
// enough, that fee sniping isn't a problem yet, but by implementing a fix
// now we ensure code won't be written that makes assumptions about
// nLockTime that preclude a fix later.
if (IsCurrentForAntiFeeSniping(chain, block_hash)) {
locktime = block_height;
So bitcoin core sets locktime to the current block height to discourage fee sniping.
Release Notes
Release notes for version 0.11 (updated Aug 2015) say
Wallet
#2340 811c71d Discourage fee sniping with nLockTime
When using RPCs
Locktime is zero when transaction is created manually using RPCs like createpsbt
or createrawtransaction
. Else a random nearest block height is used in bitcoin core. (Source: OP experimentation)
Documented meaning of Locktime
It may help to consider the meaning of Locktime
The Protocol Documentation says
The block number or timestamp at which this transaction is unlocked:
Value |
Description |
0 |
Not locked |
< 500000000 |
Block number at which this transaction is unlocked |
>= 500000000 |
UNIX timestamp at which this transaction isunlocked |
If all TxIn inputs have final (0xffffffff) sequence numbers then
lock_time is irrelevant. Otherwise, the transaction may not be added
to a block until after lock_time (see NLockTime).
If we look at some transactions in block 654,915 we can see that most transactions have either no lock (0) or a block number ...
C> blockchain -file blk02300.dat -block 100
Transactions in block 00000000000000000002FC97B15D50E38CEB0C6A83DA4344CE836B01FC841EEA
No. |
V |
Type |
Ins |
Outs |
Lock |
Satoshi |
Id |
1 |
1 |
Segwit |
1 |
3 |
0 |
743862941 |
36FD1CE479F30EF1CA1E44F7B2CF63D21C5CA8DBB04C1D5551A26735DB0266B8 |
2 |
1 |
Legacy |
1 |
2 |
0 |
99992768 |
D889E605D3DD5986C89A66F446FEB228A7BCE58278C5F62EED579DB730148001 |
3 |
1 |
Segwit |
3 |
1 |
0 |
7685144 |
268705614DCB68AED6A3750172F445EDCE6179D149BE769AF08D253B7A21F22D |
4 |
2 |
Legacy |
2 |
1 |
0 |
7907240 |
F3E237ED279F039DA0FAAAF5285362FAD18BADFA9E97455007F39D2C0D275112 |
5 |
2 |
Legacy |
6 |
36 |
654900 |
5739473461 |
8F5B79B4CAC8EEF4525996E7B0B1B02DF83F9199816F27BA7FDD3E57C098D069 |
6 |
2 |
Segwit |
1 |
1 |
0 |
1310690 |
819977BFF99145BCD38024FE0A3BC5B03A82A0A5FAA4A8A8073DB2D1893CC4D0 |
7 |
1 |
Segwit |
1 |
3 |
0 |
1376557 |
378F17EB675E8998D9E1B4283E2AA3E8FB0213827296E6FAB54C8ED78C5E3777 |
8 |
1 |
Segwit |
2 |
5 |
0 |
558656062 |
800C2349DF26295A3AFFBAD35CBB98D82A26AD7F69C8A6BF78ACFE4483414130 |
9 |
2 |
Legacy |
1 |
2 |
0 |
9845471 |
119308B6C68AFB33D621202B704EB3D77867C7CDF7496FBDDB3176EA33481992 |
10 |
2 |
Legacy |
1 |
2 |
0 |
9845471 |
618CD9BBE59448307C2941183A6AB7AD9DB1837B5139BDFC578108D5B710D980 |
11 |
1 |
Segwit |
1 |
1 |
0 |
3469300 |
28AAF790C652C2B07F67FF4FD0E37997CC562B8DB92633DF5E8684399E15DACB |
12 |
1 |
Legacy |
1 |
1 |
0 |
375540 |
1EE38AFEB454980F694E654713D01188EBCC91BFC4213874CAA5376C0B37CD66 |
13 |
1 |
Legacy |
1 |
1 |
0 |
223479 |
FE02E255EF4200A1D16C5DEBB442933C4A22EA544E55452B905D47EA99ED9675 |
14 |
1 |
Legacy |
1 |
1 |
0 |
1127700 |
CB227B308DC46B4B312BAE6704D6D3B7E2FC8CE3CB7B17E26BB1DB9EE5BAEFB8 |
15 |
1 |
Segwit |
1 |
1 |
0 |
877025 |
EA1871DC8D71A2C8B9224DC3937DF90324AFBD3D266EBBD27AC7A4FE2FB15F89 |
16 |
1 |
Segwit |
1 |
2 |
0 |
40824845 |
2C0607F2B4FA6A15CABBEDABAFDA198CC4A98D18EA134963CAC08E26FF31C3E8 |
17 |
2 |
Legacy |
1 |
2 |
654914 |
550200 |
24EBB2CB89EAE962E89939D48BD35D731791D1ABE8AC911FC8909BCEB299D9F9 |
18 |
2 |
Legacy |
1 |
2 |
654914 |
134435250 |
A7E6E408D619DEF6E43D00B03ACFD247DE7268EB34E751D437BB03961954DFF6 |
19 |
2 |
Segwit |
1 |
2 |
654862 |
463598 |
DAF9BF664D3C4CFEB1D625351922C51867EE1C2A648BBE5281D5505D7DE54ECF |
20 |
1 |
Segwit |
2 |
2 |
0 |
6316861 |
7FBD3F3079E0AA2404D28800329974F2D9C2D1115E3BE39DAD4898CFC123C8BC |
I'd guess most wallets make the default 0 since my quick look at this block seemed to show about 90% with that value.
Footnote: If we assume general use of block height in locktime is consistent with the approach in Bitcoin core, we can probably use this to see how fee rate affected delays in getting a transaction mined. Transactions 17 and 18 above were mined quickly, transaction 19 probably had to wait 52 blocks? (8-9 hours?)