diff options
author | Wei-Ning Huang <w@dexon.org> | 2019-02-25 19:35:06 +0800 |
---|---|---|
committer | Wei-Ning Huang <w@dexon.org> | 2019-03-12 12:19:09 +0800 |
commit | 4ce8adb190ae3351a507be54f38afa6b47ce18a3 (patch) | |
tree | 585b6f4bb5ee06b0fdab0f8e68f34e15b6ae0fed /dex | |
parent | 0ac79d780ba63c574d648552f887c9411fdd76fe (diff) | |
download | dexon-4ce8adb190ae3351a507be54f38afa6b47ce18a3.tar.gz dexon-4ce8adb190ae3351a507be54f38afa6b47ce18a3.tar.zst dexon-4ce8adb190ae3351a507be54f38afa6b47ce18a3.zip |
core: vm: flatten governance
Diffstat (limited to 'dex')
-rw-r--r-- | dex/downloader/downloader.go | 11 | ||||
-rw-r--r-- | dex/downloader/testchain_test.go | 76 | ||||
-rw-r--r-- | dex/governance.go | 33 | ||||
-rw-r--r-- | dex/handler.go | 27 | ||||
-rw-r--r-- | dex/helper_test.go | 6 | ||||
-rw-r--r-- | dex/protocol.go | 4 |
6 files changed, 79 insertions, 78 deletions
diff --git a/dex/downloader/downloader.go b/dex/downloader/downloader.go index e15844668..3d3a78e18 100644 --- a/dex/downloader/downloader.go +++ b/dex/downloader/downloader.go @@ -496,17 +496,6 @@ func (d *Downloader) syncWithPeer(p *peerConnection, hash common.Hash, number ui } d.verifierCache = dexCore.NewTSigVerifierCache(d.gov, 5) - - // warm up verifierCache - if originHeader.Round > 0 { - ok, err := d.verifierCache.Update(originHeader.Round - 1) - if err != nil { - return err - } - if !ok { - return fmt.Errorf("can not update verifier cache") - } - } } // Initiate the sync using a concurrent header and content retrieval algorithm diff --git a/dex/downloader/testchain_test.go b/dex/downloader/testchain_test.go index 73d4863a5..722159bc0 100644 --- a/dex/downloader/testchain_test.go +++ b/dex/downloader/testchain_test.go @@ -21,6 +21,9 @@ import ( "fmt" "math/big" + dexCore "github.com/dexon-foundation/dexon-consensus/core" + coreTypes "github.com/dexon-foundation/dexon-consensus/core/types" + "github.com/dexon-foundation/dexon/common" "github.com/dexon-foundation/dexon/consensus/dexcon" "github.com/dexon-foundation/dexon/core" @@ -66,23 +69,23 @@ func genesisBlockForTesting(db ethdb.Database, Staked: new(big.Int), }, nodeaddr1: { - Balance: new(big.Int).Mul(big.NewInt(1000), ether), - Staked: new(big.Int).Mul(big.NewInt(500), ether), + Balance: new(big.Int).Mul(big.NewInt(2e6), ether), + Staked: new(big.Int).Mul(big.NewInt(1e6), ether), PublicKey: crypto.FromECDSAPub(&nodekey1.PublicKey), }, nodeaddr2: { - Balance: new(big.Int).Mul(big.NewInt(1000), ether), - Staked: new(big.Int).Mul(big.NewInt(500), ether), + Balance: new(big.Int).Mul(big.NewInt(2e6), ether), + Staked: new(big.Int).Mul(big.NewInt(1e6), ether), PublicKey: crypto.FromECDSAPub(&nodekey2.PublicKey), }, nodeaddr3: { - Balance: new(big.Int).Mul(big.NewInt(1000), ether), - Staked: new(big.Int).Mul(big.NewInt(500), ether), + Balance: new(big.Int).Mul(big.NewInt(2e6), ether), + Staked: new(big.Int).Mul(big.NewInt(1e6), ether), PublicKey: crypto.FromECDSAPub(&nodekey3.PublicKey), }, nodeaddr4: { - Balance: new(big.Int).Mul(big.NewInt(1000), ether), - Staked: new(big.Int).Mul(big.NewInt(500), ether), + Balance: new(big.Int).Mul(big.NewInt(2e6), ether), + Staked: new(big.Int).Mul(big.NewInt(1e6), ether), PublicKey: crypto.FromECDSAPub(&nodekey4.PublicKey), }, }, @@ -166,50 +169,23 @@ func (tc *testChain) generate(n int, seed byte, parent *types.Block, nodes *dexc blocks, receipts := core.GenerateDexonChain(params.TestnetChainConfig, parent, engine, testDB, n, func(i int, block *core.DexonBlockGen) { block.SetCoinbase(common.Address{seed}) - if round == 0 { - switch i { - case 1: - testNodes.RunDKG(round, 2) - // Add DKG MasterPublicKeys - for _, node := range testNodes.Nodes(round) { - data, err := vm.PackAddDKGMasterPublicKey(round, node.MasterPublicKey(round)) - if err != nil { - panic(err) - } - addTx(block, node, data) - } - case 2: - // Add DKG MPKReady - for _, node := range testNodes.Nodes(round) { - data, err := vm.PackAddDKGMPKReady(round, node.DKGMPKReady(round)) - if err != nil { - panic(err) - } - addTx(block, node, data) - } - case 3: - // Add DKG Finalize - for _, node := range testNodes.Nodes(round) { - data, err := vm.PackAddDKGFinalize(round, node.DKGFinalize(round)) - if err != nil { - panic(err) - } - addTx(block, node, data) - } - } - } - + block.SetPosition(coreTypes.Position{ + Round: round, + Height: uint64(i), + }) half := roundInterval / 2 switch i % roundInterval { case half: - // Sign current CRS to geneate the next round CRS and propose it. - testNodes.SignCRS(round) - node := testNodes.Nodes(round)[0] - data, err := vm.PackProposeCRS(round, testNodes.SignedCRS(round+1)) - if err != nil { - panic(err) + if round >= dexCore.DKGDelayRound { + // Sign current CRS to geneate the next round CRS and propose it. + testNodes.SignCRS(round) + node := testNodes.Nodes(round)[0] + data, err := vm.PackProposeCRS(round, testNodes.SignedCRS(round+1)) + if err != nil { + panic(err) + } + addTx(block, node, data) } - addTx(block, node, data) case half + 1: // Run the DKG for next round. testNodes.RunDKG(round+1, 2) @@ -356,13 +332,13 @@ func (g *govStateFetcher) SnapshotRound(round uint64, root common.Hash) { g.rootByRound[round] = root } -func (g *govStateFetcher) GetGovStateHelperAtRound(round uint64) *vm.GovernanceStateHelper { +func (g *govStateFetcher) GetStateForConfigAtRound(round uint64) *vm.GovernanceState { if root, ok := g.rootByRound[round]; ok { s, err := state.New(root, g.db) if err != nil { panic(err) } - return &vm.GovernanceStateHelper{s} + return &vm.GovernanceState{s} } return nil } diff --git a/dex/governance.go b/dex/governance.go index 56d08975e..d9cf8fb65 100644 --- a/dex/governance.go +++ b/dex/governance.go @@ -67,7 +67,7 @@ func NewDexconGovernance(backend *DexAPIBackend, chainConfig *params.ChainConfig // DexconConfiguration return raw config in state. func (d *DexconGovernance) DexconConfiguration(round uint64) *params.DexconConfig { - return d.GetGovStateHelperAtRound(round).Configuration() + return d.GetStateForConfigAtRound(round).Configuration() } func (d *DexconGovernance) sendGovTx(ctx context.Context, data []byte) error { @@ -107,13 +107,32 @@ func (d *DexconGovernance) sendGovTx(ctx context.Context, data []byte) error { // CRS returns the CRS for a given round. func (d *DexconGovernance) CRS(round uint64) coreCommon.Hash { - s := d.GetHeadHelper() - return coreCommon.Hash(s.CRS(big.NewInt(int64(round)))) + if round <= dexCore.DKGDelayRound { + s := d.GetStateAtRound(0) + crs := s.CRS() + for i := uint64(0); i < round; i++ { + crs = crypto.Keccak256Hash(crs[:]) + } + return coreCommon.Hash(crs) + } + if round > d.CRSRound() { + return coreCommon.Hash{} + } + var s *vm.GovernanceState + if round == d.CRSRound() { + s = d.GetHeadState() + } else { + s = d.GetStateAtRound(round) + } + return coreCommon.Hash(s.CRS()) +} + +func (d *DexconGovernance) Round() uint64 { + return d.b.CurrentBlock().Round() } -func (d *DexconGovernance) LenCRS() uint64 { - s := d.GetHeadHelper() - return s.LenCRS().Uint64() +func (d *DexconGovernance) CRSRound() uint64 { + return d.GetHeadState().CRSRound().Uint64() } // ProposeCRS send proposals of a new CRS @@ -132,7 +151,7 @@ func (d *DexconGovernance) ProposeCRS(round uint64, signedCRS []byte) { // NodeSet returns the current node set. func (d *DexconGovernance) NodeSet(round uint64) []coreCrypto.PublicKey { - s := d.GetGovStateHelperAtRound(round) + s := d.GetStateForConfigAtRound(round) var pks []coreCrypto.PublicKey for _, n := range s.QualifiedNodes() { diff --git a/dex/handler.go b/dex/handler.go index 4ffa01244..5bd615c86 100644 --- a/dex/handler.go +++ b/dex/handler.go @@ -45,6 +45,7 @@ import ( "time" coreCommon "github.com/dexon-foundation/dexon-consensus/common" + dexCore "github.com/dexon-foundation/dexon-consensus/core" coreCrypto "github.com/dexon-foundation/dexon-consensus/core/crypto" coreTypes "github.com/dexon-foundation/dexon-consensus/core/types" dkgTypes "github.com/dexon-foundation/dexon-consensus/core/types/dkg" @@ -1199,13 +1200,18 @@ func (pm *ProtocolManager) recordBroadcastLoop() { // a loop keep building and maintaining peers in notary set. // TODO: finish this func (pm *ProtocolManager) peerSetLoop() { - log.Debug("start peer set loop") - round := pm.gov.LenCRS() - 1 - log.Trace("first len crs", "len", round+1, "round", round) - if round >= 1 { - pm.peers.BuildConnection(round - 1) + log.Debug("ProtocolManager: started peer set loop") + + round := pm.gov.Round() + log.Trace("ProtocolManager: startup round", "round", round) + + if round < dexCore.DKGDelayRound { + for i := round; i <= dexCore.DKGDelayRound; i++ { + pm.peers.BuildConnection(i) + } + } else { + pm.peers.BuildConnection(round) } - pm.peers.BuildConnection(round) for { select { @@ -1216,11 +1222,16 @@ func (pm *ProtocolManager) peerSetLoop() { break } - newRound := pm.gov.LenCRS() - 1 - log.Trace("new round", "round", newRound) + newRound := pm.gov.CRSRound() + if newRound == 0 { + break + } + + log.Debug("ProtocolManager: new round", "round", newRound) if newRound == round { break } + if newRound == round+1 { pm.peers.BuildConnection(newRound) if round >= 1 { diff --git a/dex/helper_test.go b/dex/helper_test.go index 3f901e6ec..c8bf62a6b 100644 --- a/dex/helper_test.go +++ b/dex/helper_test.go @@ -215,7 +215,11 @@ type testGovernance struct { dkgSetFunc func(uint64) (map[string]struct{}, error) } -func (g *testGovernance) LenCRS() uint64 { +func (g *testGovernance) Round() uint64 { + return g.lenCRSFunc() +} + +func (g *testGovernance) CRSRound() uint64 { return g.lenCRSFunc() } diff --git a/dex/protocol.go b/dex/protocol.go index 0e3f50eba..d72e95478 100644 --- a/dex/protocol.go +++ b/dex/protocol.go @@ -152,7 +152,9 @@ type txPool interface { type governance interface { GetRoundHeight(uint64) uint64 - LenCRS() uint64 + Round() uint64 + + CRSRound() uint64 NotarySet(uint64) (map[string]struct{}, error) |