mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-11-26 19:42:23 +00:00
network: drop checksums from messages
Follow neo-project/neo#710 changes.
This commit is contained in:
parent
8556c828fa
commit
9a67bfac0b
1 changed files with 5 additions and 33 deletions
|
@ -1,15 +1,12 @@
|
|||
package network
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/nspcc-dev/neo-go/pkg/config"
|
||||
"github.com/nspcc-dev/neo-go/pkg/consensus"
|
||||
"github.com/nspcc-dev/neo-go/pkg/core/block"
|
||||
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
||||
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
|
||||
"github.com/nspcc-dev/neo-go/pkg/io"
|
||||
"github.com/nspcc-dev/neo-go/pkg/network/payload"
|
||||
)
|
||||
|
@ -20,10 +17,6 @@ const (
|
|||
cmdSize = 12
|
||||
)
|
||||
|
||||
var (
|
||||
errChecksumMismatch = errors.New("checksum mismatch")
|
||||
)
|
||||
|
||||
// Message is the complete message send between nodes.
|
||||
type Message struct {
|
||||
// NetMode of the node that sends this message.
|
||||
|
@ -36,10 +29,6 @@ type Message struct {
|
|||
// Length of the payload.
|
||||
Length uint32
|
||||
|
||||
// Checksum is the first 4 bytes of the value that two times SHA256
|
||||
// hash of the payload.
|
||||
Checksum uint32
|
||||
|
||||
// Payload send with the message.
|
||||
Payload payload.Payload
|
||||
}
|
||||
|
@ -74,8 +63,7 @@ const (
|
|||
// NewMessage returns a new message with the given payload.
|
||||
func NewMessage(magic config.NetMode, cmd CommandType, p payload.Payload) *Message {
|
||||
var (
|
||||
size uint32
|
||||
checksum []byte
|
||||
size uint32
|
||||
)
|
||||
|
||||
if p != nil {
|
||||
|
@ -86,17 +74,13 @@ func NewMessage(magic config.NetMode, cmd CommandType, p payload.Payload) *Messa
|
|||
}
|
||||
b := buf.Bytes()
|
||||
size = uint32(len(b))
|
||||
checksum = hash.Checksum(b)
|
||||
} else {
|
||||
checksum = hash.Checksum([]byte{})
|
||||
}
|
||||
|
||||
return &Message{
|
||||
Magic: magic,
|
||||
Command: cmdToByteArray(cmd),
|
||||
Length: size,
|
||||
Payload: p,
|
||||
Checksum: binary.LittleEndian.Uint32(checksum[:4]),
|
||||
Magic: magic,
|
||||
Command: cmdToByteArray(cmd),
|
||||
Length: size,
|
||||
Payload: p,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -152,7 +136,6 @@ func (m *Message) Decode(br *io.BinReader) error {
|
|||
m.Magic = config.NetMode(br.ReadU32LE())
|
||||
br.ReadBytes(m.Command[:])
|
||||
m.Length = br.ReadU32LE()
|
||||
m.Checksum = br.ReadU32LE()
|
||||
if br.Err != nil {
|
||||
return br.Err
|
||||
}
|
||||
|
@ -169,10 +152,6 @@ func (m *Message) decodePayload(br *io.BinReader) error {
|
|||
if br.Err != nil {
|
||||
return br.Err
|
||||
}
|
||||
// Compare the checksum of the payload.
|
||||
if !compareChecksum(m.Checksum, buf) {
|
||||
return errChecksumMismatch
|
||||
}
|
||||
|
||||
r := io.NewBinReaderFromBuf(buf)
|
||||
var p payload.Payload
|
||||
|
@ -215,7 +194,6 @@ func (m *Message) Encode(br *io.BinWriter) error {
|
|||
br.WriteU32LE(uint32(m.Magic))
|
||||
br.WriteBytes(m.Command[:])
|
||||
br.WriteU32LE(m.Length)
|
||||
br.WriteU32LE(m.Checksum)
|
||||
if m.Payload != nil {
|
||||
m.Payload.EncodeBinary(br)
|
||||
|
||||
|
@ -264,9 +242,3 @@ func cmdByteArrayToString(cmd [cmdSize]byte) string {
|
|||
}
|
||||
return string(buf)
|
||||
}
|
||||
|
||||
func compareChecksum(have uint32, b []byte) bool {
|
||||
sum := hash.Checksum(b)
|
||||
want := binary.LittleEndian.Uint32(sum)
|
||||
return have == want
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue