1+ /*
2+ * Copyright 2023 ACINQ SAS
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+
17+ package fr .acinq .eclair .reputation
18+
19+ import fr .acinq .eclair .{MilliSatoshiLong , TimestampMilli }
20+ import fr .acinq .eclair .reputation .Reputation .ReputationConfig
21+ import org .scalatest .funsuite .AnyFunSuite
22+ import org .scalactic .Tolerance .convertNumericToPlusOrMinusWrapper
23+
24+ import java .util .UUID
25+ import scala .concurrent .duration .DurationInt
26+
27+ class ReputationSpec extends AnyFunSuite {
28+ val (uuid1, uuid2, uuid3, uuid4, uuid5, uuid6, uuid7) = (UUID .randomUUID(), UUID .randomUUID(), UUID .randomUUID(), UUID .randomUUID(), UUID .randomUUID(), UUID .randomUUID(), UUID .randomUUID())
29+
30+ test(" basic" ) {
31+ var r = Reputation .init(ReputationConfig (1000000000 msat, 30 seconds))
32+ r = r.attempt(uuid1, 10000 msat)
33+ assert(r.confidence() == 0 )
34+ r = r.record(uuid1, isSuccess = true )
35+ r = r.attempt(uuid2, 10000 msat)
36+ assert(r.confidence() == 0.5 )
37+ r = r.attempt(uuid3, 10000 msat)
38+ assert(r.confidence() === 0.333 +- 0.001 )
39+ r = r.record(uuid2, isSuccess = true )
40+ r = r.record(uuid3, isSuccess = true )
41+ r = r.attempt(uuid4, 1 msat)
42+ assert(r.confidence() === 1.0 +- 0.001 )
43+ r = r.attempt(uuid5, 90000 msat)
44+ assert(r.confidence() === 0.25 +- 0.001 )
45+ r = r.attempt(uuid6, 10000 msat)
46+ assert(r.confidence() === (3.0 / 13 ) +- 0.001 )
47+ r = r.cancel(uuid5)
48+ assert(r.confidence() === 0.75 +- 0.001 )
49+ r = r.record(uuid6, isSuccess = false )
50+ assert(r.confidence() === 0.75 +- 0.001 )
51+ r = r.attempt(uuid7, 10000 msat)
52+ assert(r.confidence() === 0.6 +- 0.001 )
53+ }
54+
55+ test(" long HTLC" ) {
56+ var r = Reputation .init(ReputationConfig (1000000000 msat, 1 second))
57+ r = r.attempt(uuid1, 100000 msat)
58+ assert(r.confidence() == 0 )
59+ r = r.record(uuid1, isSuccess = true )
60+ assert(r.confidence() == 1 )
61+ r = r.attempt(uuid2, 1000 msat, TimestampMilli (0 ))
62+ assert(r.confidence(TimestampMilli (0 )) === 0.99 +- 0.001 )
63+ assert(r.confidence(TimestampMilli (0 ) + 100 .seconds) == 0.5 )
64+ r = r.record(uuid2, isSuccess = false , now = TimestampMilli (0 ) + 100 .seconds)
65+ assert(r.confidence() == 0.5 )
66+ }
67+ }
0 commit comments