mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-26 06:36:43 +00:00
whisper: new test added (rlp encode/decode)
This commit is contained in:
parent
5b682d6485
commit
03ef121919
3 changed files with 42 additions and 0 deletions
|
|
@ -172,6 +172,10 @@ func (e *Envelope) DecodeRLP(s *rlp.Stream) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return e.DecodeBytes(raw)
|
||||
}
|
||||
|
||||
func (e *Envelope) DecodeBytes(raw []byte) error {
|
||||
// The decoding of Envelope uses the struct fields but also needs
|
||||
// to compute the hash of the whole RLP-encoded envelope. This
|
||||
// type has the same structure as Envelope but is not an
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/ethereum/go-ethereum/crypto"
|
||||
"github.com/ethereum/go-ethereum/rlp"
|
||||
)
|
||||
|
||||
func copyFromBuf(dst []byte, src []byte, beg int) int {
|
||||
|
|
@ -315,3 +316,35 @@ func TestEncryptWithZeroKey(t *testing.T) {
|
|||
t.Fatalf("wrapped with nil key, seed: %d.", seed)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRlpEncode(t *testing.T) {
|
||||
InitSingleTest()
|
||||
|
||||
params, err := generateMessageParams()
|
||||
if err != nil {
|
||||
t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
|
||||
}
|
||||
msg := NewSentMessage(params)
|
||||
env, err := msg.Wrap(params)
|
||||
if err != nil {
|
||||
t.Fatalf("wrapped with zero key, seed: %d.", seed)
|
||||
}
|
||||
|
||||
rlpEncoded, err := rlp.EncodeToBytes(env)
|
||||
if err != nil {
|
||||
t.Fatalf("RLP encode failed: %s.", err)
|
||||
}
|
||||
|
||||
var decoded Envelope
|
||||
err = decoded.DecodeBytes(rlpEncoded)
|
||||
if err != nil {
|
||||
t.Fatalf("RLP decode failed: %s.", err)
|
||||
}
|
||||
|
||||
he := env.Hash()
|
||||
hd := decoded.Hash()
|
||||
|
||||
if he != hd {
|
||||
t.Fatalf("Hashes are not equal: %x vs. %x", he, hd)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -175,3 +175,8 @@ func (p *Peer) broadcast() error {
|
|||
glog.V(logger.Detail).Infoln(p.peer, "broadcasted", len(transmit), "message(s)")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Peer) ID() []byte {
|
||||
id := p.peer.ID()
|
||||
return id[:]
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue