Skip to content
Snippets Groups Projects
Unverified Commit f4711320 authored by Wolfgang Welz's avatar Wolfgang Welz Committed by GitHub
Browse files

Feat: Add PoW for messages (#511)


* add pow and nonce to the message

* add pow plugin

* fix pow tests

* fix linter warnings

* fix typo

* fix panic when messagelayer is disabled

* improve logging

* fix typo

* make the pow check a byte filter

* decrease the test PoW difficulty even further

* expose the pow filter and use it in the pow plugin

* Apply suggestions from code review

* make length of nonce a const

Co-authored-by: default avatarLuca Moser <moser.luca@gmail.com>
parent 14fce79a
No related branches found
No related tags found
No related merge requests found
package pow
import (
"context"
"crypto"
"crypto/ed25519"
"errors"
"sync"
"time"
"github.com/iotaledger/goshimmer/packages/pow"
"github.com/iotaledger/goshimmer/plugins/config"
"github.com/iotaledger/hive.go/logger"
_ "golang.org/x/crypto/blake2b" // required by crypto.BLAKE2b_512
)
var (
// ErrMessageTooSmall is returned when the message is smaller than the 8-byte nonce.
ErrMessageTooSmall = errors.New("message too small")
)
// parameters
var (
hash = crypto.BLAKE2b_512
// configured via parameters
difficulty int
numWorkers int
timeout time.Duration
)
var (
log *logger.Logger
workerOnce sync.Once
worker *pow.Worker
)
// Worker returns the PoW worker instance of the PoW plugin.
func Worker() *pow.Worker {
workerOnce.Do(func() {
log = logger.NewLogger(PluginName)
// load the parameters
difficulty = config.Node.GetInt(CfgPOWDifficulty)
numWorkers = config.Node.GetInt(CfgPOWNumThreads)
timeout = config.Node.GetDuration(CfgPOWTimeout)
// create the worker
worker = pow.New(hash, numWorkers)
})
return worker
}
// DoPOW performs the PoW on the provided msg and returns the nonce.
func DoPOW(msg []byte) (uint64, error) {
content, err := powData(msg)
if err != nil {
return 0, err
}
// get the PoW worker
worker := Worker()
log.Debugw("start PoW", "difficulty", difficulty, "numWorkers", numWorkers)
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
nonce, err := worker.Mine(ctx, content[:len(content)-pow.NonceBytes], difficulty)
log.Debugw("PoW stopped", "nonce", nonce, "err", err)
return nonce, err
}
// powData returns the bytes over which PoW should be computed.
func powData(msgBytes []byte) ([]byte, error) {
contentLength := len(msgBytes) - ed25519.SignatureSize
if contentLength < pow.NonceBytes {
return nil, ErrMessageTooSmall
}
return msgBytes[:contentLength], nil
}
...@@ -59,6 +59,11 @@ ...@@ -59,6 +59,11 @@
"disablePlugins": "portcheck", "disablePlugins": "portcheck",
"enablePlugins": [] "enablePlugins": []
}, },
"pow": {
"difficulty": 4,
"numThreads": 1,
"timeout": "10s"
},
"webapi": { "webapi": {
"auth": { "auth": {
"password": "goshimmer", "password": "goshimmer",
......
...@@ -29,7 +29,7 @@ services: ...@@ -29,7 +29,7 @@ services:
--metrics.global=true --metrics.global=true
--prometheus.bindAddress=0.0.0.0:9312 --prometheus.bindAddress=0.0.0.0:9312
--node.enablePlugins=analysis-server,analysis-dashboard,prometheus --node.enablePlugins=analysis-server,analysis-dashboard,prometheus
--node.disablePlugins=portcheck,dashboard,analysis-client,gossip,drng,issuer,sync,messagelayer,valuetransfers,webapi,webapibroadcastdataendpoint,webapifindtransactionhashesendpoint,webapigetneighborsendpoint,webapigettransactionobjectsbyhashendpoint,webapigettransactiontrytesbyhashendpoint --node.disablePlugins=portcheck,dashboard,analysis-client,gossip,drng,issuer,sync,messagelayer,pow,valuetransfers,webapi,webapibroadcastdataendpoint,webapifindtransactionhashesendpoint,webapigetneighborsendpoint,webapigettransactionobjectsbyhashendpoint,webapigettransactiontrytesbyhashendpoint
volumes: volumes:
- ./config.docker.json:/tmp/config.json:ro - ./config.docker.json:/tmp/config.json:ro
- goshimmer-cache:/go - goshimmer-cache:/go
......
...@@ -13,7 +13,7 @@ const ( ...@@ -13,7 +13,7 @@ const (
logsDir = "/tmp/logs/" logsDir = "/tmp/logs/"
disabledPluginsEntryNode = "portcheck,dashboard,analysis-client,profiling,gossip,drng,issuer,sync,metrics,valuetransfers,messagelayer,webapi,webapibroadcastdataendpoint,webapifindtransactionhashesendpoint,webapigetneighborsendpoint,webapigettransactionobjectsbyhashendpoint,webapigettransactiontrytesbyhashendpoint" disabledPluginsEntryNode = "portcheck,dashboard,analysis-client,profiling,gossip,drng,issuer,sync,metrics,valuetransfers,messagelayer,pow,webapi,webapibroadcastdataendpoint,webapifindtransactionhashesendpoint,webapigetneighborsendpoint,webapigettransactionobjectsbyhashendpoint,webapigettransactiontrytesbyhashendpoint"
disabledPluginsPeer = "portcheck,dashboard,analysis-client,profiling" disabledPluginsPeer = "portcheck,dashboard,analysis-client,profiling"
snapshotFilePath = "/assets/7R1itJx5hVuo9w9hjg5cwKFmek4HMSoBDgJZN8hKGxih.bin" snapshotFilePath = "/assets/7R1itJx5hVuo9w9hjg5cwKFmek4HMSoBDgJZN8hKGxih.bin"
dockerLogsPrefixLen = 8 dockerLogsPrefixLen = 8
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment