diff options
author | Jimmy Hu <jimmy.hu@dexon.org> | 2019-02-09 16:40:21 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-02-09 16:40:21 +0800 |
commit | b76cdf502a13b2635be271658dccd72c7b3367cd (patch) | |
tree | c2d8c6e79be81a9c1bede0395a7de26394e5dd85 /core/types/dkg/dkg.go | |
parent | b29c4d5ab2313ecbc7822bf7c6d0374a44bf4a04 (diff) | |
download | dexon-consensus-b76cdf502a13b2635be271658dccd72c7b3367cd.tar dexon-consensus-b76cdf502a13b2635be271658dccd72c7b3367cd.tar.gz dexon-consensus-b76cdf502a13b2635be271658dccd72c7b3367cd.tar.bz2 dexon-consensus-b76cdf502a13b2635be271658dccd72c7b3367cd.tar.lz dexon-consensus-b76cdf502a13b2635be271658dccd72c7b3367cd.tar.xz dexon-consensus-b76cdf502a13b2635be271658dccd72c7b3367cd.tar.zst dexon-consensus-b76cdf502a13b2635be271658dccd72c7b3367cd.zip |
core: Fix rlp encode/decode for DKGComplaint. (#441)
* types: Add RLP Encode/Decode to DKGComplaint
* Add test
* fix state test
Diffstat (limited to 'core/types/dkg/dkg.go')
-rw-r--r-- | core/types/dkg/dkg.go | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/core/types/dkg/dkg.go b/core/types/dkg/dkg.go index efe8467..db58168 100644 --- a/core/types/dkg/dkg.go +++ b/core/types/dkg/dkg.go @@ -163,6 +163,64 @@ func (c *Complaint) Equal(other *Complaint) bool { bytes.Compare(c.Signature.Signature, other.Signature.Signature) == 0 } +type rlpComplaint struct { + ProposerID types.NodeID + Round uint64 + IsNack bool + PrivateShare []byte + Signature crypto.Signature +} + +// EncodeRLP implements rlp.Encoder +func (c *Complaint) EncodeRLP(w io.Writer) error { + if c.IsNack() { + return rlp.Encode(w, rlpComplaint{ + ProposerID: c.ProposerID, + Round: c.Round, + IsNack: true, + PrivateShare: c.PrivateShare.ProposerID.Hash[:], + Signature: c.Signature, + }) + } + prvShare, err := rlp.EncodeToBytes(&c.PrivateShare) + if err != nil { + return err + } + return rlp.Encode(w, rlpComplaint{ + ProposerID: c.ProposerID, + Round: c.Round, + IsNack: false, + PrivateShare: prvShare, + Signature: c.Signature, + }) +} + +// DecodeRLP implements rlp.Decoder +func (c *Complaint) DecodeRLP(s *rlp.Stream) error { + var dec rlpComplaint + if err := s.Decode(&dec); err != nil { + return err + } + + var prvShare PrivateShare + if dec.IsNack { + copy(prvShare.ProposerID.Hash[:], dec.PrivateShare) + prvShare.Round = dec.Round + } else { + if err := rlp.DecodeBytes(dec.PrivateShare, &prvShare); err != nil { + return err + } + } + + *c = Complaint{ + ProposerID: dec.ProposerID, + Round: dec.Round, + PrivateShare: prvShare, + Signature: dec.Signature, + } + return nil +} + // PartialSignature describe a partial signature in DKG protocol. type PartialSignature struct { ProposerID types.NodeID `json:"proposer_id"` |