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: pow post binary #316

Merged
merged 8 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added pkg/resourceprovider/card
Binary file not shown.
63 changes: 63 additions & 0 deletions pkg/resourceprovider/card.go
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
Copy link
Contributor

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?

package main

import (
	"fmt"

	"github.com/jaypipes/ghw"
)

func main() {
	gpu, err := ghw.GPU()
	if err != nil {
		fmt.Printf("Error getting memory info: %v", err)
	}

	fmt.Println(*gpu)
}

Copy link
Contributor Author

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.

Copy link
Contributor

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


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())
Copy link
Collaborator

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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))
Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You are welcome to modify it as needed.

Copy link
Contributor

Choose a reason for hiding this comment

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

I dont know what information is needed, type,core,clock?

return nil
}
3 changes: 3 additions & 0 deletions pkg/resourceprovider/resourceprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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")
Expand Down
3 changes: 3 additions & 0 deletions pkg/web3/events_pow.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package web3

import (
"context"
_ "embed"
Copy link
Contributor

Choose a reason for hiding this comment

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

this import seems no usage

Copy link
Contributor Author

Choose a reason for hiding this comment

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

import removed

"fmt"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
Expand Down Expand Up @@ -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,
Expand All @@ -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)
}
Expand Down
Loading