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,17 +48,16 @@ const (
|
|||
p2pMessageCode = 127 // peer-to-peer message (to be consumed by the peer, but not forwarded any further)
|
||||
NumberOfMessageCodes = 128
|
||||
|
||||
auxFieldSizeMask = byte(3) // mask used to extract the size of auxiliary field from the flags
|
||||
signatureFlag = byte(4)
|
||||
SizeMask = byte(3) // mask used to extract the size of payload size field from the flags
|
||||
signatureFlag = byte(4)
|
||||
|
||||
TopicLength = 4 // in bytes
|
||||
signatureLength = 65 // in bytes
|
||||
aesKeyLength = 32 // in bytes
|
||||
aesNonceLength = 12 // in bytes; for more info please see cipher.gcmStandardNonceSize & aesgcm.NonceSize()
|
||||
keyIdSize = 32 // in bytes
|
||||
bloomFilterSize = 64 // in bytes
|
||||
flagsLength = 1
|
||||
auxiliaryFieldMaxSize = 4
|
||||
TopicLength = 4 // in bytes
|
||||
signatureLength = 65 // in bytes
|
||||
aesKeyLength = 32 // in bytes
|
||||
aesNonceLength = 12 // in bytes; for more info please see cipher.gcmStandardNonceSize & aesgcm.NonceSize()
|
||||
keyIdSize = 32 // in bytes
|
||||
bloomFilterSize = 64 // in bytes
|
||||
flagsLength = 1
|
||||
|
||||
EnvelopeHeaderLength = 20
|
||||
|
||||
|
|
|
|||
|
|
@ -89,19 +89,20 @@ func (msg *ReceivedMessage) isAsymmetricEncryption() bool {
|
|||
|
||||
// NewMessage creates and initializes a non-signed, non-encrypted Whisper message.
|
||||
func NewSentMessage(params *MessageParams) (*sentMessage, error) {
|
||||
const payloadSizeFieldMaxSize = 4
|
||||
msg := sentMessage{}
|
||||
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.addAuxiliaryField(params.Payload)
|
||||
msg.addPayloadSizeField(params.Payload)
|
||||
msg.Raw = append(msg.Raw, params.Payload...)
|
||||
err := msg.appendPadding(params)
|
||||
return &msg, err
|
||||
}
|
||||
|
||||
// appendPayloadSizeField appends the auxiliary field containing the size of payload
|
||||
func (msg *sentMessage) addAuxiliaryField(payload []byte) {
|
||||
fieldSize := getAuxiliaryFieldSize(payload)
|
||||
// addPayloadSizeField appends the auxiliary field containing the size of payload
|
||||
func (msg *sentMessage) addPayloadSizeField(payload []byte) {
|
||||
fieldSize := getSizeOfPayloadSizeField(payload)
|
||||
field := make([]byte, 4)
|
||||
binary.LittleEndian.PutUint32(field, uint32(len(payload)))
|
||||
field = field[:fieldSize]
|
||||
|
|
@ -109,8 +110,8 @@ func (msg *sentMessage) addAuxiliaryField(payload []byte) {
|
|||
msg.Raw[0] |= byte(fieldSize)
|
||||
}
|
||||
|
||||
// getAuxFieldSize returns the number of bytes necessary to encode the size of payload
|
||||
func getAuxiliaryFieldSize(payload []byte) int {
|
||||
// getSizeOfPayloadSizeField returns the number of bytes necessary to encode the size of payload
|
||||
func getSizeOfPayloadSizeField(payload []byte) int {
|
||||
s := 1
|
||||
for i := len(payload); i >= 256; i /= 256 {
|
||||
s++
|
||||
|
|
@ -127,8 +128,7 @@ func (msg *sentMessage) appendPadding(params *MessageParams) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
auxFieldSize := getAuxiliaryFieldSize(params.Payload)
|
||||
rawSize := flagsLength + auxFieldSize + len(params.Payload)
|
||||
rawSize := flagsLength + getSizeOfPayloadSizeField(params.Payload) + len(params.Payload)
|
||||
if params.Src != nil {
|
||||
rawSize += signatureLength
|
||||
}
|
||||
|
|
@ -316,13 +316,13 @@ func (msg *ReceivedMessage) ValidateAndParse() bool {
|
|||
|
||||
beg := 1
|
||||
payloadSize := 0
|
||||
auxFieldSize := int(msg.Raw[0] & auxFieldSizeMask) // number of bytes indicating the size of payload
|
||||
if auxFieldSize != 0 {
|
||||
payloadSize = int(bytesToUintLittleEndian(msg.Raw[beg : beg+auxFieldSize]))
|
||||
sizeOfPayloadSizeField := int(msg.Raw[0] & SizeMask) // number of bytes indicating the size of payload
|
||||
if sizeOfPayloadSizeField != 0 {
|
||||
payloadSize = int(bytesToUintLittleEndian(msg.Raw[beg : beg+sizeOfPayloadSizeField]))
|
||||
if payloadSize+1 > end {
|
||||
return false
|
||||
}
|
||||
beg += auxFieldSize
|
||||
beg += sizeOfPayloadSizeField
|
||||
msg.Payload = msg.Raw[beg : beg+payloadSize]
|
||||
}
|
||||
|
||||
|
|
@ -343,7 +343,7 @@ func (msg *ReceivedMessage) SigToPubKey() *ecdsa.PublicKey {
|
|||
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 {
|
||||
if isMessageSigned(msg.Raw[0]) {
|
||||
sz := len(msg.Raw) - signatureLength
|
||||
|
|
|
|||
|
|
@ -438,8 +438,8 @@ func TestPaddingAppendedToSymMessagesWithSignature(t *testing.T) {
|
|||
// payload + flag + signature > 256. Check that the result
|
||||
// is padded on the next 256 boundary.
|
||||
msg := sentMessage{}
|
||||
const auxiliaryFieldMinSize = 1
|
||||
msg.Raw = make([]byte, flagsLength+auxiliaryFieldMinSize+len(params.Payload))
|
||||
const payloadSizeFieldMinSize = 1
|
||||
msg.Raw = make([]byte, flagsLength+payloadSizeFieldMinSize+len(params.Payload))
|
||||
|
||||
err = msg.appendPadding(params)
|
||||
|
||||
|
|
|
|||
|
|
@ -385,20 +385,37 @@ func TestPeerBasic(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
|
||||
for i, node := range nodes {
|
||||
for peer := range node.shh.peers {
|
||||
if peer.peer.ID() == discover.PubkeyID(&nodes[0].id.PublicKey) {
|
||||
cnt++
|
||||
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 {
|
||||
t.Fatalf("no matching peers found.")
|
||||
t.Fatalf("looking for node zero: no matching peers found.")
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func checkPowExchange(t *testing.T) {
|
||||
|
|
@ -432,7 +449,7 @@ func checkBloomFilterExchangeOnce(t *testing.T, mustPass bool) bool {
|
|||
}
|
||||
|
||||
func checkBloomFilterExchange(t *testing.T) {
|
||||
const iterations = 128
|
||||
const iterations = 200
|
||||
for j := 0; j < iterations; j++ {
|
||||
lastCycle := (j == iterations-1)
|
||||
ok := checkBloomFilterExchangeOnce(t, lastCycle)
|
||||
|
|
|
|||
Loading…
Reference in a new issue