Skip to content

Commit 750f5e5

Browse files
committed
BIP 8: Refactor back to wholly-contained spec
This partially reverts 7f6a0f8
1 parent 7abb1c1 commit 750f5e5

File tree

4 files changed

+176
-9
lines changed

4 files changed

+176
-9
lines changed

README.mediawiki

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Those proposing changes should consider that ultimately consent may rest with th
3030
|-
3131
| [[bip-0008.mediawiki|8]]
3232
|
33-
| Version bits with guaranteed lock-in
33+
| Version bits 2017
3434
| Shaolin Fry
3535
| Informational
3636
| Draft

bip-0008.mediawiki

Lines changed: 124 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<pre>
22
BIP: 8
3-
Title: Version bits with guaranteed lock-in
3+
Title: Version bits 2017
44
Author: Shaolin Fry <[email protected]>
55
Comments-Summary: No comments yet.
66
Comments-URI: https://github.com/bitcoin/bips/wiki/Comments:BIP-0008
@@ -13,7 +13,7 @@
1313

1414
==Abstract==
1515

16-
This document specifies an extension to [[bip-0009.mediawiki|BIP9]] that introduces an additional activation parameter to guarantee activation of backward-compatible changes (further called "soft forks").
16+
This document specifies an alternative to [[bip-0009.mediawiki|BIP9]] that introduces an additional activation parameter to guarantee activation of backward-compatible changes (further called "soft forks").
1717

1818
==Motivation==
1919

@@ -23,21 +23,105 @@ This specification provides a way to optionally guarantee lock-in at the end of
2323

2424
==Specification==
2525

26-
This specification is the same as [[bip-0009.mediawiki|BIP9]] except from the STARTED state, there is no FAILED condition. The state transition from '''STARTED''' to '''LOCKED_IN''' will occur under two condition:
26+
Each soft fork deployment is specified by the following per-chain parameters (further elaborated below):
2727

28-
The first is when the threshold of blocks signalling is reached as per BIP9. The second is if the timeout is still '''STARTED'''.
28+
# The '''name''' specifies a very brief description of the soft fork, reasonable for use as an identifier. For deployments described in a single BIP, it is recommended to use the name "bipN" where N is the appropriate BIP number.
29+
# The '''bit''' determines which bit in the nVersion field of the block is to be used to signal the soft fork lock-in and activation. It is chosen from the set {0,1,2,...,28}.
30+
# The '''starttime''' specifies a minimum median time past of a block at which the bit gains its meaning.
31+
# The '''timeout''' specifies a time at which the deployment is considered failed. If the median time past of a block >= timeout and the soft fork has not yet locked in (including this block's bit state), the deployment is considered failed on all descendants of the block.
32+
# The '''lockinontimeout''' boolean if set to true, will transition state to '''LOCKED_IN''' at timeout if not already '''LOCKED_IN''' or '''ACTIVE'''.
33+
34+
===Selection guidelines===
35+
36+
The following guidelines are suggested for selecting these parameters for a soft fork:
37+
38+
# '''name''' should be selected such that no two softforks, concurrent or otherwise, ever use the same name.
39+
# '''bit''' should be selected such that no two concurrent softforks use the same bit.
40+
# '''starttime''' should be set to some date in the future, approximately one month after a software release date including the soft fork. This allows for some release delays, while preventing triggers as a result of parties running pre-release software.
41+
# '''timeout''' should be 1 year (31536000 seconds) after starttime.
42+
# '''lockinontimeout''' should be set to true for any softfork that isn't exclusively for miner benefit.
43+
44+
A later deployment using the same bit is possible as long as the starttime is after the previous one's
45+
timeout or activation, but it is discouraged until necessary, and even then recommended to have a pause in between to detect buggy software.
46+
47+
===States===
48+
49+
With each block and soft fork, we associate a deployment state. The possible states are:
50+
51+
# '''DEFINED''' is the first state that each soft fork starts out as. The genesis block is by definition in this state for each deployment.
52+
# '''STARTED''' for blocks past the starttime.
53+
# '''LOCKED_IN''' for one retarget period after the first retarget period with STARTED blocks of which at least threshold have the associated bit set in nVersion, or for one retarget period after the timeout when '''lockinontimeout''' is true.
54+
# '''ACTIVE''' for all blocks after the LOCKED_IN retarget period.
55+
# '''FAILED''' for all blocks after the timeout time, if LOCKED_IN was not reached and '''lockinontimeout''' is false.
56+
57+
===Bit flags===
58+
59+
The nVersion block header field is to be interpreted as a 32-bit little-endian integer (as present), and bits are selected within this integer as values (1 << N) where N is the bit number.
60+
61+
Blocks in the STARTED state get an nVersion whose bit position bit is set to 1. The top 3 bits of such blocks must be
62+
001, so the range of actually possible nVersion values is [0x20000000...0x3FFFFFFF], inclusive.
63+
64+
Due to the constraints set by BIP 34, BIP 66 and BIP 65, we only have 0x7FFFFFFB possible nVersion values available.
65+
This restricts us to at most 30 independent deployments. By restricting the top 3 bits to 001 we get 29 out of those
66+
for the purposes of this proposal, and support two future upgrades for different mechanisms (top bits 010 and 011).
67+
When a block nVersion does not have top bits 001, it is treated as if all
68+
bits are 0 for the purposes of deployments.
69+
70+
Miners should continue setting the bit in LOCKED_IN phase so uptake is visible, though this has no effect on
71+
consensus rules.
72+
73+
===New consensus rules===
74+
75+
The new consensus rules for each soft fork are enforced for each block that has ACTIVE state.
2976

