diff options
author | Sonic <sonic@cobinhood.com> | 2018-10-20 10:10:47 +0800 |
---|---|---|
committer | Wei-Ning Huang <w@dexon.org> | 2019-04-09 21:32:50 +0800 |
commit | dca561583b7657dd65b39e0a196cb026d608ba11 (patch) | |
tree | e3ef80028dab1c72e1666009ffed69c54cbbdb3d /dex | |
parent | a18db064261a0336d7cf5b375583ece163d42dbb (diff) | |
download | dexon-dca561583b7657dd65b39e0a196cb026d608ba11.tar.gz dexon-dca561583b7657dd65b39e0a196cb026d608ba11.tar.zst dexon-dca561583b7657dd65b39e0a196cb026d608ba11.zip |
dex: no need to rlp encode/decode now, consensus supports it
Diffstat (limited to 'dex')
-rw-r--r-- | dex/handler.go | 18 | ||||
-rw-r--r-- | dex/peer.go | 16 | ||||
-rw-r--r-- | dex/protocol.go | 6 | ||||
-rw-r--r-- | dex/protocol_test.go | 32 |
4 files changed, 31 insertions, 41 deletions
diff --git a/dex/handler.go b/dex/handler.go index e9ef91fd5..ce6a0366c 100644 --- a/dex/handler.go +++ b/dex/handler.go @@ -709,11 +709,11 @@ func (pm *ProtocolManager) handleMsg(p *peer) error { } pm.nodeTable.Add(metas) case msg.Code == LatticeBlockMsg: - var rb rlpLatticeBlock - if err := msg.Decode(&rb); err != nil { + var block coreTypes.Block + if err := msg.Decode(&block); err != nil { return errResp(ErrDecode, "msg %v: %v", msg, err) } - pm.receiveCh <- fromRLPLatticeBlock(&rb) + pm.receiveCh <- &block case msg.Code == VoteMsg: var vote coreTypes.Vote if err := msg.Decode(&vote); err != nil { @@ -736,11 +736,11 @@ func (pm *ProtocolManager) handleMsg(p *peer) error { pm.receiveCh <- &randomness case msg.Code == DKGPrivateShareMsg: // Do not relay this msg - var rps rlpDKGPrivateShare - if err := msg.Decode(&rps); err != nil { + var ps coreTypes.DKGPrivateShare + if err := msg.Decode(&ps); err != nil { return errResp(ErrDecode, "msg %v: %v", msg, err) } - pm.receiveCh <- fromRLPDKGPrivateShare(&rps) + pm.receiveCh <- &ps case msg.Code == DKGPartialSignatureMsg: // broadcast in DKG set var psig coreTypes.DKGPartialSignature @@ -826,8 +826,7 @@ func (pm *ProtocolManager) BroadcastMetas(metas []*NodeMeta) { // TODO(sonic): block size is big, try not to send to all peers // to reduce traffic func (pm *ProtocolManager) BroadcastLatticeBlock(block *coreTypes.Block) { - hash := rlpHash(toRLPLatticeBlock(block)) - for _, peer := range pm.peers.PeersWithoutLatticeBlock(hash) { + for _, peer := range pm.peers.PeersWithoutLatticeBlock(rlpHash(block)) { peer.AsyncSendLatticeBlock(block) } } @@ -893,9 +892,8 @@ func (pm *ProtocolManager) SendDKGPrivateShare( func (pm *ProtocolManager) BroadcastDKGPrivateShare( privateShare *coreTypes.DKGPrivateShare) { label := peerLabel{set: dkgset, round: privateShare.Round} - h := rlpHash(toRLPDKGPrivateShare(privateShare)) for _, peer := range pm.peers.PeersWithLabel(label) { - if !peer.knownDKGPrivateShares.Contains(h) { + if !peer.knownDKGPrivateShares.Contains(rlpHash(privateShare)) { peer.AsyncSendDKGPrivateShare(privateShare) } } diff --git a/dex/peer.go b/dex/peer.go index db68ea590..a5e0dd21a 100644 --- a/dex/peer.go +++ b/dex/peer.go @@ -388,16 +388,14 @@ func (p *peer) AsyncSendNewBlock(block *types.Block, td *big.Int) { } func (p *peer) SendLatticeBlock(block *coreTypes.Block) error { - r := toRLPLatticeBlock(block) - p.knownLatticeBlocks.Add(rlpHash(r)) - return p2p.Send(p.rw, LatticeBlockMsg, toRLPLatticeBlock(block)) + p.knownLatticeBlocks.Add(rlpHash(block)) + return p2p.Send(p.rw, LatticeBlockMsg, block) } func (p *peer) AsyncSendLatticeBlock(block *coreTypes.Block) { select { case p.queuedLatticeBlocks <- block: - r := toRLPLatticeBlock(block) - p.knownLatticeBlocks.Add(rlpHash(r)) + p.knownLatticeBlocks.Add(rlpHash(block)) default: p.Log().Debug("Dropping lattice block propagation") } @@ -446,16 +444,14 @@ func (p *peer) AsyncSendRandomness(randomness *coreTypes.BlockRandomnessResult) } func (p *peer) SendDKGPrivateShare(privateShare *coreTypes.DKGPrivateShare) error { - r := toRLPDKGPrivateShare(privateShare) - p.knownDKGPrivateShares.Add(rlpHash(r)) - return p2p.Send(p.rw, DKGPrivateShareMsg, toRLPDKGPrivateShare(privateShare)) + p.knownDKGPrivateShares.Add(rlpHash(privateShare)) + return p2p.Send(p.rw, DKGPrivateShareMsg, privateShare) } func (p *peer) AsyncSendDKGPrivateShare(privateShare *coreTypes.DKGPrivateShare) { select { case p.queuedDKGPrivateShares <- privateShare: - r := toRLPDKGPrivateShare(privateShare) - p.knownDKGPrivateShares.Add(rlpHash(r)) + p.knownDKGPrivateShares.Add(rlpHash(privateShare)) default: p.Log().Debug("Dropping DKG private share") } diff --git a/dex/protocol.go b/dex/protocol.go index c17398ffb..6e531c7af 100644 --- a/dex/protocol.go +++ b/dex/protocol.go @@ -21,11 +21,7 @@ import ( "fmt" "io" "math/big" - "time" - coreCommon "github.com/dexon-foundation/dexon-consensus-core/common" - "github.com/dexon-foundation/dexon-consensus-core/core/crypto" - coreTypes "github.com/dexon-foundation/dexon-consensus-core/core/types" "github.com/dexon-foundation/dexon/common" "github.com/dexon-foundation/dexon/core" "github.com/dexon-foundation/dexon/core/types" @@ -229,6 +225,7 @@ func rlpHash(x interface{}) (h common.Hash) { return h } +/* type rlpDKGPrivateShare struct { ProposerID coreTypes.NodeID ReceiverID coreTypes.NodeID @@ -341,3 +338,4 @@ func fromMillisecond(s uint64) time.Time { func toMillisecond(t time.Time) uint64 { return uint64(t.UnixNano() / 1000000) } +*/ diff --git a/dex/protocol_test.go b/dex/protocol_test.go index 8bc24e8de..f2d668463 100644 --- a/dex/protocol_test.go +++ b/dex/protocol_test.go @@ -326,19 +326,19 @@ func TestRecvLatticeBlock(t *testing.T) { Round: 12, Height: 13, }, - Timestamp: fromMillisecond(toMillisecond(time.Now())), + Timestamp: time.Now().UTC(), Acks: coreCommon.NewSortedHashes(coreCommon.Hashes([]coreCommon.Hash{ coreCommon.Hash{101}, coreCommon.Hash{100}, coreCommon.Hash{102}, })), Payload: []byte{3, 3, 3, 3, 3}, Witness: coreTypes.Witness{ - Timestamp: fromMillisecond(toMillisecond(time.Now())), + Timestamp: time.Now().UTC(), Height: 13, Data: []byte{4, 4, 4, 4, 4}, }, Finalization: coreTypes.FinalizationResult{ Randomness: []byte{5, 5, 5, 5, 5}, - Timestamp: fromMillisecond(toMillisecond(time.Now())), + Timestamp: time.Now().UTC(), Height: 13, }, Signature: coreCrypto.Signature{ @@ -351,7 +351,7 @@ func TestRecvLatticeBlock(t *testing.T) { }, } - if err := p2p.Send(p.app, LatticeBlockMsg, toRLPLatticeBlock(&block)); err != nil { + if err := p2p.Send(p.app, LatticeBlockMsg, &block); err != nil { t.Fatalf("send error: %v", err) } @@ -382,19 +382,19 @@ func TestSendLatticeBlock(t *testing.T) { Round: 12, Height: 13, }, - Timestamp: fromMillisecond(toMillisecond(time.Now())), + Timestamp: time.Now().UTC(), Acks: coreCommon.NewSortedHashes(coreCommon.Hashes([]coreCommon.Hash{ coreCommon.Hash{101}, coreCommon.Hash{100}, coreCommon.Hash{102}, })), Payload: []byte{3, 3, 3, 3, 3}, Witness: coreTypes.Witness{ - Timestamp: fromMillisecond(toMillisecond(time.Now())), + Timestamp: time.Now().UTC(), Height: 13, Data: []byte{4, 4, 4, 4, 4}, }, Finalization: coreTypes.FinalizationResult{ Randomness: []byte{5, 5, 5, 5, 5}, - Timestamp: fromMillisecond(toMillisecond(time.Now())), + Timestamp: time.Now().UTC(), Height: 13, }, Signature: coreCrypto.Signature{ @@ -416,12 +416,12 @@ func TestSendLatticeBlock(t *testing.T) { t.Errorf("%v: got code %d, want %d", p.Peer, msg.Code, LatticeBlockMsg) } - var rb rlpLatticeBlock - if err := msg.Decode(&rb); err != nil { + var b coreTypes.Block + if err := msg.Decode(&b); err != nil { t.Errorf("%v: %v", p.Peer, err) } - if !reflect.DeepEqual(fromRLPLatticeBlock(&rb), &block) { + if !reflect.DeepEqual(b, block) { t.Errorf("block mismatch") } } @@ -592,7 +592,7 @@ func TestRecvDKGPrivateShare(t *testing.T) { } if err := p2p.Send( - p.app, DKGPrivateShareMsg, toRLPDKGPrivateShare(&privateShare)); err != nil { + p.app, DKGPrivateShareMsg, &privateShare); err != nil { t.Fatalf("send error: %v", err) } @@ -600,8 +600,7 @@ func TestRecvDKGPrivateShare(t *testing.T) { select { case msg := <-ch: rps := msg.(*coreTypes.DKGPrivateShare) - if !reflect.DeepEqual( - toRLPDKGPrivateShare(rps), toRLPDKGPrivateShare(&privateShare)) { + if !reflect.DeepEqual(rps, &privateShare) { t.Errorf("vote mismatch") } case <-time.After(1 * time.Second): @@ -637,13 +636,12 @@ func TestSendDKGPrivateShare(t *testing.T) { t.Errorf("%v: got code %d, want %d", p1.Peer, msg.Code, DKGPrivateShareMsg) } - var rps rlpDKGPrivateShare - if err := msg.Decode(&rps); err != nil { + var ps coreTypes.DKGPrivateShare + if err := msg.Decode(&ps); err != nil { t.Errorf("%v: %v", p1.Peer, err) } - expected := toRLPDKGPrivateShare(&privateShare) - if !reflect.DeepEqual(rps, *expected) { + if !reflect.DeepEqual(ps, privateShare) { t.Errorf("DKG private share mismatch") } |