aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWei-Ning Huang <w@cobinhood.com>2018-10-18 15:47:10 +0800
committerWei-Ning Huang <w@dexon.org>2019-04-09 21:32:50 +0800
commit34b26693e4058a6231ffd320e209834336b6ba73 (patch)
tree7238e3512ca018d04ad5938c5a9fcb29e01708e6
parent6137f79f726e8ec6a282ee0ca4ed45c05020ed4c (diff)
downloaddexon-34b26693e4058a6231ffd320e209834336b6ba73.tar
dexon-34b26693e4058a6231ffd320e209834336b6ba73.tar.gz
dexon-34b26693e4058a6231ffd320e209834336b6ba73.tar.bz2
dexon-34b26693e4058a6231ffd320e209834336b6ba73.tar.lz
dexon-34b26693e4058a6231ffd320e209834336b6ba73.tar.xz
dexon-34b26693e4058a6231ffd320e209834336b6ba73.tar.zst
dexon-34b26693e4058a6231ffd320e209834336b6ba73.zip
core/dex: fix governance contract implementation
-rw-r--r--core/vm/governance.go32
-rw-r--r--dex/governance.go16
2 files changed, 21 insertions, 27 deletions
diff --git a/core/vm/governance.go b/core/vm/governance.go
index 9b4e2dde6..d9c9a8142 100644
--- a/core/vm/governance.go
+++ b/core/vm/governance.go
@@ -18,6 +18,7 @@
package vm
import (
+ "encoding/json"
"math/big"
"strings"
@@ -26,7 +27,6 @@ import (
"github.com/dexon-foundation/dexon/core/types"
"github.com/dexon-foundation/dexon/crypto"
"github.com/dexon-foundation/dexon/params"
- "github.com/dexon-foundation/dexon/rlp"
coreCommon "github.com/dexon-foundation/dexon-consensus-core/common"
"github.com/dexon-foundation/dexon-consensus-core/core"
@@ -1127,6 +1127,9 @@ func (s *GovernanceStateHelper) PushCRS(crs common.Hash) {
s.setState(common.BigToHash(loc), crs)
}
+func (s *GovernanceStateHelper) Round() *big.Int {
+ return new(big.Int).Sub(s.getStateBigInt(big.NewInt(crsLoc)), big.NewInt(1))
+}
// bytes[][] public dkgMasterPublicKeys;
func (s *GovernanceStateHelper) DKGMasterPublicKeys(round *big.Int) [][]byte {
@@ -1323,8 +1326,7 @@ func (g *GovernanceContract) inDKGSet(nodeID coreTypes.NodeID) bool {
}
func (g *GovernanceContract) addDKGComplaint(round *big.Int, comp []byte) ([]byte, error) {
- nextRound := g.state.LenCRS()
- if round.Cmp(nextRound) != 0 {
+ if round.Cmp(g.state.Round()) != 0 {
g.penalize()
return nil, errExecutionReverted
}
@@ -1348,7 +1350,7 @@ func (g *GovernanceContract) addDKGComplaint(round *big.Int, comp []byte) ([]byt
}
var dkgComplaint coreTypes.DKGComplaint
- if err := rlp.DecodeBytes(comp, &dkgComplaint); err != nil {
+ if err := json.Unmarshal(comp, &dkgComplaint); err != nil {
g.penalize()
return nil, errExecutionReverted
}
@@ -1373,8 +1375,7 @@ func (g *GovernanceContract) addDKGComplaint(round *big.Int, comp []byte) ([]byt
}
func (g *GovernanceContract) addDKGMasterPublicKey(round *big.Int, mpk []byte) ([]byte, error) {
- nextRound := g.state.LenCRS()
- if round.Cmp(nextRound) != 0 {
+ if round.Cmp(g.state.Round()) != 0 {
g.penalize()
return nil, errExecutionReverted
}
@@ -1388,7 +1389,7 @@ func (g *GovernanceContract) addDKGMasterPublicKey(round *big.Int, mpk []byte) (
}
var dkgMasterPK coreTypes.DKGMasterPublicKey
- if err := rlp.DecodeBytes(mpk, &dkgMasterPK); err != nil {
+ if err := json.Unmarshal(mpk, &dkgMasterPK); err != nil {
g.penalize()
return nil, errExecutionReverted
}
@@ -1413,8 +1414,7 @@ func (g *GovernanceContract) addDKGMasterPublicKey(round *big.Int, mpk []byte) (
}
func (g *GovernanceContract) addDKGFinalize(round *big.Int, finalize []byte) ([]byte, error) {
- nextRound := g.state.LenCRS()
- if round.Cmp(nextRound) != 0 {
+ if round.Cmp(g.state.Round()) != 0 {
g.penalize()
return nil, errExecutionReverted
}
@@ -1422,7 +1422,7 @@ func (g *GovernanceContract) addDKGFinalize(round *big.Int, finalize []byte) ([]
caller := g.contract.Caller()
var dkgFinalize coreTypes.DKGFinalize
- if err := rlp.DecodeBytes(finalize, &dkgFinalize); err != nil {
+ if err := json.Unmarshal(finalize, &dkgFinalize); err != nil {
g.penalize()
return nil, errExecutionReverted
}
@@ -1510,21 +1510,15 @@ func (g *GovernanceContract) unstake() ([]byte, error) {
}
func (g *GovernanceContract) proposeCRS(signedCRS []byte) ([]byte, error) {
- round := g.state.LenCRS()
+ round := g.state.Round()
prevCRS := g.state.CRS(round)
- // round should be the next round number, abort otherwise.
- if new(big.Int).Add(round, big.NewInt(1)).Cmp(round) != 0 {
- g.penalize()
- return nil, errExecutionReverted
- }
-
// Prepare DKGMasterPublicKeys.
// TODO(w): make sure DKGMasterPKs are unique.
var dkgMasterPKs []*coreTypes.DKGMasterPublicKey
for _, mpk := range g.state.DKGMasterPublicKeys(round) {
x := new(coreTypes.DKGMasterPublicKey)
- if err := rlp.DecodeBytes(mpk, x); err != nil {
+ if err := json.Unmarshal(mpk, x); err != nil {
panic(err)
}
dkgMasterPKs = append(dkgMasterPKs, x)
@@ -1534,7 +1528,7 @@ func (g *GovernanceContract) proposeCRS(signedCRS []byte) ([]byte, error) {
var dkgComplaints []*coreTypes.DKGComplaint
for _, comp := range g.state.DKGComplaints(round) {
x := new(coreTypes.DKGComplaint)
- if err := rlp.DecodeBytes(comp, x); err != nil {
+ if err := json.Unmarshal(comp, x); err != nil {
panic(err)
}
dkgComplaints = append(dkgComplaints, x)
diff --git a/dex/governance.go b/dex/governance.go
index 19b9e578c..9235ffce1 100644
--- a/dex/governance.go
+++ b/dex/governance.go
@@ -3,6 +3,7 @@ package dex
import (
"context"
"crypto/ecdsa"
+ "encoding/json"
"math/big"
"time"
@@ -75,7 +76,6 @@ func (d *DexconGovernance) getGovStateAtRound(round uint64) *vm.GovernanceStateH
if state == nil || err != nil {
return nil
}
-
return &vm.GovernanceStateHelper{state}
}
@@ -191,9 +191,9 @@ func (d *DexconGovernance) NotifyRoundHeight(targetRound, consensusHeight uint64
func (d *DexconGovernance) AddDKGComplaint(round uint64, complaint *coreTypes.DKGComplaint) {
method := vm.GovernanceContractName2Method["addDKGComplaint"]
- encoded, err := rlp.EncodeToBytes(complaint)
+ encoded, err := json.Marshal(complaint)
if err != nil {
- log.Error("failed to RLP encode complaint to bytes", "err", err)
+ log.Error("failed to JSON encode complaint to bytes", "err", err)
return
}
@@ -214,7 +214,7 @@ func (d *DexconGovernance) AddDKGComplaint(round uint64, complaint *coreTypes.DK
func (d *DexconGovernance) DKGComplaints(round uint64) []*coreTypes.DKGComplaint {
s := d.getGovState()
var dkgComplaints []*coreTypes.DKGComplaint
- for _, pk := range s.DKGMasterPublicKeys(big.NewInt(int64(round))) {
+ for _, pk := range s.DKGComplaints(big.NewInt(int64(round))) {
x := new(coreTypes.DKGComplaint)
if err := rlp.DecodeBytes(pk, x); err != nil {
panic(err)
@@ -228,9 +228,9 @@ func (d *DexconGovernance) DKGComplaints(round uint64) []*coreTypes.DKGComplaint
func (d *DexconGovernance) AddDKGMasterPublicKey(round uint64, masterPublicKey *coreTypes.DKGMasterPublicKey) {
method := vm.GovernanceContractName2Method["addDKGMasterPublicKey"]
- encoded, err := rlp.EncodeToBytes(masterPublicKey)
+ encoded, err := json.Marshal(masterPublicKey)
if err != nil {
- log.Error("failed to RLP encode mpk to bytes", "err", err)
+ log.Error("failed to JSON encode mpk to bytes", "err", err)
return
}
@@ -265,9 +265,9 @@ func (d *DexconGovernance) DKGMasterPublicKeys(round uint64) []*coreTypes.DKGMas
func (d *DexconGovernance) AddDKGFinalize(round uint64, final *coreTypes.DKGFinalize) {
method := vm.GovernanceContractName2Method["addDKGFinalize"]
- encoded, err := rlp.EncodeToBytes(final)
+ encoded, err := json.Marshal(final)
if err != nil {
- log.Error("failed to RLP encode finalize to bytes", "err", err)
+ log.Error("failed to JSON encode finalize to bytes", "err", err)
return
}