Skip to content

Commit

Permalink
corrected PlugSeed (#172)
Browse files Browse the repository at this point in the history
* corrected PlugSeed

* corrected dbPath

* Update go-test.yml

* Update go-test.yml

* Update go-test.yml

* Update keystore.go
  • Loading branch information
ehsan6sha authored Nov 2, 2023
1 parent 564a5f2 commit 8459ca5
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 12 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/go-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v3
with:
Expand All @@ -15,7 +16,21 @@ jobs:
run: |
go version
go env
- name: Print GITHUB_WORKSPACE
run: |
echo "GITHUB_WORKSPACE: $GITHUB_WORKSPACE"
- name: Create directory for secrets
run: |
mkdir -p $GITHUB_WORKSPACE/secrets
chmod 777 $GITHUB_WORKSPACE/secrets
- name: Check permissions
run: ls -lha $GITHUB_WORKSPACE/secrets
- name: Run tests
run: go test -v -shuffle=on ./...
env:
SECRETS_DIR: ${{ github.workspace }}/secrets

- name: Run tests with race detector
run: go test -v -race ./...
env:
SECRETS_DIR: ${{ github.workspace }}/secrets
44 changes: 36 additions & 8 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,6 @@ func (bl *FxBlockchain) callBlockchain(ctx context.Context, method string, actio
}
}

type ReqInterface interface{}

func (bl *FxBlockchain) PlugSeedIfNeeded(ctx context.Context, action string, req interface{}) interface{} {
switch action {
case actionSeeded, actionAccountExists, actionPoolCreate, actionPoolJoin, actionPoolCancelJoin, actionPoolRequests, actionPoolList, actionPoolVote, actionPoolLeave, actionManifestUpload, actionManifestStore, actionManifestAvailable, actionManifestRemove, actionManifestRemoveStorer, actionManifestRemoveStored:
Expand All @@ -179,17 +177,47 @@ func (bl *FxBlockchain) PlugSeedIfNeeded(ctx context.Context, action string, req
log.Errorw("seed is empty", "err", err)
seed = ""
}
return struct {
ReqInterface
Seed string
}{
ReqInterface: req,
Seed: seed,
log.Debugf("seed is %s", seed)
log.Debugf("request is %v", req)

// Make sure we are dealing with a pointer to a struct
val := reflect.ValueOf(req)
if val.Kind() != reflect.Ptr || val.Elem().Kind() != reflect.Struct {
log.Error("req is not a pointer to a struct")
return req
}

// Create a new struct based on the req's type and then set the Seed field
reqVal := val.Elem()
seededReqType := reflect.StructOf([]reflect.StructField{
{
Name: "Seed",
Type: reflect.TypeOf(""),
Tag: `json:"seed"`,
},
})
seededReqVal := reflect.New(seededReqType).Elem()
seededReqVal.FieldByName("Seed").SetString(seed)

// Create a new struct that is a combination of the request struct and the Seed field
combinedReqType := reflect.StructOf(append(reflect.VisibleFields(reqVal.Type()), seededReqVal.Type().Field(0)))
combinedReq := reflect.New(combinedReqType).Elem()

// Copy the request struct fields to the new combined struct
for i := 0; i < reqVal.NumField(); i++ {
combinedReq.Field(i).Set(reqVal.Field(i))
}
// Set the Seed field
combinedReq.FieldByName("Seed").SetString(seed)

log.Debugf("seeded request is %v", combinedReq.Interface())
return combinedReq.Interface()

default:
return req
}
}

func (bl *FxBlockchain) serve(w http.ResponseWriter, r *http.Request) {

from, err := peer.Decode(r.RemoteAddr)
Expand Down
7 changes: 5 additions & 2 deletions blockchain/keystore.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,18 @@ func NewSimpleKeyStorer(dbPath string) *SimpleKeyStorer {
err := os.MkdirAll(dbPath, 0755)
if err != nil {
// Fallback to a local directory
dbPath = "."
dbPath = os.Getenv("SECRETS_DIR")
if dbPath == "" {
dbPath = "."
}
}
}

return &SimpleKeyStorer{dbPath: dbPath}
}

func (s *SimpleKeyStorer) SaveKey(ctx context.Context, key string) error {
return os.WriteFile(s.dbPath+"/secret_seed.txt", []byte(key), 0400)
return os.WriteFile(s.dbPath+"/secret_seed.txt", []byte(key), 0600)
}

func (s *SimpleKeyStorer) LoadKey(ctx context.Context) (string, error) {
Expand Down
15 changes: 13 additions & 2 deletions blockchain/keystore_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
package blockchain

import (
"fmt"
"context"
"testing"
)

func TestSimpleKeyStore(t *testing.T) {
fmt.Printf("test removed")
keyStore := NewSimpleKeyStorer("")
err := keyStore.SaveKey(context.Background(), "dummy")
if err != nil {
t.Errorf("while save key: %v", err)
}
key, err := keyStore.LoadKey(context.Background())
if err != nil {
t.Errorf("while load key: %v", err)
}
if string(key) != "dummy" {
t.Errorf("error loading the stored key: %v != dummy", string(key))
}
}

0 comments on commit 8459ca5

Please sign in to comment.