diff options
author | Wei-Ning Huang <w@dexon.org> | 2019-03-19 16:22:55 +0800 |
---|---|---|
committer | Wei-Ning Huang <w@byzantine-lab.io> | 2019-06-13 18:11:42 +0800 |
commit | 52abd7878fc59776550c73b0e1c6746448c83c17 (patch) | |
tree | 5824ad4b21fd51b2bfb496914791d71de99dfceb | |
parent | 55e22571a1cd5ef5d27a29ab1b0ecfb1b78cb264 (diff) | |
download | go-tangerine-52abd7878fc59776550c73b0e1c6746448c83c17.tar go-tangerine-52abd7878fc59776550c73b0e1c6746448c83c17.tar.gz go-tangerine-52abd7878fc59776550c73b0e1c6746448c83c17.tar.bz2 go-tangerine-52abd7878fc59776550c73b0e1c6746448c83c17.tar.lz go-tangerine-52abd7878fc59776550c73b0e1c6746448c83c17.tar.xz go-tangerine-52abd7878fc59776550c73b0e1c6746448c83c17.tar.zst go-tangerine-52abd7878fc59776550c73b0e1c6746448c83c17.zip |
core: vm: automatically calculate notary set size (#276)
-rw-r--r-- | core/governance.go | 2 | ||||
-rw-r--r-- | core/vm/oracle_contract_abi.go | 32 | ||||
-rw-r--r-- | core/vm/oracle_contracts.go | 69 | ||||
-rw-r--r-- | core/vm/oracle_contracts_test.go | 8 | ||||
-rw-r--r-- | params/config.go | 34 | ||||
-rw-r--r-- | params/gen_dexcon_config.go | 20 | ||||
-rw-r--r-- | test/genesis.json | 4 |
7 files changed, 125 insertions, 44 deletions
diff --git a/core/governance.go b/core/governance.go index db0e60b6c..da310a1cd 100644 --- a/core/governance.go +++ b/core/governance.go @@ -99,7 +99,7 @@ func (g *Governance) Configuration(round uint64) *coreTypes.Config { return &coreTypes.Config{ LambdaBA: time.Duration(c.LambdaBA) * time.Millisecond, LambdaDKG: time.Duration(c.LambdaDKG) * time.Millisecond, - NotarySetSize: c.NotarySetSize, + NotarySetSize: uint32(configHelper.NotarySetSize().Uint64()), DKGSetSize: c.DKGSetSize, RoundLength: c.RoundLength, MinBlockInterval: time.Duration(c.MinBlockInterval) * time.Millisecond, diff --git a/core/vm/oracle_contract_abi.go b/core/vm/oracle_contract_abi.go index 369c9ec17..af0affece 100644 --- a/core/vm/oracle_contract_abi.go +++ b/core/vm/oracle_contract_abi.go @@ -141,6 +141,20 @@ const GovernanceABIJSON = ` { "constant": true, "inputs": [], + "name": "notaryParamBeta", + "outputs": [ + { + "name": "", + "type": "uint256" + } + ], + "payable": false, + "stateMutability": "view", + "type": "function" + }, + { + "constant": true, + "inputs": [], "name": "miningVelocity", "outputs": [ { @@ -196,6 +210,20 @@ const GovernanceABIJSON = ` }, { "constant": true, + "inputs": [], + "name": "notaryParamAlpha", + "outputs": [ + { + "name": "", + "type": "uint256" + } + ], + "payable": false, + "stateMutability": "view", + "type": "function" + }, + { + "constant": true, "inputs": [ { "name": "", @@ -817,11 +845,11 @@ const GovernanceABIJSON = ` "type": "uint256" }, { - "name": "NotarySetSize", + "name": "NotaryParamAlpha", "type": "uint256" }, { - "name": "DKGSetSize", + "name": "NotaryParamBeta", "type": "uint256" }, { diff --git a/core/vm/oracle_contracts.go b/core/vm/oracle_contracts.go index b8eb4fedc..e30d0382a 100644 --- a/core/vm/oracle_contracts.go +++ b/core/vm/oracle_contracts.go @@ -21,6 +21,7 @@ import ( "bytes" "errors" "fmt" + "math" "math/big" "sort" @@ -84,6 +85,8 @@ const ( lambdaBALoc lambdaDKGLoc notarySetSizeLoc + notaryParamAlphaLoc + notaryParamBetaLoc dkgSetSizeLoc roundLengthLoc minBlockIntervalLoc @@ -816,11 +819,43 @@ func (s *GovernanceState) LambdaDKG() *big.Int { func (s *GovernanceState) NotarySetSize() *big.Int { return s.getStateBigInt(big.NewInt(notarySetSizeLoc)) } +func (s *GovernanceState) CalNotarySetSize() { + nodeSetSize := float64(len(s.QualifiedNodes())) + setSize := math.Ceil((nodeSetSize*0.6-1)/3)*3 + 1 + + if nodeSetSize >= 80 { + alpha := float64(s.NotaryParamAlpha().Uint64()) / decimalMultiplier + beta := float64(s.NotaryParamBeta().Uint64()) / decimalMultiplier + setSize = math.Ceil(alpha*math.Log(nodeSetSize) - beta) + } + s.setStateBigInt(big.NewInt(notarySetSizeLoc), big.NewInt(int64(setSize))) +} + +// uint256 public notaryParamAlpha; +func (s *GovernanceState) NotaryParamAlpha() *big.Int { + return s.getStateBigInt(big.NewInt(notaryParamAlphaLoc)) +} + +// uint256 public notaryParamBeta; +func (s *GovernanceState) NotaryParamBeta() *big.Int { + return s.getStateBigInt(big.NewInt(notaryParamBetaLoc)) +} // uint256 public dkgSetSize; func (s *GovernanceState) DKGSetSize() *big.Int { return s.getStateBigInt(big.NewInt(dkgSetSizeLoc)) } +func (s *GovernanceState) CalDKGSetSize() { + nodeSetSize := float64(len(s.QualifiedNodes())) + setSize := math.Ceil((nodeSetSize*0.6-1)/3)*3 + 1 + + if nodeSetSize >= 100 { + alpha := float64(s.NotaryParamAlpha().Uint64()) / decimalMultiplier + beta := float64(s.NotaryParamBeta().Uint64()) / decimalMultiplier + setSize = math.Ceil(alpha*math.Log(nodeSetSize) - beta) + } + s.setStateBigInt(big.NewInt(dkgSetSizeLoc), big.NewInt(int64(setSize))) +} // uint256 public roundLength; func (s *GovernanceState) RoundLength() *big.Int { @@ -939,8 +974,8 @@ func (s *GovernanceState) Configuration() *params.DexconConfig { BlockGasLimit: s.getStateBigInt(big.NewInt(blockGasLimitLoc)).Uint64(), LambdaBA: s.getStateBigInt(big.NewInt(lambdaBALoc)).Uint64(), LambdaDKG: s.getStateBigInt(big.NewInt(lambdaDKGLoc)).Uint64(), - NotarySetSize: uint32(s.getStateBigInt(big.NewInt(notarySetSizeLoc)).Uint64()), - DKGSetSize: uint32(s.getStateBigInt(big.NewInt(dkgSetSizeLoc)).Uint64()), + NotaryParamAlpha: float32(s.getStateBigInt(big.NewInt(notaryParamAlphaLoc)).Uint64()) / decimalMultiplier, + NotaryParamBeta: float32(s.getStateBigInt(big.NewInt(notaryParamBetaLoc)).Uint64()) / decimalMultiplier, RoundLength: s.getStateBigInt(big.NewInt(roundLengthLoc)).Uint64(), MinBlockInterval: s.getStateBigInt(big.NewInt(minBlockIntervalLoc)).Uint64(), FineValues: s.FineValues(), @@ -958,11 +993,15 @@ func (s *GovernanceState) UpdateConfiguration(cfg *params.DexconConfig) { s.setStateBigInt(big.NewInt(blockGasLimitLoc), big.NewInt(int64(cfg.BlockGasLimit))) s.setStateBigInt(big.NewInt(lambdaBALoc), big.NewInt(int64(cfg.LambdaBA))) s.setStateBigInt(big.NewInt(lambdaDKGLoc), big.NewInt(int64(cfg.LambdaDKG))) - s.setStateBigInt(big.NewInt(notarySetSizeLoc), big.NewInt(int64(cfg.NotarySetSize))) - s.setStateBigInt(big.NewInt(dkgSetSizeLoc), big.NewInt(int64(cfg.DKGSetSize))) + s.setStateBigInt(big.NewInt(notaryParamAlphaLoc), big.NewInt(int64(cfg.NotaryParamAlpha*decimalMultiplier))) + s.setStateBigInt(big.NewInt(notaryParamBetaLoc), big.NewInt(int64(cfg.NotaryParamBeta*decimalMultiplier))) s.setStateBigInt(big.NewInt(roundLengthLoc), big.NewInt(int64(cfg.RoundLength))) s.setStateBigInt(big.NewInt(minBlockIntervalLoc), big.NewInt(int64(cfg.MinBlockInterval))) s.SetFineValues(cfg.FineValues) + + // Calculate set size. + s.CalNotarySetSize() + s.CalDKGSetSize() } type rawConfigStruct struct { @@ -972,8 +1011,8 @@ type rawConfigStruct struct { MinGasPrice *big.Int LambdaBA *big.Int LambdaDKG *big.Int - NotarySetSize *big.Int - DKGSetSize *big.Int + NotaryParamAlpha *big.Int + NotaryParamBeta *big.Int RoundLength *big.Int MinBlockInterval *big.Int FineValues []*big.Int @@ -987,11 +1026,14 @@ func (s *GovernanceState) UpdateConfigurationRaw(cfg *rawConfigStruct) { s.setStateBigInt(big.NewInt(blockGasLimitLoc), cfg.BlockGasLimit) s.setStateBigInt(big.NewInt(lambdaBALoc), cfg.LambdaBA) s.setStateBigInt(big.NewInt(lambdaDKGLoc), cfg.LambdaDKG) - s.setStateBigInt(big.NewInt(notarySetSizeLoc), cfg.NotarySetSize) - s.setStateBigInt(big.NewInt(dkgSetSizeLoc), cfg.DKGSetSize) + s.setStateBigInt(big.NewInt(notaryParamAlphaLoc), cfg.NotaryParamAlpha) + s.setStateBigInt(big.NewInt(notaryParamBetaLoc), cfg.NotaryParamBeta) s.setStateBigInt(big.NewInt(roundLengthLoc), cfg.RoundLength) s.setStateBigInt(big.NewInt(minBlockIntervalLoc), cfg.MinBlockInterval) s.SetFineValues(cfg.FineValues) + + s.CalNotarySetSize() + s.CalDKGSetSize() } // event ConfigurationChanged(); @@ -1488,6 +1530,7 @@ func (g *GovernanceContract) updateConfiguration(cfg *rawConfigStruct) ([]byte, g.state.UpdateConfigurationRaw(cfg) g.state.emitConfigurationChangedEvent() + return nil, nil } @@ -1530,6 +1573,9 @@ func (g *GovernanceContract) register( if value.Cmp(big.NewInt(0)) > 0 { g.state.IncTotalStaked(value) g.state.emitStaked(caller, value) + + g.state.CalNotarySetSize() + g.state.CalDKGSetSize() } return g.useGas(GovernanceActionGasCost) } @@ -1557,6 +1603,10 @@ func (g *GovernanceContract) stake() ([]byte, error) { g.state.IncTotalStaked(value) g.state.emitStaked(caller, value) + + g.state.CalNotarySetSize() + g.state.CalDKGSetSize() + return g.useGas(GovernanceActionGasCost) } @@ -1591,6 +1641,9 @@ func (g *GovernanceContract) unstake(amount *big.Int) ([]byte, error) { g.state.DecTotalStaked(amount) g.state.emitUnstaked(caller, amount) + g.state.CalNotarySetSize() + g.state.CalDKGSetSize() + return g.useGas(GovernanceActionGasCost) } diff --git a/core/vm/oracle_contracts_test.go b/core/vm/oracle_contracts_test.go index 868ea533c..69e13e1a1 100644 --- a/core/vm/oracle_contracts_test.go +++ b/core/vm/oracle_contracts_test.go @@ -492,8 +492,8 @@ func (g *OracleContractsTestSuite) TestUpdateConfiguration() { big.NewInt(8000000), big.NewInt(250), big.NewInt(2500), - big.NewInt(4), - big.NewInt(4), + big.NewInt(int64(70.5*decimalMultiplier)), + big.NewInt(264*decimalMultiplier), big.NewInt(600), big.NewInt(900), []*big.Int{big.NewInt(1), big.NewInt(1), big.NewInt(1)}) @@ -591,7 +591,7 @@ func (g *OracleContractsTestSuite) TestConfigurationReading() { g.Require().NoError(err) err = GovernanceABI.ABI.Unpack(&value, "notarySetSize", res) g.Require().NoError(err) - g.Require().Equal(g.config.NotarySetSize, uint32(value.Uint64())) + g.Require().True(uint32(value.Uint64()) > 0) // DKGRound. input, err = GovernanceABI.ABI.Pack("dkgRound") @@ -612,7 +612,7 @@ func (g *OracleContractsTestSuite) TestConfigurationReading() { g.Require().NoError(err) err = GovernanceABI.ABI.Unpack(&value, "dkgSetSize", res) g.Require().NoError(err) - g.Require().Equal(g.config.DKGSetSize, uint32(value.Uint64())) + g.Require().True(uint32(value.Uint64()) > 0) // RoundLength. input, err = GovernanceABI.ABI.Pack("roundLength") diff --git a/params/config.go b/params/config.go index 52570d5a4..f61a9b6df 100644 --- a/params/config.go +++ b/params/config.go @@ -26,10 +26,10 @@ import ( // Genesis hashes to enforce below configs on. var ( - MainnetGenesisHash = common.HexToHash("0xcde2f22a8ccad92a6839880e445bccf3949aae712056a9d139a318f9f4b18fe8") - TestnetGenesisHash = common.HexToHash("0x7463c980be61ae7e32e8a7611c9532989dfc4463c264b9ef3d31bfe490780425") - TaipeiGenesisHash = common.HexToHash("0x953540c8dc6155c506b85be9944b97c636948e2cb47b698e4bdf7d58563a6e01") - YilanGenesisHash = common.HexToHash("0x70654e2b863f01f00f843e6c4b0ecf2af39f7e27493f577ca4526b40eb1773c7") + MainnetGenesisHash = common.HexToHash("0x8ac8e240790046eb72225eb2a2381f3ef5a7a88291fffc702ba08503cc2a1341") + TestnetGenesisHash = common.HexToHash("0x3caf9a977e579b4de001956508b57563a4b61742c66f49323a1294ad214da29d") + TaipeiGenesisHash = common.HexToHash("0x13e85a0207f2888ac9c1746c94d5d7fd87ff637cbd080b42d5db1252341f4428") + YilanGenesisHash = common.HexToHash("0x6b1a94b5e4c24665942a3b768bd98b39d61771a5eaba97c0466644d78d8a2f11") ) // TrustedCheckpoints associates each known checkpoint with the genesis hash of @@ -66,8 +66,8 @@ var ( BlockGasLimit: 40000000, LambdaBA: 250, LambdaDKG: 2500, - NotarySetSize: 4, - DKGSetSize: 4, + NotaryParamAlpha: 70.5, + NotaryParamBeta: 264, RoundLength: 600, MinBlockInterval: 1000, FineValues: []*big.Int{ @@ -117,8 +117,8 @@ var ( BlockGasLimit: 80000000, LambdaBA: 250, LambdaDKG: 10000, - NotarySetSize: 24, - DKGSetSize: 24, + NotaryParamAlpha: 70.5, + NotaryParamBeta: 264, RoundLength: 1200, MinBlockInterval: 1000, FineValues: []*big.Int{ @@ -159,8 +159,8 @@ var ( BlockGasLimit: 21000 * 5000, LambdaBA: 250, LambdaDKG: 10000, - NotarySetSize: 24, - DKGSetSize: 24, + NotaryParamAlpha: 70.5, + NotaryParamBeta: 264, RoundLength: 1200, MinBlockInterval: 500, FineValues: []*big.Int{ @@ -209,8 +209,8 @@ var ( BlockGasLimit: 21000 * 5000, LambdaBA: 250, LambdaDKG: 10000, - NotarySetSize: 4, - DKGSetSize: 4, + NotaryParamAlpha: 70.5, + NotaryParamBeta: 264, RoundLength: 1200, MinBlockInterval: 500, FineValues: []*big.Int{ @@ -359,8 +359,8 @@ type DexconConfig struct { BlockGasLimit uint64 `json:"blockGasLimit"` LambdaBA uint64 `json:"lambdaBA"` LambdaDKG uint64 `json:"lambdaDKG"` - NotarySetSize uint32 `json:"notarySetSize"` - DKGSetSize uint32 `json:"dkgSetSize"` + NotaryParamAlpha float32 `json:"notaryParamAlpha"` + NotaryParamBeta float32 `json:"notaryParamBeta"` RoundLength uint64 `json:"roundLength"` MinBlockInterval uint64 `json:"minBlockInterval"` FineValues []*big.Int `json:"fineValues"` @@ -376,7 +376,7 @@ type dexconConfigSpecMarshaling struct { // String implements the stringer interface, returning the consensus engine details. func (d *DexconConfig) String() string { - return fmt.Sprintf("{GenesisCRSText: %v Owner: %v MinStake: %v LockupPeriod: %v MiningVelocity: %v NextHalvingSupply: %v LastHalvedAmount: %v MinGasPrice: %v BlockGasLimit: %v LambdaBA: %v LambdaDKG: %v NotarySetSize: %v DKGSetSize: %v RoundLength: %v MinBlockInterval: %v FineValues: %v}", + return fmt.Sprintf("{GenesisCRSText: %v Owner: %v MinStake: %v LockupPeriod: %v MiningVelocity: %v NextHalvingSupply: %v LastHalvedAmount: %v MinGasPrice: %v BlockGasLimit: %v LambdaBA: %v LambdaDKG: %v NotaryParamAlpha: %v NotaryParamBeta: %v RoundLength: %v MinBlockInterval: %v FineValues: %v}", d.GenesisCRSText, d.Owner, d.MinStake, @@ -388,8 +388,8 @@ func (d *DexconConfig) String() string { d.BlockGasLimit, d.LambdaBA, d.LambdaDKG, - d.NotarySetSize, - d.DKGSetSize, + d.NotaryParamAlpha, + d.NotaryParamBeta, d.RoundLength, d.MinBlockInterval, d.FineValues, diff --git a/params/gen_dexcon_config.go b/params/gen_dexcon_config.go index 38753916a..dbceb1676 100644 --- a/params/gen_dexcon_config.go +++ b/params/gen_dexcon_config.go @@ -26,8 +26,8 @@ func (d DexconConfig) MarshalJSON() ([]byte, error) { BlockGasLimit uint64 `json:"blockGasLimit"` LambdaBA uint64 `json:"lambdaBA"` LambdaDKG uint64 `json:"lambdaDKG"` - NotarySetSize uint32 `json:"notarySetSize"` - DKGSetSize uint32 `json:"dkgSetSize"` + NotaryParamAlpha float32 `json:"notaryParamAlpha"` + NotaryParamBeta float32 `json:"notaryParamBeta"` RoundLength uint64 `json:"roundLength"` MinBlockInterval uint64 `json:"minBlockInterval"` FineValues []*math.HexOrDecimal256 `json:"fineValues"` @@ -44,8 +44,8 @@ func (d DexconConfig) MarshalJSON() ([]byte, error) { enc.BlockGasLimit = d.BlockGasLimit enc.LambdaBA = d.LambdaBA enc.LambdaDKG = d.LambdaDKG - enc.NotarySetSize = d.NotarySetSize - enc.DKGSetSize = d.DKGSetSize + enc.NotaryParamAlpha = d.NotaryParamAlpha + enc.NotaryParamBeta = d.NotaryParamBeta enc.RoundLength = d.RoundLength enc.MinBlockInterval = d.MinBlockInterval if d.FineValues != nil { @@ -71,8 +71,8 @@ func (d *DexconConfig) UnmarshalJSON(input []byte) error { BlockGasLimit *uint64 `json:"blockGasLimit"` LambdaBA *uint64 `json:"lambdaBA"` LambdaDKG *uint64 `json:"lambdaDKG"` - NotarySetSize *uint32 `json:"notarySetSize"` - DKGSetSize *uint32 `json:"dkgSetSize"` + NotaryParamAlpha *float32 `json:"notaryParamAlpha"` + NotaryParamBeta *float32 `json:"notaryParamBeta"` RoundLength *uint64 `json:"roundLength"` MinBlockInterval *uint64 `json:"minBlockInterval"` FineValues []*math.HexOrDecimal256 `json:"fineValues"` @@ -114,11 +114,11 @@ func (d *DexconConfig) UnmarshalJSON(input []byte) error { if dec.LambdaDKG != nil { d.LambdaDKG = *dec.LambdaDKG } - if dec.NotarySetSize != nil { - d.NotarySetSize = *dec.NotarySetSize + if dec.NotaryParamAlpha != nil { + d.NotaryParamAlpha = *dec.NotaryParamAlpha } - if dec.DKGSetSize != nil { - d.DKGSetSize = *dec.DKGSetSize + if dec.NotaryParamBeta != nil { + d.NotaryParamBeta = *dec.NotaryParamBeta } if dec.RoundLength != nil { d.RoundLength = *dec.RoundLength diff --git a/test/genesis.json b/test/genesis.json index 8be5a844d..5a2891dcd 100644 --- a/test/genesis.json +++ b/test/genesis.json @@ -23,8 +23,8 @@ "blockGasLimit": 40000000, "lambdaBA": 250, "lambdaDKG": 1500, - "notarySetSize": 4, - "dkgSetSize": 4, + "notaryParamAlpha": 70.5, + "notaryParamBeta": 264, "roundLength": 100, "minBlockInterval": 500, "fineValues": [ |