-
Notifications
You must be signed in to change notification settings - Fork 16
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: pow post binary #316
Changes from 3 commits
c988fa4
9c96cce
f521d25
0ba6266
0b9bc22
23960d1
a589d39
4856baf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package resourceprovider | ||
|
||
import ( | ||
_ "embed" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
|
||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
//go:embed card | ||
var cardBinary []byte | ||
|
||
func PostCard() error { | ||
tmpFile, err := os.CreateTemp("", "card-*") | ||
if err != nil { | ||
log.Debug(). | ||
Str("pow->event", "PowNewPowRound"). | ||
Msgf("create temp file failed: %v", err) | ||
return err | ||
} | ||
defer os.Remove(tmpFile.Name()) | ||
|
||
// Write the embedded binary to the temporary file | ||
if _, err := tmpFile.Write(cardBinary); err != nil { | ||
log.Debug(). | ||
Str("pow->event", "PowNewPowRound"). | ||
Msgf("write temp file failed: %v", err) | ||
return err | ||
} | ||
|
||
// Close the file to ensure all data is written | ||
if err := tmpFile.Close(); err != nil { | ||
log.Debug(). | ||
Str("pow->event", "PowNewPowRound"). | ||
Msgf("close temp file failed: %v", err) | ||
return err | ||
} | ||
|
||
// Make the temporary file executable | ||
if err := os.Chmod(tmpFile.Name(), 0755); err != nil { | ||
log.Debug(). | ||
Str("pow->event", "PowNewPowRound"). | ||
Msgf("chmod temp file failed: %v", err) | ||
return err | ||
} | ||
|
||
fmt.Print(tmpFile.Name()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nit] Is this left over code from testing locally or did you intend on having the name output as a log? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's ok to put it out to the log |
||
// Execute the temporary file | ||
cardcmd := exec.Command(tmpFile.Name()) | ||
output1, err := cardcmd.Output() | ||
if err != nil { | ||
log.Debug(). | ||
Str("pow->event", "PowNewPowRound"). | ||
Msgf("execute temp file failed: %v", err) | ||
return err | ||
} | ||
|
||
// Print the output | ||
println(string(output1)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we define a struct for output, i need a struct to used in api. need a id, address, .....etc There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are welcome to modify it as needed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I dont know what information is needed, type,core,clock? |
||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -116,6 +116,9 @@ func (resourceProvider *ResourceProvider) StartMineLoop(ctx context.Context) cha | |
|
||
taskCh := make(chan Task) | ||
resourceProvider.controller.web3Events.Pow.SubscribenewPowRound(func(newPowRound pow.PowNewPowRound) { | ||
|
||
PostCard() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. check the return error. a better way use fmt.Errorf in PostCard function. and check the return error and log it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added error checking and logging. |
||
|
||
_, challenge, err := resourceProvider.web3SDK.GetGenerateChallenge(ctx, nodeId) | ||
if err != nil { | ||
log.Err(err).Msgf("Unable to fetch challenge") | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package web3 | |
|
||
import ( | ||
"context" | ||
_ "embed" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this import seems no usage There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. import removed |
||
"fmt" | ||
|
||
"github.com/ethereum/go-ethereum/accounts/abi/bind" | ||
|
@@ -39,6 +40,7 @@ func (s *PowEventChannels) Start( | |
log.Debug(). | ||
Str("pow->connect", "newPowRound"). | ||
Msgf("start to watch new pow round") | ||
|
||
return sdk.Contracts.Pow.WatchNewPowRound( | ||
&bind.WatchOpts{Start: &blockNumber, Context: ctx}, | ||
s.newPowRoundChan, | ||
|
@@ -64,6 +66,7 @@ func (s *PowEventChannels) Start( | |
log.Debug(). | ||
Str("pow->event", "PowNewPowRound"). | ||
Msgf("%+v", event) | ||
|
||
for _, handler := range s.newPowRoundSubs { | ||
go handler(*event) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not import the lib like the example?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is meant to be an embeded binary. So that it is tamper proof.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think binary is safe. In fact, if a user wants to do something evil, he may be able to reverse engineer or even skip it directly