network: drop checksums from messages

Follow neo-project/neo#710 changes.
This commit is contained in:
Roman Khimov 2020-04-19 23:40:31 +03:00
parent 8556c828fa
commit 9a67bfac0b

View file

@ -1,15 +1,12 @@
package network package network
import ( import (
"encoding/binary"
"errors"
"fmt" "fmt"
"github.com/nspcc-dev/neo-go/pkg/config" "github.com/nspcc-dev/neo-go/pkg/config"
"github.com/nspcc-dev/neo-go/pkg/consensus" "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/block"
"github.com/nspcc-dev/neo-go/pkg/core/transaction" "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/io"
"github.com/nspcc-dev/neo-go/pkg/network/payload" "github.com/nspcc-dev/neo-go/pkg/network/payload"
) )
@ -20,10 +17,6 @@ const (
cmdSize = 12 cmdSize = 12
) )
var (
errChecksumMismatch = errors.New("checksum mismatch")
)
// Message is the complete message send between nodes. // Message is the complete message send between nodes.
type Message struct { type Message struct {
// NetMode of the node that sends this message. // NetMode of the node that sends this message.
@ -36,10 +29,6 @@ type Message struct {
// Length of the payload. // Length of the payload.
Length uint32 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 send with the message.
Payload payload.Payload Payload payload.Payload
} }
@ -74,8 +63,7 @@ const (
// NewMessage returns a new message with the given payload. // NewMessage returns a new message with the given payload.
func NewMessage(magic config.NetMode, cmd CommandType, p payload.Payload) *Message { func NewMessage(magic config.NetMode, cmd CommandType, p payload.Payload) *Message {
var ( var (
size uint32 size uint32
checksum []byte
) )
if p != nil { if p != nil {
@ -86,17 +74,13 @@ func NewMessage(magic config.NetMode, cmd CommandType, p payload.Payload) *Messa
} }
b := buf.Bytes() b := buf.Bytes()
size = uint32(len(b)) size = uint32(len(b))
checksum = hash.Checksum(b)
} else {
checksum = hash.Checksum([]byte{})
} }
return &Message{ return &Message{
Magic: magic, Magic: magic,
Command: cmdToByteArray(cmd), Command: cmdToByteArray(cmd),
Length: size, Length: size,
Payload: p, Payload: p,
Checksum: binary.LittleEndian.Uint32(checksum[:4]),
} }
} }
@ -152,7 +136,6 @@ func (m *Message) Decode(br *io.BinReader) error {
m.Magic = config.NetMode(br.ReadU32LE()) m.Magic = config.NetMode(br.ReadU32LE())
br.ReadBytes(m.Command[:]) br.ReadBytes(m.Command[:])
m.Length = br.ReadU32LE() m.Length = br.ReadU32LE()
m.Checksum = br.ReadU32LE()
if br.Err != nil { if br.Err != nil {
return br.Err return br.Err
} }
@ -169,10 +152,6 @@ func (m *Message) decodePayload(br *io.BinReader) error {
if br.Err != nil { if br.Err != nil {
return br.Err return br.Err
} }
// Compare the checksum of the payload.
if !compareChecksum(m.Checksum, buf) {
return errChecksumMismatch
}
r := io.NewBinReaderFromBuf(buf) r := io.NewBinReaderFromBuf(buf)
var p payload.Payload var p payload.Payload
@ -215,7 +194,6 @@ func (m *Message) Encode(br *io.BinWriter) error {
br.WriteU32LE(uint32(m.Magic)) br.WriteU32LE(uint32(m.Magic))
br.WriteBytes(m.Command[:]) br.WriteBytes(m.Command[:])
br.WriteU32LE(m.Length) br.WriteU32LE(m.Length)
br.WriteU32LE(m.Checksum)
if m.Payload != nil { if m.Payload != nil {
m.Payload.EncodeBinary(br) m.Payload.EncodeBinary(br)
@ -264,9 +242,3 @@ func cmdByteArrayToString(cmd [cmdSize]byte) string {
} }
return string(buf) return string(buf)
} }
func compareChecksum(have uint32, b []byte) bool {
sum := hash.Checksum(b)
want := binary.LittleEndian.Uint32(sum)
return have == want
}