Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
Geth-Modification
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
张蕾
Geth-Modification
Commits
422604b4
Commit
422604b4
authored
Nov 18, 2019
by
Felföldi Zsolt
Committed by
Felix Lange
Nov 18, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
les: rename UpdateBalance to AddBalance and simplify return format (#20304)
parent
57d69762
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
28 additions
and
31 deletions
+28
-31
web3ext.go
internal/web3ext/web3ext.go
+2
-2
api.go
les/api.go
+4
-7
balance.go
les/balance.go
+10
-10
clientpool.go
les/clientpool.go
+2
-2
clientpool_test.go
les/clientpool_test.go
+10
-10
No files found.
internal/web3ext/web3ext.go
View file @
422604b4
...
@@ -824,8 +824,8 @@ web3._extend({
...
@@ -824,8 +824,8 @@ web3._extend({
params: 1
params: 1
}),
}),
new web3._extend.Method({
new web3._extend.Method({
name: '
update
Balance',
name: '
add
Balance',
call: 'les_
update
Balance',
call: 'les_
add
Balance',
params: 3
params: 3
}),
}),
],
],
...
...
les/api.go
View file @
422604b4
...
@@ -170,14 +170,11 @@ func (api *PrivateLightServerAPI) setParams(params map[string]interface{}, clien
...
@@ -170,14 +170,11 @@ func (api *PrivateLightServerAPI) setParams(params map[string]interface{}, clien
return
return
}
}
//
Update
Balance updates the balance of a client (either overwrites it or adds to it).
//
Add
Balance updates the balance of a client (either overwrites it or adds to it).
// It also updates the balance meta info string.
// It also updates the balance meta info string.
func
(
api
*
PrivateLightServerAPI
)
UpdateBalance
(
id
enode
.
ID
,
value
int64
,
meta
string
)
(
map
[
string
]
uint64
,
error
)
{
func
(
api
*
PrivateLightServerAPI
)
AddBalance
(
id
enode
.
ID
,
value
int64
,
meta
string
)
([
2
]
uint64
,
error
)
{
oldBalance
,
newBalance
,
err
:=
api
.
server
.
clientPool
.
updateBalance
(
id
,
value
,
meta
)
oldBalance
,
newBalance
,
err
:=
api
.
server
.
clientPool
.
addBalance
(
id
,
value
,
meta
)
m
:=
make
(
map
[
string
]
uint64
)
return
[
2
]
uint64
{
oldBalance
,
newBalance
},
err
m
[
"old"
]
=
oldBalance
m
[
"new"
]
=
newBalance
return
m
,
err
}
}
// SetClientParams sets client parameters for all clients listed in the ids list
// SetClientParams sets client parameters for all clients listed in the ids list
...
...
les/balance.go
View file @
422604b4
...
@@ -80,7 +80,7 @@ func (bt *balanceTracker) stop(now mclock.AbsTime) {
...
@@ -80,7 +80,7 @@ func (bt *balanceTracker) stop(now mclock.AbsTime) {
defer
bt
.
lock
.
Unlock
()
defer
bt
.
lock
.
Unlock
()
bt
.
stopped
=
true
bt
.
stopped
=
true
bt
.
update
Balance
(
now
)
bt
.
add
Balance
(
now
)
bt
.
negTimeFactor
=
0
bt
.
negTimeFactor
=
0
bt
.
negRequestFactor
=
0
bt
.
negRequestFactor
=
0
bt
.
timeFactor
=
0
bt
.
timeFactor
=
0
...
@@ -173,7 +173,7 @@ func (bt *balanceTracker) getPriority(now mclock.AbsTime) int64 {
...
@@ -173,7 +173,7 @@ func (bt *balanceTracker) getPriority(now mclock.AbsTime) int64 {
bt
.
lock
.
Lock
()
bt
.
lock
.
Lock
()
defer
bt
.
lock
.
Unlock
()
defer
bt
.
lock
.
Unlock
()
bt
.
update
Balance
(
now
)
bt
.
add
Balance
(
now
)
return
bt
.
balanceToPriority
(
bt
.
balance
)
return
bt
.
balanceToPriority
(
bt
.
balance
)
}
}
...
@@ -194,8 +194,8 @@ func (bt *balanceTracker) estimatedPriority(at mclock.AbsTime, addReqCost bool)
...
@@ -194,8 +194,8 @@ func (bt *balanceTracker) estimatedPriority(at mclock.AbsTime, addReqCost bool)
return
bt
.
balanceToPriority
(
bt
.
reducedBalance
(
at
,
avgReqCost
))
return
bt
.
balanceToPriority
(
bt
.
reducedBalance
(
at
,
avgReqCost
))
}
}
//
update
Balance updates balance based on the time factor
//
add
Balance updates balance based on the time factor
func
(
bt
*
balanceTracker
)
update
Balance
(
now
mclock
.
AbsTime
)
{
func
(
bt
*
balanceTracker
)
add
Balance
(
now
mclock
.
AbsTime
)
{
if
now
>
bt
.
lastUpdate
{
if
now
>
bt
.
lastUpdate
{
bt
.
balance
=
bt
.
reducedBalance
(
now
,
0
)
bt
.
balance
=
bt
.
reducedBalance
(
now
,
0
)
bt
.
lastUpdate
=
now
bt
.
lastUpdate
=
now
...
@@ -253,7 +253,7 @@ func (bt *balanceTracker) updateAfter(dt time.Duration) {
...
@@ -253,7 +253,7 @@ func (bt *balanceTracker) updateAfter(dt time.Duration) {
if
bt
.
callbackCount
!=
0
{
if
bt
.
callbackCount
!=
0
{
now
:=
bt
.
clock
.
Now
()
now
:=
bt
.
clock
.
Now
()
bt
.
update
Balance
(
now
)
bt
.
add
Balance
(
now
)
bt
.
checkCallbacks
(
now
)
bt
.
checkCallbacks
(
now
)
}
}
})
})
...
@@ -270,7 +270,7 @@ func (bt *balanceTracker) requestCost(cost uint64) {
...
@@ -270,7 +270,7 @@ func (bt *balanceTracker) requestCost(cost uint64) {
return
return
}
}
now
:=
bt
.
clock
.
Now
()
now
:=
bt
.
clock
.
Now
()
bt
.
update
Balance
(
now
)
bt
.
add
Balance
(
now
)
fcost
:=
float64
(
cost
)
fcost
:=
float64
(
cost
)
if
bt
.
balance
.
pos
!=
0
{
if
bt
.
balance
.
pos
!=
0
{
...
@@ -302,7 +302,7 @@ func (bt *balanceTracker) getBalance(now mclock.AbsTime) (uint64, uint64) {
...
@@ -302,7 +302,7 @@ func (bt *balanceTracker) getBalance(now mclock.AbsTime) (uint64, uint64) {
bt
.
lock
.
Lock
()
bt
.
lock
.
Lock
()
defer
bt
.
lock
.
Unlock
()
defer
bt
.
lock
.
Unlock
()
bt
.
update
Balance
(
now
)
bt
.
add
Balance
(
now
)
return
bt
.
balance
.
pos
,
bt
.
balance
.
neg
return
bt
.
balance
.
pos
,
bt
.
balance
.
neg
}
}
...
@@ -312,7 +312,7 @@ func (bt *balanceTracker) setBalance(pos, neg uint64) error {
...
@@ -312,7 +312,7 @@ func (bt *balanceTracker) setBalance(pos, neg uint64) error {
defer
bt
.
lock
.
Unlock
()
defer
bt
.
lock
.
Unlock
()
now
:=
bt
.
clock
.
Now
()
now
:=
bt
.
clock
.
Now
()
bt
.
update
Balance
(
now
)
bt
.
add
Balance
(
now
)
bt
.
balance
.
pos
=
pos
bt
.
balance
.
pos
=
pos
bt
.
balance
.
neg
=
neg
bt
.
balance
.
neg
=
neg
bt
.
checkCallbacks
(
now
)
bt
.
checkCallbacks
(
now
)
...
@@ -329,7 +329,7 @@ func (bt *balanceTracker) setFactors(neg bool, timeFactor, requestFactor float64
...
@@ -329,7 +329,7 @@ func (bt *balanceTracker) setFactors(neg bool, timeFactor, requestFactor float64
return
return
}
}
now
:=
bt
.
clock
.
Now
()
now
:=
bt
.
clock
.
Now
()
bt
.
update
Balance
(
now
)
bt
.
add
Balance
(
now
)
if
neg
{
if
neg
{
bt
.
negTimeFactor
=
timeFactor
bt
.
negTimeFactor
=
timeFactor
bt
.
negRequestFactor
=
requestFactor
bt
.
negRequestFactor
=
requestFactor
...
@@ -360,7 +360,7 @@ func (bt *balanceTracker) addCallback(id int, threshold int64, callback func())
...
@@ -360,7 +360,7 @@ func (bt *balanceTracker) addCallback(id int, threshold int64, callback func())
bt
.
callbackIndex
[
id
]
=
idx
bt
.
callbackIndex
[
id
]
=
idx
bt
.
callbacks
[
idx
]
=
balanceCallback
{
id
,
threshold
,
callback
}
bt
.
callbacks
[
idx
]
=
balanceCallback
{
id
,
threshold
,
callback
}
now
:=
bt
.
clock
.
Now
()
now
:=
bt
.
clock
.
Now
()
bt
.
update
Balance
(
now
)
bt
.
add
Balance
(
now
)
bt
.
checkCallbacks
(
now
)
bt
.
checkCallbacks
(
now
)
}
}
...
...
les/clientpool.go
View file @
422604b4
...
@@ -553,9 +553,9 @@ func (f *clientPool) getPosBalance(id enode.ID) posBalance {
...
@@ -553,9 +553,9 @@ func (f *clientPool) getPosBalance(id enode.ID) posBalance {
return
f
.
ndb
.
getOrNewPB
(
id
)
return
f
.
ndb
.
getOrNewPB
(
id
)
}
}
//
update
Balance updates the balance of a client (either overwrites it or adds to it).
//
add
Balance updates the balance of a client (either overwrites it or adds to it).
// It also updates the balance meta info string.
// It also updates the balance meta info string.
func
(
f
*
clientPool
)
update
Balance
(
id
enode
.
ID
,
amount
int64
,
meta
string
)
(
uint64
,
uint64
,
error
)
{
func
(
f
*
clientPool
)
add
Balance
(
id
enode
.
ID
,
amount
int64
,
meta
string
)
(
uint64
,
uint64
,
error
)
{
f
.
lock
.
Lock
()
f
.
lock
.
Lock
()
defer
f
.
lock
.
Unlock
()
defer
f
.
lock
.
Unlock
()
...
...
les/clientpool_test.go
View file @
422604b4
...
@@ -111,7 +111,7 @@ func testClientPool(t *testing.T, connLimit, clientCount, paidCount int, randomD
...
@@ -111,7 +111,7 @@ func testClientPool(t *testing.T, connLimit, clientCount, paidCount int, randomD
// give a positive balance to some of the peers
// give a positive balance to some of the peers
amount
:=
testClientPoolTicks
/
2
*
int64
(
time
.
Second
)
// enough for half of the simulation period
amount
:=
testClientPoolTicks
/
2
*
int64
(
time
.
Second
)
// enough for half of the simulation period
for
i
:=
0
;
i
<
paidCount
;
i
++
{
for
i
:=
0
;
i
<
paidCount
;
i
++
{
pool
.
update
Balance
(
poolTestPeer
(
i
)
.
ID
(),
amount
,
""
)
pool
.
add
Balance
(
poolTestPeer
(
i
)
.
ID
(),
amount
,
""
)
}
}
}
}
...
@@ -178,7 +178,7 @@ func TestConnectPaidClient(t *testing.T) {
...
@@ -178,7 +178,7 @@ func TestConnectPaidClient(t *testing.T) {
pool
.
setDefaultFactors
(
priceFactors
{
1
,
0
,
1
},
priceFactors
{
1
,
0
,
1
})
pool
.
setDefaultFactors
(
priceFactors
{
1
,
0
,
1
},
priceFactors
{
1
,
0
,
1
})
// Add balance for an external client and mark it as paid client
// Add balance for an external client and mark it as paid client
pool
.
update
Balance
(
poolTestPeer
(
0
)
.
ID
(),
1000
,
""
)
pool
.
add
Balance
(
poolTestPeer
(
0
)
.
ID
(),
1000
,
""
)
if
!
pool
.
connect
(
poolTestPeer
(
0
),
10
)
{
if
!
pool
.
connect
(
poolTestPeer
(
0
),
10
)
{
t
.
Fatalf
(
"Failed to connect paid client"
)
t
.
Fatalf
(
"Failed to connect paid client"
)
...
@@ -196,7 +196,7 @@ func TestConnectPaidClientToSmallPool(t *testing.T) {
...
@@ -196,7 +196,7 @@ func TestConnectPaidClientToSmallPool(t *testing.T) {
pool
.
setDefaultFactors
(
priceFactors
{
1
,
0
,
1
},
priceFactors
{
1
,
0
,
1
})
pool
.
setDefaultFactors
(
priceFactors
{
1
,
0
,
1
},
priceFactors
{
1
,
0
,
1
})
// Add balance for an external client and mark it as paid client
// Add balance for an external client and mark it as paid client
pool
.
update
Balance
(
poolTestPeer
(
0
)
.
ID
(),
1000
,
""
)
pool
.
add
Balance
(
poolTestPeer
(
0
)
.
ID
(),
1000
,
""
)
// Connect a fat paid client to pool, should reject it.
// Connect a fat paid client to pool, should reject it.
if
pool
.
connect
(
poolTestPeer
(
0
),
100
)
{
if
pool
.
connect
(
poolTestPeer
(
0
),
100
)
{
...
@@ -216,15 +216,15 @@ func TestConnectPaidClientToFullPool(t *testing.T) {
...
@@ -216,15 +216,15 @@ func TestConnectPaidClientToFullPool(t *testing.T) {
pool
.
setDefaultFactors
(
priceFactors
{
1
,
0
,
1
},
priceFactors
{
1
,
0
,
1
})
pool
.
setDefaultFactors
(
priceFactors
{
1
,
0
,
1
},
priceFactors
{
1
,
0
,
1
})
for
i
:=
0
;
i
<
10
;
i
++
{
for
i
:=
0
;
i
<
10
;
i
++
{
pool
.
update
Balance
(
poolTestPeer
(
i
)
.
ID
(),
1000000000
,
""
)
pool
.
add
Balance
(
poolTestPeer
(
i
)
.
ID
(),
1000000000
,
""
)
pool
.
connect
(
poolTestPeer
(
i
),
1
)
pool
.
connect
(
poolTestPeer
(
i
),
1
)
}
}
pool
.
update
Balance
(
poolTestPeer
(
11
)
.
ID
(),
1000
,
""
)
// Add low balance to new paid client
pool
.
add
Balance
(
poolTestPeer
(
11
)
.
ID
(),
1000
,
""
)
// Add low balance to new paid client
if
pool
.
connect
(
poolTestPeer
(
11
),
1
)
{
if
pool
.
connect
(
poolTestPeer
(
11
),
1
)
{
t
.
Fatalf
(
"Low balance paid client should be rejected"
)
t
.
Fatalf
(
"Low balance paid client should be rejected"
)
}
}
clock
.
Run
(
time
.
Second
)
clock
.
Run
(
time
.
Second
)
pool
.
update
Balance
(
poolTestPeer
(
12
)
.
ID
(),
1000000000
*
60
*
3
,
""
)
// Add high balance to new paid client
pool
.
add
Balance
(
poolTestPeer
(
12
)
.
ID
(),
1000000000
*
60
*
3
,
""
)
// Add high balance to new paid client
if
!
pool
.
connect
(
poolTestPeer
(
12
),
1
)
{
if
!
pool
.
connect
(
poolTestPeer
(
12
),
1
)
{
t
.
Fatalf
(
"High balance paid client should be accpected"
)
t
.
Fatalf
(
"High balance paid client should be accpected"
)
}
}
...
@@ -243,7 +243,7 @@ func TestPaidClientKickedOut(t *testing.T) {
...
@@ -243,7 +243,7 @@ func TestPaidClientKickedOut(t *testing.T) {
pool
.
setDefaultFactors
(
priceFactors
{
1
,
0
,
1
},
priceFactors
{
1
,
0
,
1
})
pool
.
setDefaultFactors
(
priceFactors
{
1
,
0
,
1
},
priceFactors
{
1
,
0
,
1
})
for
i
:=
0
;
i
<
10
;
i
++
{
for
i
:=
0
;
i
<
10
;
i
++
{
pool
.
update
Balance
(
poolTestPeer
(
i
)
.
ID
(),
1000000000
,
""
)
// 1 second allowance
pool
.
add
Balance
(
poolTestPeer
(
i
)
.
ID
(),
1000000000
,
""
)
// 1 second allowance
pool
.
connect
(
poolTestPeer
(
i
),
1
)
pool
.
connect
(
poolTestPeer
(
i
),
1
)
clock
.
Run
(
time
.
Millisecond
)
clock
.
Run
(
time
.
Millisecond
)
}
}
...
@@ -351,7 +351,7 @@ func TestPositiveBalanceCalculation(t *testing.T) {
...
@@ -351,7 +351,7 @@ func TestPositiveBalanceCalculation(t *testing.T) {
pool
.
setLimits
(
10
,
uint64
(
10
))
// Total capacity limit is 10
pool
.
setLimits
(
10
,
uint64
(
10
))
// Total capacity limit is 10
pool
.
setDefaultFactors
(
priceFactors
{
1
,
0
,
1
},
priceFactors
{
1
,
0
,
1
})
pool
.
setDefaultFactors
(
priceFactors
{
1
,
0
,
1
},
priceFactors
{
1
,
0
,
1
})
pool
.
update
Balance
(
poolTestPeer
(
0
)
.
ID
(),
int64
(
time
.
Minute
*
3
),
""
)
pool
.
add
Balance
(
poolTestPeer
(
0
)
.
ID
(),
int64
(
time
.
Minute
*
3
),
""
)
pool
.
connect
(
poolTestPeer
(
0
),
10
)
pool
.
connect
(
poolTestPeer
(
0
),
10
)
clock
.
Run
(
time
.
Minute
)
clock
.
Run
(
time
.
Minute
)
...
@@ -377,7 +377,7 @@ func TestDowngradePriorityClient(t *testing.T) {
...
@@ -377,7 +377,7 @@ func TestDowngradePriorityClient(t *testing.T) {
p
:=
&
poolTestPeerWithCap
{
p
:=
&
poolTestPeerWithCap
{
poolTestPeer
:
poolTestPeer
(
0
),
poolTestPeer
:
poolTestPeer
(
0
),
}
}
pool
.
update
Balance
(
p
.
ID
(),
int64
(
time
.
Minute
),
""
)
pool
.
add
Balance
(
p
.
ID
(),
int64
(
time
.
Minute
),
""
)
pool
.
connect
(
p
,
10
)
pool
.
connect
(
p
,
10
)
if
p
.
cap
!=
10
{
if
p
.
cap
!=
10
{
t
.
Fatalf
(
"The capcacity of priority peer hasn't been updated, got: %d"
,
p
.
cap
)
t
.
Fatalf
(
"The capcacity of priority peer hasn't been updated, got: %d"
,
p
.
cap
)
...
@@ -393,7 +393,7 @@ func TestDowngradePriorityClient(t *testing.T) {
...
@@ -393,7 +393,7 @@ func TestDowngradePriorityClient(t *testing.T) {
t
.
Fatalf
(
"Positive balance mismatch, want %v, got %v"
,
0
,
pb
.
value
)
t
.
Fatalf
(
"Positive balance mismatch, want %v, got %v"
,
0
,
pb
.
value
)
}
}
pool
.
update
Balance
(
poolTestPeer
(
0
)
.
ID
(),
int64
(
time
.
Minute
),
""
)
pool
.
add
Balance
(
poolTestPeer
(
0
)
.
ID
(),
int64
(
time
.
Minute
),
""
)
pb
=
pool
.
ndb
.
getOrNewPB
(
poolTestPeer
(
0
)
.
ID
())
pb
=
pool
.
ndb
.
getOrNewPB
(
poolTestPeer
(
0
)
.
ID
())
if
pb
.
value
!=
uint64
(
time
.
Minute
)
{
if
pb
.
value
!=
uint64
(
time
.
Minute
)
{
t
.
Fatalf
(
"Positive balance mismatch, want %v, got %v"
,
uint64
(
time
.
Minute
),
pb
.
value
)
t
.
Fatalf
(
"Positive balance mismatch, want %v, got %v"
,
uint64
(
time
.
Minute
),
pb
.
value
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment