aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWei-Ning Huang <w@dexon.org>2019-03-24 00:03:11 +0800
committerWei-Ning Huang <w@dexon.org>2019-04-09 21:32:58 +0800
commit7065f0d194c0eea5f08d835e15308f957cb1c7eb (patch)
treedd933fac0a7ed3501448a7774ec3e4444db9a24d
parent30978cea5aa86a00d640bec0076daf73a130abbf (diff)
downloaddexon-7065f0d194c0eea5f08d835e15308f957cb1c7eb.tar
dexon-7065f0d194c0eea5f08d835e15308f957cb1c7eb.tar.gz
dexon-7065f0d194c0eea5f08d835e15308f957cb1c7eb.tar.bz2
dexon-7065f0d194c0eea5f08d835e15308f957cb1c7eb.tar.lz
dexon-7065f0d194c0eea5f08d835e15308f957cb1c7eb.tar.xz
dexon-7065f0d194c0eea5f08d835e15308f957cb1c7eb.tar.zst
dexon-7065f0d194c0eea5f08d835e15308f957cb1c7eb.zip
core: vm: fix DKG reset (#296)
-rw-r--r--core/vm/oracle_contracts.go45
-rw-r--r--core/vm/oracle_contracts_test.go4
-rw-r--r--test/genesis.json2
3 files changed, 27 insertions, 24 deletions
diff --git a/core/vm/oracle_contracts.go b/core/vm/oracle_contracts.go
index 8c10a98e8..155b3ea95 100644
--- a/core/vm/oracle_contracts.go
+++ b/core/vm/oracle_contracts.go
@@ -1230,8 +1230,7 @@ func getConfigState(evm *EVM, round *big.Int) (*GovernanceState, error) {
}
type coreDKGUtils interface {
- SetState(GovernanceState)
- NewGroupPublicKey(*big.Int, int) (tsigVerifierIntf, error)
+ NewGroupPublicKey(*GovernanceState, *big.Int, int) (tsigVerifierIntf, error)
}
type tsigVerifierIntf interface {
VerifySignature(coreCommon.Hash, coreCrypto.Signature) bool
@@ -1247,20 +1246,17 @@ type GovernanceContract struct {
// defaultCoreDKGUtils implements coreDKGUtils.
type defaultCoreDKGUtils struct {
- state GovernanceState
}
-func (c *defaultCoreDKGUtils) SetState(state GovernanceState) {
- c.state = state
-}
+func (c *defaultCoreDKGUtils) NewGroupPublicKey(
+ state *GovernanceState, round *big.Int, threshold int) (tsigVerifierIntf, error) {
-func (c *defaultCoreDKGUtils) NewGroupPublicKey(round *big.Int, threshold int) (tsigVerifierIntf, error) {
// Prepare DKGMasterPublicKeys.
- mpks := c.state.UniqueDKGMasterPublicKeys()
+ mpks := state.UniqueDKGMasterPublicKeys()
// Prepare DKGComplaints.
var complaints []*dkgTypes.Complaint
- for _, comp := range c.state.DKGComplaints() {
+ for _, comp := range state.DKGComplaints() {
x := new(dkgTypes.Complaint)
if err := rlp.DecodeBytes(comp, x); err != nil {
panic(err)
@@ -1806,8 +1802,9 @@ func (g *GovernanceContract) proposeCRS(nextRound *big.Int, signedCRS []byte) ([
}
}
- threshold := coreUtils.GetDKGThreshold(&coreTypes.Config{DKGSetSize: uint32(g.state.DKGSetSize().Uint64())})
- dkgGPK, err := g.coreDKGUtils.NewGroupPublicKey(nextRound, threshold)
+ threshold := coreUtils.GetDKGThreshold(&coreTypes.Config{
+ DKGSetSize: uint32(g.state.DKGSetSize().Uint64())})
+ dkgGPK, err := g.coreDKGUtils.NewGroupPublicKey(&g.state, nextRound, threshold)
if err != nil {
return nil, errExecutionReverted
}
@@ -1922,7 +1919,7 @@ func (g *GovernanceContract) resetDKG(newSignedCRS []byte) ([]byte, error) {
round := g.evm.Round
nextRound := new(big.Int).Add(round, big.NewInt(1))
- resetCount := g.state.DKGResetCount(round)
+ resetCount := g.state.DKGResetCount(nextRound)
// Just restart DEXON if failed at round 0.
if round.Cmp(big.NewInt(0)) == 0 {
@@ -1958,11 +1955,12 @@ func (g *GovernanceContract) resetDKG(newSignedCRS []byte) ([]byte, error) {
threshold := new(big.Int).Mul(
big.NewInt(2),
new(big.Int).Div(g.state.DKGSetSize(), big.NewInt(3)))
- tsigThreshold := coreUtils.GetDKGThreshold(&coreTypes.Config{DKGSetSize: uint32(g.state.DKGSetSize().Uint64())})
+ tsigThreshold := coreUtils.GetDKGThreshold(&coreTypes.Config{
+ DKGSetSize: uint32(g.state.DKGSetSize().Uint64())})
// If 2f + 1 of DKG set is finalized, check if DKG succeeded.
if g.state.DKGFinalizedsCount().Cmp(threshold) > 0 {
- _, err := g.coreDKGUtils.NewGroupPublicKey(nextRound, tsigThreshold)
+ _, err := g.coreDKGUtils.NewGroupPublicKey(&g.state, nextRound, tsigThreshold)
// DKG success.
if err == nil {
return nil, errExecutionReverted
@@ -1975,16 +1973,24 @@ func (g *GovernanceContract) resetDKG(newSignedCRS []byte) ([]byte, error) {
}
// Update CRS.
- headState, err := getRoundState(g.evm, round)
+ state, err := getRoundState(g.evm, round)
if err != nil {
return nil, errExecutionReverted
}
- prevCRS := headState.CRS()
+ prevCRS := state.CRS()
+
+ // CRS(n) = hash(CRS(n-1)) if n <= core.DKGRoundDelay
+ if round.Uint64() == dexCore.DKGDelayRound {
+ for i := uint64(0); i < dexCore.DKGDelayRound; i++ {
+ prevCRS = crypto.Keccak256Hash(prevCRS[:])
+ }
+ }
+
for i := uint64(0); i < resetCount.Uint64()+1; i++ {
prevCRS = crypto.Keccak256Hash(prevCRS[:])
}
- dkgGPK, err := g.coreDKGUtils.NewGroupPublicKey(round, tsigThreshold)
+ dkgGPK, err := g.coreDKGUtils.NewGroupPublicKey(state, round, tsigThreshold)
if err != nil {
return nil, errExecutionReverted
}
@@ -2011,9 +2017,9 @@ func (g *GovernanceContract) resetDKG(newSignedCRS []byte) ([]byte, error) {
g.state.emitCRSProposed(newRound, crs)
// Increase reset count.
- g.state.IncDKGResetCount(new(big.Int).Add(round, big.NewInt(1)))
-
+ g.state.IncDKGResetCount(nextRound)
g.state.emitDKGReset(round, blockHeight)
+
return nil, nil
}
@@ -2027,7 +2033,6 @@ func (g *GovernanceContract) Run(evm *EVM, input []byte, contract *Contract) (re
g.evm = evm
g.state = GovernanceState{evm.StateDB}
g.contract = contract
- g.coreDKGUtils.SetState(g.state)
// Parse input.
method, exists := GovernanceABI.Sig2Method[string(input[:4])]
diff --git a/core/vm/oracle_contracts_test.go b/core/vm/oracle_contracts_test.go
index 5a9250ee5..80bc9a73f 100644
--- a/core/vm/oracle_contracts_test.go
+++ b/core/vm/oracle_contracts_test.go
@@ -918,9 +918,7 @@ type testCoreMock struct {
tsigReturn bool
}
-func (m *testCoreMock) SetState(GovernanceState) {}
-
-func (m *testCoreMock) NewGroupPublicKey(*big.Int, int) (tsigVerifierIntf, error) {
+func (m *testCoreMock) NewGroupPublicKey(*GovernanceState, *big.Int, int) (tsigVerifierIntf, error) {
if m.newDKGGPKError != nil {
return nil, m.newDKGGPKError
}
diff --git a/test/genesis.json b/test/genesis.json
index 5a2891dcd..02ad15409 100644
--- a/test/genesis.json
+++ b/test/genesis.json
@@ -22,7 +22,7 @@
"minGasPrice": "0x3b9aca00",
"blockGasLimit": 40000000,
"lambdaBA": 250,
- "lambdaDKG": 1500,
+ "lambdaDKG": 1000,
"notaryParamAlpha": 70.5,
"notaryParamBeta": 264,
"roundLength": 100,