From 040ed56f11127a858d201fa74e2a41021c30b72b Mon Sep 17 00:00:00 2001 From: Jamie Holdstock Date: Thu, 20 Jan 2022 08:54:52 +0000 Subject: [PATCH] Use DCP-0010 status to calculate fees. vspd will now call dcrd RPC `getblockchaininfo` to determine if DCP-0010 is active, and take that information into consideration when calculating ticket registration fee. --- README.md | 1 + background/background.go | 10 +++++----- go.mod | 2 +- go.sum | 6 +++--- rpc/dcrd.go | 21 +++++++++++++++++++++ webapi/getfeeaddress.go | 11 ++++++++--- 6 files changed, 39 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 9169b99..3184dac 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,7 @@ [![Build Status](https://github.com/decred/vspd/workflows/Build%20and%20Test/badge.svg)](https://github.com/decred/vspd/actions) [![ISC License](https://img.shields.io/badge/license-ISC-blue.svg)](http://copyfree.org) [![Go Report Card](https://goreportcard.com/badge/github.com/decred/vspd)](https://goreportcard.com/report/github.com/decred/vspd) +[![Release](https://img.shields.io/github/release/decred/vspd.svg?style=flat-square)](https://github.com/decred/vspd/releases/latest) ## Overview diff --git a/background/background.go b/background/background.go index b3ee198..daa2084 100644 --- a/background/background.go +++ b/background/background.go @@ -63,6 +63,11 @@ func (n *NotificationHandler) Notify(method string, params json.RawMessage) erro return nil } +func (n *NotificationHandler) Close() error { + close(notifierClosed) + return nil +} + // blockConnected is called once when vspd starts up, and once each time a // blockconnected notification is received from dcrd. func blockConnected() { @@ -306,11 +311,6 @@ func blockConnected() { } -func (n *NotificationHandler) Close() error { - close(notifierClosed) - return nil -} - func connectNotifier(shutdownCtx context.Context, dcrdWithNotifs rpc.DcrdConnect) error { notifierClosed = make(chan struct{}) diff --git a/go.mod b/go.mod index 614820a..66805fa 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/decred/vspd go 1.16 require ( - decred.org/dcrwallet/v2 v2.0.0-rc1 + decred.org/dcrwallet/v2 v2.0.0-rc3 github.com/decred/dcrd/blockchain/stake/v4 v4.0.0 github.com/decred/dcrd/blockchain/v4 v4.0.0 github.com/decred/dcrd/chaincfg/chainhash v1.0.3 diff --git a/go.sum b/go.sum index 710a7fa..08f9435 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,7 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= -decred.org/cspp/v2 v2.0.0-20211207170141-a6b5f958a91f/go.mod h1:USyJS44Kqxz2wT/VaNsf9iTAONegO/qKXRdLg1nvrWI= -decred.org/dcrwallet/v2 v2.0.0-rc1 h1:lJuI3srFWmy5SNB5X4e2X9xJZXXJ6hMbI3bsXEqBfPQ= -decred.org/dcrwallet/v2 v2.0.0-rc1/go.mod h1:IOIVwOFCpA1Xhqz4Xo26IaD5qZN01EZRGyCJOZYnoc4= +decred.org/cspp/v2 v2.0.0-20220117153402-4f26c92d52a3/go.mod h1:USyJS44Kqxz2wT/VaNsf9iTAONegO/qKXRdLg1nvrWI= +decred.org/dcrwallet/v2 v2.0.0-rc3 h1:xxmZndkTheyfORD16nf8fUsevR3dBHdI8hhdAUFumZE= +decred.org/dcrwallet/v2 v2.0.0-rc3/go.mod h1:NJnnnOrPISrCt5oxrkXgTu0feCnxcLUsbqsvdXtFVBI= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/agl/ed25519 v0.0.0-20170116200512-5312a6153412 h1:w1UutsfOrms1J05zt7ISrnJIXKzwaspym5BTKGx93EI= github.com/agl/ed25519 v0.0.0-20170116200512-5312a6153412/go.mod h1:WPjqKcmVOxf0XSf3YxCJs6N6AOSrOx3obionmG7T0y0= diff --git a/rpc/dcrd.go b/rpc/dcrd.go index 6a9f732..f966e2f 100644 --- a/rpc/dcrd.go +++ b/rpc/dcrd.go @@ -12,6 +12,7 @@ import ( "errors" "fmt" + "github.com/decred/dcrd/blockchain/v4" "github.com/decred/dcrd/chaincfg/v3" dcrdtypes "github.com/decred/dcrd/rpc/jsonrpc/types/v3" "github.com/decred/dcrd/wire" @@ -21,6 +22,8 @@ import ( var ( requiredDcrdVersion = semver{Major: 7, Minor: 0, Patch: 0} + + activeStatus = blockchain.ThresholdStateTuple{State: blockchain.ThresholdActive}.String() ) // These error codes are defined in dcrd/dcrjson. They are copied here so we @@ -140,6 +143,24 @@ func (c *DcrdRPC) SendRawTransaction(txHex string) error { return nil } +// IsDCP0010Active uses getblockchaininfo RPC to determine if the DCP-0010 +// agenda has activated on the current network. +func (c *DcrdRPC) IsDCP0010Active() (bool, error) { + var info dcrdtypes.GetBlockChainInfoResult + err := c.Call(c.ctx, "getblockchaininfo", &info) + if err != nil { + return false, err + } + + agenda, ok := info.Deployments[chaincfg.VoteIDChangeSubsidySplit] + if !ok { + return false, fmt.Errorf("getblockchaininfo did not return agenda %q", + chaincfg.VoteIDChangeSubsidySplit) + } + + return agenda.Status == activeStatus, nil +} + // NotifyBlocks uses notifyblocks RPC to request new block notifications from dcrd. func (c *DcrdRPC) NotifyBlocks() error { return c.Call(c.ctx, "notifyblocks", nil) diff --git a/webapi/getfeeaddress.go b/webapi/getfeeaddress.go index 45e8395..0df6644 100644 --- a/webapi/getfeeaddress.go +++ b/webapi/getfeeaddress.go @@ -40,11 +40,14 @@ func getNewFeeAddress(db *database.VspDatabase, addrGen *addressGenerator) (stri return addr, idx, nil } +// getCurrentFee returns the minimum fee amount a client should pay in order to +// register a ticket with the VSP at the current block height. func getCurrentFee(dcrdClient *rpc.DcrdRPC) (dcrutil.Amount, error) { bestBlock, err := dcrdClient.GetBestBlockHeader() if err != nil { return 0, err } + sDiff, err := dcrutil.NewAmount(bestBlock.SBits) if err != nil { return 0, err @@ -53,13 +56,15 @@ func getCurrentFee(dcrdClient *rpc.DcrdRPC) (dcrutil.Amount, error) { // Using a hard-coded amount for relay fee is acceptable here because this // amount is never actually used to construct or broadcast transactions. It // is only used to calculate the fee charged for adding a ticket to the VSP. - relayFee, err := dcrutil.NewAmount(0.0001) + const defaultMinRelayTxFee = dcrutil.Amount(1e4) + + isDCP0010Active, err := dcrdClient.IsDCP0010Active() if err != nil { return 0, err } - fee := txrules.StakePoolTicketFee(sDiff, relayFee, int32(bestBlock.Height), - cfg.VSPFee, cfg.NetParams) + fee := txrules.StakePoolTicketFee(sDiff, defaultMinRelayTxFee, + int32(bestBlock.Height), cfg.VSPFee, cfg.NetParams, isDCP0010Active) if err != nil { return 0, err }