From 4e1d33354439e0b81d644a83ddaa4892b7badff0 Mon Sep 17 00:00:00 2001 From: Cassandra Heart Date: Tue, 5 Nov 2024 19:39:50 -0600 Subject: [PATCH] resolved --- .../token/application/token_handle_mint.go | 46 ++++++++++- .../token_handle_prover_join_test.go | 77 +++++++++++++++++-- node/rpc/data_worker_ipc_server.go | 3 +- 3 files changed, 116 insertions(+), 10 deletions(-) diff --git a/node/execution/intrinsics/token/application/token_handle_mint.go b/node/execution/intrinsics/token/application/token_handle_mint.go index 49b7a5e..493c9f0 100644 --- a/node/execution/intrinsics/token/application/token_handle_mint.go +++ b/node/execution/intrinsics/token/application/token_handle_mint.go @@ -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") } diff --git a/node/execution/intrinsics/token/application/token_handle_prover_join_test.go b/node/execution/intrinsics/token/application/token_handle_prover_join_test.go index f98743b..65d4697 100644 --- a/node/execution/intrinsics/token/application/token_handle_prover_join_test.go +++ b/node/execution/intrinsics/token/application/token_handle_prover_join_test.go @@ -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) } diff --git a/node/rpc/data_worker_ipc_server.go b/node/rpc/data_worker_ipc_server.go index 709e0df..db50b95 100644 --- a/node/rpc/data_worker_ipc_server.go +++ b/node/rpc/data_worker_ipc_server.go @@ -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 {