diff options
author | Wei-Ning Huang <w@dexon.org> | 2019-04-04 10:33:55 +0800 |
---|---|---|
committer | Wei-Ning Huang <w@dexon.org> | 2019-04-09 21:32:59 +0800 |
commit | d52097c148c5247be8e346c8eca3c3cfc8fdd770 (patch) | |
tree | 577e129ddc2ee59676bbc890316d7501e7388e42 | |
parent | b8eb11fc1ecba52c05ea150cf77217e61bb16c55 (diff) | |
download | dexon-d52097c148c5247be8e346c8eca3c3cfc8fdd770.tar.gz dexon-d52097c148c5247be8e346c8eca3c3cfc8fdd770.tar.zst dexon-d52097c148c5247be8e346c8eca3c3cfc8fdd770.zip |
core: vm: cache GPK to prevent DoS (#335)
Cache GPK so one can not slow the chain down with the ProposeCRS method
call.
-rw-r--r-- | core/vm/oracle_contracts.go | 19 | ||||
-rw-r--r-- | dex/governance.go | 32 |
2 files changed, 34 insertions, 17 deletions
diff --git a/core/vm/oracle_contracts.go b/core/vm/oracle_contracts.go index a698b87f1..06b9c2a89 100644 --- a/core/vm/oracle_contracts.go +++ b/core/vm/oracle_contracts.go @@ -24,6 +24,7 @@ import ( "math" "math/big" "sort" + "sync/atomic" "github.com/dexon-foundation/dexon/accounts/abi" "github.com/dexon-foundation/dexon/common" @@ -1250,16 +1251,32 @@ type GovernanceContract struct { // defaultCoreDKGUtils implements coreDKGUtils. type defaultCoreDKGUtils struct { + gpk atomic.Value } func (c *defaultCoreDKGUtils) NewGroupPublicKey( state *GovernanceState, round *big.Int, threshold int) (tsigVerifierIntf, error) { + var gpk *dkgTypes.GroupPublicKey + var err error + + v := c.gpk.Load() + if v != nil { + gpk = v.(*dkgTypes.GroupPublicKey) + if gpk.Round == round.Uint64() { + return gpk, nil + } + } // Prepare DKGMasterPublicKeys. mpks := state.DKGMasterPublicKeyItems() comps := state.DKGComplaintItems() - return dkgTypes.NewGroupPublicKey(round.Uint64(), mpks, comps, threshold) + gpk, err = dkgTypes.NewGroupPublicKey(round.Uint64(), mpks, comps, threshold) + if err != nil { + return nil, err + } + c.gpk.Store(gpk) + return gpk, nil } func (g *GovernanceContract) Address() common.Address { diff --git a/dex/governance.go b/dex/governance.go index 0614ae910..b8dcf1589 100644 --- a/dex/governance.go +++ b/dex/governance.go @@ -111,13 +111,13 @@ func (d *DexconGovernance) Round() uint64 { func (d *DexconGovernance) ProposeCRS(round uint64, signedCRS []byte) { data, err := vm.PackProposeCRS(round, signedCRS) if err != nil { - log.Error("failed to pack proposeCRS input", "err", err) + log.Error("Failed to pack proposeCRS input", "err", err) return } err = d.sendGovTx(context.Background(), data) if err != nil { - log.Error("failed to send proposeCRS tx", "err", err) + log.Error("Failed to send proposeCRS tx", "err", err) } } @@ -125,13 +125,13 @@ func (d *DexconGovernance) ProposeCRS(round uint64, signedCRS []byte) { func (d *DexconGovernance) AddDKGComplaint(complaint *dkgTypes.Complaint) { data, err := vm.PackAddDKGComplaint(complaint) if err != nil { - log.Error("failed to pack addDKGComplaint input", "err", err) + log.Error("Failed to pack addDKGComplaint input", "err", err) return } err = d.sendGovTx(context.Background(), data) if err != nil { - log.Error("failed to send addDKGComplaint tx", "err", err) + log.Error("Failed to send addDKGComplaint tx", "err", err) } } @@ -139,13 +139,13 @@ func (d *DexconGovernance) AddDKGComplaint(complaint *dkgTypes.Complaint) { func (d *DexconGovernance) AddDKGMasterPublicKey(masterPublicKey *dkgTypes.MasterPublicKey) { data, err := vm.PackAddDKGMasterPublicKey(masterPublicKey) if err != nil { - log.Error("failed to pack addDKGMasterPublicKey input", "err", err) + log.Error("Failed to pack addDKGMasterPublicKey input", "err", err) return } err = d.sendGovTx(context.Background(), data) if err != nil { - log.Error("failed to send addDKGMasterPublicKey tx", "err", err) + log.Error("Failed to send addDKGMasterPublicKey tx", "err", err) } } @@ -153,13 +153,13 @@ func (d *DexconGovernance) AddDKGMasterPublicKey(masterPublicKey *dkgTypes.Maste func (d *DexconGovernance) AddDKGMPKReady(ready *dkgTypes.MPKReady) { data, err := vm.PackAddDKGMPKReady(ready) if err != nil { - log.Error("failed to pack addDKGMPKReady input", "err", err) + log.Error("Failed to pack addDKGMPKReady input", "err", err) return } err = d.sendGovTx(context.Background(), data) if err != nil { - log.Error("failed to send addDKGMPKReady tx", "err", err) + log.Error("Failed to send addDKGMPKReady tx", "err", err) } } @@ -167,13 +167,13 @@ func (d *DexconGovernance) AddDKGMPKReady(ready *dkgTypes.MPKReady) { func (d *DexconGovernance) AddDKGFinalize(final *dkgTypes.Finalize) { data, err := vm.PackAddDKGFinalize(final) if err != nil { - log.Error("failed to pack addDKGFinalize input", "err", err) + log.Error("Failed to pack addDKGFinalize input", "err", err) return } err = d.sendGovTx(context.Background(), data) if err != nil { - log.Error("failed to send addDKGFinalize tx", "err", err) + log.Error("Failed to send addDKGFinalize tx", "err", err) } } @@ -181,13 +181,13 @@ func (d *DexconGovernance) AddDKGFinalize(final *dkgTypes.Finalize) { func (d *DexconGovernance) ReportForkVote(vote1, vote2 *coreTypes.Vote) { data, err := vm.PackReportForkVote(vote1, vote2) if err != nil { - log.Error("failed to pack report fork vote input", "err", err) + log.Error("Failed to pack report fork vote input", "err", err) return } err = d.sendGovTx(context.Background(), data) if err != nil { - log.Error("failed to send report fork vote tx", "err", err) + log.Error("Failed to send report fork vote tx", "err", err) } } @@ -195,25 +195,25 @@ func (d *DexconGovernance) ReportForkVote(vote1, vote2 *coreTypes.Vote) { func (d *DexconGovernance) ReportForkBlock(block1, block2 *coreTypes.Block) { data, err := vm.PackReportForkBlock(block1, block2) if err != nil { - log.Error("failed to pack report fork block input", "err", err) + log.Error("Failed to pack report fork block input", "err", err) return } err = d.sendGovTx(context.Background(), data) if err != nil { - log.Error("failed to send report fork block tx", "err", err) + log.Error("Failed to send report fork block tx", "err", err) } } func (d *DexconGovernance) ResetDKG(newSignedCRS []byte) { data, err := vm.PackResetDKG(newSignedCRS) if err != nil { - log.Error("failed to pack resetDKG input", "err", err) + log.Error("Failed to pack resetDKG input", "err", err) return } err = d.sendGovTx(context.Background(), data) if err != nil { - log.Error("failed to send resetDKG tx", "err", err) + log.Error("Failed to send resetDKG tx", "err", err) } } |