Skip to content

Commit

Permalink
get private doc when trying to handle claim
Browse files Browse the repository at this point in the history
  • Loading branch information
steezeburger committed Jan 19, 2024
1 parent 47356a2 commit c6689cf
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
3 changes: 2 additions & 1 deletion cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -33,6 +33,7 @@ func init() {
}
}

// Execute creates a store manager and server, then runs the server.
func Execute() {
smOpts := &store.NewManagerOpts{
ProjectID: *firestoreProjectID,
Expand Down
11 changes: 11 additions & 0 deletions internal/chain/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package chain
import (
"context"
"crypto/ecdsa"
"fmt"
"math/big"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"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 {
Expand All @@ -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
Expand Down Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down
6 changes: 3 additions & 3 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
61 changes: 46 additions & 15 deletions internal/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

type RollupStoreManager interface {
FindRollupByName(name string) (RollupDoc, error)
RollupByNameWithPrivate(name string) (RollupDoc, error)
}

type Manager struct {
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
}

Expand Down
2 changes: 1 addition & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit c6689cf

Please sign in to comment.