1
0
mirror of https://github.com/bitcoin/bips.git synced 2025-06-30 12:42:43 +00:00

Merge pull request #333 from btcdrak/112update

Update BIP112 reference example
This commit is contained in:
Luke-Jr 2016-02-24 05:37:51 +00:00
commit 30fb3ae942

View File

@ -229,22 +229,23 @@ The 2-way pegged sidechain requires a new REORGPROOFVERIFY opcode, the semantics
Refer to the reference implementation, reproduced below, for the precise Refer to the reference implementation, reproduced below, for the precise
semantics and detailed rationale for those semantics. semantics and detailed rationale for those semantics.
<pre>
/* If this flag set, CTxIn::nSequence is NOT interpreted as a /* Below flags apply in the context of BIP 68 */
/* If this flag set, CTxIn::nSequence is NOT interpreted as a
* relative lock-time. */ * relative lock-time. */
static const uint32_t SEQUENCE_LOCKTIME_DISABLE_FLAG = (1 << 31); static const uint32_t SEQUENCE_LOCKTIME_DISABLE_FLAG = (1 << 31);
/* If CTxIn::nSequence encodes a relative lock-time and this flag /* If CTxIn::nSequence encodes a relative lock-time and this flag
* is set, the relative lock-time has units of 512 seconds, * is set, the relative lock-time has units of 512 seconds,
* otherwise it specifies blocks with a granularity of 1. */ * otherwise it specifies blocks with a granularity of 1. */
static const uint32_t SEQUENCE_LOCKTIME_TYPE_FLAG = (1 << 22); static const uint32_t SEQUENCE_LOCKTIME_TYPE_FLAG = (1 << 22);
/* If CTxIn::nSequence encodes a relative lock-time, this mask is /* If CTxIn::nSequence encodes a relative lock-time, this mask is
* applied to extract that lock-time from the sequence field. */ * applied to extract that lock-time from the sequence field. */
static const uint32_t SEQUENCE_LOCKTIME_MASK = 0x0000ffff; static const uint32_t SEQUENCE_LOCKTIME_MASK = 0x0000ffff;
case OP_NOP3: case OP_NOP3:
{ {
if (!(flags & SCRIPT_VERIFY_CHECKSEQUENCEVERIFY)) { if (!(flags & SCRIPT_VERIFY_CHECKSEQUENCEVERIFY)) {
// not enabled; treat as a NOP3 // not enabled; treat as a NOP3
if (flags & SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS) { if (flags & SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS) {
@ -284,10 +285,10 @@ semantics and detailed rationale for those semantics.
return set_error(serror, SCRIPT_ERR_UNSATISFIED_LOCKTIME); return set_error(serror, SCRIPT_ERR_UNSATISFIED_LOCKTIME);
break; break;
} }
bool TransactionSignatureChecker::CheckSequence(const CScriptNum& nSequence) const bool TransactionSignatureChecker::CheckSequence(const CScriptNum& nSequence) const
{ {
// Relative lock times are supported by comparing the passed // Relative lock times are supported by comparing the passed
// in operand to the sequence number of the input. // in operand to the sequence number of the input.
const int64_t txToSequence = (int64_t)txTo->vin[nIn].nSequence; const int64_t txToSequence = (int64_t)txTo->vin[nIn].nSequence;
@ -298,48 +299,39 @@ semantics and detailed rationale for those semantics.
return false; return false;
// Sequence numbers with their most significant bit set are not // Sequence numbers with their most significant bit set are not
// defined by BIP68. Testing that the transaction's sequence // consensus constrained. Testing that the transaction's sequence
// number do not have this bit set prevents using this property // number do not have this bit set prevents using this property
// to get around a CHECKSEQUENCEVERIFY check. // to get around a CHECKSEQUENCEVERIFY check.
if (txToSequence & CTxIn::SEQUENCE_LOCKTIME_DISABLE_FLAG) if (txToSequence & CTxIn::SEQUENCE_LOCKTIME_DISABLE_FLAG)
return false; return false;
// Mask off any bits that do not have BIP68 consensus-enforced meaning // Mask off any bits that do not have consensus-enforced meaning
// before doing the integer comparisons of ::VerifySequence. // before doing the integer comparisons
const uint32_t nLockTimeMask = CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG const uint32_t nLockTimeMask = CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG | CTxIn::SEQUENCE_LOCKTIME_MASK;
| CTxIn::SEQUENCE_LOCKTIME_MASK; const int64_t txToSequenceMasked = txToSequence & nLockTimeMask;
const CScriptNum nSequenceMasked = nSequence & nLockTimeMask;
if (!::VerifySequence(txToSequence & nLockTimeMask, // There are two kinds of nSequence: lock-by-blockheight
CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG,
nSequence & nLockTimeMask))
return false;
return true;
}
static bool VerifySequence(int64_t txToSequence, int64_t nThreshold, const CScriptNum& nSequence)
{
// There are two kinds of nLockTime: lock-by-blockheight
// and lock-by-blocktime, distinguished by whether // and lock-by-blocktime, distinguished by whether
// nSequence < nThreshold (CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG). // nSequenceMasked < CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG.
// //
// We want to compare apples to apples, so fail the script // We want to compare apples to apples, so fail the script
// unless the type of nSequence being tested is the same as // unless the type of nSequenceMasked being tested is the same as
// the nSequence in the transaction. // the nSequenceMasked in the transaction.
if (!( if (!(
(txToSequence < nThreshold && nSequence < nThreshold) || (txToSequenceMasked < CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG && nSequenceMasked < CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG) ||
(txToSequence >= nThreshold && nSequence >= nThreshold) (txToSequenceMasked >= CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG && nSequenceMasked >= CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG)
)) ))
return false; return false;
// Now that we know we're comparing apples-to-apples, the // Now that we know we're comparing apples-to-apples, the
// comparison is a simple numeric one. // comparison is a simple numeric one.
if (nSequence > txToSequence) if (nSequenceMasked > txToSequenceMasked)
return false; return false;
return true; return true;
} }
</pre>
==Reference Implementation== ==Reference Implementation==