Skip to content

Commit

Permalink
change cut to comp-rate(compensation rate)
Browse files Browse the repository at this point in the history
  • Loading branch information
longzhi committed May 18, 2018
1 parent 70ee80c commit 3e65fa0
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 46 deletions.
7 changes: 4 additions & 3 deletions app/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ func (app *BaseApp) CheckTx(txBytes []byte) abci.ResponseCheckTx {
func (app *BaseApp) BeginBlock(req abci.RequestBeginBlock) (res abci.ResponseBeginBlock) {
app.EthApp.BeginBlock(req)
app.AbsentValidators = req.AbsentValidators
app.logger.Info("BeginBlock", "absentvalidators", app.AbsentValidators)
app.ByzantineValidators = req.ByzantineValidators

return abci.ResponseBeginBlock{}
Expand All @@ -147,9 +148,9 @@ func (app *BaseApp) EndBlock(req abci.RequestEndBlock) (res abci.ResponseEndBloc
cs := stake.GetCandidates()
cs.Sort()
validators := cs.Validators()
//for _, i := range app.AbsentValidators {
// validators.Remove(i)
//}
for _, i := range app.AbsentValidators {
validators.Remove(i)
}
stake.NewAwardCalculator(app.WorkingHeight(), validators, utils.BlockGasFee).AwardAll()

// punish Byzantine validators
Expand Down
5 changes: 1 addition & 4 deletions app/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,6 @@ func (app *StoreApp) Commit() (res abci.ResponseCommit) {
return abci.ResponseCommit{Data: hash}
}

// BeginBlock - ABCI
func (app *StoreApp) BeginBlock(_ abci.RequestBeginBlock) (res abci.ResponseBeginBlock) { return }

// EndBlock - ABCI
// Returns a list of all validator changes made in this block
func (app *StoreApp) EndBlock(_ abci.RequestEndBlock) (res abci.ResponseEndBlock) {
Expand Down Expand Up @@ -322,7 +319,7 @@ func initTravisDb() error {
defer db.Close()

sqlStmt := `
create table candidates(address text not null primary key, pub_key text not null, shares text not null default '0', voting_power integer default 0, max_shares text not null default '0', cut text not null default '0', website text not null default '', location text not null default '', details text not null default '', verified text not null default 'N', active text not null default 'Y', created_at text not null, updated_at text not null default '');
create table candidates(address text not null primary key, pub_key text not null, shares text not null default '0', voting_power integer default 0, max_shares text not null default '0', comp_rate text not null default '0', website text not null default '', location text not null default '', details text not null default '', verified text not null default 'N', active text not null default 'Y', created_at text not null, updated_at text not null default '');
create unique index idx_candidates_pub_key on candidates(pub_key);
create table delegators(address text not null primary key, created_at text not null);
create table delegations(delegator_address text not null, pub_key text not null, delegate_amount text not null default '0', award_amount text not null default '0', withdraw_amount not null default '0', slash_amount not null default '0', created_at text not null, updated_at text not null default '');
Expand Down
4 changes: 2 additions & 2 deletions modules/stake/award_calculator.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (ac awardCalculator) getMintableAmount() (result *big.Int) {
year := ac.height / yearlyBlockNumber
pow := big.NewFloat(math.Pow(float64(1+inflationRate/100), float64(year)))
new(big.Float).Mul(base, pow).Int(result)
fmt.Printf("year: %d, minable amount: %v\n", year, result)
fmt.Printf("year: %d, mintable amount: %v\n", year, result)
return
}

Expand Down Expand Up @@ -81,7 +81,7 @@ func (ac awardCalculator) AwardAll() {
validator.shares = shares
validator.ownerAddress = candidate.OwnerAddress
validator.pubKey = candidate.PubKey
validator.cut = candidate.ParseCut()
validator.cut = candidate.ParseCompRate()
totalShares.Add(totalShares, shares)

// Get all of the delegators
Expand Down
18 changes: 9 additions & 9 deletions modules/stake/commands/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const (
FlagPubKey = "pubkey"
FlagAmount = "amount"
FlagMaxAmount = "max-amount"
FlagCut = "cut"
FlagCompRate = "comp-rate"
FlagAddress = "address"
FlagValidatorAddress = "validator-address"
FlagWebsite = "website"
Expand Down Expand Up @@ -108,8 +108,8 @@ func init() {
fsCandidate.String(FlagLocation, "", "optional location")
fsCandidate.String(FlagDetails, "", "optional detailed description")

fsCut := flag.NewFlagSet("", flag.ContinueOnError)
fsCut.String(FlagCut, "0", "The percentage of block awards to be distributed back to the delegators")
fsCompRate := flag.NewFlagSet("", flag.ContinueOnError)
fsCompRate.String(FlagCompRate, "0", "The compensation percentage of block awards to be distributed to the validator")

fsAddr := flag.NewFlagSet("", flag.ContinueOnError)
fsAddr.String(FlagAddress, "", "Account address")
Expand All @@ -123,7 +123,7 @@ func init() {
// add the flags
CmdDeclareCandidacy.Flags().AddFlagSet(fsPk)
CmdDeclareCandidacy.Flags().AddFlagSet(fsCandidate)
CmdDeclareCandidacy.Flags().AddFlagSet(fsCut)
CmdDeclareCandidacy.Flags().AddFlagSet(fsCompRate)

CmdUpdateCandidacy.Flags().AddFlagSet(fsCandidate)

Expand All @@ -150,10 +150,10 @@ func cmdDeclareCandidacy(cmd *cobra.Command, args []string) error {
return fmt.Errorf("max-amount must be positive interger")
}

cut := viper.GetString(FlagCut)
fcut := utils.ParseFloat(cut)
if fcut <= 0 || fcut >= 1 {
return fmt.Errorf("cut must between 0 and 1")
compRate := viper.GetString(FlagCompRate)
fCompRate := utils.ParseFloat(compRate)
if fCompRate <= 0 || fCompRate >= 1 {
return fmt.Errorf("comp-rate must between 0 and 1")
}

description := stake.Description{
Expand All @@ -162,7 +162,7 @@ func cmdDeclareCandidacy(cmd *cobra.Command, args []string) error {
Details: viper.GetString(FlagDetails),
}

tx := stake.NewTxDeclareCandidacy(pk, maxAmount, cut, description)
tx := stake.NewTxDeclareCandidacy(pk, maxAmount, compRate, description)
return txcmd.DoTx(tx)
}

Expand Down
32 changes: 16 additions & 16 deletions modules/stake/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ func getDb() *sql.DB {
func GetCandidateByAddress(address common.Address) *Candidate {
db := getDb()
defer db.Close()
stmt, err := db.Prepare("select pub_key, shares, voting_power, max_shares, cut, website, location, details, verified, active, created_at, updated_at from candidates where address = ?")
stmt, err := db.Prepare("select pub_key, shares, voting_power, max_shares, comp_rate, website, location, details, verified, active, created_at, updated_at from candidates where address = ?")
if err != nil {
panic(err)
}
defer stmt.Close()

var pubKey, createdAt, updatedAt, shares, maxShares, website, location, details, verified, active, cut string
var pubKey, createdAt, updatedAt, shares, maxShares, website, location, details, verified, active, compRate string
var votingPower int64
err = stmt.QueryRow(address.String()).Scan(&pubKey, &shares, &votingPower, &maxShares, &cut, &website, &location, &details, &verified, &active, &createdAt, &updatedAt)
err = stmt.QueryRow(address.String()).Scan(&pubKey, &shares, &votingPower, &maxShares, &compRate, &website, &location, &details, &verified, &active, &createdAt, &updatedAt)

switch {
case err == sql.ErrNoRows:
Expand All @@ -53,7 +53,7 @@ func GetCandidateByAddress(address common.Address) *Candidate {
Shares: shares,
VotingPower: votingPower,
MaxShares: maxShares,
Cut: cut,
CompRate: compRate,
Description: description,
CreatedAt: createdAt,
UpdatedAt: updatedAt,
Expand All @@ -65,15 +65,15 @@ func GetCandidateByAddress(address common.Address) *Candidate {
func GetCandidateByPubKey(pubKey string) *Candidate {
db := getDb()
defer db.Close()
stmt, err := db.Prepare("select address, shares, voting_power, max_shares, cut, website, location, details, verified, active, created_at, updated_at from candidates where pub_key = ?")
stmt, err := db.Prepare("select address, shares, voting_power, max_shares, comp_rate, website, location, details, verified, active, created_at, updated_at from candidates where pub_key = ?")
if err != nil {
panic(err)
}
defer stmt.Close()

var address, createdAt, updatedAt, shares, maxShares, website, location, details, verified, active, cut string
var address, createdAt, updatedAt, shares, maxShares, website, location, details, verified, active, compRate string
var votingPower int64
err = stmt.QueryRow(pubKey).Scan(&address, &shares, &votingPower, &maxShares, &cut, &website, &location, &details, &verified, &active, &createdAt, &updatedAt)
err = stmt.QueryRow(pubKey).Scan(&address, &shares, &votingPower, &maxShares, &compRate, &website, &location, &details, &verified, &active, &createdAt, &updatedAt)

switch {
case err == sql.ErrNoRows:
Expand All @@ -94,7 +94,7 @@ func GetCandidateByPubKey(pubKey string) *Candidate {
Shares: shares,
VotingPower: votingPower,
MaxShares: maxShares,
Cut: cut,
CompRate: compRate,
Description: description,
Verified: verified,
CreatedAt: createdAt,
Expand All @@ -107,16 +107,16 @@ func GetCandidates() (candidates Candidates) {
db := getDb()
defer db.Close()

rows, err := db.Query("select pub_key, address, shares, voting_power, max_shares, cut, website, location, details, verified, active, created_at, updated_at from candidates where active=?", "Y")
rows, err := db.Query("select pub_key, address, shares, voting_power, max_shares, comp_rate, website, location, details, verified, active, created_at, updated_at from candidates where active=?", "Y")
if err != nil {
panic(err)
}
defer rows.Close()

for rows.Next() {
var pubKey, address, createdAt, updatedAt, shares, maxShares, website, location, details, verified, active, cut string
var pubKey, address, createdAt, updatedAt, shares, maxShares, website, location, details, verified, active, compRate string
var votingPower int64
err = rows.Scan(&pubKey, &address, &shares, &votingPower, &maxShares, &cut, &website, &location, &details, &verified, &active, &createdAt, &updatedAt)
err = rows.Scan(&pubKey, &address, &shares, &votingPower, &maxShares, &compRate, &website, &location, &details, &verified, &active, &createdAt, &updatedAt)
if err != nil {
panic(err)
}
Expand All @@ -133,7 +133,7 @@ func GetCandidates() (candidates Candidates) {
Shares: shares,
VotingPower: votingPower,
MaxShares: maxShares,
Cut: cut,
CompRate: compRate,
Description: description,
Verified: verified,
CreatedAt: createdAt,
Expand All @@ -160,7 +160,7 @@ func SaveCandidate(candidate *Candidate) {
}
defer tx.Commit()

stmt, err := tx.Prepare("insert into candidates(pub_key, address, shares, voting_power, max_shares, cut, website, location, details, verified, active, created_at, updated_at) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
stmt, err := tx.Prepare("insert into candidates(pub_key, address, shares, voting_power, max_shares, comp_rate, website, location, details, verified, active, created_at, updated_at) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
if err != nil {
panic(err)
}
Expand All @@ -172,7 +172,7 @@ func SaveCandidate(candidate *Candidate) {
candidate.Shares,
candidate.VotingPower,
candidate.MaxShares,
candidate.Cut,
candidate.CompRate,
candidate.Description.Website,
candidate.Description.Location,
candidate.Description.Details,
Expand All @@ -195,7 +195,7 @@ func updateCandidate(candidate *Candidate) {
}
defer tx.Commit()

stmt, err := tx.Prepare("update candidates set address = ?, shares = ?, voting_power = ?, max_shares = ?, cut = ?, website = ?, location = ?, details = ?, verified = ?, active = ?, updated_at = ? where pub_key = ?")
stmt, err := tx.Prepare("update candidates set address = ?, shares = ?, voting_power = ?, max_shares = ?, comp_rate = ?, website = ?, location = ?, details = ?, verified = ?, active = ?, updated_at = ? where pub_key = ?")
if err != nil {
panic(err)
}
Expand All @@ -206,7 +206,7 @@ func updateCandidate(candidate *Candidate) {
candidate.Shares,
candidate.VotingPower,
candidate.MaxShares,
candidate.Cut,
candidate.CompRate,
candidate.Description.Website,
candidate.Description.Location,
candidate.Description.Details,
Expand Down
2 changes: 1 addition & 1 deletion modules/stake/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ type genesisValidator struct {
Power int64 `json:"power"`
Name string `json:"name"`
MaxAmount int64 `json:"max_amount"`
Cut string `json:"cut"`
CompRate string `json:"cut"`
}
6 changes: 3 additions & 3 deletions modules/stake/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func setValidator(value string, store state.SimpleDB) error {
params: params,
}

tx := TxDeclareCandidacy{val.PubKey, utils.ToWei(val.MaxAmount).String(), val.Cut, Description{}}
tx := TxDeclareCandidacy{val.PubKey, utils.ToWei(val.MaxAmount).String(), val.CompRate, Description{}}
return deliverer.declareGenesisCandidacy(tx, val.Power)
}

Expand Down Expand Up @@ -360,7 +360,7 @@ var _ delegatedProofOfStake = deliver{} // enforce interface at compile time
// now we just perform action and save
func (d deliver) declareCandidacy(tx TxDeclareCandidacy) error {
// create and save the empty candidate
candidate := NewCandidate(tx.PubKey, d.sender, "0", 0, tx.MaxAmount, tx.Cut, tx.Description, "N", "Y")
candidate := NewCandidate(tx.PubKey, d.sender, "0", 0, tx.MaxAmount, tx.CompRate, tx.Description, "N", "Y")
SaveCandidate(candidate)

// delegate a part of the max staked CMT amount
Expand All @@ -371,7 +371,7 @@ func (d deliver) declareCandidacy(tx TxDeclareCandidacy) error {

func (d deliver) declareGenesisCandidacy(tx TxDeclareCandidacy, votingPower int64) error {
// create and save the empty candidate
candidate := NewCandidate(tx.PubKey, d.sender, "0", votingPower, tx.MaxAmount, tx.Cut, tx.Description, "N", "Y")
candidate := NewCandidate(tx.PubKey, d.sender, "0", votingPower, tx.MaxAmount, tx.CompRate, tx.Description, "N", "Y")
SaveCandidate(candidate)

// delegate a part of the max staked CMT amount
Expand Down
6 changes: 3 additions & 3 deletions modules/stake/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ var _, _, _, _, _, _ sdk.TxInner = &TxDeclareCandidacy{}, &TxUpdateCandidacy{},
type TxDeclareCandidacy struct {
PubKey crypto.PubKey `json:"pub_key"`
MaxAmount string `json:"max_amount"`
Cut string `json:"cut"`
CompRate string `json:"comp_rate"`
Description
}

Expand All @@ -68,11 +68,11 @@ func (tx TxDeclareCandidacy) ReserveRequirement(ratio string) (result *big.Int)
return
}

func NewTxDeclareCandidacy(pubKey crypto.PubKey, maxAmount, cut string, descrpition Description) sdk.Tx {
func NewTxDeclareCandidacy(pubKey crypto.PubKey, maxAmount, compRate string, descrpition Description) sdk.Tx {
return TxDeclareCandidacy{
PubKey: pubKey,
MaxAmount: maxAmount,
Cut: cut,
CompRate: compRate,
Description: descrpition,
}.Wrap()
}
Expand Down
10 changes: 5 additions & 5 deletions modules/stake/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type Candidate struct {
Shares string `json:"shares"` // Total number of delegated shares to this candidate, equivalent to coins held in bond account
VotingPower int64 `json:"voting_power"` // Voting power if pubKey is a considered a validator
MaxShares string `json:"max_shares"`
Cut string `json:"cut"`
CompRate string `json:"cut"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
Description Description `json:"description"`
Expand All @@ -62,15 +62,15 @@ type Description struct {
}

// NewCandidate - initialize a new candidate
func NewCandidate(pubKey crypto.PubKey, ownerAddress common.Address, shares string, votingPower int64, maxShares, cut string, description Description, verified string, active string) *Candidate {
func NewCandidate(pubKey crypto.PubKey, ownerAddress common.Address, shares string, votingPower int64, maxShares, compRate string, description Description, verified string, active string) *Candidate {
now := utils.GetNow()
return &Candidate{
PubKey: pubKey,
OwnerAddress: ownerAddress,
Shares: shares,
VotingPower: votingPower,
MaxShares: maxShares,
Cut: cut,
CompRate: compRate,
CreatedAt: now,
UpdatedAt: now,
Description: description,
Expand All @@ -93,8 +93,8 @@ func (c *Candidate) ParseMaxShares() *big.Int {
return utils.ParseInt(c.MaxShares)
}

func (c *Candidate) ParseCut() float64 {
return utils.ParseFloat(c.Cut)
func (c *Candidate) ParseCompRate() float64 {
return utils.ParseFloat(c.CompRate)
}

func (c *Candidate) AddShares(value *big.Int) *big.Int {
Expand Down

0 comments on commit 3e65fa0

Please sign in to comment.