Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

les: add unit test for node state machine #7

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion les/distributor.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ type selectPeerItem struct {
}

func selectPeerWeight(i interface{}) uint64 {
return i.(*selectPeerItem).weight
return i.(selectPeerItem).weight
}

// nextRequest returns the next possible request from any peer, along with the
Expand Down
4 changes: 2 additions & 2 deletions les/lespay/client/requestbasket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func TestConvertMapping(t *testing.T) {
func TestReqValueFactor(t *testing.T) {
var ref referenceBasket
ref.basket = requestBasket{items: make([]basketItem, 4)}
for i, _ := range ref.basket.items {
for i := range ref.basket.items {
ref.basket.items[i].amount = uint64(i+1) * basketFactor
ref.basket.items[i].value = uint64(i+1) * basketFactor
}
Expand All @@ -102,7 +102,7 @@ func TestReqValueAdjustment(t *testing.T) {
cost2 := []uint64{100000, 200000, 300000}
var ref referenceBasket
ref.basket = requestBasket{items: make([]basketItem, 3)}
for i, _ := range ref.basket.items {
for i := range ref.basket.items {
ref.basket.items[i].amount = 123 * basketFactor
ref.basket.items[i].value = 123 * basketFactor
}
Expand Down
6 changes: 3 additions & 3 deletions les/lespay/client/valuetracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestValueTracker(t *testing.T) {
requestList := make([]RequestInfo, testReqTypes)
relPrices := make([]float64, testReqTypes)
totalAmount := make([]uint64, testReqTypes)
for i, _ := range requestList {
for i := range requestList {
requestList[i] = RequestInfo{Name: "testreq" + strconv.Itoa(i), InitAmount: 1, InitValue: 1}
totalAmount[i] = 1
relPrices[i] = rand.Float64() + 0.1
Expand All @@ -61,12 +61,12 @@ func TestValueTracker(t *testing.T) {
updateCosts := func(i int) {
costList := make([]uint64, testReqTypes)
baseCost := rand.Float64()*10000000 + 100000
for j, _ := range costList {
for j := range costList {
costList[j] = uint64(baseCost * relPrices[j])
}
vt.UpdateCosts(nodes[i], costList)
}
for i, _ := range nodes {
for i := range nodes {
nodes[i] = vt.Register(enode.ID{byte(i)})
updateCosts(i)
}
Expand Down
39 changes: 22 additions & 17 deletions les/serverpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ type nodeHistory struct {
totalValue float64
}

func (n *nodeHistory) EncodeRLP(w io.Writer) error {
return rlp.Encode(w, &n.dialCost)
}

func (n *nodeHistory) DecodeRLP(s *rlp.Stream) error {
return s.Decode(&n.dialCost)
}

var (
sfDiscovered = utils.NewNodeStateFlag("discovered", false, true)
sfHasValue = utils.NewNodeStateFlag("hasValue", true, false)
Expand All @@ -83,11 +91,19 @@ func newServerPool(db ethdb.Database, dbKey []byte, discovery enode.Iterator, cl
timeout: minTimeout,
quit: make(chan struct{}),
}
enrFieldId := s.ns.FieldIndex(sfiEnr)
s.nodeHistoryFieldId = s.ns.FieldIndex(sfiNodeHistory)

knownSelector := lpc.NewWrsIterator(s.ns, s.ns.StatesMask(knownSelectorRequire), s.ns.StatesMask(knownSelectorDisable), s.ns.StateMask(sfSelected), s.knownSelectWeight, enrFieldId)
stDiscovered := s.ns.StateMask(sfDiscovered)
// Register all serverpool-defined states
stDiscovered, _ := s.ns.RegisterState(sfDiscovered)
s.stHasValue, _ = s.ns.RegisterState(sfHasValue)
stSelected, _ := s.ns.RegisterState(sfSelected)
s.stDialed, _ = s.ns.RegisterState(sfDialed)
s.stConnected, _ = s.ns.RegisterState(sfConnected)
s.stRedialWait, _ = s.ns.RegisterState(sfRedialWait)

// Register all serverpool-defined node fields.
enrFieldId, _ := s.ns.RegisterField(sfiEnr)
s.nodeHistoryFieldId, _ = s.ns.RegisterField(sfiNodeHistory)

knownSelector := lpc.NewWrsIterator(s.ns, s.ns.StatesMask(knownSelectorRequire), s.ns.StatesMask(knownSelectorDisable), stSelected, s.knownSelectWeight, enrFieldId)
discEnrStored := enode.Filter(discovery, func(node *enode.Node) bool {
s.ns.UpdateState(node.ID(), stDiscovered, 0, time.Hour)
s.ns.SetField(node.ID(), enrFieldId, node.Record())
Expand All @@ -96,12 +112,9 @@ func newServerPool(db ethdb.Database, dbKey []byte, discovery enode.Iterator, cl
iter := enode.NewFairMix(time.Second)
iter.AddSource(discEnrStored)
iter.AddSource(knownSelector)

// preNegotiationFilter will be added in series with iter here when les4 is available

s.stDialed = s.ns.StateMask(sfDialed)
s.stConnected = s.ns.StateMask(sfConnected)
s.stRedialWait = s.ns.StateMask(sfRedialWait)
s.stHasValue = s.ns.StateMask(sfHasValue)
s.dialIterator = enode.Filter(iter, func(node *enode.Node) bool {
n, _ := s.ns.GetField(node.ID(), s.nodeHistoryFieldId).(*nodeHistory)
if n == nil {
Expand Down Expand Up @@ -227,11 +240,3 @@ func init() {
requestMapping[uint32(code)] = rm
}
}

func (n *nodeHistory) EncodeRLP(w io.Writer) error {
return rlp.Encode(w, &n.dialCost)
}

func (n *nodeHistory) DecodeRLP(s *rlp.Stream) error {
return s.Decode(&n.dialCost)
}
Loading