2018-02-04 19:54:51 +00:00
|
|
|
package payload
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/core"
|
2019-09-16 09:18:13 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/io"
|
2019-08-29 17:11:03 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
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 {
|
|
|
|
Hdrs []*core.Header
|
|
|
|
}
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// Users can at most request 2k header.
|
2019-08-29 17:11:03 +00:00
|
|
|
const (
|
|
|
|
maxHeadersAllowed = 2000
|
|
|
|
)
|
|
|
|
|
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-08-29 17:11:03 +00:00
|
|
|
// C# node does it silently
|
|
|
|
if lenHeaders > maxHeadersAllowed {
|
|
|
|
log.Warnf("received %d headers, capping to %d", lenHeaders, maxHeadersAllowed)
|
|
|
|
lenHeaders = maxHeadersAllowed
|
|
|
|
}
|
2018-02-04 19:54:51 +00:00
|
|
|
|
|
|
|
p.Hdrs = make([]*core.Header, lenHeaders)
|
|
|
|
|
|
|
|
for i := 0; i < int(lenHeaders); i++ {
|
|
|
|
header := &core.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-09-16 16:31:49 +00:00
|
|
|
// EncodeBinary implements Serializable interface.
|
|
|
|
func (p *Headers) EncodeBinary(bw *io.BinWriter) {
|
2019-08-28 12:43:56 +00:00
|
|
|
bw.WriteVarUint(uint64(len(p.Hdrs)))
|
|
|
|
|
2018-02-07 14:16:50 +00:00
|
|
|
for _, header := range p.Hdrs {
|
2019-09-16 16:31:49 +00:00
|
|
|
header.EncodeBinary(bw)
|
2018-02-07 14:16:50 +00:00
|
|
|
}
|
2018-02-04 19:54:51 +00:00
|
|
|
}
|