Skip to content

Commit

Permalink
Merge pull request #430 from gcash/hardforkcleanup
Browse files Browse the repository at this point in the history
Update chain params for post hardfork activation
  • Loading branch information
cpacia authored Dec 8, 2020
2 parents 54d604d + 5d01556 commit e5c8348
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 90 deletions.
29 changes: 5 additions & 24 deletions blockchain/difficulty.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ var (
// oneLsh256 is 1 shifted left 256 bits. It is defined here to avoid
// the overhead of creating it multiple times.
oneLsh256 = new(big.Int).Lsh(bigOne, 256)

// anchorNode is the node used for the asert difficult algorithm. This is the
// block just prior to the fork. After the fork this will be hardcoded, but for
// now it will be set at runtime.
anchorNode *blockNode
)

const (
Expand Down Expand Up @@ -74,12 +69,12 @@ const (
// should be used when validating a block at the given height.
func (b *BlockChain) SelectDifficultyAdjustmentAlgorithm(prevNode *blockNode) DifficultyAlgorithm {
height := prevNode.height + 1
if uint64(prevNode.CalcPastMedianTime().Unix()) >= b.chainParams.AxionActivationTime {
return DifficultyAsert
} else if height > b.chainParams.UahfForkHeight && height <= b.chainParams.DaaForkHeight {
if height > b.chainParams.UahfForkHeight && height <= b.chainParams.DaaForkHeight {
return DifficultyEDA
} else if height > b.chainParams.DaaForkHeight {
} else if height > b.chainParams.DaaForkHeight && height <= b.chainParams.AxionActivationHeight {
return DifficultyDAA
} else if height > b.chainParams.AxionActivationHeight {
return DifficultyAsert
}
return DifficultyLegacy
}
Expand Down Expand Up @@ -284,21 +279,7 @@ func (b *BlockChain) calcNextRequiredDifficulty(lastNode *blockNode, newBlockTim
case DifficultyDAA:
return b.calcDAARequiredDifficulty(lastNode, newBlockTime)
case DifficultyAsert:
if anchorNode == nil {
// If block 1 has a median timestamp less than the activation time and..
// block 2 has a median timestamp greater than or equal to the activation time,
// then block 3 is the first block to contain the new rules and block 2 is the
// "reference block".
node := b.bestChain.Tip()
for {
if uint64(node.CalcPastMedianTime().Unix()) < b.chainParams.AxionActivationTime {
anchorNode = b.bestChain.next(node)
break
}
node = node.parent
}
}
return b.calcAsertRequiredDifficulty(lastNode, anchorNode.height, anchorNode.parent.timestamp, anchorNode.bits, newBlockTime)
return b.calcAsertRequiredDifficulty(lastNode, b.chainParams.AsertDifficultyAnchorHeight, b.chainParams.AsertDifficultyAnchorParentTimestamp, b.chainParams.AsertDifficultyAnchorBits, newBlockTime)
}
return 0, errors.New("unknown difficulty algorithm")
}
Expand Down
158 changes: 95 additions & 63 deletions chaincfg/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,7 @@ type Params struct {
GreatWallForkHeight int32 // May 15, 2019 hardfork
GravitonForkHeight int32 // Nov 15, 2019 hardfork
PhononForkHeight int32 // May 15, 2020 hardfork

// Planned hardforks
AxionActivationTime uint64 // Nov 15, 2020 hardfork
AxionActivationHeight int32 // Nov 15, 2020 hardfork

// CoinbaseMaturity is the number of blocks required before newly mined
// coins (coinbase transactions) can be spent.
Expand Down Expand Up @@ -189,10 +187,22 @@ type Params struct {
// NOTE: This only applies if ReduceMinDifficulty is true.
MinDiffReductionTime time.Duration

// AsertDifficultyHalflife the halflife parameter used by the asert
// AsertDifficultyHalflife is the halflife parameter used by the asert
// difficulty adjustment algorithm for the given network.
AsertDifficultyHalflife int64

// AsertDifficultyAnchorHeight is the height of the asert difficulty
// anchor block.
AsertDifficultyAnchorHeight int32

// AsertDifficultyAnchorParentTimestamp is the timestamp of the asert difficulty
// anchor block's parent.
AsertDifficultyAnchorParentTimestamp int64

// AsertDifficultyAnchorBits is the bits of the asert difficulty
// anchor block.
AsertDifficultyAnchorBits uint32

// GenerateSupported specifies whether or not CPU mining is allowed.
GenerateSupported bool

Expand Down Expand Up @@ -264,19 +274,21 @@ var MainNetParams = Params{
GreatWallForkHeight: 582679, // 0000000000000000018596bdfd350a9fbc7297a62a3f510b74565d992d63d2ef
GravitonForkHeight: 609135, // 0000000000000000026f7ec9e79be2f5bb839f29ebcf734066d4bb9a13f6ea83
PhononForkHeight: 635258, // 000000000000000003302c47d01e78f1c86aa3b0e96b066761a5059bc8f5781a

AxionActivationTime: 1605441600,

CoinbaseMaturity: 100,
SubsidyReductionInterval: 210000,
TargetTimespan: time.Hour * 24 * 14, // 14 days
TargetTimePerBlock: time.Minute * 10, // 10 minutes
RetargetAdjustmentFactor: 4, // 25% less, 400% more
ReduceMinDifficulty: false,
NoDifficultyAdjustment: false,
MinDiffReductionTime: 0,
AsertDifficultyHalflife: 2 * 24 * 3600, // 2 days in seconds
GenerateSupported: false,
AxionActivationHeight: 661647, // 00000000000000000083ed4b7a780d59e3983513215518ad75654bb02deee62f

CoinbaseMaturity: 100,
SubsidyReductionInterval: 210000,
TargetTimespan: time.Hour * 24 * 14, // 14 days
TargetTimePerBlock: time.Minute * 10, // 10 minutes
RetargetAdjustmentFactor: 4, // 25% less, 400% more
ReduceMinDifficulty: false,
NoDifficultyAdjustment: false,
MinDiffReductionTime: 0,
AsertDifficultyHalflife: 2 * 24 * 3600, // 2 days in seconds
AsertDifficultyAnchorHeight: 661647,
AsertDifficultyAnchorParentTimestamp: 1605447844,
AsertDifficultyAnchorBits: 402971390,
GenerateSupported: false,

// Checkpoints ordered from oldest to newest.
Checkpoints: []Checkpoint{
Expand Down Expand Up @@ -347,6 +359,17 @@ var MainNetParams = Params{
"https://ipfs.io/ipfs/QmYhcrsLgGfRTuxoZUCPCEj5xzZx5sAgV32Z7p1qPerJBr",
},
},
{
Height: 661648,
Hash: newHashFromStr("0000000000000000029e471c41818d24b8b74c911071c4ef0b4a0509f9b5a8ce"),
UtxoSetHash: newHashFromStr("fff228b2f788d2be35868fc2517d2557f856cbb9d6e2dad7310ab6054a29ef67"),
UtxoSetSize: 2931107971,
UtxoSetSources: []string{
"http://localhost:8080/ipfs/QmY9Anst9NB42RVSGZehNCF52B2DxAzAYXEPrLrar75VMT",
"https://ipfs.greyh.at/ipfs/QmY9Anst9NB42RVSGZehNCF52B2DxAzAYXEPrLrar75VMT",
"https://ipfs.io/ipfs/QmY9Anst9NB42RVSGZehNCF52B2DxAzAYXEPrLrar75VMT",
},
},
},

// Consensus rule change deployments.
Expand Down Expand Up @@ -411,18 +434,20 @@ var RegressionNetParams = Params{
DaaForkHeight: 0, // Always active on regtest
MagneticAnonomalyForkHeight: 1000,
PhononForkHeight: 1000,

AxionActivationTime: 1605441600,

SubsidyReductionInterval: 150,
TargetTimespan: time.Hour * 24 * 14, // 14 days
TargetTimePerBlock: time.Minute * 10, // 10 minutes
RetargetAdjustmentFactor: 4, // 25% less, 400% more
ReduceMinDifficulty: true,
NoDifficultyAdjustment: true,
MinDiffReductionTime: time.Minute * 20, // TargetTimePerBlock * 2
AsertDifficultyHalflife: 3600, // 1 hour
GenerateSupported: true,
AxionActivationHeight: 0, // Always active on regtest

SubsidyReductionInterval: 150,
TargetTimespan: time.Hour * 24 * 14, // 14 days
TargetTimePerBlock: time.Minute * 10, // 10 minutes
RetargetAdjustmentFactor: 4, // 25% less, 400% more
ReduceMinDifficulty: true,
NoDifficultyAdjustment: true,
MinDiffReductionTime: time.Minute * 20, // TargetTimePerBlock * 2
AsertDifficultyHalflife: 3600, // 1 hour
AsertDifficultyAnchorHeight: 0,
AsertDifficultyAnchorParentTimestamp: regTestGenesisBlock.Header.Timestamp.Unix(),
AsertDifficultyAnchorBits: regTestGenesisBlock.Header.Bits,
GenerateSupported: true,

// Checkpoints ordered from oldest to newest.
Checkpoints: nil,
Expand Down Expand Up @@ -494,19 +519,21 @@ var TestNet3Params = Params{
GreatWallForkHeight: 1303884, // 00000000000001a749d7aa418c582a0e234ebc15643bf23a4f3107fa55120388
GravitonForkHeight: 1341711, // 00000000c678f67ea16d5bf803f68ce42991839d13849f77332d6f586f62d421
PhononForkHeight: 1378460, // 0000000070f33c64cb94629680fbc57d17bea354a73e693affcb366d023db324

AxionActivationTime: 1605441600,

CoinbaseMaturity: 100,
SubsidyReductionInterval: 210000,
TargetTimespan: time.Hour * 24 * 14, // 14 days
TargetTimePerBlock: time.Minute * 10, // 10 minutes
RetargetAdjustmentFactor: 4, // 25% less, 400% more
ReduceMinDifficulty: true,
NoDifficultyAdjustment: false,
MinDiffReductionTime: time.Minute * 20, // TargetTimePerBlock * 2
AsertDifficultyHalflife: 3600, // 1 hour
GenerateSupported: false,
AxionActivationHeight: 1421481, // 00000000062c7f32591d883c99fc89ebe74a83287c0f2b7ffeef72e62217d40b

CoinbaseMaturity: 100,
SubsidyReductionInterval: 210000,
TargetTimespan: time.Hour * 24 * 14, // 14 days
TargetTimePerBlock: time.Minute * 10, // 10 minutes
RetargetAdjustmentFactor: 4, // 25% less, 400% more
ReduceMinDifficulty: true,
NoDifficultyAdjustment: false,
MinDiffReductionTime: time.Minute * 20, // TargetTimePerBlock * 2
AsertDifficultyHalflife: 3600, // 1 hour
AsertDifficultyAnchorHeight: 1421481,
AsertDifficultyAnchorParentTimestamp: 1605445400,
AsertDifficultyAnchorBits: 486604799,
GenerateSupported: false,

// Checkpoints ordered from oldest to newest.
Checkpoints: []Checkpoint{
Expand All @@ -523,6 +550,7 @@ var TestNet3Params = Params{
{Height: 1000007, Hash: newHashFromStr("00000000001ccb893d8a1f25b70ad173ce955e5f50124261bbbc50379a612ddf")},
{Height: 1341712, Hash: newHashFromStr("00000000fffc44ea2e202bd905a9fbbb9491ef9e9d5a9eed4039079229afa35b")},
{Height: 1378461, Hash: newHashFromStr("0000000099f5509b5f36b1926bcf82b21d936ebeadee811030dfbbb7fae915d7")},
{Height: 1421482, Hash: newHashFromStr("0000000023e0680a8a062b3cc289a4a341124ce7fcb6340ede207e194d73b60a")},
},

// Consensus rule change deployments.
Expand Down Expand Up @@ -578,27 +606,31 @@ var SimNetParams = Params{
DNSSeeds: []DNSSeed{}, // NOTE: There must NOT be any seeds.

// Chain parameters
GenesisBlock: &simNetGenesisBlock,
GenesisHash: &simNetGenesisHash,
PowLimit: simNetPowLimit,
PowLimitBits: 0x207fffff,
BIP0034Height: 0, // Always active on simnet
BIP0065Height: 0, // Always active on simnet
BIP0066Height: 0, // Always active on simnet
UahfForkHeight: 0, // Always active on simnet
DaaForkHeight: 2000,
MagneticAnonomalyForkHeight: 3000,
GreatWallForkHeight: 0,
CoinbaseMaturity: 100,
SubsidyReductionInterval: 210000,
TargetTimespan: time.Hour * 24 * 14, // 14 days
TargetTimePerBlock: time.Minute * 10, // 10 minutes
RetargetAdjustmentFactor: 4, // 25% less, 400% more
ReduceMinDifficulty: true,
NoDifficultyAdjustment: true,
MinDiffReductionTime: time.Minute * 20, // TargetTimePerBlock * 2
AsertDifficultyHalflife: 3600, // 1 hour
GenerateSupported: true,
GenesisBlock: &simNetGenesisBlock,
GenesisHash: &simNetGenesisHash,
PowLimit: simNetPowLimit,
PowLimitBits: 0x207fffff,
BIP0034Height: 0, // Always active on simnet
BIP0065Height: 0, // Always active on simnet
BIP0066Height: 0, // Always active on simnet
UahfForkHeight: 0, // Always active on simnet
DaaForkHeight: 2000,
MagneticAnonomalyForkHeight: 3000,
GreatWallForkHeight: 0,
AxionActivationHeight: 4000,
CoinbaseMaturity: 100,
SubsidyReductionInterval: 210000,
TargetTimespan: time.Hour * 24 * 14, // 14 days
TargetTimePerBlock: time.Minute * 10, // 10 minutes
RetargetAdjustmentFactor: 4, // 25% less, 400% more
ReduceMinDifficulty: true,
NoDifficultyAdjustment: true,
MinDiffReductionTime: time.Minute * 20, // TargetTimePerBlock * 2
AsertDifficultyHalflife: 3600, // 1 hour
AsertDifficultyAnchorHeight: 0,
AsertDifficultyAnchorParentTimestamp: simNetGenesisBlock.Header.Timestamp.Unix(),
AsertDifficultyAnchorBits: simNetGenesisBlock.Header.Bits,
GenerateSupported: true,

// Checkpoints ordered from oldest to newest.
Checkpoints: nil,
Expand Down
6 changes: 3 additions & 3 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2274,12 +2274,12 @@ func (s *server) handleRelayInvMsg(state *peerState, msg relayMsg) {
// handleRelayCmpctBlock deals with direct relaying a compact block to
// peers which both want a compact block and accept direct relay.
func (s *server) handleRelayCmpctBlock(state *peerState, msg *wire.MsgCmpctBlock) {
blockHash := msg.BlockHash()
iv := wire.NewInvVect(wire.InvTypeBlock, &blockHash)
state.forAllPeers(func(sp *serverPeer) {
if sp.WantsCompactBlocks() && sp.WantsDirectBlockRelay() &&
sp.ProtocolVersion() >= wire.NoValidationRelayVersion {
sp.ProtocolVersion() >= wire.NoValidationRelayVersion && !sp.HasKnownInventory(iv) {

blockHash := msg.BlockHash()
iv := wire.NewInvVect(wire.InvTypeBlock, &blockHash)
sp.AddKnownInventory(iv)
sp.QueueMessage(msg, nil)
}
Expand Down

0 comments on commit e5c8348

Please sign in to comment.