Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
V
validator
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
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
Odysseus
validator
Commits
1754c613
Commit
1754c613
authored
Mar 14, 2024
by
贾浩@五瓣科技
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update
parent
11e211a9
Changes
9
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
87 additions
and
59 deletions
+87
-59
flags.go
cmd/validator/flags.go
+11
-17
main.go
cmd/validator/main.go
+9
-17
config.go
conf/config.go
+2
-3
config.toml
config.toml
+7
-7
validator.go
contract/validator.go
+32
-1
chain.go
core/chain.go
+2
-2
quest.go
core/quest.go
+1
-0
tree.go
core/tree.go
+4
-2
validator.go
core/validator.go
+19
-10
No files found.
cmd/validator/flags.go
View file @
1754c613
...
...
@@ -15,6 +15,12 @@ var (
Value
:
"config.toml"
,
}
debugFlag
=
&
cli
.
BoolFlag
{
Name
:
"debug"
,
Usage
:
"Enable debug mode"
,
Value
:
false
,
}
dataDirFlag
=
&
cli
.
StringFlag
{
Name
:
"data-dir"
,
Aliases
:
[]
string
{
"d"
},
...
...
@@ -25,25 +31,13 @@ var (
metricsListenAddrFlag
=
&
cli
.
StringFlag
{
Name
:
"metrics-listen"
,
Usage
:
"The listen address of the metrics server"
,
Value
:
"0.0.0.0:9429"
,
}
logLevelFlag
=
&
cli
.
StringFlag
{
Name
:
"log-level"
,
Usage
:
"The log level"
,
Value
:
"info"
,
}
grpcListenAddrFlag
=
&
cli
.
StringFlag
{
Name
:
"grpc-listen"
,
Usage
:
"The listen address of the grpc server"
,
Value
:
"0.0.0.0:9430"
,
Value
:
"0.0.0.0:20010"
,
}
rpcListenAddrFlag
=
&
cli
.
StringFlag
{
Name
:
"rpc-listen"
,
Usage
:
"The listen address of the jsonrpc server"
,
Value
:
"0.0.0.0:
9431
"
,
Value
:
"0.0.0.0:
20012
"
,
}
privateKeyFlag
=
&
cli
.
StringFlag
{
...
...
@@ -67,9 +61,9 @@ var (
Usage
:
"The address of the reward contract"
,
}
commit
Time
Flag
=
&
cli
.
IntFlag
{
Name
:
"commit-
time
"
,
Usage
:
"The
time to commit the proof
"
,
commit
Offset
Flag
=
&
cli
.
IntFlag
{
Name
:
"commit-
offset
"
,
Usage
:
"The
offset of the commit time, based on UTC, in seconds
"
,
}
)
...
...
cmd/validator/main.go
View file @
1754c613
...
...
@@ -19,14 +19,13 @@ var (
configFileFlag
,
dataDirFlag
,
metricsListenAddrFlag
,
logLevelFlag
,
grpcListenAddrFlag
,
debugFlag
,
rpcListenAddrFlag
,
privateKeyFlag
,
chainRPCFlag
,
storeContractFlag
,
validatorContractFlag
,
commit
Time
Flag
,
commit
Offset
Flag
,
questHostFlag
,
questPortFlag
,
questUserFlag
,
...
...
@@ -56,18 +55,20 @@ func main() {
func
run
(
ctx
*
cli
.
Context
)
{
cfg
:=
&
conf
.
Config
{
MetricsListenAddr
:
ctx
.
String
(
metricsListenAddrFlag
.
Name
),
LogLevel
:
ctx
.
String
(
logLevelFlag
.
Name
),
GRPCListenAddr
:
ctx
.
String
(
grpcListenAddrFlag
.
Name
),
Debug
:
ctx
.
Bool
(
debugFlag
.
Name
),
RPCListenAddr
:
ctx
.
String
(
rpcListenAddrFlag
.
Name
),
PrivateKey
:
ctx
.
String
(
privateKeyFlag
.
Name
),
ChainRPC
:
ctx
.
String
(
chainRPCFlag
.
Name
),
StoreContract
:
ctx
.
String
(
storeContractFlag
.
Name
),
ValidatorContract
:
ctx
.
String
(
validatorContractFlag
.
Name
),
DataDir
:
ctx
.
String
(
dataDirFlag
.
Name
),
CommitTime
:
ctx
.
Int
(
commitTimeFlag
.
Name
),
CommitOffset
:
ctx
.
Int
(
commitOffsetFlag
.
Name
),
}
if
cfg
.
Debug
{
log
.
SetLevel
(
log
.
DebugLevel
)
}
setLogLevel
(
cfg
.
LogLevel
)
runMetrics
(
cfg
.
MetricsListenAddr
)
qCfg
:=
&
conf
.
QuestConfig
{
...
...
@@ -76,6 +77,7 @@ func run(ctx *cli.Context) {
User
:
ctx
.
String
(
questUserFlag
.
Name
),
Password
:
ctx
.
String
(
questPassFlag
.
Name
),
Database
:
ctx
.
String
(
questDBFlag
.
Name
),
Debug
:
cfg
.
Debug
,
}
q
:=
quest
.
NewQuest
(
qCfg
)
...
...
@@ -85,16 +87,6 @@ func run(ctx *cli.Context) {
select
{}
}
func
setLogLevel
(
level
string
)
{
_level
,
err
:=
log
.
ParseLevel
(
level
)
if
err
!=
nil
{
log
.
WithError
(
err
)
.
Error
(
"failed to parse log level"
)
return
}
log
.
WithField
(
"log level"
,
_level
)
.
Info
(
"set log level"
)
log
.
SetLevel
(
_level
)
}
func
runMetrics
(
listen
string
)
{
http
.
Handle
(
"/metrics"
,
promhttp
.
Handler
())
log
.
WithField
(
"listen"
,
listen
)
.
Info
(
"start prometheus metrics server"
)
...
...
conf/config.go
View file @
1754c613
...
...
@@ -2,8 +2,7 @@ package conf
type
Config
struct
{
MetricsListenAddr
string
LogLevel
string
GRPCListenAddr
string
Debug
bool
RPCListenAddr
string
ChainRPC
string
PrivateKey
string
...
...
@@ -11,7 +10,7 @@ type Config struct {
ValidatorContract
string
RewardContract
string
DataDir
string
Commit
Time
int
Commit
Offset
int
}
type
QuestConfig
struct
{
...
...
config.toml
View file @
1754c613
log-level
=
"debug"
debug
=
true
metrics-listen
=
"0.0.0.0:20010
"
data-dir
=
"./data
"
grpc-listen
=
"0.0.0.0:20011
"
metrics-listen
=
"0.0.0.0:20010
"
rpc-listen
=
"0.0.0.0:20012"
chain-rpc
=
"https://ethereum-holesky-rpc.publicnode.com"
private-key
=
"529f4efb80ac534f17d873104c71881c0970dbd5a886f183f63c5c6bb7a1fcd9"
chain-rpc
=
"https://ethereum-holesky-rpc.publicnode.com"
store-contract
=
"0x7Cd36Bc2a477f60A14f08442179B2f626bE026Ea"
witness-contract
=
"0x60376A7A4F5013CCca347A9B320D0b8dD57D87F4
"
validator-contract
=
"0x6c77B761DC30e7844D40C8CA681731db8Ee61Cbd
"
commit-
time
=
3600
# utc + n seconds
commit-
offset
=
3600
# utc + n seconds
quest-host
=
"43.198.252.255"
...
...
contract/validator.go
View file @
1754c613
This diff is collapsed.
Click to expand it.
core/chain.go
View file @
1754c613
...
...
@@ -77,10 +77,10 @@ func (r *ChainRPC) GetNMAddresses() (addrs []common.Address, err error) {
}
func
(
r
*
ChainRPC
)
GetWorkloadThreshold
(
totalWorkload
uint64
)
(
threshold
*
big
.
Int
,
err
error
)
{
// return r.storageContract.GetWorkloadPercent(nil)
return
big
.
NewInt
(
1
),
nil
return
r
.
validatorContract
.
GetWorkloadThreshold
(
nil
,
big
.
NewInt
(
int64
(
totalWorkload
)))
}
// SubmitProofs 调用合约提交root
func
(
r
*
ChainRPC
)
SubmitProofs
(
dateTimestamp
int64
,
merkleSumTreeRoot
,
merkleTreeRoot
common
.
Hash
)
(
txHash
common
.
Hash
,
err
error
)
{
ctx
,
cancel
:=
context
.
WithTimeout
(
context
.
Background
(),
time
.
Minute
)
defer
cancel
()
...
...
core/quest.go
View file @
1754c613
...
...
@@ -9,6 +9,7 @@ import (
log
"github.com/sirupsen/logrus"
)
// LoadPendingProofs 从questdb读取当天proofs并验证
func
(
v
*
Validator
)
LoadPendingProofs
(
startTimestamp
,
endTimestamp
int64
)
{
defaultLimit
:=
100
lastTaskID
:=
""
...
...
core/tree.go
View file @
1754c613
...
...
@@ -16,7 +16,7 @@ import (
"github.com/syndtr/goleveldb/leveldb/errors"
)
// CommitMST
commit workload of per day
// CommitMST
merkle sum tree 保存当天的workload
func
(
v
*
Validator
)
CommitMST
(
proofMap
map
[
common
.
Address
]
*
validatorv1
.
ValidatedProof
)
(
root
common
.
Hash
,
sum
*
big
.
Int
,
err
error
)
{
if
len
(
proofMap
)
==
0
{
return
common
.
Hash
{},
big
.
NewInt
(
0
),
nil
...
...
@@ -71,7 +71,7 @@ func (v *Validator) CommitMST(proofMap map[common.Address]*validatorv1.Validated
return
rootNode
.
Value
.
Hash
,
rootNode
.
Value
.
BigValue
,
nil
}
// CommitMT
commit all workload
// CommitMT
merkle tree 保存 获取到的总余额(已提取+未提取)
func
(
v
*
Validator
)
CommitMT
(
objects
[]
*
validatorv1
.
MinerObject
)
(
root
common
.
Hash
,
err
error
)
{
if
len
(
objects
)
==
0
{
return
common
.
Hash
{},
nil
...
...
@@ -120,6 +120,7 @@ func (v *Validator) CommitMT(objects []*validatorv1.MinerObject) (root common.Ha
return
}
// LoadMerkleTree 从leveldb加载merkle tree
func
(
v
*
Validator
)
LoadMerkleTree
(
date
string
)
(
ok
bool
)
{
if
date
==
""
{
return
false
...
...
@@ -156,6 +157,7 @@ func (v *Validator) LoadMerkleTree(date string) (ok bool) {
return
true
}
// LoadMerkleSumTree 从leveldb加载merkle sum tree
func
(
v
*
Validator
)
LoadMerkleSumTree
(
date
string
)
(
ok
bool
)
{
if
date
==
""
{
return
false
...
...
core/validator.go
View file @
1754c613
...
...
@@ -85,9 +85,6 @@ func RunValidator(q *quest.Quest, cfg *conf.Config) *Validator {
go
v
.
UpdateGlobalWorkloadJob
()
<-
time
.
After
(
time
.
Second
*
3
)
go
v
.
Ticker
()
// go v.Mock()
// go v.ProcessDay()
return
v
}
...
...
@@ -114,8 +111,14 @@ func (v *Validator) AddPendingProof(miner common.Address, proof *validatorv1.Val
})
.
Debug
(
"add validated proof"
)
}
// ProcessDayJob 每日任务
func
(
v
*
Validator
)
ProcessDayJob
()
{
log
.
Debugln
(
"process day job"
,
v
.
yesterdayTimestamp
(),
v
.
todayTimestamp
())
log
.
WithFields
(
log
.
Fields
{
"today"
:
v
.
todayString
(),
"today timestamp"
:
v
.
todayTimestamp
(),
"yesterday"
:
v
.
yesterdayString
(),
"yesterday timestamp"
:
v
.
yesterdayTimestamp
(),
})
.
Debug
(
"process day job"
)
v
.
LoadPendingProofs
(
v
.
yesterdayTimestamp
(),
v
.
todayTimestamp
())
v
.
date
=
v
.
yesterdayString
()
dayProof
:=
v
.
Commit
()
...
...
@@ -149,6 +152,7 @@ func (v *Validator) ProcessDayJob() {
log
.
Info
(
"process day done"
)
}
// Commit statedb提交
func
(
v
*
Validator
)
Commit
()
(
dayProofs
map
[
common
.
Address
]
*
validatorv1
.
ValidatedProof
)
{
st
:=
time
.
Now
()
proof
,
totalWorkload
:=
v
.
RefreshPendingProof
()
...
...
@@ -157,6 +161,7 @@ func (v *Validator) Commit() (dayProofs map[common.Address]*validatorv1.Validate
log
.
WithError
(
err
)
.
Error
(
"failed to get workload threshold"
)
return
}
log
.
WithField
(
"balance_per_workload"
,
balancePerWorkload
.
String
())
.
Info
(
"get workload threshold"
)
for
miner
,
proof
:=
range
proof
{
balance
:=
big
.
NewInt
(
0
)
.
Mul
(
balancePerWorkload
,
big
.
NewInt
(
int64
(
proof
.
Workload
)))
...
...
@@ -214,18 +219,23 @@ func (v *Validator) RefreshPendingProof() (proof map[common.Address]*validatorv1
}
func
(
v
*
Validator
)
Ticker
()
{
executionTime
:=
time
.
Date
(
time
.
Now
()
.
Year
(),
time
.
Now
()
.
Month
(),
time
.
Now
()
.
Day
(),
0
,
0
,
v
.
cfg
.
CommitTime
,
0
,
time
.
UTC
)
.
Add
(
v
.
duration
())
// executionTime := time.Now().Add(v.duration())
executionTime
:=
time
.
Now
()
.
Add
(
v
.
duration
())
waitTime
:=
executionTime
.
Sub
(
time
.
Now
())
timer
:=
time
.
NewTimer
(
waitTime
)
log
.
WithField
(
"wait_time"
,
waitTime
.
String
())
.
Info
(
"prepare commit task"
)
log
.
WithFields
(
log
.
Fields
{
"execution_time"
:
executionTime
.
String
(),
"wait_time"
:
waitTime
.
String
(),
})
.
Info
(
"prepare commit task"
)
for
{
<-
timer
.
C
v
.
ProcessDayJob
()
executionTime
=
executionTime
.
Add
(
v
.
duration
())
waitTime
=
executionTime
.
Sub
(
time
.
Now
())
timer
.
Reset
(
waitTime
)
log
.
WithField
(
"wait time"
,
waitTime
.
String
())
.
Info
(
"prepare commit task"
)
log
.
WithFields
(
log
.
Fields
{
"execution_time"
:
executionTime
.
String
(),
"wait_time"
:
waitTime
.
String
(),
})
.
Info
(
"prepare commit task"
)
}
}
...
...
@@ -275,8 +285,7 @@ func (v *Validator) yesterdayString() string {
func
(
v
*
Validator
)
todayTimestamp
()
int64
{
now
:=
time
.
Now
()
.
UTC
()
// today := time.Date(nov.Year(), nov.Month(), nov.Day(), 0, 0, 0, 0, time.UTC)
today
:=
time
.
Date
(
now
.
Year
(),
now
.
Month
(),
6
,
0
,
0
,
0
,
0
,
time
.
UTC
)
today
:=
time
.
Date
(
now
.
Year
(),
now
.
Month
(),
now
.
Day
(),
0
,
0
,
0
,
0
,
time
.
UTC
)
return
today
.
Unix
()
}
...
...
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