Skip to content

Commit

Permalink
fix 2 bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
jiuquxzy committed Jul 3, 2023
1 parent af641a8 commit 1ca34f1
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 8 deletions.
4 changes: 3 additions & 1 deletion pois/prove.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,13 @@ func (p *Prover) Recovery(key acc.RsaKey, front, rear int64) error {
int64(expanders.HashSize)*(p.Expanders.K+2)
generated, err := calcGeneratedFile(IdleFilePath, rear, total)
if err != nil {
return errors.Wrap(err, "init prover error")
return errors.Wrap(err, "recovery prover error")
}
p.generated = rear + generated
p.added = rear + generated
p.commited = rear
p.space -= (p.rear - p.front) * FileSize //calc proved space
p.space -= generated * (FileSize * (p.Expanders.K + 1)) //calc generated space
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions pois/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (v *Verifier) ReceiveCommits(ID []byte, commits []Commit) bool {
hash := expanders.NewHash()
for i := 0; i < len(commits); i++ {

if commits[i].FileIndex <= pNode.Rear {
if commits[i].FileIndex <= pNode.Front {
return false
}

Expand Down Expand Up @@ -325,7 +325,7 @@ func (v *Verifier) VerifyAcc(ID []byte, chals [][]int64, proof *AccProof) error
}
label := make([]byte, len(ID)+8+expanders.HashSize)
for i := 0; i < len(chals); i++ {
if chals[i][0] != proof.Indexs[i] && chals[i][0] != pNode.Rear+int64(i)+1 {
if chals[i][0] != proof.Indexs[i] || chals[i][0] != pNode.Rear+int64(i)+1 {
err := errors.New("bad file index")
return errors.Wrap(err, "verify acc proofs error")
}
Expand Down
2 changes: 1 addition & 1 deletion test/node_add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestGetBytes(t *testing.T) {
for i := 0; i < 4096; i++ {
b := expanders.GetBytes(expanders.NodeType(i))
if len(b) != 4 {
t.Log("error length", len(b))
t.Log("error length", len(b), b)
break
}
}
Expand Down
58 changes: 54 additions & 4 deletions test/pois_test.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
package test

import (
"bytes"
"cess_pois/acc"
"cess_pois/pois"
"cess_pois/util"
"encoding/binary"
"math/big"
"testing"
"time"
)

func TestPois(t *testing.T) {
//Initialize the execution environment
k, n, d := int64(7), int64(1024*1024*4), int64(64)
key := acc.RsaKeygen(2048)
key, err := ParseKey("./key")
if err != nil {
t.Fatal("save key error", err)
}
prover, err := pois.NewProver(k, n, d, []byte("test miner id"), 8192*2)
if err != nil {
t.Fatal("new prover error", err)
}
err = prover.Recovery(key, 4, 4)
err = prover.Recovery(key, 0, 0)
if err != nil {
t.Fatal("init prover error", err)
}
Expand All @@ -41,7 +48,7 @@ func TestPois(t *testing.T) {
t.Log("get commits time", time.Since(ts))

//register prover
verifier.RegisterProverNode(prover.ID, key, prover.AccManager.GetSnapshot().Accs.Value, 4, 4)
verifier.RegisterProverNode(prover.ID, key, prover.AccManager.GetSnapshot().Accs.Value, 0, 0)

//verifier receive commits
ts = time.Now()
Expand Down Expand Up @@ -103,7 +110,7 @@ func TestPois(t *testing.T) {

//prove space
ts = time.Now()
spaceProof, err := prover.ProveSpace(spaceChals, 5, 9)
spaceProof, err := prover.ProveSpace(spaceChals, 1, 5)
if err != nil {
t.Fatal("prove space error", err)
}
Expand Down Expand Up @@ -151,3 +158,46 @@ func TestPois(t *testing.T) {
}
t.Log("update prover status time", time.Since(ts))
}

func ToBytes(key acc.RsaKey) []byte {

n, g := key.N.Bytes(), key.G.Bytes()
nl, gl := int64(len(n)), int64(len(g))
buf := bytes.NewBuffer([]byte{})
binary.Write(buf, binary.BigEndian, nl)
binary.Write(buf, binary.BigEndian, gl)
data := make([]byte, buf.Len()+int(nl+gl))
copy(data[:16], buf.Bytes())
copy(data[16:16+nl], n)
copy(data[16+nl:], g)
return data
}

func GetKeyFromBytes(data []byte) acc.RsaKey {
if len(data) < 8 {
return acc.RsaKeygen(2048)
}
nl := binary.BigEndian.Uint64(data[:8])
gl := binary.BigEndian.Uint64(data[8:16])
if nl <= 0 || gl <= 0 || len(data)-16 != int(nl+gl) {
return acc.RsaKeygen(2048)
}
key := acc.RsaKey{
N: *(big.NewInt(0).SetBytes(data[16 : 16+nl])),
G: *(big.NewInt(0).SetBytes(data[16+nl:])),
}
return key
}

func SaveKey(path string, key acc.RsaKey) error {
bytes := ToBytes(key)
return util.SaveFile(path, bytes)
}

func ParseKey(path string) (acc.RsaKey, error) {
bytes, err := util.ReadFile(path)
if err != nil {
return acc.RsaKeygen(2048), err
}
return GetKeyFromBytes(bytes), nil
}

0 comments on commit 1ca34f1

Please sign in to comment.