diff options
author | Sonic <sonic@cobinhood.com> | 2018-10-12 15:02:33 +0800 |
---|---|---|
committer | Wei-Ning Huang <w@dexon.org> | 2018-12-19 20:54:27 +0800 |
commit | a468fd43993733d7ad89c14d038d91e88e12f611 (patch) | |
tree | 8aef4eb4e6c587edcdff15e85094c15f9e4322bd /dex/protocol.go | |
parent | 0f649b2cb78a3f35b870b2db94a92ec9297886b6 (diff) | |
download | dexon-a468fd43993733d7ad89c14d038d91e88e12f611.tar dexon-a468fd43993733d7ad89c14d038d91e88e12f611.tar.gz dexon-a468fd43993733d7ad89c14d038d91e88e12f611.tar.bz2 dexon-a468fd43993733d7ad89c14d038d91e88e12f611.tar.lz dexon-a468fd43993733d7ad89c14d038d91e88e12f611.tar.xz dexon-a468fd43993733d7ad89c14d038d91e88e12f611.tar.zst dexon-a468fd43993733d7ad89c14d038d91e88e12f611.zip |
dex: network: implement the network interface
Diffstat (limited to 'dex/protocol.go')
-rw-r--r-- | dex/protocol.go | 134 |
1 files changed, 133 insertions, 1 deletions
diff --git a/dex/protocol.go b/dex/protocol.go index 7b01217ff..71add16ee 100644 --- a/dex/protocol.go +++ b/dex/protocol.go @@ -20,10 +20,15 @@ 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" + "github.com/dexon-foundation/dexon/crypto/sha3" "github.com/dexon-foundation/dexon/event" "github.com/dexon-foundation/dexon/p2p/enode" "github.com/dexon-foundation/dexon/rlp" @@ -41,7 +46,7 @@ var ProtocolName = "dex" var ProtocolVersions = []uint{dex64} // ProtocolLengths are the number of implemented message corresponding to different protocol versions. -var ProtocolLengths = []uint64{18} +var ProtocolLengths = []uint64{38} const ProtocolMaxMsgSize = 10 * 1024 * 1024 // Maximum cap on the size of a protocol message @@ -65,6 +70,13 @@ const ( // Protocol messages belonging to dex/64 MetaMsg = 0x11 + + LatticeBlockMsg = 0x20 + VoteMsg = 0x21 + AgreementMsg = 0x22 + RandomnessMsg = 0x23 + DKGPrivateShareMsg = 0x24 + DKGPartialSignatureMsg = 0x25 ) type errCode int @@ -206,3 +218,123 @@ type blockBody struct { // blockBodiesData is the network packet for block content distribution. type blockBodiesData []*blockBody + +func rlpHash(x interface{}) (h common.Hash) { + hw := sha3.NewKeccak256() + rlp.Encode(hw, x) + hw.Sum(h[:0]) + return h +} + +type rlpDKGPrivateShare struct { + ProposerID coreTypes.NodeID + ReceiverID coreTypes.NodeID + Round uint64 + PrivateShare []byte + Signature crypto.Signature +} + +func toRLPDKGPrivateShare(ps *coreTypes.DKGPrivateShare) *rlpDKGPrivateShare { + return &rlpDKGPrivateShare{ + ProposerID: ps.ProposerID, + ReceiverID: ps.ReceiverID, + Round: ps.Round, + PrivateShare: ps.PrivateShare.Bytes(), + Signature: ps.Signature, + } +} + +func fromRLPDKGPrivateShare(rps *rlpDKGPrivateShare) *coreTypes.DKGPrivateShare { + ps := &coreTypes.DKGPrivateShare{ + ProposerID: rps.ProposerID, + ReceiverID: rps.ReceiverID, + Round: rps.Round, + Signature: rps.Signature, + } + ps.PrivateShare.SetBytes(rps.PrivateShare) + return ps +} + +type rlpWitness struct { + Timestamp uint64 + Height uint64 + Data []byte +} + +type rlpFinalizeResult struct { + Randomness []byte + Timestamp uint64 + Height uint64 +} + +type rlpLatticeBlock struct { + ProposerID coreTypes.NodeID `json:"proposer_id"` + ParentHash coreCommon.Hash `json:"parent_hash"` + Hash coreCommon.Hash `json:"hash"` + Position coreTypes.Position `json:"position"` + Timestamp uint64 `json:"timestamps"` + Acks coreCommon.SortedHashes `json:"acks"` + Payload []byte `json:"payload"` + Witness rlpWitness + Finalization rlpFinalizeResult + Signature crypto.Signature `json:"signature"` + CRSSignature crypto.Signature `json:"crs_signature"` +} + +func toRLPLatticeBlock(b *coreTypes.Block) *rlpLatticeBlock { + return &rlpLatticeBlock{ + ProposerID: b.ProposerID, + ParentHash: b.ParentHash, + Hash: b.Hash, + Position: b.Position, + Timestamp: toMillisecond(b.Timestamp), + Acks: b.Acks, + Payload: b.Payload, + Witness: rlpWitness{ + Timestamp: toMillisecond(b.Witness.Timestamp), + Height: b.Witness.Height, + Data: b.Witness.Data, + }, + Finalization: rlpFinalizeResult{ + Randomness: b.Finalization.Randomness, + Timestamp: toMillisecond(b.Finalization.Timestamp), + Height: b.Finalization.Height, + }, + Signature: b.Signature, + CRSSignature: b.CRSSignature, + } +} + +func fromRLPLatticeBlock(rb *rlpLatticeBlock) *coreTypes.Block { + return &coreTypes.Block{ + ProposerID: rb.ProposerID, + ParentHash: rb.ParentHash, + Hash: rb.Hash, + Position: rb.Position, + Timestamp: fromMillisecond(rb.Timestamp), + Acks: rb.Acks, + Payload: rb.Payload, + Witness: coreTypes.Witness{ + Timestamp: fromMillisecond(rb.Witness.Timestamp), + Height: rb.Witness.Height, + Data: rb.Witness.Data, + }, + Finalization: coreTypes.FinalizationResult{ + Randomness: rb.Finalization.Randomness, + Timestamp: fromMillisecond(rb.Finalization.Timestamp), + Height: rb.Finalization.Height, + }, + Signature: rb.Signature, + CRSSignature: rb.CRSSignature, + } +} + +func fromMillisecond(s uint64) time.Time { + sec := int64(s / 1000) + nsec := int64((s % 1000) * 1000000) + return time.Unix(sec, nsec) +} + +func toMillisecond(t time.Time) uint64 { + return uint64(t.UnixNano() / 1000000) +} |