Skip to content

Commit

Permalink
send transactions in the proof verification handler
Browse files Browse the repository at this point in the history
  • Loading branch information
freigeistig committed Mar 1, 2024
1 parent f728d1c commit 50e5628
Show file tree
Hide file tree
Showing 6 changed files with 1,124 additions and 136 deletions.
9 changes: 6 additions & 3 deletions internal/assets/migrations/001_initial.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
-- +migrate Up
create table proofs(
id uuid default gen_random_uuid(),
tx_data text not null,
created_at timestamp default now()
id uuid default gen_random_uuid(),
voting_session text not null,
document_nullifier text not null,
created_at timestamp default now(),
unique (voting_session, document_nullifier)
);

-- +migrate Down
drop table proofs;
1,091 changes: 1,091 additions & 0 deletions internal/contracts/Verifier.go

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions internal/data/proofs.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ type ProofsQ interface {
ResetFilter() ProofsQ
}

type Proof struct { // TODO unique(voting session, nullifier)
ID uuid.UUID `db:"id" structs:"-"`
TxData string `db:"tx_data" structs:"tx_data"`
CreatedAt time.Time `db:"created_at" structs:"-"`
type Proof struct {
ID uuid.UUID `db:"id" structs:"-"`
VotingSession string `db:"voting_session" structs:"voting_session"`
DocumentNullifier string `db:"document_nullifier" structs:"document_nullifier"`
CreatedAt time.Time `db:"created_at" structs:"-"`
}
29 changes: 22 additions & 7 deletions internal/service/api/handlers/verify_proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/rarimo/proof-verification-relayer/internal/data"
"github.com/rarimo/proof-verification-relayer/internal/service/api/requests"
"github.com/rarimo/proof-verification-relayer/resources"
"gitlab.com/distributed_lab/ape"
"gitlab.com/distributed_lab/ape/problems"
"net/http"
Expand All @@ -27,6 +27,8 @@ func VerifyProof(w http.ResponseWriter, r *http.Request) {
return
}

// TODO: check if voting session and document nullifier is already in the database

gasPrice, err := EthClient(r).SuggestGasPrice(r.Context())
if err != nil {
Log(r).WithError(err).Error("failed to suggest gas price")
Expand All @@ -51,7 +53,7 @@ func VerifyProof(w http.ResponseWriter, r *http.Request) {
NetworkConfig(r).LockNonce()
defer NetworkConfig(r).UnlockNonce()

if _, err := types.SignNewTx(
tx, err := types.SignNewTx(
NetworkConfig(r).PrivateKey,
types.NewCancunSigner(NetworkConfig(r).ChainID),
&types.LegacyTx{
Expand All @@ -61,17 +63,30 @@ func VerifyProof(w http.ResponseWriter, r *http.Request) {
To: &verifierAddress,
Data: dataBytes,
},
); err != nil {
)
if err != nil {
Log(r).WithError(err).Error("failed to sign new tx")
ape.RenderErr(w, problems.InternalError())
return
}

if err := MasterQ(r).Proofs().Insert(data.Proof{
TxData: req.Data.TxData,
}); err != nil {
Log(r).WithError(err).Error("failed to insert proof into the database")
// TODO: write voting session and document nullifier to the database

if err := EthClient(r).SendTransaction(r.Context(), tx); err != nil {
Log(r).WithError(err).Error("failed to send transaction")
ape.RenderErr(w, problems.InternalError())
return
}

NetworkConfig(r).IncrementNonce()

ape.Render(w, resources.Tx{
Key: resources.Key{
ID: tx.Hash().String(),
Type: resources.TXS,
},
Attributes: resources.TxAttributes{
TxHash: tx.Hash().String(),
},
})
}
9 changes: 0 additions & 9 deletions internal/service/main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package service

import (
"context"
"net"
"net/http"

"github.com/rarimo/proof-verification-relayer/internal/config"
"github.com/rarimo/proof-verification-relayer/internal/data/pg"
"github.com/rarimo/proof-verification-relayer/internal/service/proofs_sender"
"gitlab.com/distributed_lab/kit/copus/types"
"gitlab.com/distributed_lab/logan/v3"
"gitlab.com/distributed_lab/logan/v3/errors"
Expand All @@ -28,12 +25,6 @@ func (s *service) run() error {
return errors.Wrap(err, "cop failed")
}

proofsSender, err := proofs_sender.NewProofsSender(s.log, pg.NewMasterQ(s.cfg.DB().Clone()), s.cfg.NetworkConfig())
if err != nil {
return errors.Wrap(err, "failed to initialize new proofs sender")
}
go proofsSender.Run(context.Background())

return http.Serve(s.listener, r)
}

Expand Down
113 changes: 0 additions & 113 deletions internal/service/proofs_sender/main.go

This file was deleted.

0 comments on commit 50e5628

Please sign in to comment.