From c6689cf0eab0cfa88ebd3a66ff91e42e6a31dbbf Mon Sep 17 00:00:00 2001 From: Jesse Snyder Date: Fri, 19 Jan 2024 16:59:56 -0700 Subject: [PATCH] get private doc when trying to handle claim --- README.md | 2 +- cmd/server.go | 3 +- internal/chain/transaction.go | 11 +++++++ internal/server/server.go | 6 ++-- internal/store/store.go | 61 ++++++++++++++++++++++++++--------- justfile | 2 +- 6 files changed, 64 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index deb99e6a..bcc8e679 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,7 @@ The following are the available command-line flags(excluding above wallet flags) | Flag | Description | Default Value | |-----------------|--------------------------------------------------|----------------| -| -httpport | Listener port to serve HTTP connection | 8080 | +| -httpport | Listener port to serve HTTP connection | 8089 | | -proxycount | Count of reverse proxies in front of the server | 0 | | -queuecap | Maximum transactions waiting to be sent | 100 | | -faucet.amount | Number of Ethers to transfer per user request | 1 | diff --git a/cmd/server.go b/cmd/server.go index 7ceee92e..010e12b4 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -13,7 +13,7 @@ import ( var ( appVersion = "v2.0.0" - httpPortFlag = flag.Int("httpport", 8080, "Listener port to serve HTTP connection") + httpPortFlag = flag.Int("httpport", 8089, "Listener port to serve HTTP connection") proxyCntFlag = flag.Int("proxycount", 0, "Count of reverse proxies in front of the server") queueCapFlag = flag.Int("queuecap", 100, "Maximum transactions waiting to be sent") versionFlag = flag.Bool("version", false, "Print version number") @@ -33,6 +33,7 @@ func init() { } } +// Execute creates a store manager and server, then runs the server. func Execute() { smOpts := &store.NewManagerOpts{ ProjectID: *firestoreProjectID, diff --git a/internal/chain/transaction.go b/internal/chain/transaction.go index 32f04270..37ccc3f7 100644 --- a/internal/chain/transaction.go +++ b/internal/chain/transaction.go @@ -3,6 +3,7 @@ package chain import ( "context" "crypto/ecdsa" + "fmt" "math/big" "github.com/ethereum/go-ethereum/accounts/abi/bind" @@ -10,6 +11,7 @@ import ( "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethclient" + log "github.com/sirupsen/logrus" ) type TxBuilder interface { @@ -25,6 +27,12 @@ type TxBuild struct { } func NewTxBuilder(provider string, privateKey *ecdsa.PrivateKey, chainID *big.Int) (TxBuilder, error) { + log.WithFields(log.Fields{ + "provider": provider, + "chainID": chainID, + "private": privateKey, + }).Info("creating new tx builder") + client, err := ethclient.Dial(provider) if err != nil { return nil, err @@ -52,12 +60,14 @@ func (b *TxBuild) Sender() common.Address { func (b *TxBuild) Transfer(ctx context.Context, to string, value *big.Int) (common.Hash, error) { nonce, err := b.client.PendingNonceAt(ctx, b.Sender()) if err != nil { + err = fmt.Errorf("could not get pending nonce: %v", err) return common.Hash{}, err } gasLimit := uint64(21000) gasPrice, err := b.client.SuggestGasPrice(ctx) if err != nil { + err = fmt.Errorf("could not suggest gas price: %v", err) return common.Hash{}, err } @@ -72,6 +82,7 @@ func (b *TxBuild) Transfer(ctx context.Context, to string, value *big.Int) (comm signedTx, err := types.SignTx(unsignedTx, b.signer, b.privateKey) if err != nil { + err = fmt.Errorf("could not sign tx: %v", err) return common.Hash{}, err } diff --git a/internal/server/server.go b/internal/server/server.go index bb3da770..e767130e 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -200,7 +200,7 @@ func (s *Server) txBuilderFromRequest(r *http.Request) (chain.TxBuilder, error) // txBuilderFromRollupName creates and returns a TxBuilder from the given name func (s *Server) txBuilderFromRollupName(name string) (chain.TxBuilder, error) { - rollup, err := s.sm.FindRollupByName(name) + rollup, err := s.sm.RollupByNameWithPrivate(name) if err != nil { err = fmt.Errorf("failed to find rollup by name: %w", err) return nil, err @@ -217,8 +217,8 @@ func (s *Server) txBuilderFromRollupName(name string) (chain.TxBuilder, error) { return nil, err } - // TODO - generate proper rpc url - rpcURL := fmt.Sprintf("https://rollups.%v.rpc.blahblah.com", rollup.Name) + // FIXME - generate url from template string passed in as flag? + rpcURL := fmt.Sprintf("http://%v.rpc.localdev.me", rollup.Name) txBuilder, err := chain.NewTxBuilder(rpcURL, privKey, big.NewInt(int64(rollup.NetworkID))) if err != nil { return nil, err diff --git a/internal/store/store.go b/internal/store/store.go index d2f7a386..709a9816 100644 --- a/internal/store/store.go +++ b/internal/store/store.go @@ -11,6 +11,7 @@ import ( type RollupStoreManager interface { FindRollupByName(name string) (RollupDoc, error) + RollupByNameWithPrivate(name string) (RollupDoc, error) } type Manager struct { @@ -62,8 +63,7 @@ type RollupDoc struct { Status RollupDocumentStatus `firestore:"status"` RollupPublicDetails - // FIXME - private isn't a field on the doc but is another collection, so this won't work - PrivateDetails RollupPrivateDoc `firestore:"private"` + PrivateDetails RollupPrivateDoc } type RollupPrivateDoc struct { @@ -80,28 +80,59 @@ type RollupPublicDetails struct { // FindRollupByName queries the store to find a rollup with the given name func (m *Manager) FindRollupByName(name string) (RollupDoc, error) { + snapshot, err := m.RollupDocSnapshotByName(name) + if err != nil { + return RollupDoc{}, err + } + + var rollup RollupDoc + if err := snapshot.DataTo(&rollup); err != nil { + return RollupDoc{}, err + } + rollup.ID = snapshot.Ref.ID + return rollup, nil +} + +// RollupByNameWithPrivate finds a rollup by name and returns it along with its private details +func (m *Manager) RollupByNameWithPrivate(name string) (RollupDoc, error) { + snapshot, err := m.RollupDocSnapshotByName(name) + if err != nil { + return RollupDoc{}, err + } + var rollup RollupDoc + if err := snapshot.DataTo(&rollup); err != nil { + return RollupDoc{}, err + } + + privateDoc, err := snapshot.Ref.Collection(m.rollupPrivateCollection).Doc(m.rollupPrivateDoc).Get(context.Background()) + if err != nil { + return RollupDoc{}, err + } + var priv RollupPrivateDoc + if err := privateDoc.DataTo(&priv); err != nil { + return RollupDoc{}, err + } + + rollup.ID = snapshot.Ref.ID + rollup.PrivateDetails = priv + return rollup, nil +} + +// RollupDocSnapshotByName queries the store to find a rollup with the given name +func (m *Manager) RollupDocSnapshotByName(name string) (*firestore.DocumentSnapshot, error) { ctx := context.Background() - iter := m.client.CollectionGroup(m.rollupsCollection).Where("name", "==", name).Documents(ctx) + iter := m.client.CollectionGroup(m.rollupsCollection).Where("name", "==", name).Where("status", "==", StatusDeployed).Documents(ctx) defer iter.Stop() for { doc, err := iter.Next() if errors.Is(err, iterator.Done) { - return RollupDoc{}, errors.New("rollup not found") + return nil, errors.New("rollup not found") } if err != nil { - return RollupDoc{}, err - } - - var rollup RollupDoc - err = doc.DataTo(&rollup) - if err != nil { - return RollupDoc{}, err - } - if rollup.Name != "" { - rollup.ID = doc.Ref.ID - return rollup, nil + return nil, err } + return doc, nil } } diff --git a/justfile b/justfile index 1b54ebbd..99e81fb1 100644 --- a/justfile +++ b/justfile @@ -17,7 +17,7 @@ web-install-deps: # runs the full web app. generates the front end app before starting the server. run: go generate -x - go run -v ./... -httpport 8080 -firestoreprojectid $FIRESTORE_PROJECT_ID + go run -v ./... -httpport 8089 -firestoreprojectid $FIRESTORE_PROJECT_ID # run cli and restart when code changes run-watch: