Skip to content

Commit

Permalink
Follow DCP0012 once activated.
Browse files Browse the repository at this point in the history
This is currently being done using an extra getblockchaininfo RPC call
any time votes may need to be created.  While not ideal, and caching of
previously observed active and lockedin statuses could be utilized, this
is the bare minimum needed to make wallet follow the DCP0012 hard fork
if it is approved.
  • Loading branch information
davecgh committed May 17, 2023
1 parent 99be203 commit 8374bd5
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 12 deletions.
40 changes: 38 additions & 2 deletions deployments/deployments.go
Expand Up @@ -74,8 +74,8 @@ func (d *HardcodedDeployment) Active(height int32, net wire.CurrencyNet) bool {
}

const (
lockedinStatus = "lockedin"
activeStatus = "active"
lockedinStatus = dcrdtypes.AgendaInfoStatusLockedIn
activeStatus = dcrdtypes.AgendaInfoStatusActive
)

// DCP0010Active returns whether the consensus rules for the next block with the
Expand Down Expand Up @@ -116,3 +116,39 @@ func DCP0010Active(ctx context.Context, height int32, params *chaincfg.Params,
return false, nil
}
}

// DCP0012Active returns whether the consensus rules for the next block with the
// current chain tip height requires the version 2 subsidy split as specified in
// DCP0012. DCP0012 requires the RPC syncer to detect activation on mainnet,
// testnet3 and simnet.
func DCP0012Active(ctx context.Context, height int32, params *chaincfg.Params,
syncer interface{}) (bool, error) {

net := params.Net
rcai := int32(params.RuleChangeActivationInterval)

if net != wire.MainNet && net != wire.TestNet3 && net != wire.SimNet {
return false, nil
}
rpc, ok := syncer.(*dcrd.RPC)
if !ok {
return false, errors.E(errors.Bug, "DCP0012 activation check requires RPC syncer")
}
var resp dcrdtypes.GetBlockChainInfoResult
err := rpc.Call(ctx, "getblockchaininfo", &resp)
if err != nil {
return false, err
}
d, ok := resp.Deployments[chaincfg.VoteIDChangeSubsidySplitR2]
if !ok {
return false, nil
}
switch {
case d.Status == lockedinStatus && height == int32(d.Since)+rcai-1:
return true, nil
case d.Status == activeStatus:
return true, nil
default:
return false, nil
}
}
4 changes: 2 additions & 2 deletions go.mod
Expand Up @@ -6,11 +6,11 @@ require (
decred.org/cspp/v2 v2.0.0
github.com/decred/dcrd/addrmgr/v2 v2.0.1
github.com/decred/dcrd/blockchain/stake/v5 v5.0.0
github.com/decred/dcrd/blockchain/standalone/v2 v2.1.0
github.com/decred/dcrd/blockchain/standalone/v2 v2.1.1-0.20230430213532-f95870f9c6af
github.com/decred/dcrd/blockchain/v5 v5.0.0-20221022042529-0a0cc3b3bf92
github.com/decred/dcrd/certgen v1.1.1
github.com/decred/dcrd/chaincfg/chainhash v1.0.4
github.com/decred/dcrd/chaincfg/v3 v3.1.1
github.com/decred/dcrd/chaincfg/v3 v3.1.2-0.20230412145739-9aa79ec168f6
github.com/decred/dcrd/connmgr/v3 v3.1.0
github.com/decred/dcrd/crypto/blake256 v1.0.1
github.com/decred/dcrd/crypto/ripemd160 v1.0.1
Expand Down
7 changes: 4 additions & 3 deletions go.sum
Expand Up @@ -28,8 +28,8 @@ github.com/decred/dcrd/addrmgr/v2 v2.0.1 h1:o+AetOWZcSa2j2uVRf0gHvTSCmt4jMviKKpX
github.com/decred/dcrd/addrmgr/v2 v2.0.1/go.mod h1:HcDrmMGqo2ilwjMi73YLwJQScv8djDPHgTV8kON8Wx4=
github.com/decred/dcrd/blockchain/stake/v5 v5.0.0-20221022042529-0a0cc3b3bf92 h1:GTIg6r54cgNhUyZMNmTsmxM8OneEJ8t6QcWQCqu+al0=
github.com/decred/dcrd/blockchain/stake/v5 v5.0.0-20221022042529-0a0cc3b3bf92/go.mod h1:fij5xS9IBfJ5e/F5ytp/g/TWjrETEMXUFlE6C7KYOvA=
github.com/decred/dcrd/blockchain/standalone/v2 v2.1.0 h1:aXh7a+86p+H65MGy0QKu4Juf3/j+Y5koVSyVYFMdqP0=
github.com/decred/dcrd/blockchain/standalone/v2 v2.1.0/go.mod h1:t2qaZ3hNnxHZ5kzVJDgW5sp47/8T5hYJt7SR+/JtRhI=
github.com/decred/dcrd/blockchain/standalone/v2 v2.1.1-0.20230430213532-f95870f9c6af h1:tmfTIkxkq7NdeLj3mrMk0uEdRyU1/oO1MWx8dfwOmm8=
github.com/decred/dcrd/blockchain/standalone/v2 v2.1.1-0.20230430213532-f95870f9c6af/go.mod h1:PpM/jdMaD5MnBcSoFd+rJZE4q8tU0xPTTAyVzgegLQI=
github.com/decred/dcrd/blockchain/v5 v5.0.0-20221022042529-0a0cc3b3bf92 h1:AYgHfuWXQh5NTv3mciQleTm2K1IuM7ozgfXy1Vg6V7k=
github.com/decred/dcrd/blockchain/v5 v5.0.0-20221022042529-0a0cc3b3bf92/go.mod h1:RTUDExWsbCsPBRdLeK/Zix/Go87jG/3BfmEWy3xbCaI=
github.com/decred/dcrd/certgen v1.1.1 h1:MYPG5jCysnbF4OiJ1++YumFEu2p/MsM/zxmmqC9mVFg=
Expand All @@ -39,8 +39,9 @@ github.com/decred/dcrd/chaincfg/chainhash v1.0.3/go.mod h1:BpbrGgrPTr3YJYRN3Bm+D
github.com/decred/dcrd/chaincfg/chainhash v1.0.4 h1:zRCv6tdncLfLTKYqu7hrXvs7hW+8FO/NvwoFvGsrluU=
github.com/decred/dcrd/chaincfg/chainhash v1.0.4/go.mod h1:hA86XxlBWwHivMvxzXTSD0ZCG/LoYsFdWnCekkTMCqY=
github.com/decred/dcrd/chaincfg/v3 v3.1.0/go.mod h1:4XF9nlx2NeGD4xzw1+L0DGICZMl0a5rKV8nnuHLgk8o=
github.com/decred/dcrd/chaincfg/v3 v3.1.1 h1:Ki8kq5IXGmjriiQyPCrCTF1aZSBiORb91/Sr5xW4otw=
github.com/decred/dcrd/chaincfg/v3 v3.1.1/go.mod h1:4XF9nlx2NeGD4xzw1+L0DGICZMl0a5rKV8nnuHLgk8o=
github.com/decred/dcrd/chaincfg/v3 v3.1.2-0.20230412145739-9aa79ec168f6 h1:rKAzrv3gIEQaEQpnWU4IKxXrvx6QfXkdiOUKLvwEpQw=
github.com/decred/dcrd/chaincfg/v3 v3.1.2-0.20230412145739-9aa79ec168f6/go.mod h1:aEEti0kQSBFAlzHln4FB+3L30k9ZN1M7YDfYuK5VWtc=
github.com/decred/dcrd/connmgr/v3 v3.1.0 h1:M197w+xsZQ8CVidigrchoab31wWRUlZhudQDDlq7/Gk=
github.com/decred/dcrd/connmgr/v3 v3.1.0/go.mod h1:NVzQpMSu87fzwEgYmoz+xfVHI6un4+xMkvcMoDjdaRs=
github.com/decred/dcrd/crypto/blake256 v1.0.0/go.mod h1:sQl2p6Y26YV+ZOcSTP6thNdn47hh8kt6rqSlvmrXFAc=
Expand Down
7 changes: 6 additions & 1 deletion wallet/chainntfns.go
Expand Up @@ -833,6 +833,11 @@ func (w *Wallet) VoteOnOwnedTickets(ctx context.Context, winningTicketHashes []*
if err != nil {
return errors.E(op, err)
}
dcp0012Active, err := deployments.DCP0012Active(ctx, blockHeight,
w.chainParams, n)
if err != nil {
return errors.E(op, err)
}

// TODO The behavior of this is not quite right if tons of blocks
// are coming in quickly, because the transaction store will end up
Expand Down Expand Up @@ -908,7 +913,7 @@ func (w *Wallet) VoteOnOwnedTickets(ctx context.Context, winningTicketHashes []*
// Dealwith consensus votes
vote, err := createUnsignedVote(ticketHash, ticketPurchase,
blockHeight, blockHash, ticketVoteBits, w.subsidyCache,
w.chainParams, dcp0010Active)
w.chainParams, dcp0010Active, dcp0012Active)
if err != nil {
log.Errorf("Failed to create vote transaction for ticket "+
"hash %v: %v", ticketHash, err)
Expand Down
12 changes: 9 additions & 3 deletions wallet/createtx.go
Expand Up @@ -2229,16 +2229,22 @@ func newVoteScript(voteBits stake.VoteBits) ([]byte, error) {
func createUnsignedVote(ticketHash *chainhash.Hash, ticketPurchase *wire.MsgTx,
blockHeight int32, blockHash *chainhash.Hash, voteBits stake.VoteBits,
subsidyCache *blockchain.SubsidyCache, params *chaincfg.Params,
dcp0010Active bool) (*wire.MsgTx, error) {
dcp0010Active, dcp0012Active bool) (*wire.MsgTx, error) {

// Parse the ticket purchase transaction to determine the required output
// destinations for vote rewards or revocations.
ticketPayKinds, ticketHash160s, ticketValues, _, _, _ :=
stake.TxSStxStakeOutputInfo(ticketPurchase)

// Calculate the subsidy for votes at this height.
subsidy := subsidyCache.CalcStakeVoteSubsidyV2(int64(blockHeight),
dcp0010Active)
ssv := blockchain.SSVOriginal
switch {
case dcp0012Active:
ssv = blockchain.SSVDCP0012
case dcp0010Active:
ssv = blockchain.SSVDCP0010
}
subsidy := subsidyCache.CalcStakeVoteSubsidyV3(int64(blockHeight), ssv)

// Calculate the output values from this vote using the subsidy.
voteRewardValues := stake.CalculateRewards(ticketValues,
Expand Down
3 changes: 2 additions & 1 deletion wallet/tickets.go
Expand Up @@ -30,6 +30,7 @@ func (w *Wallet) GenerateVoteTx(ctx context.Context, blockHash *chainhash.Hash,

var vote *wire.MsgTx
const dcp0010Active = false
const dcp0012Active = false
err := walletdb.View(ctx, w.db, func(dbtx walletdb.ReadTx) error {
addrmgrNs := dbtx.ReadBucket(waddrmgrNamespaceKey)
txmgrNs := dbtx.ReadBucket(wtxmgrNamespaceKey)
Expand All @@ -39,7 +40,7 @@ func (w *Wallet) GenerateVoteTx(ctx context.Context, blockHash *chainhash.Hash,
}
vote, err = createUnsignedVote(ticketHash, ticketPurchase,
height, blockHash, voteBits, w.subsidyCache, w.chainParams,
dcp0010Active)
dcp0010Active, dcp0012Active)
if err != nil {
return errors.E(op, err)
}
Expand Down

0 comments on commit 8374bd5

Please sign in to comment.