diff options
author | Sonic <sonic@dexon.org> | 2019-01-24 10:38:28 +0800 |
---|---|---|
committer | Wei-Ning Huang <w@byzantine-lab.io> | 2019-06-12 17:27:21 +0800 |
commit | 51d06935552f8475f95217a268666a8aa24d8a5d (patch) | |
tree | 443413591e4ecd8446fc677800f0a37609162d95 /dex/governance.go | |
parent | b9defd2cc002d3b5bb3284f9bf11bb08a8e50939 (diff) | |
download | go-tangerine-51d06935552f8475f95217a268666a8aa24d8a5d.tar go-tangerine-51d06935552f8475f95217a268666a8aa24d8a5d.tar.gz go-tangerine-51d06935552f8475f95217a268666a8aa24d8a5d.tar.bz2 go-tangerine-51d06935552f8475f95217a268666a8aa24d8a5d.tar.lz go-tangerine-51d06935552f8475f95217a268666a8aa24d8a5d.tar.xz go-tangerine-51d06935552f8475f95217a268666a8aa24d8a5d.tar.zst go-tangerine-51d06935552f8475f95217a268666a8aa24d8a5d.zip |
core, dex/downloader: polish headers verification and blocks insertion logic (#168)
Refactor GenerateDexonChain function, move governance tx logic to
the user of GenerateDexonChain (testchain_test.go) and move fake node set
code to FakeDexcon.
Diffstat (limited to 'dex/governance.go')
-rw-r--r-- | dex/governance.go | 94 |
1 files changed, 10 insertions, 84 deletions
diff --git a/dex/governance.go b/dex/governance.go index 0d5a7c926..e9ea1f89c 100644 --- a/dex/governance.go +++ b/dex/governance.go @@ -37,7 +37,6 @@ import ( "github.com/dexon-foundation/dexon/crypto" "github.com/dexon-foundation/dexon/log" "github.com/dexon-foundation/dexon/params" - "github.com/dexon-foundation/dexon/rlp" ) type DexconGovernance struct { @@ -119,15 +118,12 @@ func (d *DexconGovernance) LenCRS() uint64 { // ProposeCRS send proposals of a new CRS func (d *DexconGovernance) ProposeCRS(round uint64, signedCRS []byte) { - method := vm.GovernanceContractName2Method["proposeCRS"] - - res, err := method.Inputs.Pack(big.NewInt(int64(round)), signedCRS) + data, err := vm.PackProposeCRS(round, signedCRS) if err != nil { log.Error("failed to pack proposeCRS input", "err", err) return } - data := append(method.Id(), res...) err = d.sendGovTx(context.Background(), data) if err != nil { log.Error("failed to send proposeCRS tx", "err", err) @@ -151,16 +147,12 @@ func (d *DexconGovernance) NodeSet(round uint64) []coreCrypto.PublicKey { // NotifyRoundHeight register the mapping between round and height. func (d *DexconGovernance) NotifyRoundHeight(targetRound, consensusHeight uint64) { - method := vm.GovernanceContractName2Method["snapshotRound"] - - res, err := method.Inputs.Pack( - big.NewInt(int64(targetRound)), big.NewInt(int64(consensusHeight))) + data, err := vm.PackNotifyRoundHeight(targetRound, consensusHeight) if err != nil { log.Error("failed to pack snapshotRound input", "err", err) return } - data := append(method.Id(), res...) err = d.sendGovTx(context.Background(), data) if err != nil { log.Error("failed to send snapshotRound tx", "err", err) @@ -169,21 +161,12 @@ func (d *DexconGovernance) NotifyRoundHeight(targetRound, consensusHeight uint64 // AddDKGComplaint adds a DKGComplaint. func (d *DexconGovernance) AddDKGComplaint(round uint64, complaint *dkgTypes.Complaint) { - method := vm.GovernanceContractName2Method["addDKGComplaint"] - - encoded, err := rlp.EncodeToBytes(complaint) - if err != nil { - log.Error("failed to RLP encode complaint to bytes", "err", err) - return - } - - res, err := method.Inputs.Pack(big.NewInt(int64(round)), encoded) + data, err := vm.PackAddDKGComplaint(round, complaint) if err != nil { log.Error("failed to pack addDKGComplaint input", "err", err) return } - data := append(method.Id(), res...) err = d.sendGovTx(context.Background(), data) if err != nil { log.Error("failed to send addDKGComplaint tx", "err", err) @@ -192,21 +175,12 @@ func (d *DexconGovernance) AddDKGComplaint(round uint64, complaint *dkgTypes.Com // AddDKGMasterPublicKey adds a DKGMasterPublicKey. func (d *DexconGovernance) AddDKGMasterPublicKey(round uint64, masterPublicKey *dkgTypes.MasterPublicKey) { - method := vm.GovernanceContractName2Method["addDKGMasterPublicKey"] - - encoded, err := rlp.EncodeToBytes(masterPublicKey) - if err != nil { - log.Error("failed to RLP encode mpk to bytes", "err", err) - return - } - - res, err := method.Inputs.Pack(big.NewInt(int64(round)), encoded) + data, err := vm.PackAddDKGMasterPublicKey(round, masterPublicKey) if err != nil { log.Error("failed to pack addDKGMasterPublicKey input", "err", err) return } - data := append(method.Id(), res...) err = d.sendGovTx(context.Background(), data) if err != nil { log.Error("failed to send addDKGMasterPublicKey tx", "err", err) @@ -215,21 +189,12 @@ func (d *DexconGovernance) AddDKGMasterPublicKey(round uint64, masterPublicKey * // AddDKGMPKReady adds a DKG mpk ready message. func (d *DexconGovernance) AddDKGMPKReady(round uint64, ready *dkgTypes.MPKReady) { - method := vm.GovernanceContractName2Method["addDKGMPKReady"] - - encoded, err := rlp.EncodeToBytes(ready) - if err != nil { - log.Error("failed to RLP encode mpk ready to bytes", "err", err) - return - } - - res, err := method.Inputs.Pack(big.NewInt(int64(round)), encoded) + data, err := vm.PackAddDKGMPKReady(round, ready) if err != nil { log.Error("failed to pack addDKGMPKReady input", "err", err) return } - data := append(method.Id(), res...) err = d.sendGovTx(context.Background(), data) if err != nil { log.Error("failed to send addDKGMPKReady tx", "err", err) @@ -238,21 +203,12 @@ func (d *DexconGovernance) AddDKGMPKReady(round uint64, ready *dkgTypes.MPKReady // AddDKGFinalize adds a DKG finalize message. func (d *DexconGovernance) AddDKGFinalize(round uint64, final *dkgTypes.Finalize) { - method := vm.GovernanceContractName2Method["addDKGFinalize"] - - encoded, err := rlp.EncodeToBytes(final) - if err != nil { - log.Error("failed to RLP encode finalize to bytes", "err", err) - return - } - - res, err := method.Inputs.Pack(big.NewInt(int64(round)), encoded) + data, err := vm.PackAddDKGFinalize(round, final) if err != nil { log.Error("failed to pack addDKGFinalize input", "err", err) return } - data := append(method.Id(), res...) err = d.sendGovTx(context.Background(), data) if err != nil { log.Error("failed to send addDKGFinalize tx", "err", err) @@ -261,27 +217,12 @@ func (d *DexconGovernance) AddDKGFinalize(round uint64, final *dkgTypes.Finalize // ReportForkVote reports a node for forking votes. func (d *DexconGovernance) ReportForkVote(vote1, vote2 *coreTypes.Vote) { - method := vm.GovernanceContractName2Method["report"] - - vote1Bytes, err := rlp.EncodeToBytes(vote1) - if err != nil { - log.Error("failed to RLP encode vote1 to bytes", "err", err) - return - } - - vote2Bytes, err := rlp.EncodeToBytes(vote2) - if err != nil { - log.Error("failed to RLP encode vote2 to bytes", "err", err) - return - } - - res, err := method.Inputs.Pack(big.NewInt(vm.ReportTypeForkVote), vote1Bytes, vote2Bytes) + data, err := vm.PackReportForkVote(vote1, vote2) if err != nil { - log.Error("failed to pack report input", "err", err) + log.Error("failed to pack report fork vote input", "err", err) return } - data := append(method.Id(), res...) err = d.sendGovTx(context.Background(), data) if err != nil { log.Error("failed to send report fork vote tx", "err", err) @@ -290,27 +231,12 @@ func (d *DexconGovernance) ReportForkVote(vote1, vote2 *coreTypes.Vote) { // ReportForkBlock reports a node for forking blocks. func (d *DexconGovernance) ReportForkBlock(block1, block2 *coreTypes.Block) { - method := vm.GovernanceContractName2Method["report"] - - block1Bytes, err := rlp.EncodeToBytes(block1) - if err != nil { - log.Error("failed to RLP encode block1 to bytes", "err", err) - return - } - - block2Bytes, err := rlp.EncodeToBytes(block2) - if err != nil { - log.Error("failed to RLP encode block2 to bytes", "err", err) - return - } - - res, err := method.Inputs.Pack(big.NewInt(vm.ReportTypeForkBlock), block1Bytes, block2Bytes) + data, err := vm.PackReportForkBlock(block1, block2) if err != nil { - log.Error("failed to pack report input", "err", err) + log.Error("failed to pack report fork block input", "err", err) return } - data := append(method.Id(), res...) err = d.sendGovTx(context.Background(), data) if err != nil { log.Error("failed to send report fork block tx", "err", err) |