From 8e4340ed4be0cb8d5d2fe9f14d5f2b559cf3360c Mon Sep 17 00:00:00 2001 From: Wei-Ning Huang Date: Fri, 12 Oct 2018 17:55:45 +0800 Subject: dex: implement some governance interface functions --- dex/api_backend.go | 2 +- dex/backend.go | 2 +- dex/config.go | 2 +- dex/governance.go | 114 ++++++++++++++++++++++++++++++++++++++++++++--------- 4 files changed, 98 insertions(+), 22 deletions(-) (limited to 'dex') diff --git a/dex/api_backend.go b/dex/api_backend.go index 791d3ab8f..3c9be5f3d 100644 --- a/dex/api_backend.go +++ b/dex/api_backend.go @@ -29,8 +29,8 @@ import ( "github.com/dexon-foundation/dexon/core/state" "github.com/dexon-foundation/dexon/core/types" "github.com/dexon-foundation/dexon/core/vm" + "github.com/dexon-foundation/dexon/dex/gasprice" "github.com/dexon-foundation/dexon/eth/downloader" - "github.com/dexon-foundation/dexon/eth/gasprice" "github.com/dexon-foundation/dexon/ethdb" "github.com/dexon-foundation/dexon/event" diff --git a/dex/backend.go b/dex/backend.go index 8213f582e..b6a7bdc4d 100644 --- a/dex/backend.go +++ b/dex/backend.go @@ -147,7 +147,7 @@ func New(ctx *node.ServiceContext, config *Config) (*Dexon, error) { } dex.txPool = core.NewTxPool(config.TxPool, dex.chainConfig, dex.blockchain) - dex.APIBackend = &DexAPIBackend{dexon, nil} + dex.APIBackend = &DexAPIBackend{dex, nil} gpoParams := config.GPO //if gpoParams.Default == nil { // gpoParams.Default = config.MinerGasPrice diff --git a/dex/config.go b/dex/config.go index 75afb4b7f..df5babc73 100644 --- a/dex/config.go +++ b/dex/config.go @@ -26,8 +26,8 @@ import ( "github.com/dexon-foundation/dexon/consensus/dexcon" "github.com/dexon-foundation/dexon/core" + "github.com/dexon-foundation/dexon/dex/gasprice" "github.com/dexon-foundation/dexon/eth/downloader" - "github.com/dexon-foundation/dexon/eth/gasprice" ) // DefaultConfig contains default settings for use on the Ethereum main net. diff --git a/dex/governance.go b/dex/governance.go index f49355816..d2cfe05c6 100644 --- a/dex/governance.go +++ b/dex/governance.go @@ -1,10 +1,17 @@ package dex import ( + "context" + "math/big" + "time" + coreCommon "github.com/dexon-foundation/dexon-consensus-core/common" - "github.com/dexon-foundation/dexon-consensus-core/core/crypto" - "github.com/dexon-foundation/dexon-consensus-core/core/types" + coreCrypto "github.com/dexon-foundation/dexon-consensus-core/core/crypto" + "github.com/dexon-foundation/dexon-consensus-core/core/crypto/ecdsa" + coreTypes "github.com/dexon-foundation/dexon-consensus-core/core/types" "github.com/dexon-foundation/dexon/core/vm" + "github.com/dexon-foundation/dexon/rlp" + "github.com/dexon-foundation/dexon/rpc" ) type DexconGovernance struct { @@ -19,21 +26,63 @@ func NewDexconGovernance(backend *DexAPIBackend) *DexconGovernance { } } -// Configuration return the total ordering K constant. -func (d *DexconGovernance) Configuration(round uint64) *types.Config { - state, _, err := d.b.StateAndHeaderByNumber(ctx, blockNr) +func (d *DexconGovernance) getRoundHeight(ctx context.Context, round uint64) (uint64, error) { + state, _, err := d.b.StateAndHeaderByNumber(ctx, rpc.LatestBlockNumber) if state == nil || err != nil { - return nil, err + return 0, err } - s := vm.GovernanceStateHelper{state} + return s.RoundHeight(big.NewInt(int64(round))).Uint64(), nil +} + +func (d *DexconGovernance) getGovState() *vm.GovernanceStateHelper { + ctx := context.Background() + state, _, err := d.b.StateAndHeaderByNumber(ctx, rpc.LatestBlockNumber) + if state == nil || err != nil { + return nil + } - return &types.Config{} + return &vm.GovernanceStateHelper{state} +} + +func (d *DexconGovernance) getGovStateAtRound(round uint64) *vm.GovernanceStateHelper { + ctx := context.Background() + blockHeight, err := d.getRoundHeight(ctx, round) + if err != nil { + return nil + } + + state, _, err := d.b.StateAndHeaderByNumber(ctx, rpc.BlockNumber(blockHeight)) + if state == nil || err != nil { + return nil + } + + return &vm.GovernanceStateHelper{state} +} + +// Configuration return the total ordering K constant. +func (d *DexconGovernance) Configuration(round uint64) *coreTypes.Config { + s := d.getGovStateAtRound(round) + c := s.Configuration() + + return &coreTypes.Config{ + NumChains: c.NumChains, + LambdaBA: time.Duration(c.LambdaBA) * time.Millisecond, + LambdaDKG: time.Duration(c.LambdaDKG) * time.Millisecond, + K: c.K, + PhiRatio: c.PhiRatio, + NotarySetSize: c.NotarySetSize, + DKGSetSize: c.DKGSetSize, + RoundInterval: time.Duration(c.RoundInterval) * time.Millisecond, + MinBlockInterval: time.Duration(c.MinBlockInterval) * time.Millisecond, + MaxBlockInterval: time.Duration(c.MaxBlockInterval) * time.Millisecond, + } } // CRS returns the CRS for a given round. func (d *DexconGovernance) CRS(round uint64) coreCommon.Hash { - return coreCommon.Hash{} + s := d.getGovStateAtRound(round) + return coreCommon.Hash(s.CRS(big.NewInt(int64(round)))) } // ProposeCRS send proposals of a new CRS @@ -41,33 +90,60 @@ func (d *DexconGovernance) ProposeCRS(signedCRS []byte) { } // NodeSet returns the current notary set. -func (d *DexconGovernance) NodeSet(round uint64) []crypto.PublicKey { - return nil +func (d *DexconGovernance) NodeSet(round uint64) []coreCrypto.PublicKey { + s := d.getGovStateAtRound(round) + var pks []coreCrypto.PublicKey + + for _, n := range s.Nodes() { + pks = append(pks, ecdsa.NewPublicKeyFromByteSlice(n.PublicKey)) + } + return pks } // AddDKGComplaint adds a DKGComplaint. -func (d *DexconGovernance) AddDKGComplaint(round uint64, complaint *types.DKGComplaint) { +func (d *DexconGovernance) AddDKGComplaint(round uint64, complaint *coreTypes.DKGComplaint) { } // DKGComplaints gets all the DKGComplaints of round. -func (d *DexconGovernance) DKGComplaints(round uint64) []*types.DKGComplaint { - return nil +func (d *DexconGovernance) DKGComplaints(round uint64) []*coreTypes.DKGComplaint { + s := d.getGovState() + var dkgComplaints []*coreTypes.DKGComplaint + for _, pk := range s.DKGMasterPublicKeys(big.NewInt(int64(round))) { + x := new(coreTypes.DKGComplaint) + if err := rlp.DecodeBytes(pk, x); err != nil { + panic(err) + } + dkgComplaints = append(dkgComplaints, x) + } + return dkgComplaints } // AddDKGMasterPublicKey adds a DKGMasterPublicKey. -func (d *DexconGovernance) AddDKGMasterPublicKey(round uint64, masterPublicKey *types.DKGMasterPublicKey) { +func (d *DexconGovernance) AddDKGMasterPublicKey(round uint64, masterPublicKey *coreTypes.DKGMasterPublicKey) { } // DKGMasterPublicKeys gets all the DKGMasterPublicKey of round. -func (d *DexconGovernance) DKGMasterPublicKeys(round uint64) []*types.DKGMasterPublicKey { - return nil +func (d *DexconGovernance) DKGMasterPublicKeys(round uint64) []*coreTypes.DKGMasterPublicKey { + s := d.getGovState() + var dkgMasterPKs []*coreTypes.DKGMasterPublicKey + for _, pk := range s.DKGMasterPublicKeys(big.NewInt(int64(round))) { + x := new(coreTypes.DKGMasterPublicKey) + if err := rlp.DecodeBytes(pk, x); err != nil { + panic(err) + } + dkgMasterPKs = append(dkgMasterPKs, x) + } + return dkgMasterPKs } // AddDKGFinalize adds a DKG finalize message. -func (d *DexconGovernance) AddDKGFinalize(round uint64, final *types.DKGFinalize) { +func (d *DexconGovernance) AddDKGFinalize(round uint64, final *coreTypes.DKGFinalize) { } // IsDKGFinal checks if DKG is final. func (d *DexconGovernance) IsDKGFinal(round uint64) bool { - return false + s := d.getGovStateAtRound(round) + threshold := 2*s.DKGSetSize().Uint64()/3 + 1 + count := s.DKGFinalizedsCount(big.NewInt(int64(round))).Uint64() + return count >= threshold } -- cgit v1.2.3