2020-02-11 16:59:21 +00:00
|
|
|
package waku
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"math"
|
|
|
|
"reflect"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
|
|
)
|
|
|
|
|
2020-04-07 17:24:34 +00:00
|
|
|
type statusOptionKey uint
|
|
|
|
|
|
|
|
var (
|
|
|
|
defaultMinPoW = math.Float64bits(0.001)
|
|
|
|
idxFieldKey = map[int]statusOptionKey{
|
|
|
|
0: 0,
|
|
|
|
1: 1,
|
|
|
|
2: 2,
|
|
|
|
3: 3,
|
|
|
|
4: 4,
|
|
|
|
5: 5,
|
|
|
|
}
|
|
|
|
keyFieldIdx = map[statusOptionKey]int{
|
|
|
|
0: 0,
|
|
|
|
1: 1,
|
|
|
|
2: 2,
|
|
|
|
3: 3,
|
|
|
|
4: 4,
|
|
|
|
5: 5,
|
|
|
|
}
|
|
|
|
)
|
2020-04-07 12:44:15 +00:00
|
|
|
|
2020-02-11 16:59:21 +00:00
|
|
|
// statusOptions defines additional information shared between peers
|
|
|
|
// during the handshake.
|
|
|
|
// There might be more options provided then fields in statusOptions
|
|
|
|
// and they should be ignored during deserialization to stay forward compatible.
|
|
|
|
// In the case of RLP, options should be serialized to an array of tuples
|
|
|
|
// where the first item is a field name and the second is a RLP-serialized value.
|
|
|
|
type statusOptions struct {
|
2020-02-21 14:48:53 +00:00
|
|
|
PoWRequirement *uint64 `rlp:"key=0"` // RLP does not support float64 natively
|
2020-02-11 16:59:21 +00:00
|
|
|
BloomFilter []byte `rlp:"key=1"`
|
2020-02-21 14:48:53 +00:00
|
|
|
LightNodeEnabled *bool `rlp:"key=2"`
|
|
|
|
ConfirmationsEnabled *bool `rlp:"key=3"`
|
|
|
|
RateLimits *RateLimits `rlp:"key=4"`
|
2020-02-11 16:59:21 +00:00
|
|
|
TopicInterest []TopicType `rlp:"key=5"`
|
|
|
|
}
|
|
|
|
|
2020-02-21 14:48:53 +00:00
|
|
|
// WithDefaults adds the default values for a given peer.
|
|
|
|
// This are not the host default values, but the default values that ought to
|
|
|
|
// be used when receiving from an update from a peer.
|
|
|
|
func (o statusOptions) WithDefaults() statusOptions {
|
|
|
|
if o.PoWRequirement == nil {
|
|
|
|
o.PoWRequirement = &defaultMinPoW
|
|
|
|
}
|
|
|
|
|
|
|
|
if o.LightNodeEnabled == nil {
|
|
|
|
lightNodeEnabled := false
|
|
|
|
o.LightNodeEnabled = &lightNodeEnabled
|
|
|
|
}
|
|
|
|
|
|
|
|
if o.ConfirmationsEnabled == nil {
|
|
|
|
confirmationsEnabled := false
|
|
|
|
o.ConfirmationsEnabled = &confirmationsEnabled
|
|
|
|
}
|
|
|
|
|
|
|
|
if o.RateLimits == nil {
|
|
|
|
o.RateLimits = &RateLimits{}
|
|
|
|
}
|
|
|
|
|
|
|
|
if o.BloomFilter == nil {
|
|
|
|
o.BloomFilter = MakeFullNodeBloom()
|
|
|
|
}
|
|
|
|
|
|
|
|
return o
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o statusOptions) PoWRequirementF() *float64 {
|
|
|
|
if o.PoWRequirement == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
result := math.Float64frombits(*o.PoWRequirement)
|
|
|
|
return &result
|
2020-02-11 16:59:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (o *statusOptions) SetPoWRequirementFromF(val float64) {
|
2020-02-21 14:48:53 +00:00
|
|
|
requirement := math.Float64bits(val)
|
|
|
|
o.PoWRequirement = &requirement
|
2020-02-11 16:59:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (o statusOptions) EncodeRLP(w io.Writer) error {
|
|
|
|
v := reflect.ValueOf(o)
|
2020-02-21 14:48:53 +00:00
|
|
|
var optionsList []interface{}
|
2020-02-11 16:59:21 +00:00
|
|
|
for i := 0; i < v.NumField(); i++ {
|
2020-02-21 14:48:53 +00:00
|
|
|
field := v.Field(i)
|
|
|
|
if !field.IsNil() {
|
|
|
|
value := field.Interface()
|
2020-04-07 17:24:34 +00:00
|
|
|
key, ok := idxFieldKey[i]
|
2020-02-21 14:48:53 +00:00
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if value != nil {
|
|
|
|
optionsList = append(optionsList, []interface{}{key, value})
|
|
|
|
}
|
2020-02-11 16:59:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return rlp.Encode(w, optionsList)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *statusOptions) DecodeRLP(s *rlp.Stream) error {
|
|
|
|
_, err := s.List()
|
|
|
|
if err != nil {
|
2020-02-21 14:48:53 +00:00
|
|
|
return fmt.Errorf("expected an outer list: %v", err)
|
2020-02-11 16:59:21 +00:00
|
|
|
}
|
|
|
|
v := reflect.ValueOf(o)
|
|
|
|
|
|
|
|
loop:
|
|
|
|
for {
|
|
|
|
_, err := s.List()
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
// continue to decode a key
|
|
|
|
case rlp.EOL:
|
|
|
|
break loop
|
|
|
|
default:
|
2020-02-21 14:48:53 +00:00
|
|
|
return fmt.Errorf("expected an inner list: %v", err)
|
2020-02-11 16:59:21 +00:00
|
|
|
}
|
2020-04-07 17:24:34 +00:00
|
|
|
|
2020-04-07 12:44:15 +00:00
|
|
|
var key statusOptionKey
|
2020-02-11 16:59:21 +00:00
|
|
|
if err := s.Decode(&key); err != nil {
|
2020-02-21 14:48:53 +00:00
|
|
|
return fmt.Errorf("invalid key: %v", err)
|
2020-02-11 16:59:21 +00:00
|
|
|
}
|
2020-04-07 17:24:34 +00:00
|
|
|
|
|
|
|
// TODO encode as a string and parse in case incoming is encoded as string
|
|
|
|
|
|
|
|
// TODO once key data type is determined record the data type against the key value
|
|
|
|
|
|
|
|
// TODO this will mean that using a static keyFieldIdx mapping won't work as there is no way to know for any
|
|
|
|
// incoming stream what data types the field keys will be defined as. Therefore the keyFieldIdx would need to
|
|
|
|
// be associated with the instantiated struct.
|
|
|
|
|
2020-02-11 16:59:21 +00:00
|
|
|
// Skip processing if a key does not exist.
|
|
|
|
// It might happen when there is a new peer
|
|
|
|
// which supports a new option with
|
|
|
|
// a higher index.
|
2020-04-07 17:24:34 +00:00
|
|
|
idx, ok := keyFieldIdx[key]
|
2020-02-11 16:59:21 +00:00
|
|
|
if !ok {
|
|
|
|
// Read the rest of the list items and dump them.
|
|
|
|
_, err := s.Raw()
|
|
|
|
if err != nil {
|
2020-04-07 12:44:15 +00:00
|
|
|
return fmt.Errorf("failed to read the value of key %d: %v", key, err)
|
2020-02-11 16:59:21 +00:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := s.Decode(v.Elem().Field(idx).Addr().Interface()); err != nil {
|
2020-04-07 12:44:15 +00:00
|
|
|
return fmt.Errorf("failed to decode an option %d: %v", key, err)
|
2020-02-11 16:59:21 +00:00
|
|
|
}
|
|
|
|
if err := s.ListEnd(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.ListEnd()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o statusOptions) Validate() error {
|
|
|
|
if len(o.TopicInterest) > 1000 {
|
|
|
|
return errors.New("topic interest is limited by 1000 items")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|