diff options
author | Wei-Ning Huang <w@dexon.org> | 2019-03-19 16:22:55 +0800 |
---|---|---|
committer | Wei-Ning Huang <w@dexon.org> | 2019-04-09 21:32:58 +0800 |
commit | 625bdb6b3ecdd5bed3adb4cd1f8fc0e56ae1f49f (patch) | |
tree | e376057c9fc6d5f5355ccc5d6976c529bb69fde7 /core | |
parent | 4ae37d25a2bd79fa98a5e7845342b98133d2cc6f (diff) | |
download | dexon-625bdb6b3ecdd5bed3adb4cd1f8fc0e56ae1f49f.tar dexon-625bdb6b3ecdd5bed3adb4cd1f8fc0e56ae1f49f.tar.gz dexon-625bdb6b3ecdd5bed3adb4cd1f8fc0e56ae1f49f.tar.bz2 dexon-625bdb6b3ecdd5bed3adb4cd1f8fc0e56ae1f49f.tar.lz dexon-625bdb6b3ecdd5bed3adb4cd1f8fc0e56ae1f49f.tar.xz dexon-625bdb6b3ecdd5bed3adb4cd1f8fc0e56ae1f49f.tar.zst dexon-625bdb6b3ecdd5bed3adb4cd1f8fc0e56ae1f49f.zip |
core: vm: automatically calculate notary set size (#276)
Diffstat (limited to 'core')
-rw-r--r-- | core/vm/oracle_contract_abi.go | 32 | ||||
-rw-r--r-- | core/vm/oracle_contracts.go | 65 | ||||
-rw-r--r-- | core/vm/oracle_contracts_test.go | 8 |
3 files changed, 95 insertions, 10 deletions
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..62261a36b 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 { @@ -941,6 +976,8 @@ func (s *GovernanceState) Configuration() *params.DexconConfig { 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(), @@ -959,10 +996,16 @@ func (s *GovernanceState) UpdateConfiguration(cfg *params.DexconConfig) { 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(notaryParamAlphaLoc), big.NewInt(int64(cfg.NotaryParamAlpha*decimalMultiplier))) + s.setStateBigInt(big.NewInt(notaryParamBetaLoc), big.NewInt(int64(cfg.NotaryParamBeta*decimalMultiplier))) s.setStateBigInt(big.NewInt(dkgSetSizeLoc), big.NewInt(int64(cfg.DKGSetSize))) 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 +1015,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 +1030,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 +1534,7 @@ func (g *GovernanceContract) updateConfiguration(cfg *rawConfigStruct) ([]byte, g.state.UpdateConfigurationRaw(cfg) g.state.emitConfigurationChangedEvent() + return nil, nil } @@ -1530,6 +1577,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 +1607,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 +1645,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") |