mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-17 01:13:45 +00:00
whisper: fixed names, fixed failing tests
This commit is contained in:
parent
80b347d0bc
commit
cc812c347f
4 changed files with 45 additions and 29 deletions
|
|
@ -48,7 +48,7 @@ const (
|
||||||
p2pMessageCode = 127 // peer-to-peer message (to be consumed by the peer, but not forwarded any further)
|
p2pMessageCode = 127 // peer-to-peer message (to be consumed by the peer, but not forwarded any further)
|
||||||
NumberOfMessageCodes = 128
|
NumberOfMessageCodes = 128
|
||||||
|
|
||||||
auxFieldSizeMask = byte(3) // mask used to extract the size of auxiliary field from the flags
|
SizeMask = byte(3) // mask used to extract the size of payload size field from the flags
|
||||||
signatureFlag = byte(4)
|
signatureFlag = byte(4)
|
||||||
|
|
||||||
TopicLength = 4 // in bytes
|
TopicLength = 4 // in bytes
|
||||||
|
|
@ -58,7 +58,6 @@ const (
|
||||||
keyIdSize = 32 // in bytes
|
keyIdSize = 32 // in bytes
|
||||||
bloomFilterSize = 64 // in bytes
|
bloomFilterSize = 64 // in bytes
|
||||||
flagsLength = 1
|
flagsLength = 1
|
||||||
auxiliaryFieldMaxSize = 4
|
|
||||||
|
|
||||||
EnvelopeHeaderLength = 20
|
EnvelopeHeaderLength = 20
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -89,19 +89,20 @@ func (msg *ReceivedMessage) isAsymmetricEncryption() bool {
|
||||||
|
|
||||||
// NewMessage creates and initializes a non-signed, non-encrypted Whisper message.
|
// NewMessage creates and initializes a non-signed, non-encrypted Whisper message.
|
||||||
func NewSentMessage(params *MessageParams) (*sentMessage, error) {
|
func NewSentMessage(params *MessageParams) (*sentMessage, error) {
|
||||||
|
const payloadSizeFieldMaxSize = 4
|
||||||
msg := sentMessage{}
|
msg := sentMessage{}
|
||||||
msg.Raw = make([]byte, 1,
|
msg.Raw = make([]byte, 1,
|
||||||
flagsLength+auxiliaryFieldMaxSize+len(params.Payload)+len(params.Padding)+signatureLength+padSizeLimit)
|
flagsLength+payloadSizeFieldMaxSize+len(params.Payload)+len(params.Padding)+signatureLength+padSizeLimit)
|
||||||
msg.Raw[0] = 0 // set all the flags to zero
|
msg.Raw[0] = 0 // set all the flags to zero
|
||||||
msg.addAuxiliaryField(params.Payload)
|
msg.addPayloadSizeField(params.Payload)
|
||||||
msg.Raw = append(msg.Raw, params.Payload...)
|
msg.Raw = append(msg.Raw, params.Payload...)
|
||||||
err := msg.appendPadding(params)
|
err := msg.appendPadding(params)
|
||||||
return &msg, err
|
return &msg, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// appendPayloadSizeField appends the auxiliary field containing the size of payload
|
// addPayloadSizeField appends the auxiliary field containing the size of payload
|
||||||
func (msg *sentMessage) addAuxiliaryField(payload []byte) {
|
func (msg *sentMessage) addPayloadSizeField(payload []byte) {
|
||||||
fieldSize := getAuxiliaryFieldSize(payload)
|
fieldSize := getSizeOfPayloadSizeField(payload)
|
||||||
field := make([]byte, 4)
|
field := make([]byte, 4)
|
||||||
binary.LittleEndian.PutUint32(field, uint32(len(payload)))
|
binary.LittleEndian.PutUint32(field, uint32(len(payload)))
|
||||||
field = field[:fieldSize]
|
field = field[:fieldSize]
|
||||||
|
|
@ -109,8 +110,8 @@ func (msg *sentMessage) addAuxiliaryField(payload []byte) {
|
||||||
msg.Raw[0] |= byte(fieldSize)
|
msg.Raw[0] |= byte(fieldSize)
|
||||||
}
|
}
|
||||||
|
|
||||||
// getAuxFieldSize returns the number of bytes necessary to encode the size of payload
|
// getSizeOfPayloadSizeField returns the number of bytes necessary to encode the size of payload
|
||||||
func getAuxiliaryFieldSize(payload []byte) int {
|
func getSizeOfPayloadSizeField(payload []byte) int {
|
||||||
s := 1
|
s := 1
|
||||||
for i := len(payload); i >= 256; i /= 256 {
|
for i := len(payload); i >= 256; i /= 256 {
|
||||||
s++
|
s++
|
||||||
|
|
@ -127,8 +128,7 @@ func (msg *sentMessage) appendPadding(params *MessageParams) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
auxFieldSize := getAuxiliaryFieldSize(params.Payload)
|
rawSize := flagsLength + getSizeOfPayloadSizeField(params.Payload) + len(params.Payload)
|
||||||
rawSize := flagsLength + auxFieldSize + len(params.Payload)
|
|
||||||
if params.Src != nil {
|
if params.Src != nil {
|
||||||
rawSize += signatureLength
|
rawSize += signatureLength
|
||||||
}
|
}
|
||||||
|
|
@ -316,13 +316,13 @@ func (msg *ReceivedMessage) ValidateAndParse() bool {
|
||||||
|
|
||||||
beg := 1
|
beg := 1
|
||||||
payloadSize := 0
|
payloadSize := 0
|
||||||
auxFieldSize := int(msg.Raw[0] & auxFieldSizeMask) // number of bytes indicating the size of payload
|
sizeOfPayloadSizeField := int(msg.Raw[0] & SizeMask) // number of bytes indicating the size of payload
|
||||||
if auxFieldSize != 0 {
|
if sizeOfPayloadSizeField != 0 {
|
||||||
payloadSize = int(bytesToUintLittleEndian(msg.Raw[beg : beg+auxFieldSize]))
|
payloadSize = int(bytesToUintLittleEndian(msg.Raw[beg : beg+sizeOfPayloadSizeField]))
|
||||||
if payloadSize+1 > end {
|
if payloadSize+1 > end {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
beg += auxFieldSize
|
beg += sizeOfPayloadSizeField
|
||||||
msg.Payload = msg.Raw[beg : beg+payloadSize]
|
msg.Payload = msg.Raw[beg : beg+payloadSize]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -343,7 +343,7 @@ func (msg *ReceivedMessage) SigToPubKey() *ecdsa.PublicKey {
|
||||||
return pub
|
return pub
|
||||||
}
|
}
|
||||||
|
|
||||||
// hash calculates the SHA3 checksum of the message flags, auxiliary field, payload and padding.
|
// hash calculates the SHA3 checksum of the message flags, payload size field, payload and padding.
|
||||||
func (msg *ReceivedMessage) hash() []byte {
|
func (msg *ReceivedMessage) hash() []byte {
|
||||||
if isMessageSigned(msg.Raw[0]) {
|
if isMessageSigned(msg.Raw[0]) {
|
||||||
sz := len(msg.Raw) - signatureLength
|
sz := len(msg.Raw) - signatureLength
|
||||||
|
|
|
||||||
|
|
@ -438,8 +438,8 @@ func TestPaddingAppendedToSymMessagesWithSignature(t *testing.T) {
|
||||||
// payload + flag + signature > 256. Check that the result
|
// payload + flag + signature > 256. Check that the result
|
||||||
// is padded on the next 256 boundary.
|
// is padded on the next 256 boundary.
|
||||||
msg := sentMessage{}
|
msg := sentMessage{}
|
||||||
const auxiliaryFieldMinSize = 1
|
const payloadSizeFieldMinSize = 1
|
||||||
msg.Raw = make([]byte, flagsLength+auxiliaryFieldMinSize+len(params.Payload))
|
msg.Raw = make([]byte, flagsLength+payloadSizeFieldMinSize+len(params.Payload))
|
||||||
|
|
||||||
err = msg.appendPadding(params)
|
err = msg.appendPadding(params)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -385,20 +385,37 @@ func TestPeerBasic(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkPowExchangeForNodeZero(t *testing.T) {
|
func checkPowExchangeForNodeZero(t *testing.T) {
|
||||||
|
const iterations = 200
|
||||||
|
for j := 0; j < iterations; j++ {
|
||||||
|
lastCycle := (j == iterations-1)
|
||||||
|
ok := checkPowExchangeForNodeZeroOnce(t, lastCycle)
|
||||||
|
if ok {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
time.Sleep(50 * time.Millisecond)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func checkPowExchangeForNodeZeroOnce(t *testing.T, mustPass bool) bool {
|
||||||
cnt := 0
|
cnt := 0
|
||||||
for i, node := range nodes {
|
for i, node := range nodes {
|
||||||
for peer := range node.shh.peers {
|
for peer := range node.shh.peers {
|
||||||
if peer.peer.ID() == discover.PubkeyID(&nodes[0].id.PublicKey) {
|
if peer.peer.ID() == discover.PubkeyID(&nodes[0].id.PublicKey) {
|
||||||
cnt++
|
cnt++
|
||||||
if peer.powRequirement != masterPow {
|
if peer.powRequirement != masterPow {
|
||||||
t.Fatalf("node %d: failed to set the new pow requirement.", i)
|
if mustPass {
|
||||||
|
t.Fatalf("node %d: failed to set the new pow requirement for node zero.", i)
|
||||||
|
} else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if cnt == 0 {
|
if cnt == 0 {
|
||||||
t.Fatalf("no matching peers found.")
|
t.Fatalf("looking for node zero: no matching peers found.")
|
||||||
}
|
}
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkPowExchange(t *testing.T) {
|
func checkPowExchange(t *testing.T) {
|
||||||
|
|
@ -432,7 +449,7 @@ func checkBloomFilterExchangeOnce(t *testing.T, mustPass bool) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkBloomFilterExchange(t *testing.T) {
|
func checkBloomFilterExchange(t *testing.T) {
|
||||||
const iterations = 128
|
const iterations = 200
|
||||||
for j := 0; j < iterations; j++ {
|
for j := 0; j < iterations; j++ {
|
||||||
lastCycle := (j == iterations-1)
|
lastCycle := (j == iterations-1)
|
||||||
ok := checkBloomFilterExchangeOnce(t, lastCycle)
|
ok := checkBloomFilterExchangeOnce(t, lastCycle)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue