Skip to content

Commit

Permalink
update chall,reward,workspace (#259)
Browse files Browse the repository at this point in the history
  • Loading branch information
AstaFrode committed Apr 29, 2024
1 parent f0b5b2d commit 0907247
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 29 deletions.
35 changes: 35 additions & 0 deletions cmd/console/reward.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package console
import (
"context"
"fmt"
"math/big"
"os"
"strings"

Expand Down Expand Up @@ -66,6 +67,29 @@ func Command_Reward_Runfunc(cmd *cobra.Command, args []string) {
}
var total string
var claimed string
var unclaimed string

if len(rewardInfo.Total) == 0 {
rewardInfo.Total = "0"
}
if len(rewardInfo.Claimed) == 0 {
rewardInfo.Claimed = "0"
}

t, ok := new(big.Int).SetString(rewardInfo.Total, 10)
if !ok {
out.Err(err.Error())
os.Exit(1)
}
c, ok := new(big.Int).SetString(rewardInfo.Claimed, 10)
if !ok {
out.Err(err.Error())
os.Exit(1)
}

t = t.Sub(t, c)
u := t.String()

var sep uint8 = 0
for i := len(rewardInfo.Total) - 1; i >= 0; i-- {
total = fmt.Sprintf("%c%s", rewardInfo.Total[i], total)
Expand All @@ -86,9 +110,20 @@ func Command_Reward_Runfunc(cmd *cobra.Command, args []string) {
}
claimed = strings.TrimPrefix(claimed, "_")

sep = 0
for i := len(u) - 1; i >= 0; i-- {
unclaimed = fmt.Sprintf("%c%s", u[i], unclaimed)
sep++
if sep%3 == 0 {
unclaimed = fmt.Sprintf("_%s", unclaimed)
}
}
unclaimed = strings.TrimPrefix(unclaimed, "_")

var tableRows = []table.Row{
{"total reward", total},
{"claimed reward", claimed},
{"unclaimed reward", unclaimed},
}
tw := table.NewWriter()
tw.AppendRows(tableRows)
Expand Down
2 changes: 1 addition & 1 deletion cmd/console/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ func runCmd(cmd *cobra.Command, args []string) {

if len(reportFileCh) > 0 {
<-reportFileCh
go node.ReportFiles(reportFileCh, cli, runtime, wspace, l)
go node.ReportFiles(reportFileCh, cli, runtime, l, wspace.GetFileDir(), wspace.GetTmpDir())
}

if len(attestationIdleCh) > 0 {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.20
require (
github.com/CESSProject/cess-go-sdk v0.5.1-0.20240415124817-f6bf0e9d41e4
github.com/CESSProject/cess_pois v0.5.15
github.com/CESSProject/p2p-go v0.3.12
github.com/CESSProject/p2p-go v0.3.14
github.com/btcsuite/btcutil v1.0.3-0.20201208143702-a53e38424cce
github.com/centrifuge/go-substrate-rpc-client/v4 v4.2.1
github.com/gin-contrib/cors v1.7.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ github.com/CESSProject/cess_pois v0.5.15 h1:oQH8xEtxVzB+SeI+gNS6iSeFgjb/xDAsmgyu
github.com/CESSProject/cess_pois v0.5.15/go.mod h1:rztEZjjG+MbKzVgh5WtQcZc/7ZDkBvDNABc7Em8BKPc=
github.com/CESSProject/go-keyring v0.0.0-20220614131247-ee3a8da30fde h1:5MDRjjtg6PEhqyVjupwaapN96cOZiddOGAYwKQeaTu0=
github.com/CESSProject/go-keyring v0.0.0-20220614131247-ee3a8da30fde/go.mod h1:RUXBd3ROP98MYepEEa0Y0l/T0vQlIKqFJxI/ocdnRLM=
github.com/CESSProject/p2p-go v0.3.12 h1:PZxiainVQ5ylY34WpMrXlLY06tMosaLrAyWH1SB07uk=
github.com/CESSProject/p2p-go v0.3.12/go.mod h1:e/XFv66S2Yy2QthgSyXEO3TlfYWewNVFz8MB/oC2JX4=
github.com/CESSProject/p2p-go v0.3.14 h1:t7xLWTC8C+tuELtD6f2k4Ds6Z0zulMA0v83ONTDTthw=
github.com/CESSProject/p2p-go v0.3.14/go.mod h1:NXlRF0kaJs7pUu0dFUnp5K5HpxL//wq4DJ4fcnFNfiI=
github.com/ChainSafe/go-schnorrkel v1.0.0 h1:3aDA67lAykLaG1y3AOjs88dMxC88PgUuHRrLeDnvGIM=
github.com/ChainSafe/go-schnorrkel v1.0.0/go.mod h1:dpzHYVxLZcp8pjlV+O+UR8K0Hp/z7vcchBSbMBEhCw4=
github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBAUSII=
Expand Down
7 changes: 5 additions & 2 deletions node/calculate_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ func calc_tag(cli sdk.SDK, cace cache.Cache, l logger.Logger, teeRecord *TeeReco
l.Stag("err", fmt.Sprintf("[getAllFragment(%s)] %v", fid, err))
return nil
}
//n.Stag("info", fmt.Sprintf("[%s] The file have fragments: %v", fid, fragments))

if len(fragments) == 0 {
return nil
}

if err = checkFragmentsSize(fragments); err != nil {
l.Stag("err", fmt.Sprintf("[checkFragmentsSize(%s)] %v", fid, err))
Expand Down Expand Up @@ -134,7 +137,7 @@ func calc_tag(cli sdk.SDK, cace cache.Cache, l logger.Logger, teeRecord *TeeReco
continue
}
} else {
l.Stag("info", fmt.Sprintf("[%s] The file's tag stat err: %v", fid, err))
l.Stag("err", fmt.Sprintf("[%s] The file's tag stat err: %v", fid, err))
}

isreport, err := calcTheFragmentTag(l, teeRecord, cli.GetSignatureAccPulickey(), fid, fragments[i], maxIndex, latestSig, digest)
Expand Down
2 changes: 0 additions & 2 deletions node/challenge_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,6 @@ func calcSigma(
}
}
if !isChall {
l.Del("info", fragments[j])
os.Remove(fragments[j])
continue
}
l.Schal("info", fmt.Sprintf("chall go on: %s.%s", roothash, fragmentHash))
Expand Down
56 changes: 35 additions & 21 deletions node/report_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package node

import (
"errors"
"fmt"
"os"
"path/filepath"
Expand All @@ -32,13 +33,15 @@ func init() {
reportedFile = make(map[string]struct{}, 0)
}

func ReportFiles(ch chan<- bool, cli sdk.SDK, r *RunningState, ws *Workspace, l *logger.Lg) {
func ReportFiles(ch chan<- bool, cli sdk.SDK, r *RunningState, l *logger.Lg, fileDir, tmpDir string) {
defer func() { ch <- true }()
roothashs, err := utils.Dirs(ws.GetTmpDir())

roothashs, err := utils.Dirs(tmpDir)
if err != nil {
l.Report("err", fmt.Sprintf("[Dirs(TmpDir)] %v", err))
return
}

ok := false
fid := ""
for _, file := range roothashs {
Expand All @@ -48,14 +51,14 @@ func ReportFiles(ch chan<- bool, cli sdk.SDK, r *RunningState, ws *Workspace, l
reportedFileLock.Unlock()
if ok {
l.Report("info", fmt.Sprintf("[%s] prepare to check the file", fid))
err = check_file(cli, l, ws, file)
err = check_file(cli, l, file, fileDir, tmpDir)
if err != nil {
l.Report("err", fmt.Sprintf("[%s] check the file err: %v", fid, err))
}
} else {
r.SetReportFileFlag(true)
l.Report("info", fmt.Sprintf("[%s] prepare to report the file", fid))
err = report_file(cli, l, ws, file)
err = report_file(cli, l, file, fileDir, tmpDir)
if err != nil {
l.Report("err", fmt.Sprintf("[%s] report file err: %v", fid, err))
}
Expand All @@ -68,18 +71,29 @@ func ReportFiles(ch chan<- bool, cli sdk.SDK, r *RunningState, ws *Workspace, l
}
}

func check_file(cli sdk.SDK, l logger.Logger, ws *Workspace, f string) error {
func check_file(cli sdk.SDK, l logger.Logger, f string, fileDir, tmpDir string) error {
fid := filepath.Base(f)
metadata, err := cli.QueryFileMetadata(fid)
if err != nil {
if err.Error() == pattern.ERR_Empty {
reportedFileLock.Lock()
delete(reportedFile, fid)
reportedFileLock.Unlock()
os.RemoveAll(f)
l.Del("info", fmt.Sprintf("delete folder: %s", f))
return nil
if !errors.Is(err, pattern.ERR_RPC_EMPTY_VALUE) {
return err
}
sorder, err := cli.QueryStorageOrder(fid)
if err != nil {
if !errors.Is(err, pattern.ERR_RPC_EMPTY_VALUE) {
return err
}
}
for _, v := range sorder.CompleteList {
if sutils.CompareSlice(v.Miner[:], cli.GetSignatureAccPulickey()) {
return nil
}
}
reportedFileLock.Lock()
delete(reportedFile, fid)
reportedFileLock.Unlock()
os.RemoveAll(f)
l.Del("info", fmt.Sprintf("delete folder: %s", f))
return err
}

Expand All @@ -102,36 +116,36 @@ func check_file(cli sdk.SDK, l logger.Logger, ws *Workspace, f string) error {
return nil
}

if _, err = os.Stat(filepath.Join(ws.GetFileDir(), fid)); err != nil {
err = os.Mkdir(filepath.Join(ws.GetFileDir(), fid), configs.FileMode)
if _, err = os.Stat(filepath.Join(fileDir, fid)); err != nil {
err = os.Mkdir(filepath.Join(fileDir, fid), configs.FileMode)
if err != nil {
return err
}
}

for i := 0; i < len(savedFrgment); i++ {
_, err = os.Stat(filepath.Join(ws.GetTmpDir(), fid, savedFrgment[i]))
_, err = os.Stat(filepath.Join(tmpDir, fid, savedFrgment[i]))
if err != nil {
return err
}
err = os.Rename(filepath.Join(ws.GetTmpDir(), fid, savedFrgment[i]),
filepath.Join(ws.GetFileDir(), fid, savedFrgment[i]))
err = os.Rename(filepath.Join(tmpDir, fid, savedFrgment[i]),
filepath.Join(fileDir, fid, savedFrgment[i]))
if err != nil {
return err
}
}

for _, d := range deletedFrgmentList {
err = os.Remove(filepath.Join(ws.GetTmpDir(), fid, d))
err = os.Remove(filepath.Join(tmpDir, fid, d))
if err != nil {
continue
}
l.Del("info", filepath.Join(ws.GetTmpDir(), fid, d))
l.Del("info", filepath.Join(tmpDir, fid, d))
}
return nil
}

func report_file(cli sdk.SDK, l logger.Logger, ws *Workspace, f string) error {
func report_file(cli sdk.SDK, l logger.Logger, f string, fileDir, tmpDir string) error {
fid := filepath.Base(f)
storageorder, err := cli.QueryStorageOrder(fid)
if err != nil {
Expand Down Expand Up @@ -164,7 +178,7 @@ func report_file(cli sdk.SDK, l logger.Logger, ws *Workspace, f string) error {
for idx := uint8(0); idx < uint8(pattern.DataShards+pattern.ParShards); idx++ {
sucCount = 0
for i := 0; i < len(storageorder.SegmentList); i++ {
fstat, err := os.Stat(filepath.Join(ws.GetTmpDir(), fid, string(storageorder.SegmentList[i].FragmentHash[idx][:])))
fstat, err := os.Stat(filepath.Join(tmpDir, fid, string(storageorder.SegmentList[i].FragmentHash[idx][:])))
if err != nil {
break
}
Expand Down
10 changes: 10 additions & 0 deletions node/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,16 @@ func (w *Workspace) Build(rootDir string) error {
if err := os.MkdirAll(w.spaceDir, configs.FileMode); err != nil {
return err
}

w.fileDir = filepath.Join(rootDir, fileDir)
if err := os.MkdirAll(w.fileDir, configs.FileMode); err != nil {
return err
}

w.tmpDir = filepath.Join(rootDir, tmpDir)
if err := os.MkdirAll(w.tmpDir, configs.FileMode); err != nil {
return err
}
return nil
}

Expand Down

0 comments on commit 0907247

Please sign in to comment.