nanopow

package module
v0.0.0-...-6c99019 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 25, 2024 License: MIT Imports: 6 Imported by: 0

README

Golang NanoPoW

NanoPoW is an implementation of the proof-of-work used by Nano. That supports CPU and GPU (currently only OpenCL).

Usage

func main() {
    nanopow.GenerateWork([]byte{/** PREVIOUS HASH **/}, nanopow.V1BaseDifficult)
}
CPU

By default, we always uses CPU. If GPU is available, it will use CPU and GPU, combined.

GPU

By default, we don't support GPU. You need to enable it using the "build tags", when build use:

go build -tags cl

Currently the only available option is OpenCL ("cl" tag).

Multiple GPUs

By default, we use the first detected opencl-compatible GPU. To change this or to use multiple GPUs you can create your own work pool.

Here's an example using all of the GPUs

// Using github.com/inkeliz/go-opencl
platforms, err := opencl.GetPlatforms()
if err != nil {
    return nil, err
}

var platform opencl.Platform
var name string
pool := nanopow.NewPool()
for _, curPlatform := range platforms {
    err = curPlatform.GetInfo(opencl.PlatformName, &name)
    if err != nil {
        return nil, err
    }

    var devices []opencl.Device
    devices, err = curPlatform.GetDevices(opencl.DeviceTypeAll)
    if err != nil {
        return nil, err
    }

    for _, device := range devices {
		gpu, gpuErr := nanopow.NewWorkerGPU(device)
		if gpuErr == nil {
			pool.Workers = append(pool.Workers, gpu)
		} else {
			fmt.Printf("\n⚠️ Unable to use GPU %v", gpuErr)
		}
    }
}

// Generate work
pool.GenerateWork(hash, difficulty)

"Benchmarks"

I don't have an huge amount of data to compare and limited devices to test against. Some devices doesn't supports OpenCL. All times are using the V1 difficulty.

Device OS CPU-Only CPU + OpenCL
Samsung Galaxy S20+ Android ~9.62 seg Untested
Blackberry KeyOne Android ~36.7 seg Untested
R9 3900X + RX 5700XT Windows ~0.66 seg ~0.27 seg

Limitations

Support OpenGL, OpenGLES or Vulkan:

Currently we only support OpenCL, which is not natively supported by some devices. I have some plans to add some support for OpenGL, OpenGLES or Vulkan. Seems that Vulkan Compute, if possible, is the best solution since it's compatible with Windows, Linux and Android.

Contributing

Pull requests are welcome.

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotSupported           = errors.New("that device don't support that type of work method")
	ErrNoDeviceAvailable      = errors.New("no device found")
	ErrNoDefaultPoolAvailable = errors.New("no default pool found")
)
View Source
var (
	V1BaseDifficult    = CalculateDifficulty(0)
	V2BaseDifficult    = CalculateDifficulty(8)
	V2ReceiveDifficult = CalculateDifficulty(-8)
)

Functions

func CalculateDifficulty

func CalculateDifficulty(multiplier int64) uint64

func GetDevice

func GetDevice() (interface{}, error)

func IsValid

func IsValid(previous []byte, difficult uint64, w Work) bool

func NewWorkerCPU

func NewWorkerCPU() (*cpuWorker, error)

func NewWorkerCPUThread

func NewWorkerCPUThread(threads uint64) (*cpuWorker, error)

func NewWorkerGPU

func NewWorkerGPU(_ interface{}) (*noneGPUWorker, error)

func NewWorkerGPUThread

func NewWorkerGPUThread(_ uint64, _ interface{}) (*noneGPUWorker, error)

Types

type Context

type Context struct {
	// contains filtered or unexported fields
}

func NewContext

func NewContext() *Context

func (*Context) Cancel

func (c *Context) Cancel()

func (*Context) Result

func (c *Context) Result() (result Work)

type Pool

type Pool struct {
	Workers []WorkerGenerator
}
var DefaultWorkerPool *Pool // We don't use DefaultWorkerPool = newDefaultPool() because it slow down the init

func NewPool

func NewPool(w ...WorkerGenerator) (p *Pool)

func (*Pool) GenerateWork

func (p *Pool) GenerateWork(root []byte, difficulty uint64) (w Work, err error)

type Work

type Work [8]byte

func GenerateWork

func GenerateWork(root []byte, difficulty uint64) (w Work, err error)

func NewWork

func NewWork(b []byte) (work Work)

type WorkerGenerator

type WorkerGenerator interface {
	GenerateWork(ctx *Context, root []byte, difficulty uint64) (err error)
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL