Skip to content

Commit

Permalink
fix: use cur burn block to fetch reward cycle info
Browse files Browse the repository at this point in the history
  • Loading branch information
kantai committed Sep 23, 2020
1 parent e7c0ec1 commit d28a27e
Show file tree
Hide file tree
Showing 9 changed files with 298 additions and 106 deletions.
21 changes: 6 additions & 15 deletions src/chainstate/coordinator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,30 +142,21 @@ impl From<DBError> for Error {
}

pub trait RewardSetProvider {
fn get_reward_set(&self, chainstate: &mut StacksChainState,
fn get_reward_set(&self, current_burn_height: u64, chainstate: &mut StacksChainState,
burnchain: &Burnchain, sortdb: &SortitionDB, block_id: &StacksBlockId) -> Result<Vec<StacksAddress>, Error>;
}

pub struct OnChainRewardSetProvider {

}
pub struct OnChainRewardSetProvider ();

impl RewardSetProvider for OnChainRewardSetProvider {
fn get_reward_set(&self, chainstate: &mut StacksChainState,
fn get_reward_set(&self, current_burn_height: u64, chainstate: &mut StacksChainState,
burnchain: &Burnchain, sortdb: &SortitionDB, block_id: &StacksBlockId) -> Result<Vec<StacksAddress>, Error> {
let res = chainstate.get_reward_addresses(burnchain, sortdb, block_id)?;
let res = chainstate.get_reward_addresses(burnchain, sortdb, current_burn_height, block_id)?;
let addresses = res.iter().map(|a| a.0).collect::<Vec<StacksAddress>>();
Ok(addresses)
}
}

impl OnChainRewardSetProvider {

pub fn new() -> OnChainRewardSetProvider {
OnChainRewardSetProvider {}
}
}

impl <'a, T: BlockEventDispatcher> ChainsCoordinator <'a, T, ArcCounterCoordinatorNotices, OnChainRewardSetProvider> {
pub fn run<F>(chain_state_path: &str, burnchain: Burnchain, stacks_mainnet: bool, stacks_chain_id: u32,
initial_balances: Option<Vec<(PrincipalData, u64)>>,
Expand Down Expand Up @@ -196,7 +187,7 @@ impl <'a, T: BlockEventDispatcher> ChainsCoordinator <'a, T, ArcCounterCoordinat
burnchain,
dispatcher: Some(dispatcher),
notifier: arc_notices,
reward_set_provider: OnChainRewardSetProvider::new(),
reward_set_provider: OnChainRewardSetProvider(),
};

loop {
Expand Down Expand Up @@ -284,7 +275,7 @@ pub fn get_reward_cycle_info<U: RewardSetProvider>(
let anchor_status = if anchor_block_known {
let block_id = StacksBlockHeader::make_index_block_hash(&consensus_hash, &stacks_block_hash);
let reward_set = provider.get_reward_set(
chain_state, burnchain, sort_db, &block_id)?;
burn_height, chain_state, burnchain, sort_db, &block_id)?;
PoxAnchorBlockStatus::SelectedAndKnown(stacks_block_hash, reward_set)
} else {
PoxAnchorBlockStatus::SelectedAndUnknown(stacks_block_hash)
Expand Down
4 changes: 2 additions & 2 deletions src/chainstate/coordinator/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,13 @@ impl BlockEventDispatcher for NullEventDispatcher {
}

pub fn make_coordinator<'a>(path: &str) -> ChainsCoordinator<'a, NullEventDispatcher, (), OnChainRewardSetProvider> {
ChainsCoordinator::test_new(&get_burnchain(path), path, OnChainRewardSetProvider::new())
ChainsCoordinator::test_new(&get_burnchain(path), path, OnChainRewardSetProvider())
}

struct StubbedRewardSetProvider(Vec<StacksAddress>);

impl RewardSetProvider for StubbedRewardSetProvider {
fn get_reward_set(&self, chainstate: &mut StacksChainState,
fn get_reward_set(&self, _current_burn_height: u64, chainstate: &mut StacksChainState,
burnchain: &Burnchain, sortdb: &SortitionDB, block_id: &StacksBlockId) -> Result<Vec<StacksAddress>, chainstate::coordinator::Error> {
Ok(self.0.clone())
}
Expand Down
116 changes: 69 additions & 47 deletions src/chainstate/stacks/boot/mod.rs

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions src/chainstate/stacks/boot/pox-mainnet.clar
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
;; PoX mainnet constants
;; Min/max number of reward cycles uSTX can be locked for
(define-constant MIN-POX-REWARD-CYCLES u1)
(define-constant MAX-POX-REWARD-CYCLES u12)

;; Default length of the PoX registration window, in burnchain blocks.
(define-constant REGISTRATION-WINDOW-LENGTH u250)

;; Default length of the PoX reward cycle, in burnchain blocks.
(define-constant REWARD-CYCLE-LENGTH u1000)

;; Valid values for burnchain address versions.
;; TODO: These correspond to address hash modes in Stacks 2.0,
;; but they should just be Bitcoin version bytes: we don't
;; need to constrain PoX recipient addresses the way that
;; we constrain other kinds of Bitcoin addresses
(define-constant ADDRESS-VERSION-P2PKH 0x00)
(define-constant ADDRESS-VERSION-P2SH 0x01)
(define-constant ADDRESS-VERSION-P2WPKH 0x02)
(define-constant ADDRESS-VERSION-P2WSH 0x03)

;; Stacking thresholds
(define-constant STACKING-THRESHOLD-25 u20000)
(define-constant STACKING-THRESHOLD-100 u5000)
25 changes: 25 additions & 0 deletions src/chainstate/stacks/boot/pox-testnet.clar
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
;; PoX testnet constants
;; Min/max number of reward cycles uSTX can be locked for
(define-constant MIN-POX-REWARD-CYCLES u1)
(define-constant MAX-POX-REWARD-CYCLES u12)

;; Default length of the PoX registration window, in burnchain blocks.
(define-constant REGISTRATION-WINDOW-LENGTH u30)

;; Default length of the PoX reward cycle, in burnchain blocks.
(define-constant REWARD-CYCLE-LENGTH u120)


;; Valid values for burnchain address versions.
;; TODO: These correspond to address hash modes in Stacks 2.0,
;; but they should just be Bitcoin version bytes: we don't
;; need to constrain PoX recipient addresses the way that
;; we constrain other kinds of Bitcoin addresses
(define-constant ADDRESS-VERSION-P2PKH 0x00)
(define-constant ADDRESS-VERSION-P2SH 0x01)
(define-constant ADDRESS-VERSION-P2WPKH 0x02)
(define-constant ADDRESS-VERSION-P2WSH 0x03)

;; Stacking thresholds
(define-constant STACKING-THRESHOLD-25 u480)
(define-constant STACKING-THRESHOLD-100 u120)
65 changes: 27 additions & 38 deletions src/chainstate/stacks/boot/pox.clar
Original file line number Diff line number Diff line change
Expand Up @@ -229,36 +229,30 @@
(num-cycles uint)
(amount-ustx uint)
(i uint))))
(let (
(reward-cycle (+ (get first-reward-cycle params) (get i params)))
(i (get i params))
)
(let ((reward-cycle (+ (get first-reward-cycle params) (get i params)))
(i (get i params)))
{
pox-addr: (get pox-addr params),
first-reward-cycle: (get first-reward-cycle params),
num-cycles: (get num-cycles params),
amount-ustx: (get amount-ustx params),
i: (if (< i (get num-cycles params))
(let (
(total-ustx (get-total-ustx-stacked reward-cycle))
)
;; record how many uSTX this pox-addr will stack for in the given reward cycle
(append-reward-cycle-pox-addr
(let ((total-ustx (get-total-ustx-stacked reward-cycle)))
;; record how many uSTX this pox-addr will stack for in the given reward cycle
(append-reward-cycle-pox-addr
(get pox-addr params)
reward-cycle
(get amount-ustx params))

;; update running total
(map-set reward-cycle-total-stacked
{ reward-cycle: reward-cycle }
{ total-ustx: (+ (get amount-ustx params) total-ustx) }
)

;; updated _this_ reward cycle
(+ i u1))
;; update running total
(map-set reward-cycle-total-stacked
{ reward-cycle: reward-cycle }
{ total-ustx: (+ (get amount-ustx params) total-ustx) })

;; updated _this_ reward cycle
(+ i u1))
(+ i u0))
})
)
}))

;; Add a PoX address to a given sequence of reward cycle lists.
;; A PoX address can be added to at most 12 consecutive cycles.
Expand Down Expand Up @@ -341,33 +335,28 @@
(define-read-only (can-stacks-stx (amount-ustx uint)
(pox-addr (tuple (version (buff 1)) (hashbytes (buff 20))))
(lock-period uint))
(let (
;; this stacker's first reward cycle is the _next_ reward cycle
(first-reward-cycle (+ u1 (current-pox-reward-cycle)))
)
;; amount must be valid
(asserts! (> amount-ustx u0)
;; this stacker's first reward cycle is the _next_ reward cycle
(let ((first-reward-cycle (+ u1 (current-pox-reward-cycle))))
;; amount must be valid
(asserts! (> amount-ustx u0)
(err ERR_STACKING_INVALID_AMOUNT))

;; tx-sender principal must not have rejected in this upcoming reward cycle
(asserts! (is-none (get-pox-rejection tx-sender first-reward-cycle))
;; tx-sender principal must not have rejected in this upcoming reward cycle
(asserts! (is-none (get-pox-rejection tx-sender first-reward-cycle))
(err ERR_STACKING_ALREADY_REJECTED))

;; tx-sender principal must not be stacking
(asserts! (is-none (get-stacker-info tx-sender))
;; tx-sender principal must not be stacking
(asserts! (is-none (get-stacker-info tx-sender))
(err ERR_STACKING_ALREADY_STACKED))

;; the Stacker must have sufficient unlocked funds
(asserts! (>= (stx-get-balance tx-sender) amount-ustx)
;; the Stacker must have sufficient unlocked funds
(asserts! (>= (stx-get-balance tx-sender) amount-ustx)
(err ERR_STACKING_INSUFFICIENT_FUNDS))

(ok {
amount-ustx: amount-ustx,
pox-addr: pox-addr,
first-reward-cycle: first-reward-cycle,
lock-period: lock-period
}))
)
(ok { amount-ustx: amount-ustx,
pox-addr: pox-addr,
first-reward-cycle: first-reward-cycle,
lock-period: lock-period })))

;; Lock up some uSTX for stacking! Note that the given amount here is in micro-STX (uSTX).
;; The STX will be locked for the given number of reward cycles (lock-period).
Expand Down
4 changes: 2 additions & 2 deletions src/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2010,7 +2010,7 @@ pub mod test {
},
ExecutionCost::max_value()).unwrap();

let mut coord = ChainsCoordinator::test_new(&burnchain, &test_path, OnChainRewardSetProvider::new());
let mut coord = ChainsCoordinator::test_new(&burnchain, &test_path, OnChainRewardSetProvider());
coord.handle_new_burnchain_block().unwrap();

let mut stacks_node = TestStacksNode::from_chainstate(chainstate);
Expand Down Expand Up @@ -2479,7 +2479,7 @@ pub mod test {
let leader_key_op = stacks_node.add_key_register(&mut burn_block, &mut self.miner);

// patch in reward set info
match get_next_recipients(&last_sortition_block, &mut stacks_node.chainstate, &mut sortdb, &self.config.burnchain, &OnChainRewardSetProvider::new()) {
match get_next_recipients(&last_sortition_block, &mut stacks_node.chainstate, &mut sortdb, &self.config.burnchain, &OnChainRewardSetProvider()) {
Ok(recipients) => {
block_commit_op.commit_outs = match recipients {
Some(info) => vec![info.recipient.0],
Expand Down
2 changes: 1 addition & 1 deletion testnet/stacks-node/src/neon_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ impl InitializedNeonNode {

// let's figure out the recipient set!
let recipients = match get_next_recipients(&burn_block, chain_state, burn_db,
burnchain, &OnChainRewardSetProvider::new()) {
burnchain, &OnChainRewardSetProvider()) {
Ok(x) => x,
Err(e) => {
error!("Failure fetching recipient set: {:?}", e);
Expand Down
Loading

0 comments on commit d28a27e

Please sign in to comment.