aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWei-Ning Huang <w@dexon.org>2019-04-04 10:33:55 +0800
committerWei-Ning Huang <w@byzantine-lab.io>2019-06-15 22:09:55 +0800
commit8bb384eb28d4aceba63b8077f664381fb3c06444 (patch)
treee0608b62d06234df27786c7e2de78c6758463a6a
parent83967112258e35425dae4aa943404ee76a36f591 (diff)
downloadgo-tangerine-8bb384eb28d4aceba63b8077f664381fb3c06444.tar.gz
go-tangerine-8bb384eb28d4aceba63b8077f664381fb3c06444.tar.zst
go-tangerine-8bb384eb28d4aceba63b8077f664381fb3c06444.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.go19
-rw-r--r--dex/governance.go32
2 files changed, 34 insertions, 17 deletions
diff --git a/core/vm/oracle_contracts.go b/core/vm/oracle_contracts.go
index 10459a870..c63c750a3 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"
@@ -1248,16 +1249,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)
}
}