3077
===State transitions===
3178

3279
<img src="bip-0008/states.png" align="middle"></img>
3380

34-
During the STARTED state if the '''lockinontimeout''' is set to true, the state will transition to LOCKED_IN when '''timeout''' is reached.
81+
The genesis block has state DEFINED for each deployment, by definition.
82+
83+
State GetStateForBlock(block) {
84+
if (block.height == 0) {
85+
return DEFINED;
86+
}
87+
88+
All blocks within a retarget period have the same state. This means that if
89+
floor(block1.height / 2016) = floor(block2.height / 2016), they are guaranteed to have the same state for every
90+
deployment.
91+
92+
if ((block.height % 2016) != 0) {
93+
return GetStateForBlock(block.parent);
94+
}
95+
96+
Otherwise, the next state depends on the previous state:
97+
98+
switch (GetStateForBlock(GetAncestorAtHeight(block, block.height - 2016))) {
99+
100+
We remain in the initial state until either we pass the start time or the timeout. GetMedianTimePast in the code below
101+
refers to the median nTime of a block and its 10 predecessors. The expression GetMedianTimePast(block.parent) is
102+
referred to as MTP in the diagram above, and is treated as a monotonic clock defined by the chain.
103+
104+
case DEFINED:
105+
if (GetMedianTimePast(block.parent) >= timeout) {
106+
return (lockinontimeout == true) ? LOCKED_IN : FAILED;
107+
}
108+
if (GetMedianTimePast(block.parent) >= starttime) {
109+
return STARTED;
110+
}
111+
return DEFINED;
112+
113+
After a period in the STARTED state, if we're past the timeout, we switch to LOCKED_IN or FAILED. If not, we tally the bits set,
114+
and transition to LOCKED_IN if a sufficient number of blocks in the past period set the deployment bit in their
115+
version numbers. The threshold is ≥1916 blocks (95% of 2016), or ≥1512 for testnet (75% of 2016).
116+
The transition to FAILED takes precendence, as otherwise an ambiguity can arise.
117+
There could be two non-overlapping deployments on the same bit, where the first one transitions to LOCKED_IN while the
118+
other one simultaneously transitions to STARTED, which would mean both would demand setting the bit.
119+
120+
Note that a block's state never depends on its own nVersion; only on that of its ancestors.
35121

36122
case STARTED:
37-
// BIP8/9 specification follows
38123
if (GetMedianTimePast(block.parent) >= timeout) {
39-
// implementation detail: if flag set, BIP8 workflow, else BIP9 workflow.
40-
return (fLockInOnTimeout == true) ? THRESHOLD_LOCKED_IN : THRESHOLD_FAILED
124+
return (lockinontimeout == true) ? LOCKED_IN : FAILED;
41125
}
42126
int count = 0;
43127
walk = block;
@@ -52,6 +136,38 @@ During the STARTED state if the '''lockinontimeout''' is set to true, the state
52136
}
53137
return STARTED;
54138
139+
After a retarget period of LOCKED_IN, we automatically transition to ACTIVE.
140+
141+
case LOCKED_IN:
142+
return ACTIVE;
143+
144+
And ACTIVE and FAILED are terminal states, which a deployment stays in once they're reached.
145+
146+
case ACTIVE:
147+
return ACTIVE;
148+
149+
case FAILED:
150+
return FAILED;
151+
}
152+
}
153+
154+
'''Implementation'''
155+
It should be noted that the states are maintained along block chain
156+
branches, but may need recomputation when a reorganization happens.
157+
158+
Given that the state for a specific block/deployment combination is completely determined by its ancestry before the
159+
current retarget period (i.e. up to and including its ancestor with height block.height - 1 - (block.height % 2016)),
160+
it is possible to implement the mechanism above efficiently and safely by caching the resulting state of every multiple-of-2016
161+
block, indexed by its parent.
162+
163+
===Warning mechanism===
164+
165+
To support upgrade warnings, an extra "unknown upgrade" is tracked, using the "implicit bit" mask = (block.nVersion & ~expectedVersion) != 0. Mask will be non-zero whenever an unexpected bit is set in nVersion. Whenever LOCKED_IN for the unknown upgrade is detected, the software should warn loudly about the upcoming soft fork. It should warn even more loudly after the next retarget period (when the unknown upgrade is in the ACTIVE state).
166+
167+
===getblocktemplate changes===
168+
169+
BIP 8 is compatible with and reuses the GBT changes from BIP 9.
170+
55171
=== Reference implementation ===
56172

57173
https://github.com/bitcoin/bitcoin/compare/master...shaolinfry:bip-uaversionbits

bip-0008/states.png

13.9 KB
Loading

bip-0008/states.svg

Lines changed: 51 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)