From e658ade6002ffba1beb52c9441018c0c8541abe2 Mon Sep 17 00:00:00 2001 From: Juan Batiz-Benet <juan@benet.ai> Date: Wed, 20 Aug 2014 18:29:00 -0700 Subject: [PATCH] sigmoid strat placeholder --- bitswap/strategy.go | 20 ++++++++++++++++++++ bitswap/strategy_test.go | 17 +++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 bitswap/strategy.go create mode 100644 bitswap/strategy_test.go diff --git a/bitswap/strategy.go b/bitswap/strategy.go new file mode 100644 index 000000000..6eb4c562f --- /dev/null +++ b/bitswap/strategy.go @@ -0,0 +1,20 @@ +package bitswap + +import ( + "math" +) + +func probabilitySend(ratio float64) float64 { + x := 1 + math.Exp(6-3*ratio) + y := 1 / x + return 1 - y +} + +type debtRatio struct { + BytesSent uint64 + BytesRecv uint64 +} + +func (dr *debtRatio) Value() float64 { + return float64(dr.BytesSent) / float64(dr.BytesRecv+1) +} diff --git a/bitswap/strategy_test.go b/bitswap/strategy_test.go new file mode 100644 index 000000000..a8de38ad7 --- /dev/null +++ b/bitswap/strategy_test.go @@ -0,0 +1,17 @@ +package bitswap + +import ( + "testing" +) + +func TestProbabilitySendDecreasesAsRatioIncreases(t *testing.T) { + grateful := debtRatio{BytesSent: 0, BytesRecv: 10000} + pWhenGrateful := probabilitySend(grateful.Value()) + + abused := debtRatio{BytesSent: 10000, BytesRecv: 0} + pWhenAbused := probabilitySend(abused.Value()) + + if pWhenGrateful < pWhenAbused { + t.Fail() + } +} -- GitLab