diff options
author | Mission Liao <mission.liao@dexon.org> | 2018-09-28 12:32:50 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-09-28 12:32:50 +0800 |
commit | 5fc0efa940c7663a33d0fc501807a2627d2cb573 (patch) | |
tree | 932ef05c2d091bdbe6bc38f0391d45993cc1f42c /simulation | |
parent | 189a17a6586cd30ac355bd1165c44da6a2a08569 (diff) | |
download | dexon-consensus-5fc0efa940c7663a33d0fc501807a2627d2cb573.tar dexon-consensus-5fc0efa940c7663a33d0fc501807a2627d2cb573.tar.gz dexon-consensus-5fc0efa940c7663a33d0fc501807a2627d2cb573.tar.bz2 dexon-consensus-5fc0efa940c7663a33d0fc501807a2627d2cb573.tar.lz dexon-consensus-5fc0efa940c7663a33d0fc501807a2627d2cb573.tar.xz dexon-consensus-5fc0efa940c7663a33d0fc501807a2627d2cb573.tar.zst dexon-consensus-5fc0efa940c7663a33d0fc501807a2627d2cb573.zip |
core: hide types.NodeID from full node. (#147)
* Refine core.Governance interface
- Remove types.NodeID from interface declaration.
- All parameter should be round based.
* Add core.NodeSetCache
* Agreement accepts map of nodeID directly.
* test.Transport.Peers method return public keys.
Diffstat (limited to 'simulation')
-rw-r--r-- | simulation/governance.go | 25 | ||||
-rw-r--r-- | simulation/network.go | 15 | ||||
-rw-r--r-- | simulation/node.go | 10 | ||||
-rw-r--r-- | simulation/peer-server.go | 4 |
4 files changed, 29 insertions, 25 deletions
diff --git a/simulation/governance.go b/simulation/governance.go index eb0f2a2..3290075 100644 --- a/simulation/governance.go +++ b/simulation/governance.go @@ -32,7 +32,7 @@ import ( type simGovernance struct { id types.NodeID lock sync.RWMutex - notarySet map[types.NodeID]struct{} + nodeSet map[types.NodeID]crypto.PublicKey expectedNumNodes int k int phiRatio float32 @@ -52,7 +52,7 @@ func newSimGovernance( numNodes int, consensusConfig config.Consensus) *simGovernance { return &simGovernance{ id: id, - notarySet: make(map[types.NodeID]struct{}), + nodeSet: make(map[types.NodeID]crypto.PublicKey), expectedNumNodes: numNodes, k: consensusConfig.K, phiRatio: consensusConfig.PhiRatio, @@ -71,17 +71,14 @@ func (g *simGovernance) setNetwork(network *network) { } // GetNodeSet returns the current notary set. -func (g *simGovernance) GetNodeSet( - blockHeight uint64) map[types.NodeID]struct{} { +func (g *simGovernance) GetNodeSet(round uint64) (ret []crypto.PublicKey) { g.lock.RLock() defer g.lock.RUnlock() - // Return the cloned notarySet. - ret := make(map[types.NodeID]struct{}) - for k := range g.notarySet { - ret[k] = struct{}{} + for _, pubKey := range g.nodeSet { + ret = append(ret, pubKey) } - return ret + return } // GetConfiguration returns the configuration at a given round. @@ -102,17 +99,19 @@ func (g *simGovernance) GetCRS(round uint64) []byte { } // addNode add a new node into the simulated governance contract. -func (g *simGovernance) addNode(nID types.NodeID) { +func (g *simGovernance) addNode(pubKey crypto.PublicKey) { + nID := types.NewNodeID(pubKey) + g.lock.Lock() defer g.lock.Unlock() - if _, exists := g.notarySet[nID]; exists { + if _, exists := g.nodeSet[nID]; exists { return } - if len(g.notarySet) == g.expectedNumNodes { + if len(g.nodeSet) == g.expectedNumNodes { panic(fmt.Errorf("attempt to add node when ready")) } - g.notarySet[nID] = struct{}{} + g.nodeSet[nID] = pubKey } // ProposeThresholdSignature porposes a ThresholdSignature of round. diff --git a/simulation/network.go b/simulation/network.go index 3d3acaa..f1f586a 100644 --- a/simulation/network.go +++ b/simulation/network.go @@ -26,6 +26,7 @@ import ( "time" "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/test" "github.com/dexon-foundation/dexon-consensus-core/core/types" "github.com/dexon-foundation/dexon-consensus-core/simulation/config" @@ -88,7 +89,7 @@ type network struct { // newNetwork setup network stuffs for nodes, which provides an // implementation of core.Network based on test.TransportClient. -func newNetwork(nID types.NodeID, cfg config.Networking) (n *network) { +func newNetwork(pubKey crypto.PublicKey, cfg config.Networking) (n *network) { // Construct latency model. latency := &test.NormalLatencyModel{ Mean: cfg.Mean, @@ -105,12 +106,12 @@ func newNetwork(nID types.NodeID, cfg config.Networking) (n *network) { switch cfg.Type { case config.NetworkTypeTCPLocal: n.trans = test.NewTCPTransportClient( - nID, latency, &jsonMarshaller{}, true) + pubKey, latency, &jsonMarshaller{}, true) case config.NetworkTypeTCP: n.trans = test.NewTCPTransportClient( - nID, latency, &jsonMarshaller{}, false) + pubKey, latency, &jsonMarshaller{}, false) case config.NetworkTypeFake: - n.trans = test.NewFakeTransportClient(nID, latency) + n.trans = test.NewFakeTransportClient(pubKey, latency) default: panic(fmt.Errorf("unknown network type: %v", cfg.Type)) } @@ -147,8 +148,8 @@ func (n *network) broadcast(message interface{}) { // SendDKGPrivateShare implements core.Network interface. func (n *network) SendDKGPrivateShare( - recv types.NodeID, prvShare *types.DKGPrivateShare) { - if err := n.trans.Send(recv, prvShare); err != nil { + recv crypto.PublicKey, prvShare *types.DKGPrivateShare) { + if err := n.trans.Send(types.NewNodeID(recv), prvShare); err != nil { panic(err) } } @@ -249,6 +250,6 @@ func (n *network) report(msg interface{}) error { } // peers exports 'Peers' method of test.Transport. -func (n *network) peers() map[types.NodeID]struct{} { +func (n *network) peers() []crypto.PublicKey { return n.trans.Peers() } diff --git a/simulation/node.go b/simulation/node.go index 710b5e9..c9163b3 100644 --- a/simulation/node.go +++ b/simulation/node.go @@ -49,8 +49,9 @@ func newNode( prvKey crypto.PrivateKey, config config.Config) *node { - id := types.NewNodeID(prvKey.PublicKey()) - netModule := newNetwork(id, config.Networking) + pubKey := prvKey.PublicKey() + netModule := newNetwork(pubKey, config.Networking) + id := types.NewNodeID(pubKey) db, err := blockdb.NewMemBackedBlockDB( id.String() + ".blockdb") if err != nil { @@ -85,8 +86,9 @@ func (n *node) run(serverEndpoint interface{}, legacy bool) { n.gov.setNetwork(n.netModule) // Run consensus. hashes := make(common.Hashes, 0, len(peers)) - for nID := range peers { - n.gov.addNode(nID) + for _, pubKey := range peers { + nID := types.NewNodeID(pubKey) + n.gov.addNode(pubKey) hashes = append(hashes, nID.Hash) } sort.Sort(hashes) diff --git a/simulation/peer-server.go b/simulation/peer-server.go index 30cf896..c8a3078 100644 --- a/simulation/peer-server.go +++ b/simulation/peer-server.go @@ -168,7 +168,9 @@ func (p *PeerServer) Run() { panic(err) } // Cache peers' info. - p.peers = p.trans.Peers() + for _, pubKey := range p.trans.Peers() { + p.peers[types.NewNodeID(pubKey)] = struct{}{} + } // Initialize total order result cache. for id := range p.peers { p.peerTotalOrder[id] = NewTotalOrderResult(id) |