Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
goshimmer
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
iota-imt
goshimmer
Commits
38608819
Unverified
Commit
38608819
authored
3 years ago
by
RUANO RINCON Santiago
Browse files
Options
Downloads
Patches
Plain Diff
TBR: PoW plugin requirements (again)
parent
facb02cf
No related branches found
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
plugins/pow/events.go
+22
-0
22 additions, 0 deletions
plugins/pow/events.go
plugins/pow/pow.go
+43
-0
43 additions, 0 deletions
plugins/pow/pow.go
plugins/webapi.go
+1
-0
1 addition, 0 deletions
plugins/webapi.go
plugins/webapi/pow/plugin.go
+66
-0
66 additions, 0 deletions
plugins/webapi/pow/plugin.go
with
132 additions
and
0 deletions
plugins/pow/events.go
0 → 100644
+
22
−
0
View file @
38608819
package
pow
import
(
"time"
"github.com/iotaledger/hive.go/events"
)
type
PowEvents
struct
{
// PowDone defines the pow done event.
PowDone
*
events
.
Event
}
// PowDoneEvent is used to pass information through a PowDone event.
type
PowDoneEvent
struct
{
Difficulty
int
Duration
time
.
Duration
}
func
powDoneEventCaller
(
handler
interface
{},
params
...
interface
{})
{
handler
.
(
func
(
ev
*
PowDoneEvent
))(
params
[
0
]
.
(
*
PowDoneEvent
))
}
This diff is collapsed.
Click to expand it.
plugins/pow/pow.go
+
43
−
0
View file @
38608819
...
...
@@ -8,6 +8,7 @@ import (
"time"
"github.com/cockroachdb/errors"
"github.com/iotaledger/hive.go/events"
"github.com/iotaledger/hive.go/logger"
_
"golang.org/x/crypto/blake2b"
// required by crypto.BLAKE2b_512
...
...
@@ -22,10 +23,14 @@ var (
hash
=
crypto
.
BLAKE2b_512
// configured via parameters
difficultyMutex
sync
.
RWMutex
difficulty
int
numWorkers
int
timeout
time
.
Duration
parentsRefreshInterval
time
.
Duration
powEvents
*
PowEvents
eventsOnce
sync
.
Once
)
var
(
...
...
@@ -38,6 +43,8 @@ var (
// Worker returns the PoW worker instance of the PoW plugin.
func
Worker
()
*
pow
.
Worker
{
workerOnce
.
Do
(
func
()
{
difficultyMutex
.
Lock
()
defer
difficultyMutex
.
Unlock
()
log
=
logger
.
NewLogger
(
PluginName
)
// load the parameters
difficulty
=
Parameters
.
Difficulty
...
...
@@ -46,10 +53,22 @@ func Worker() *pow.Worker {
parentsRefreshInterval
=
Parameters
.
ParentsRefreshInterval
// create the worker
worker
=
pow
.
New
(
numWorkers
)
// ensure events are initialized
Events
()
})
return
worker
}
// Events returns the pow events.
func
Events
()
*
PowEvents
{
eventsOnce
.
Do
(
func
()
{
// init the events
powEvents
=
&
PowEvents
{
events
.
NewEvent
(
powDoneEventCaller
)}
})
return
powEvents
}
// DoPOW performs the PoW on the provided msg and returns the nonce.
func
DoPOW
(
msg
[]
byte
)
(
uint64
,
error
)
{
content
,
err
:=
powData
(
msg
)
...
...
@@ -57,6 +76,9 @@ func DoPOW(msg []byte) (uint64, error) {
return
0
,
err
}
difficultyMutex
.
RLock
()
defer
difficultyMutex
.
RUnlock
()
// get the PoW worker
worker
:=
Worker
()
...
...
@@ -64,7 +86,20 @@ func DoPOW(msg []byte) (uint64, error) {
ctx
,
cancel
:=
context
.
WithTimeout
(
context
.
Background
(),
parentsRefreshInterval
)
defer
cancel
()
start
:=
time
.
Now
()
nonce
,
err
:=
worker
.
Mine
(
ctx
,
content
[
:
len
(
content
)
-
pow
.
NonceBytes
],
difficulty
)
duration
:=
time
.
Since
(
start
)
ev
:=
&
PowDoneEvent
{
Difficulty
:
difficulty
,
Duration
:
duration
,
}
Events
()
.
PowDone
.
Trigger
(
ev
)
log
.
Debugw
(
"PoW stopped"
,
"nonce"
,
nonce
,
"err"
,
err
)
// log.Debugw("PoW stopped", "nonce", nonce, "err", err)
...
...
@@ -79,3 +114,11 @@ func powData(msgBytes []byte) ([]byte, error) {
}
return
msgBytes
[
:
contentLength
],
nil
}
// Tune changes pow difficulty at runtime.
func
Tune
(
d
int
)
{
difficultyMutex
.
Lock
()
defer
difficultyMutex
.
Unlock
()
difficulty
=
d
}
This diff is collapsed.
Click to expand it.
plugins/webapi.go
+
1
−
0
View file @
38608819
...
...
@@ -13,6 +13,7 @@ import (
"github.com/iotaledger/goshimmer/plugins/webapi/ledgerstate"
"github.com/iotaledger/goshimmer/plugins/webapi/mana"
"github.com/iotaledger/goshimmer/plugins/webapi/message"
"github.com/iotaledger/goshimmer/plugins/webapi/pow"
"github.com/iotaledger/goshimmer/plugins/webapi/snapshot"
drngTools
"github.com/iotaledger/goshimmer/plugins/webapi/tools/drng"
msgTools
"github.com/iotaledger/goshimmer/plugins/webapi/tools/message"
...
...
This diff is collapsed.
Click to expand it.
plugins/webapi/pow/plugin.go
0 → 100644
+
66
−
0
View file @
38608819
package
pow
import
(
"fmt"
"net/http"
"sync"
"github.com/iotaledger/goshimmer/plugins/pow"
//"github.com/iotaledger/goshimmer/plugins/webapi"
"github.com/iotaledger/hive.go/node"
"github.com/labstack/echo"
)
// PluginName is the name of the web API PoW endpoint plugin.
const
PluginName
=
"WebAPI PoW Endpoint"
var
(
// plugin is the plugin instance of the web API PoW endpoint plugin.
Plugin
*
node
.
Plugin
once
sync
.
Once
deps
=
new
(
dependencies
)
)
type
dependencies
struct
{
Server
*
echo
.
Echo
}
func
init
()
{
Plugin
=
node
.
NewPlugin
(
PluginName
,
deps
,
node
.
Enabled
,
configure
)
}
func
configure
(
plugin
*
node
.
Plugin
)
{
deps
.
Server
.
GET
(
"pow/tune"
,
handler
)
}
// Plugin gets the plugin instance.
//func Plugin() *node.Plugin {
// once.Do(func() {
// plugin = node.NewPlugin(PluginName, node.Enabled, configure)
// })
// return plugin
//}
// tune sets the pow difficulty
func
handler
(
c
echo
.
Context
)
error
{
var
request
Request
if
err
:=
c
.
Bind
(
&
request
);
err
!=
nil
{
return
c
.
JSON
(
http
.
StatusBadRequest
,
Response
{
Error
:
err
.
Error
()})
}
pow
.
Tune
(
request
.
Difficulty
)
return
c
.
JSON
(
http
.
StatusOK
,
Response
{
Message
:
fmt
.
Sprintf
(
"PoW difficulty changed to %d"
,
request
.
Difficulty
)})
}
// Response is the HTTP response of a pow tune request.
type
Response
struct
{
Message
string
`json:"message"`
Error
string
`json:"error"`
}
// Request contains the parameters of a pow tune request.
type
Request
struct
{
Difficulty
int
`json:"difficulty"`
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment