2018-02-04 19:54:51 +00:00
|
|
|
package payload
|
|
|
|
|
|
|
|
import (
|
2021-02-05 21:06:01 +00:00
|
|
|
"errors"
|
2020-08-06 14:44:08 +00:00
|
|
|
"fmt"
|
|
|
|
|
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 {
|
2021-03-25 18:46:52 +00:00
|
|
|
Hdrs []*block.Header
|
2018-02-04 19:54:51 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Users can at most request 2k headers.
|
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
|
|
|
)
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// ErrTooManyHeaders is an error returned when too many headers have been 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
|
|
|
|
2021-02-05 21:06:01 +00:00
|
|
|
// ErrNoHeaders is returned for zero-elements Headers payload which is considered to be invalid.
|
|
|
|
var ErrNoHeaders = errors.New("no headers (zero length array)")
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// DecodeBinary implements the Serializable interface.
|
2019-09-16 16:31:49 +00:00
|
|
|
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
|
|
|
|
2021-02-05 21:06:01 +00:00
|
|
|
if br.Err == nil && lenHeaders == 0 {
|
|
|
|
br.Err = ErrNoHeaders
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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{}
|
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
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// EncodeBinary implements the Serializable interface.
|
2019-09-16 16:31:49 +00:00
|
|
|
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
|
|
|
}
|