2018-02-04 19:54:51 +00:00
|
|
|
package payload
|
|
|
|
|
|
|
|
import (
|
2020-08-06 14:44:08 +00:00
|
|
|
"fmt"
|
|
|
|
|
2020-06-18 09:00:51 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/block"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
2018-02-04 19:54:51 +00:00
|
|
|
)
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// Headers payload.
|
2018-02-04 19:54:51 +00:00
|
|
|
type Headers struct {
|
2020-06-18 09:00:51 +00:00
|
|
|
Hdrs []*block.Header
|
|
|
|
Network netmode.Magic
|
2020-11-17 12:57:50 +00:00
|
|
|
// StateRootInHeader specifies whether header contains state root.
|
|
|
|
StateRootInHeader bool
|
2018-02-04 19:54:51 +00:00
|
|
|
}
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// Users can at most request 2k header.
|
2019-08-29 17:11:03 +00:00
|
|
|
const (
|
2019-11-29 08:08:22 +00:00
|
|
|
MaxHeadersAllowed = 2000
|
2019-08-29 17:11:03 +00:00
|
|
|
)
|
|
|
|
|
2019-12-30 12:38:23 +00:00
|
|
|
// ErrTooManyHeaders is an error returned when too many headers were received.
|
2020-08-06 14:44:08 +00:00
|
|
|
var ErrTooManyHeaders = fmt.Errorf("too many headers were received (max: %d)", MaxHeadersAllowed)
|
2019-12-30 12:38:23 +00:00
|
|
|
|
2019-09-16 16:31:49 +00:00
|
|
|
// DecodeBinary implements Serializable interface.
|
|
|
|
func (p *Headers) DecodeBinary(br *io.BinReader) {
|
2019-08-28 12:43:56 +00:00
|
|
|
lenHeaders := br.ReadVarUint()
|
2019-09-16 16:31:49 +00:00
|
|
|
|
2019-12-30 12:38:23 +00:00
|
|
|
var limitExceeded bool
|
|
|
|
|
2019-08-29 17:11:03 +00:00
|
|
|
// C# node does it silently
|
2019-12-30 12:38:23 +00:00
|
|
|
if limitExceeded = lenHeaders > MaxHeadersAllowed; limitExceeded {
|
2019-11-29 08:08:22 +00:00
|
|
|
lenHeaders = MaxHeadersAllowed
|
2019-08-29 17:11:03 +00:00
|
|
|
}
|
2018-02-04 19:54:51 +00:00
|
|
|
|
2020-01-14 12:32:07 +00:00
|
|
|
p.Hdrs = make([]*block.Header, lenHeaders)
|
2018-02-04 19:54:51 +00:00
|
|
|
|
|
|
|
for i := 0; i < int(lenHeaders); i++ {
|
2020-01-14 12:32:07 +00:00
|
|
|
header := &block.Header{}
|
2020-06-18 09:00:51 +00:00
|
|
|
header.Network = p.Network
|
2020-11-17 12:57:50 +00:00
|
|
|
header.StateRootEnabled = p.StateRootInHeader
|
2019-09-16 16:31:49 +00:00
|
|
|
header.DecodeBinary(br)
|
2018-02-04 19:54:51 +00:00
|
|
|
p.Hdrs[i] = header
|
|
|
|
}
|
2019-12-30 12:38:23 +00:00
|
|
|
|
|
|
|
if br.Err == nil && limitExceeded {
|
|
|
|
br.Err = ErrTooManyHeaders
|
|
|
|
}
|
2018-02-04 19:54:51 +00:00
|
|
|
}
|
|
|
|
|
2019-09-16 16:31:49 +00:00
|
|
|
// EncodeBinary implements Serializable interface.
|
|
|
|
func (p *Headers) EncodeBinary(bw *io.BinWriter) {
|
2019-11-13 07:36:29 +00:00
|
|
|
bw.WriteArray(p.Hdrs)
|
2018-02-04 19:54:51 +00:00
|
|
|
}
|