Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Implement C-GO interface of dcurl #228

Merged
merged 3 commits into from
Apr 15, 2020
Merged

feat: Implement C-GO interface of dcurl #228

merged 3 commits into from
Apr 15, 2020

Conversation

Fangop
Copy link

@Fangop Fangop commented Apr 3, 2020

  • These code are already formatted with gofmt
  • And, the naming policy is based on iotaledger/iota.go/pow for consistency.
    • Though, I changed a little bit for more easily to read the code, like the name of mutex.

Here provides a C-GO interface for dcurl, in format of HORNET.
These API are based on dynamic linking.
We should add the path to libdcurl.so to LIBRARY_PATH and LD_LIBRARY_PATH.

  • LIBRARY_PATH
    • For avoiding relative path in dcurl/cgo, I removed cgo flags with relative path to libdcurl.so. It'll leads to compiling error for c code in cgo.
      The solution is add the path/to/libdcurl.so to LIBRARY_PATH.
      When compiling, compiler will find dcurl in LIBRARY_PATH.

Under the path of dcurl:

$ export LIBRARY_PATH=$LIBRARY_PATH:$(pwd)/build
$ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$(pwd)/build

Cause libdcurl.so is in /build after dcurl being built.

Providing 2 versions of API for the usage in HORNET, one is implemented with mutex lock.
And, the result is different from original dcurl, it returns only nonce rather than full 2673 trytes.

The usage is exactly same as api in iota.go/pow.

API usage

parameters

  • trytes: Has type Trytes, actually a string, is the trytes you want to calculate in PoW.
  • mwm: Has type int, represent minimum weight magnitude.
  • parallelism(not required): Has type int, the number of thread you want to use in this PoW calculation.

Output

  • nonce: Rather than full 2163 trytes, we use nonce here for the API format of HORNET.

Test package using GO test

This test will test for API DcurlProofOfWork(), which simultaneously test core API DcurlEntry().

  • Make sure your LIBRARY_PATH and LD_LIBRARY_PATH are ready.

Under the path of dcurl:

$ export LIBRARY_PATH=$LIBRARY_PATH:$(pwd)/build
$ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$(pwd)/build
  • Test pakcage using GO test

Under the path of dcurl/cgo/pow.

$ go test
PASS
ok      github.com/DLTcollab/dcurl/cgo/pow    3.223s

Use -v for more information.

$ go test -v
=== RUN   TestDcurlProofOfWork
--- PASS: TestDcurlProofOfWork (3.32s)
    pow_test.go:17: Use dcurl to calculate PoW with 8 threads enabled and MWM = 14
    pow_test.go:25: Hashed trytes: BJCVLLRQHEB9MVDJBQVMCCK9ZHEARJLGUMYTHAUPGCGNTLXZZU9TFSOPUJXCWSULBXLUMTU9HCECZ9999
PASS
ok      github.com/DLTcollab/dcurl/cgo/pow    3.328s

Import dcurl/cgo/pow for other go files

  • Still, make sure your LIBRARY_PATH and LD_LIBRARY_PATH are ready.
$ export LIBRARY_PATH=$LIBRARY_PATH:path/to/libdcurl.so
$ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:path/to/libdcurl.so
  • Use go get if your go files(one you want to import dcurl/cgo/pow) is not managed by go module.
    • The url should be github.com/JulianATA/dcurl/cgo/pow now.
    • After merged, it'll be github.com/DLTcollab/dcurl/cgo/pow
$ go get  github.com/JulianATA/dcurl/cgo/pow
  • Here is an example for how to import /dcurl/cgo/pow.
package main

import (
	. "github.com/iotaledger/iota.go/consts"
	"github.com/iotaledger/iota.go/curl"
	"github.com/julianATA/dcurl/cgo/pow"
)

func main() {
	rawTx := "999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999A9RGRKVGWMWMKOLVMDFWJUHNUNYWZTJADGGPZGXNLERLXYWJE9WQHWWBMCPZMVVMJUMWWBLZLNMLDCGDJ999999999999999999999999999999999999999999999999999999YGYQIVD99999999999999999999TXEFLKNPJRBYZPORHZU9CEMFIFVVQBUSTDGSJCZMBTZCDTTJVUFPTCCVHHORPMGCURKTH9VGJIXUQJVHK999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999"
	mwm := 14
	nonce, _ := pow.DcurlProofOfWork(rawTx, mwm, 8)
	rawTx = rawTx[:len(rawTx)-NonceTrinarySize/3] + nonce
	hashedTrytes := curl.MustHashTrytes(rawTx)
	println("Hashed trytes: " + hashedTrytes)
}
  • Name the file main.go
$ go run main.go
Hashed trytes: BJCVLLRQHEB9MVDJBQVMCCK9ZHEARJLGUMYTHAUPGCGNTLXZZU9TFSOPUJXCWSULBXLUMTU9HCECZ9999

Close #217

hashedTrytes := curl.MustHashTrytes(rawTx)

t.Log("hashed Trytes:", hashedTrytes)
if guards.IsTransactionHashWithMWM(hashedTrytes, 10) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be 14, which is mwm.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!
Use the mwm variable with casting instead of an exact number now.

Comment on lines 33 to 35
t.Log("Correctly hash with mwm")
} else {
t.Error("Wrong hash trytes")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unify the log and error message.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!
Use "Correct hash with mwm" and "Wrong hash with mwm" now.

@marktwtn
Copy link
Collaborator

marktwtn commented Apr 5, 2020

Fix commit message:
berfore using it. -> before using it.
Providing 2 versions of API -> The commit provides 2 versions of API
one implemented with mutex lock. -> one is implemented with mutex lock.
And, the result is -> And the result is

should aware of -> should be aware of
And, the type transfer between GOlang and C is tricky too. -> and the type transfer between GOlang and C is tricky, too.
So, implement a test in GOlang can make sure it work correctly in these 2 aspect.
-> Hence implement a test in GOlang can make sure it works correctly in these 2 aspect.
fallowing steps. -> following steps.

t.Log("Testing an 8 threads dcurl pow with 14 as mwm")
nonce, err := DcurlProofOfWork(rawTx, mwm, 8)
if err == nil {
t.Log("dcurl PoW successed")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

succeeded

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, updated!

rawTx := "999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999A9RGRKVGWMWMKOLVMDFWJUHNUNYWZTJADGGPZGXNLERLXYWJE9WQHWWBMCPZMVVMJUMWWBLZLNMLDCGDJ999999999999999999999999999999999999999999999999999999YGYQIVD99999999999999999999TXEFLKNPJRBYZPORHZU9CEMFIFVVQBUSTDGSJCZMBTZCDTTJVUFPTCCVHHORPMGCURKTH9VGJIXUQJVHK999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999"

mwm := 14
t.Log("Testing an 8 threads dcurl pow with 14 as mwm")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change to Use dcurl to calculate PoW with 8 threads enabled and MWM = 14


t.Log("hashed Trytes:", hashedTrytes)
if guards.IsTransactionHashWithMWM(hashedTrytes, uint(mwm)) {
t.Log("Correct hash wih mwm")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with

"github.com/iotaledger/iota.go/guards"
)

func TestDcrulProofOfWork(t *testing.T) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TestDcurlProofOfWork

@jserv
Copy link
Member

jserv commented Apr 14, 2020

Can we use the directory name go instead?

@marktwtn
Copy link
Collaborator

Can we use the directory name go instead?

If possible, I prefer cgo since we have another directory named as jni.

@Fangop
Copy link
Author

Fangop commented Apr 14, 2020

It could be dcurl/cgo/pow or dcurl/go/pow.

The only thing invariant is the pow package should be consistent with its folder name.

@Fangop
Copy link
Author

Fangop commented Apr 14, 2020

@jserv @marktwtn
Here is some further information about directory name.
I thought cgo is better than go, since go mostly refers to pure go implementations.

@jserv
Copy link
Member

jserv commented Apr 15, 2020

I thought cgo is better than go, since go mostly refers to pure go implementations.

Let's move forward to cgo name scheme.

JulianATA added 3 commits April 15, 2020 16:46
These API are based on dynamic linking.
User should always check LD_LIBRARY_PATH and LIBRARY_PATH
before using it.

The commit provides 2 versions of API for the usage in HORNET,
one is implemented with mutex lock.
And the result is different from original dcurl,
it returns nonce rather than full 2673 trytes.
Here is a test for C-GO interface based on GOlang testing package.
The reasons to implement a test in GOlang are format and type.
We should be aware of the format difference between HORNET and dcurl,
and the transfer between GOlang and C is tricky, too.
Hence implement a test in GOlang can make sure
it works correctly in these 2 aspect.

It'll check with following steps.
First, the PoW works with or without errors.
Then, the length of nonce should be 27.
Hashing will check if nonce is correctly encoded.
Last, the hashed trytes should be correct with mwm
GO module is a dependency management system for GO.
It makes dependency information explicit and easier to manage.
@Fangop
Copy link
Author

Fangop commented Apr 15, 2020

@jserv @marktwtn
The directory name has been changed, with a new go module name github.com/DLTcollab/dcurl/cgo.

@@ -0,0 +1,78 @@
package pow
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the filename be revised to cgo/pow/pow_dcurl.go?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Originally, I use pow_dcurl.go.
Under the module name dcurl.go or github.com/DLTcollab/dcurl/cgo now,
it should be clear that pow here is using dcurl.
Hence I thought the postfix could be redundant.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In iota.go/pow, they have different postfix for different pow implementations.

@jserv jserv merged commit 2930b63 into DLTcollab:develop Apr 15, 2020
@jserv
Copy link
Member

jserv commented Apr 15, 2020

Let's move forward!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement C-Go interface for HORNET integration
3 participants