Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add min available quantity of candidate for vote #297

Merged
merged 3 commits into from
May 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions blockchain/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
memdb "github.com/fractalplatform/fractal/utils/fdb/memdb"
)

var defaultgenesisBlockHash = common.HexToHash("0x97407c7bc850ea5509e83951c5d1b59e8cd11232c14c0dae472426f78cb22706")
var defaultgenesisBlockHash = common.HexToHash("0x68d30569539b31f10a739e9772f9b62d8ee85e63885d74446b40fce04e5e8560")

func TestDefaultGenesisBlock(t *testing.T) {
block, _ := DefaultGenesis().ToBlock(nil)
Expand All @@ -43,7 +43,7 @@ func TestDefaultGenesisBlock(t *testing.T) {

func TestSetupGenesis(t *testing.T) {
var (
customghash = common.HexToHash("a7615933918d596e6132151974a638188f04f0dbec4c0911d747614364ce1835")
customghash = common.HexToHash("0xe76c5877fca9f043314c8ea529e199142fbf71d36b29faacf4500779392d9e74")

customg = Genesis{
Config: params.DefaultChainconfig.Copy(),
Expand All @@ -53,7 +53,7 @@ func TestSetupGenesis(t *testing.T) {
}
oldcustomg = customg

oldcustomghash = common.HexToHash("56e5e65c74bcacbce0f1dcd9628ae153ff525b2ea44d4eb65b648354c8120101")
oldcustomghash = common.HexToHash("3f27fcb4e7df111d644e595f91155e37093b658fdebc7d4b6f9dcb20d4b7b1e8")
)
customg.Config.ChainID = big.NewInt(5)
oldcustomg.Config = customg.Config.Copy()
Expand Down
4 changes: 3 additions & 1 deletion build/genesis.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@
"dposParams": {
"maxURLLen": 512,
"unitStake": 1000,
"candidateAvailableMinQuantity": 10,
"candidateMinQuantity": 10,
"voterMinQuantity": 1,
"activatedMinCandidate": 3,
"activatedMinQuantity": 100,
"blockInterval": 3000,
"blockFrequency": 6,
Expand Down Expand Up @@ -57,7 +59,7 @@
}, {
"name": "fractal.asset",
"pubKey": "0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
},{
}, {
"name": "fractal.dpos",
"pubKey": "0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
}, {
Expand Down
2 changes: 2 additions & 0 deletions build/testnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@
"dposParams": {
"maxURLLen": 512,
"unitStake": 1000,
"candidateAvailableMinQuantity": 10,
"candidateMinQuantity": 10,
"voterMinQuantity": 1,
"activatedMinCandidate": 3,
"activatedMinQuantity": 100,
"blockInterval": 3000,
"blockFrequency": 6,
Expand Down
2 changes: 1 addition & 1 deletion cmd/ft/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func init() {
// the zero'd block (i.e. genesis) or will fail hard if it can't succeed.
func initGenesis() error {
// Make sure we have a valid genesis JSON
genesis := new(blockchain.Genesis)
genesis := blockchain.DefaultGenesis()
if len(ftCfgInstance.GenesisFile) != 0 {
file, err := os.Open(ftCfgInstance.GenesisFile)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/ft/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func makeNode() (*node.Node, error) {
}
defer file.Close()

genesis := new(blockchain.Genesis)
genesis := blockchain.DefaultGenesis()
if err := json.NewDecoder(file).Decode(genesis); err != nil {
return nil, fmt.Errorf("invalid genesis file: %v(%v)", ftCfgInstance.GenesisFile, err)
}
Expand Down
81 changes: 42 additions & 39 deletions consensus/dpos/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,50 +24,53 @@ import (

// DefaultConfig configures
var DefaultConfig = &Config{
MaxURLLen: 512,
UnitStake: big.NewInt(1000),
CandidateMinQuantity: big.NewInt(10),
VoterMinQuantity: big.NewInt(1),
ActivatedMinQuantity: big.NewInt(100),
BlockInterval: 3000,
BlockFrequency: 6,
CandidateScheduleSize: 3,
BackupScheduleSize: 0,
EpchoInterval: 540000,
FreezeEpchoSize: 3,
AccountName: "ftsystemdpos",
SystemName: "ftsystemio",
SystemURL: "www.fractalproject.com",
ExtraBlockReward: big.NewInt(1),
BlockReward: big.NewInt(5),
Decimals: 18,
AssetID: 1,
ReferenceTime: 1555776000000 * uint64(time.Millisecond), // 2019-04-21 00:00:00
MaxURLLen: 512,
UnitStake: big.NewInt(1000),
CandidateMinQuantity: big.NewInt(10),
CandidateAvailableMinQuantity: big.NewInt(10),
VoterMinQuantity: big.NewInt(1),
ActivatedMinCandidate: 3,
ActivatedMinQuantity: big.NewInt(100),
BlockInterval: 3000,
BlockFrequency: 6,
CandidateScheduleSize: 3,
BackupScheduleSize: 0,
EpchoInterval: 540000,
FreezeEpchoSize: 3,
AccountName: "ftsystemdpos",
SystemName: "ftsystemio",
SystemURL: "www.fractalproject.com",
ExtraBlockReward: big.NewInt(1),
BlockReward: big.NewInt(5),
Decimals: 18,
AssetID: 1,
ReferenceTime: 1555776000000 * uint64(time.Millisecond), // 2019-04-21 00:00:00
}

// Config dpos configures
type Config struct {
// consensus fileds
MaxURLLen uint64 `json:"maxURLLen"` // url length
UnitStake *big.Int `json:"unitStake"` // state unit
CandidateMinQuantity *big.Int `json:"candidateMinQuantity"` // min quantity
VoterMinQuantity *big.Int `json:"voterMinQuantity"` // min quantity
ActivatedMinQuantity *big.Int `json:"activatedMinQuantity"` // min active quantity
BlockInterval uint64 `json:"blockInterval"`
BlockFrequency uint64 `json:"blockFrequency"`
CandidateScheduleSize uint64 `json:"candidateScheduleSize"`
BackupScheduleSize uint64 `json:"backupScheduleSize"`
EpchoInterval uint64 `json:"epchoInterval"`
FreezeEpchoSize uint64 `json:"freezeEpchoSize"`
AccountName string `json:"accountName"`
SystemName string `json:"systemName"`
SystemURL string `json:"systemURL"`
ExtraBlockReward *big.Int `json:"extraBlockReward"`
BlockReward *big.Int `json:"blockReward"`
InitCandidateSchedule []string `json:"initCandidateSchedule"`
Decimals uint64 `json:"decimals"`
AssetID uint64 `json:"assetID"`
ReferenceTime uint64 `json:"referenceTime"`
MaxURLLen uint64 `json:"maxURLLen"` // url length
UnitStake *big.Int `json:"unitStake"` // state unit
CandidateMinQuantity *big.Int `json:"candidateMinQuantity"` // min quantity
CandidateAvailableMinQuantity *big.Int `json:"candidateAvailableMinQuantity"` // min quantity
VoterMinQuantity *big.Int `json:"voterMinQuantity"` // min quantity
ActivatedMinCandidate uint64 `json:"activatedMinCandidate"`
ActivatedMinQuantity *big.Int `json:"activatedMinQuantity"` // min active quantity
BlockInterval uint64 `json:"blockInterval"`
BlockFrequency uint64 `json:"blockFrequency"`
CandidateScheduleSize uint64 `json:"candidateScheduleSize"`
BackupScheduleSize uint64 `json:"backupScheduleSize"`
EpchoInterval uint64 `json:"epchoInterval"`
FreezeEpchoSize uint64 `json:"freezeEpchoSize"`
AccountName string `json:"accountName"`
SystemName string `json:"systemName"`
SystemURL string `json:"systemURL"`
ExtraBlockReward *big.Int `json:"extraBlockReward"`
BlockReward *big.Int `json:"blockReward"`
Decimals uint64 `json:"decimals"`
AssetID uint64 `json:"assetID"`
ReferenceTime uint64 `json:"referenceTime"`

// cache files
decimal atomic.Value
Expand Down
45 changes: 30 additions & 15 deletions consensus/dpos/vote.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,12 @@ func (sys *System) RegCandidate(epcho uint64, candidate string, url string, stak
if err != nil {
return err
}
if sub := new(big.Int).Sub(quantity, q); sub.Sign() == -1 {
return fmt.Errorf("invalid vote stake %v(insufficient) %v > %v", candidate, new(big.Int).Mul(quantity, sys.config.unitStake()), new(big.Int).Mul(q, sys.config.unitStake()))
} else if err := sys.SetAvailableQuantity(epcho, candidate, sub); err != nil {

sub := new(big.Int).Sub(quantity, q)
if sub.Sign() == -1 {
sub = big.NewInt(0)
}
if err := sys.SetAvailableQuantity(epcho, candidate, sub); err != nil {
return err
}

Expand Down Expand Up @@ -145,9 +148,11 @@ func (sys *System) UpdateCandidate(epcho uint64, candidate string, url string, n
if err != nil {
return err
}
if sub := new(big.Int).Sub(quantity, q); sub.Sign() == -1 {
return fmt.Errorf("invalid vote stake %v(insufficient) %v < %v", candidate, new(big.Int).Mul(quantity, sys.config.unitStake()), new(big.Int).Mul(q, sys.config.unitStake()))
} else if err := sys.SetAvailableQuantity(epcho, candidate, sub); err != nil {
sub := new(big.Int).Sub(quantity, q)
if sub.Sign() == -1 {
sub = big.NewInt(0)
}
if err := sys.SetAvailableQuantity(epcho, candidate, sub); err != nil {
return err
}
}
Expand Down Expand Up @@ -329,6 +334,22 @@ func (sys *System) VoteCandidate(epcho uint64, voter string, candidate string, s
return fmt.Errorf("invalid candidate %v", candidate)
}

gstate, err := sys.GetState(epcho)
if err != nil {
return err
}
timestamp := sys.config.epochTimeStamp(epcho)
if sys.config.epoch(sys.config.ReferenceTime) == gstate.PreEpcho {
timestamp = sys.config.epochTimeStamp(gstate.PreEpcho)
}
bquantity, err := sys.GetBalanceByTime(candidate, timestamp)
if err != nil {
return err
}
if bquantity.Cmp(sys.config.CandidateAvailableMinQuantity) == -1 {
return fmt.Errorf("invalid candidate %v,(insufficient available quantity %v < %v)", candidate, bquantity, sys.config.CandidateAvailableMinQuantity)
}

// stake validity
m := big.NewInt(0)
q, _ := new(big.Int).DivMod(stake, sys.config.unitStake(), m)
Expand Down Expand Up @@ -375,10 +396,6 @@ func (sys *System) VoteCandidate(epcho uint64, voter string, candidate string, s
return err
}

gstate, err := sys.GetState(epcho)
if err != nil {
return err
}
gstate.TotalQuantity = new(big.Int).Add(gstate.TotalQuantity, q)
if err := sys.SetState(gstate); err != nil {
return err
Expand Down Expand Up @@ -504,10 +521,8 @@ func (sys *System) UpdateElectedCandidates(pepcho uint64, epcho uint64, number u
if pstate.Dpos && strings.Compare(candidateInfo.Name, miner) == 0 {
candidateInfo.Counter++
}
if pstate.Epcho != pstate.PreEpcho {
if err := sys.SetCandidateByEpcho(pepcho, candidateInfo); err != nil {
return err
}
if err := sys.SetCandidateByEpcho(pepcho, candidateInfo); err != nil {
return err
}

if !candidateInfo.invalid() {
Expand Down Expand Up @@ -538,7 +553,7 @@ func (sys *System) UpdateElectedCandidates(pepcho uint64, epcho uint64, number u
}

if !pstate.Dpos && totalQuantity.Cmp(sys.config.ActivatedMinQuantity) >= 0 &&
cnt >= n {
cnt >= n && cnt >= sys.config.ActivatedMinCandidate {
pstate.Dpos = true
}

Expand Down
56 changes: 30 additions & 26 deletions params/chainconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,21 @@ type FrokedConfig struct {
}

type DposConfig struct {
MaxURLLen uint64 `json:"maxURLLen"` // url length
UnitStake *big.Int `json:"unitStake"` // state unit
CandidateMinQuantity *big.Int `json:"candidateMinQuantity"` // min quantity
VoterMinQuantity *big.Int `json:"voterMinQuantity"` // min quantity
ActivatedMinQuantity *big.Int `json:"activatedMinQuantity"` // min active quantity
BlockInterval uint64 `json:"blockInterval"`
BlockFrequency uint64 `json:"blockFrequency"`
CandidateScheduleSize uint64 `json:"candidateScheduleSize"`
BackupScheduleSize uint64 `json:"backupScheduleSize"`
EpchoInterval uint64 `json:"epchoInterval"`
FreezeEpchoSize uint64 `json:"freezeEpchoSize"`
ExtraBlockReward *big.Int `json:"extraBlockReward"`
BlockReward *big.Int `json:"blockReward"`
MaxURLLen uint64 `json:"maxURLLen"` // url length
UnitStake *big.Int `json:"unitStake"` // state unit
CandidateAvailableMinQuantity *big.Int `json:"candidateAvailableMinQuantity"`
CandidateMinQuantity *big.Int `json:"candidateMinQuantity"` // min quantity
VoterMinQuantity *big.Int `json:"voterMinQuantity"` // min quantity
ActivatedMinCandidate uint64 `json:"activatedMinCandidate"`
ActivatedMinQuantity *big.Int `json:"activatedMinQuantity"` // min active quantity
BlockInterval uint64 `json:"blockInterval"`
BlockFrequency uint64 `json:"blockFrequency"`
CandidateScheduleSize uint64 `json:"candidateScheduleSize"`
BackupScheduleSize uint64 `json:"backupScheduleSize"`
EpchoInterval uint64 `json:"epchoInterval"`
FreezeEpchoSize uint64 `json:"freezeEpchoSize"`
ExtraBlockReward *big.Int `json:"extraBlockReward"`
BlockReward *big.Int `json:"blockReward"`
}

var DefaultChainconfig = &ChainConfig{
Expand All @@ -100,19 +102,21 @@ var DefaultChainconfig = &ChainConfig{
Forkpercentage: 80,
},
DposCfg: &DposConfig{
MaxURLLen: 512,
UnitStake: big.NewInt(1000),
CandidateMinQuantity: big.NewInt(10),
VoterMinQuantity: big.NewInt(1),
ActivatedMinQuantity: big.NewInt(100),
BlockInterval: 3000,
BlockFrequency: 6,
CandidateScheduleSize: 3,
BackupScheduleSize: 0,
EpchoInterval: 540000,
FreezeEpchoSize: 3,
ExtraBlockReward: big.NewInt(1),
BlockReward: big.NewInt(5),
MaxURLLen: 512,
UnitStake: big.NewInt(1000),
CandidateAvailableMinQuantity: big.NewInt(10),
CandidateMinQuantity: big.NewInt(10),
VoterMinQuantity: big.NewInt(1),
ActivatedMinCandidate: 3,
ActivatedMinQuantity: big.NewInt(100),
BlockInterval: 3000,
BlockFrequency: 6,
CandidateScheduleSize: 3,
BackupScheduleSize: 0,
EpchoInterval: 540000,
FreezeEpchoSize: 3,
ExtraBlockReward: big.NewInt(1),
BlockReward: big.NewInt(5),
},
SnapshotInterval: 180000,
SysName: "fractal.admin",
Expand Down
2 changes: 1 addition & 1 deletion types/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func (a *Action) Check(conf *params.ChainConfig) error {
fallthrough
case ExitTakeOver:
if a.data.To.String() != conf.DposName {
return fmt.Errorf("Receipt should is %v", conf.AssetName)
return fmt.Errorf("Receipt should is %v", conf.DposName)
}
if a.data.AssetID != conf.SysTokenID {
return fmt.Errorf("Asset id should is %v", conf.SysTokenID)
Expand Down