一、相对时间锁(nSequence)和绝对时间锁(nLockTime)的互锁
1、如果交易中的所有输入(UTXO)的nSequence都等于0xffffffff(2^32 – 1 = 4294967295):不激活nSequence,也不激活nLockTime(交易设置的nLockTime将会被忽略,即绝对时间锁无效)。
建议设置成0xfffffffd(2^32 – 3 = 4294967293),既能激活nLockTime(如果不想启用nLockTime,可以把nLockTime设置成0),又能启用RBF(Replace-By-Fee),当给的手续费比较低导致交易迟迟无法被打包,可以再重新发起一笔更高收费的交易来替换掉先前广播的低手续费交易。
2、如果交易中有一个或多个输入的nSequence小于0xffffffff,且大于等于0x80000000(2^31 = 2147483648):不激活nSequence,只激活nLockTime。
3、如果交易中有一个或多个输入的nSequence小于0x80000000:激活nSequence,也激活nLockTime。
二、感谢@迦楼罗王2010
第二版是2017年发表的,里面有很多已经过时了. 现在nSenquce有好几个功能,与bip68和bip112相关, 具体计算变成了这样,最新的源码是这么写的
bool TransactionSignatureChecker::CheckSequence(const CScriptNum& nSequence) const
{
// Relative lock times are supported by comparing the passed
// in operand to the sequence number of the input.
const int64_t txToSequence = (int64_t)txTo->vin[nIn].nSequence;
// Fail if the transaction’s version number is not set high
// enough to trigger BIP 68 rules.
if (static_cast<uint32_t>(txTo->nVersion) < 2)
return false;
// Sequence numbers with their most significant bit set are not
// consensus constrained. Testing that the transaction’s sequence
// number do not have this bit set prevents using this property
// to get around a CHECKSEQUENCEVERIFY check.
if (txToSequence & CTxIn::SEQUENCE_LOCKTIME_DISABLE_FLAG)
return false;
// Mask off any bits that do not have consensus-enforced meaning
// before doing the integer comparisons
const uint32_t nLockTimeMask = CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG | CTxIn::SEQUENCE_LOCKTIME_MASK;
const int64_t txToSequenceMasked = txToSequence & nLockTimeMask;
const CScriptNum nSequenceMasked = nSequence & nLockTimeMask;
// There are two kinds of nSequence: lock-by-blockheight
// and lock-by-blocktime, distinguished by whether
// nSequenceMasked < CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG.
//
// We want to compare apples to apples, so fail the script
// unless the type of nSequenceMasked being tested is the same as
// the nSequenceMasked in the transaction.
if (!(
(txToSequenceMasked < CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG && nSequenceMasked < CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG) ||
(txToSequenceMasked >= CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG && nSequenceMasked >= CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG)
))
return false;
// Now that we know we’re comparing apples-to-apples, the
// comparison is a simple numeric one.
if (nSequenceMasked > txToSequenceMasked)
return false;
return true;
}, 这是解析,解析出来了再根据结果去使用, 按照最新白话文归纳就是这样:
These are the most common settings:
<=0xFFFFFFFE — Locktime.
This setting enables the transaction’s locktime field to be used.
<=0xFFFFFFFD — Replace-By-Fee (RBF).
This setting enables the RBF feature, which allows you to replace a transaction with a higher-fee one if it’s still in the mempool.
<=0xEFFFFFFF — Relative Locktime.
This setting allows you to set a locktime on the transaction relative to when the output being spent was mined.
0x00000000 to 0x0000FFFF — Blocks. Set the relative locktime as a number of blocks.
0x00400000 to 0x0040FFFF — Time. Set the relative locktime as a number of seconds.
A popular choice is to use 0xFFFFFFFD for your sequence fields, as this enables both the locktime field (in case you want to use it) and also replace-by-fee (which is generally useful).
三、
脑钱包增加了手动输入【Sequence – 相对时间锁】功能。
默认将交易所有输入(UTXO)的nSequence设置成相同值0xfffffffd(2^32 – 3 = 4294967293):
(1)既激活了RBF(Replace-By-Fee)功能,当给的手续费比较低导致交易迟迟无法被打包,可以再重新发起一笔更高收费的交易来替换掉先前广播的低手续费交易;
(2)又激活了绝对时间锁(nLockTime),可用于构建用于遗产继承的交易字符串。
如果想设置成不同值,手动在JSON交易字符串中修改。
下载脑钱包方法:
(1)电脑。https://startbitcoin.org/brainwallet/ 点击右键菜单,保存网页即可。
备用链接:https://happybole.github.io/brainwallet/
(2)手机。去github下载最新版本https://github.com/happybole/brainwallet
下载下来的脑钱包可以断网脱机运行,当冷钱包使用。