This commit is contained in:
Cassandra Heart 2024-11-05 19:39:50 -06:00
parent 6578040774
commit 4e1d333544
No known key found for this signature in database
GPG Key ID: 6352152859385958
3 changed files with 116 additions and 10 deletions

View File

@ -8,7 +8,9 @@ import (
"github.com/iden3/go-iden3-crypto/poseidon"
pcrypto "github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/mr-tron/base58"
"github.com/pkg/errors"
"go.uber.org/zap"
"golang.org/x/crypto/sha3"
"source.quilibrium.com/quilibrium/monorepo/node/crypto"
"source.quilibrium.com/quilibrium/monorepo/node/protobufs"
@ -116,18 +118,33 @@ func (a *TokenApplication) handleMint(
},
}
return outputs, nil
} else if len(t.Proofs) > 1 && len(t.Proofs) != 3 && currentFrameNumber > 0 {
} else if len(t.Proofs) > 0 && currentFrameNumber > 0 {
a.Logger.Debug(
"got mint from peer",
zap.String("peer_id", base58.Encode([]byte(peerId))),
zap.Uint64("frame_number", currentFrameNumber),
)
if _, touched := lockMap[string(t.Signature.PublicKey.KeyValue)]; touched {
a.Logger.Debug(
"already received",
zap.String("peer_id", base58.Encode([]byte(peerId))),
zap.Uint64("frame_number", currentFrameNumber),
)
return nil, errors.Wrap(ErrInvalidStateTransition, "handle mint")
}
ring := -1
proverSet := int64((len(a.Tries) - 1) * 1024)
for i, t := range a.Tries[1:] {
if t.Contains(altAddr.FillBytes(make([]byte, 32))) {
ring = i - 1
ring = i
}
}
if ring == -1 {
a.Logger.Debug(
"not in ring",
zap.String("peer_id", base58.Encode([]byte(peerId))),
zap.Uint64("frame_number", currentFrameNumber),
)
return nil, errors.Wrap(ErrInvalidStateTransition, "handle mint")
}
challenge := []byte{}
@ -155,7 +172,14 @@ func (a *TokenApplication) handleMint(
individualChallenge,
uint32(i),
)
individualChallenge = append(individualChallenge, frame.Output...)
if len(p) != 516 {
a.Logger.Debug(
"invalid size",
zap.String("peer_id", base58.Encode([]byte(peerId))),
zap.Uint64("frame_number", currentFrameNumber),
zap.Int("proof_size", len(p)),
)
return nil, errors.Wrap(ErrInvalidStateTransition, "handle mint")
}
@ -166,6 +190,11 @@ func (a *TokenApplication) handleMint(
frame.Difficulty,
p,
) {
a.Logger.Debug(
"invalid proof",
zap.String("peer_id", base58.Encode([]byte(peerId))),
zap.Uint64("frame_number", currentFrameNumber),
)
return nil, errors.Wrap(ErrInvalidStateTransition, "handle mint")
}
@ -181,6 +210,13 @@ func (a *TokenApplication) handleMint(
storage.Quo(storage, big.NewInt(proverSet))
storage.Quo(storage, ringFactor)
a.Logger.Debug(
"issued reward",
zap.String("peer_id", base58.Encode([]byte(peerId))),
zap.Uint64("frame_number", currentFrameNumber),
zap.String("reward", storage.String()),
)
outputs = append(
outputs,
&protobufs.TokenOutput{
@ -220,6 +256,10 @@ func (a *TokenApplication) handleMint(
lockMap[string(t.Signature.PublicKey.KeyValue)] = struct{}{}
return outputs, nil
}
a.Logger.Debug(
"could not find case for proof",
zap.String("peer_id", base58.Encode([]byte(peerId))),
zap.Uint64("frame_number", currentFrameNumber),
)
return nil, errors.Wrap(ErrInvalidStateTransition, "handle mint")
}

View File

@ -6,6 +6,7 @@ import (
"encoding/binary"
"encoding/hex"
"testing"
"time"
"github.com/cloudflare/circl/sign/ed448"
"github.com/iden3/go-iden3-crypto/poseidon"
@ -13,7 +14,9 @@ import (
"github.com/libp2p/go-libp2p/core/peer"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
qcrypto "source.quilibrium.com/quilibrium/monorepo/node/crypto"
"source.quilibrium.com/quilibrium/monorepo/node/execution/intrinsics/token/application"
"source.quilibrium.com/quilibrium/monorepo/node/p2p"
"source.quilibrium.com/quilibrium/monorepo/node/protobufs"
"source.quilibrium.com/quilibrium/monorepo/node/store"
"source.quilibrium.com/quilibrium/monorepo/node/tries"
@ -21,15 +24,15 @@ import (
func TestHandleProverJoin(t *testing.T) {
log, _ := zap.NewDevelopment()
bpub, _, _ := ed448.GenerateKey(rand.Reader)
bpub, bprivKey, _ := ed448.GenerateKey(rand.Reader)
app := &application.TokenApplication{
Beacon: bpub,
CoinStore: store.NewPebbleCoinStore(store.NewInMemKVDB(), log),
ClockStore: store.NewPebbleClockStore(store.NewInMemKVDB(), log),
Logger: log,
Difficulty: 200000,
Tries: []*tries.RollingFrecencyCritbitTrie{
&tries.RollingFrecencyCritbitTrie{},
&tries.RollingFrecencyCritbitTrie{},
},
}
@ -69,8 +72,21 @@ func TestHandleProverJoin(t *testing.T) {
payload = binary.BigEndian.AppendUint64(payload, 0)
payload = append(payload, bytes.Repeat([]byte{0xff}, 32)...)
sig, _ := privKey.Sign(payload)
wprover := qcrypto.NewWesolowskiFrameProver(app.Logger)
gen, _, err := wprover.CreateDataGenesisFrame(
p2p.GetBloomFilter(application.TOKEN_ADDRESS, 256, 3),
make([]byte, 516),
10000,
&qcrypto.InclusionAggregateProof{},
[][]byte{bpub},
)
selbi, _ := gen.GetSelector()
txn, _ := app.ClockStore.NewTransaction()
app.ClockStore.StageDataClockFrame(selbi.FillBytes(make([]byte, 32)), gen, txn)
app.ClockStore.CommitDataClockFrame(gen.Filter, 0, selbi.FillBytes(make([]byte, 32)), app.Tries, txn, false)
txn.Commit()
app, success, fail, err := app.ApplyTransitions(
0,
1,
&protobufs.TokenRequests{
Requests: []*protobufs.TokenRequest{
&protobufs.TokenRequest{
@ -95,9 +111,16 @@ func TestHandleProverJoin(t *testing.T) {
assert.Len(t, success.Requests, 1)
assert.Len(t, fail.Requests, 0)
app.Tries = append(app.Tries, &tries.RollingFrecencyCritbitTrie{})
app.Tries[1].Add(addr, 0)
app, success, fail, err = app.ApplyTransitions(
0,
txn, _ = app.ClockStore.NewTransaction()
frame1, _ := wprover.ProveDataClockFrame(gen, [][]byte{}, []*protobufs.InclusionAggregateProof{}, bprivKey, time.Now().UnixMilli(), 10000)
selbi, _ = frame1.GetSelector()
app.ClockStore.StageDataClockFrame(selbi.FillBytes(make([]byte, 32)), frame1, txn)
app.ClockStore.CommitDataClockFrame(frame1.Filter, 1, selbi.FillBytes(make([]byte, 32)), app.Tries, txn, false)
txn.Commit()
_, success, fail, err = app.ApplyTransitions(
2,
&protobufs.TokenRequests{
Requests: []*protobufs.TokenRequest{
&protobufs.TokenRequest{
@ -118,6 +141,48 @@ func TestHandleProverJoin(t *testing.T) {
},
false,
)
assert.Error(t, err)
txn, _ = app.ClockStore.NewTransaction()
frame2, _ := wprover.ProveDataClockFrame(frame1, [][]byte{}, []*protobufs.InclusionAggregateProof{}, bprivKey, time.Now().UnixMilli(), 10000)
selbi, _ = frame1.GetSelector()
app.ClockStore.StageDataClockFrame(selbi.FillBytes(make([]byte, 32)), frame1, txn)
app.ClockStore.CommitDataClockFrame(frame2.Filter, 1, selbi.FillBytes(make([]byte, 32)), app.Tries, txn, false)
txn.Commit()
challenge := []byte{}
challenge = append(challenge, []byte(peerId)...)
challenge = binary.BigEndian.AppendUint64(
challenge,
1,
)
individualChallenge := append([]byte{}, challenge...)
individualChallenge = binary.BigEndian.AppendUint32(
individualChallenge,
uint32(0),
)
individualChallenge = append(individualChallenge, frame1.Output...)
out, _ := wprover.CalculateChallengeProof(individualChallenge, 10000)
payload = []byte("mint")
payload = append(payload, out...)
sig, _ = privKey.Sign(payload)
_, _, _, err = app.ApplyTransitions(2, &protobufs.TokenRequests{
Requests: []*protobufs.TokenRequest{
&protobufs.TokenRequest{
Request: &protobufs.TokenRequest_Mint{
Mint: &protobufs.MintCoinRequest{
Proofs: [][]byte{out},
Signature: &protobufs.Ed448Signature{
Signature: sig,
PublicKey: &protobufs.Ed448PublicKey{
KeyValue: pubkey,
},
},
},
},
},
},
}, false)
assert.NoError(t, err)
}

View File

@ -46,9 +46,10 @@ func (r *DataWorkerIPCServer) CalculateChallengeProof(
req.ClockFrame.FrameNumber,
)
challenge = binary.BigEndian.AppendUint32(challenge, r.coreId)
challenge = append(challenge, req.ClockFrame.Output...)
proof, err := r.prover.CalculateChallengeProof(
req.ClockFrame.Output,
challenge,
req.ClockFrame.Difficulty,
)
if err != nil {