Skip to content

Commit

Permalink
working sunny staking refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
rigelrozanski committed Mar 20, 2018
1 parent 75b49ec commit 6eb2a12
Show file tree
Hide file tree
Showing 14 changed files with 840 additions and 842 deletions.
28 changes: 14 additions & 14 deletions docs/spec/staking/spec-technical.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ type TxDeclareCandidacy struct {
```
declareCandidacy(tx TxDeclareCandidacy):
// create and save the empty candidate
candidate = loadCandidate(store, tx.PubKey)
candidate = getCandidate(store, tx.PubKey)
if candidate != nil then return
candidate = NewCandidate(tx.PubKey)
Expand All @@ -251,7 +251,7 @@ declareCandidacy(tx TxDeclareCandidacy):
candidate.ProposerRewardPool = Coin(0)
candidate.Description = tx.Description
saveCandidate(store, candidate)
setCandidate(store, candidate)
// move coins from the sender account to a (self-bond) delegator account
// the candidate account and global shares are updated within here
Expand All @@ -275,12 +275,12 @@ type TxEditCandidacy struct {

```
editCandidacy(tx TxEditCandidacy):
candidate = loadCandidate(store, tx.PubKey)
candidate = getCandidate(store, tx.PubKey)
if candidate == nil or candidate.Status == Unbonded return
if tx.GovernancePubKey != nil then candidate.GovernancePubKey = tx.GovernancePubKey
if tx.Commission >= 0 then candidate.Commission = tx.Commission
if tx.Description != nil then candidate.Description = tx.Description
saveCandidate(store, candidate)
setCandidate(store, candidate)
return
```
Expand All @@ -303,7 +303,7 @@ type TxDelegate struct {

```
delegate(tx TxDelegate):
candidate = loadCandidate(store, tx.PubKey)
candidate = getCandidate(store, tx.PubKey)
if candidate == nil then return
return delegateWithCandidate(tx, candidate)
Expand All @@ -320,18 +320,18 @@ delegateWithCandidate(tx TxDelegate, candidate Candidate):
if err != nil then return
// Get or create the delegator bond
bond = loadDelegatorBond(store, sender, tx.PubKey)
bond = getDelegatorBond(store, sender, tx.PubKey)
if bond == nil then
bond = DelegatorBond{tx.PubKey,rational.Zero, Coin(0), Coin(0)}
issuedDelegatorShares = candidate.addTokens(tx.Amount, gs)
bond.Shares = bond.Shares.Add(issuedDelegatorShares)
saveCandidate(store, candidate)
setCandidate(store, candidate)
store.Set(GetDelegatorBondKey(sender, bond.PubKey), bond)
saveGlobalState(store, gs)
setGlobalState(store, gs)
return
addTokens(amount int64, gs GlobalState, candidate Candidate):
Expand Down Expand Up @@ -377,15 +377,15 @@ type TxUnbond struct {
unbond(tx TxUnbond):
// get delegator bond
bond = loadDelegatorBond(store, sender, tx.PubKey)
bond = getDelegatorBond(store, sender, tx.PubKey)
if bond == nil then return
// subtract bond tokens from delegator bond
if bond.Shares.LT(tx.Shares) return // bond shares < tx shares
bond.Shares = bond.Shares.Sub(ts.Shares)
candidate = loadCandidate(store, tx.PubKey)
candidate = getCandidate(store, tx.PubKey)
if candidate == nil return
revokeCandidacy = false
Expand All @@ -397,7 +397,7 @@ unbond(tx TxUnbond):
// remove the bond
removeDelegatorBond(store, sender, tx.PubKey)
else
saveDelegatorBond(store, sender, bond)
setDelegatorBond(store, sender, bond)
// transfer coins back to account
if candidate.Status == Bonded then
Expand Down Expand Up @@ -425,15 +425,15 @@ unbond(tx TxUnbond):
if candidate.GlobalStakeShares.IsZero() then
removeCandidate(store, tx.PubKey)
else
saveCandidate(store, candidate)
setCandidate(store, candidate)
saveGlobalState(store, gs)
setGlobalState(store, gs)
return
removeDelegatorBond(candidate Candidate):
// first remove from the list of bonds
pks = loadDelegatorCandidates(store, sender)
pks = getDelegatorCandidates(store, sender)
for i, pk := range pks {
if candidate.Equals(pk) {
pks = append(pks[:i], pks[i+1:]...)
Expand Down
3 changes: 3 additions & 0 deletions x/stake/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ func ErrBadValidatorAddr() sdk.Error {
func ErrBadCandidateAddr() sdk.Error {
return newError(CodeInvalidValidator, "Candidate does not exist for that address")
}
func ErrBadDelegatorAddr() sdk.Error {
return newError(CodeInvalidValidator, "Delegator does not exist for that address")
}
func ErrCandidateExistsAddr() sdk.Error {
return newError(CodeInvalidValidator, "Candidate already exist, cannot re-declare candidacy")
}
Expand Down
Loading

0 comments on commit 6eb2a12

Please sign in to comment.