Skip to content

Commit

Permalink
feat: early tx api (#2210)
Browse files Browse the repository at this point in the history
  • Loading branch information
ralph-pichler authored Jun 24, 2021
1 parent c491d24 commit 3d9bcfd
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 54 deletions.
6 changes: 3 additions & 3 deletions pkg/debugapi/debugapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ type Service struct {
// to expose /addresses, /health endpoints, Go metrics and pprof. It is useful to expose
// these endpoints before all dependencies are configured and injected to have
// access to basic debugging tools and /health endpoint.
func New(publicKey, pssPublicKey ecdsa.PublicKey, ethereumAddress common.Address, logger logging.Logger, tracer *tracing.Tracer, corsAllowedOrigins []string) *Service {
func New(publicKey, pssPublicKey ecdsa.PublicKey, ethereumAddress common.Address, logger logging.Logger, tracer *tracing.Tracer, corsAllowedOrigins []string, transaction transaction.Service) *Service {
s := new(Service)
s.publicKey = publicKey
s.pssPublicKey = pssPublicKey
Expand All @@ -75,6 +75,7 @@ func New(publicKey, pssPublicKey ecdsa.PublicKey, ethereumAddress common.Address
s.tracer = tracer
s.corsAllowedOrigins = corsAllowedOrigins
s.metricsRegistry = newMetricsRegistry()
s.transaction = transaction

s.setRouter(s.newBasicRouter())

Expand All @@ -84,7 +85,7 @@ func New(publicKey, pssPublicKey ecdsa.PublicKey, ethereumAddress common.Address
// Configure injects required dependencies and configuration parameters and
// constructs HTTP routes that depend on them. It is intended and safe to call
// this method only once.
func (s *Service) Configure(overlay swarm.Address, p2p p2p.DebugService, pingpong pingpong.Interface, topologyDriver topology.Driver, lightNodes *lightnode.Container, storer storage.Storer, tags *tags.Tags, accounting accounting.Interface, pseudosettle settlement.Interface, chequebookEnabled bool, swap swap.Interface, chequebook chequebook.Service, batchStore postage.Storer, transaction transaction.Service, post postage.Service, postageContract postagecontract.Interface) {
func (s *Service) Configure(overlay swarm.Address, p2p p2p.DebugService, pingpong pingpong.Interface, topologyDriver topology.Driver, lightNodes *lightnode.Container, storer storage.Storer, tags *tags.Tags, accounting accounting.Interface, pseudosettle settlement.Interface, chequebookEnabled bool, swap swap.Interface, chequebook chequebook.Service, batchStore postage.Storer, post postage.Service, postageContract postagecontract.Interface) {
s.p2p = p2p
s.pingpong = pingpong
s.topologyDriver = topologyDriver
Expand All @@ -97,7 +98,6 @@ func (s *Service) Configure(overlay swarm.Address, p2p p2p.DebugService, pingpon
s.lightNodes = lightNodes
s.batchStore = batchStore
s.pseudosettle = pseudosettle
s.transaction = transaction
s.overlay = &overlay
s.post = post
s.postageContract = postageContract
Expand Down
8 changes: 4 additions & 4 deletions pkg/debugapi/debugapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ func newTestServer(t *testing.T, o testServerOptions) *testServer {
swapserv := swapmock.New(o.SwapOpts...)
transaction := transactionmock.New(o.TransactionOpts...)
ln := lightnode.NewContainer(o.Overlay)
s := debugapi.New(o.PublicKey, o.PSSPublicKey, o.EthereumAddress, logging.New(ioutil.Discard, 0), nil, o.CORSAllowedOrigins)
s.Configure(o.Overlay, o.P2P, o.Pingpong, topologyDriver, ln, o.Storer, o.Tags, acc, settlement, true, swapserv, chequebook, o.BatchStore, transaction, o.Post, o.PostageContract)
s := debugapi.New(o.PublicKey, o.PSSPublicKey, o.EthereumAddress, logging.New(ioutil.Discard, 0), nil, o.CORSAllowedOrigins, transaction)
s.Configure(o.Overlay, o.P2P, o.Pingpong, topologyDriver, ln, o.Storer, o.Tags, acc, settlement, true, swapserv, chequebook, o.BatchStore, o.Post, o.PostageContract)
ts := httptest.NewServer(s)
t.Cleanup(ts.Close)

Expand Down Expand Up @@ -154,7 +154,7 @@ func TestServer_Configure(t *testing.T) {
swapserv := swapmock.New(o.SwapOpts...)
ln := lightnode.NewContainer(o.Overlay)
transaction := transactionmock.New(o.TransactionOpts...)
s := debugapi.New(o.PublicKey, o.PSSPublicKey, o.EthereumAddress, logging.New(ioutil.Discard, 0), nil, nil)
s := debugapi.New(o.PublicKey, o.PSSPublicKey, o.EthereumAddress, logging.New(ioutil.Discard, 0), nil, nil, transaction)
ts := httptest.NewServer(s)
t.Cleanup(ts.Close)

Expand Down Expand Up @@ -185,7 +185,7 @@ func TestServer_Configure(t *testing.T) {
}),
)

s.Configure(o.Overlay, o.P2P, o.Pingpong, topologyDriver, ln, o.Storer, o.Tags, acc, settlement, true, swapserv, chequebook, nil, transaction, mockpost.New(), nil)
s.Configure(o.Overlay, o.P2P, o.Pingpong, topologyDriver, ln, o.Storer, o.Tags, acc, settlement, true, swapserv, chequebook, nil, mockpost.New(), nil)

testBasicRouter(t, client)
jsonhttptest.Request(t, client, http.MethodGet, "/readiness", http.StatusOK,
Expand Down
20 changes: 11 additions & 9 deletions pkg/debugapi/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ func (s *Service) newBasicRouter() *mux.Router {
"GET": http.HandlerFunc(s.addressesHandler),
})

if s.transaction != nil {
router.Handle("/transactions", jsonhttp.MethodHandler{
"GET": http.HandlerFunc(s.transactionListHandler),
})
router.Handle("/transactions/{hash}", jsonhttp.MethodHandler{
"GET": http.HandlerFunc(s.transactionDetailHandler),
"POST": http.HandlerFunc(s.transactionResendHandler),
"DELETE": http.HandlerFunc(s.transactionCancelHandler),
})
}

return router
}

Expand Down Expand Up @@ -171,15 +182,6 @@ func (s *Service) newRouter() *mux.Router {
})
}

router.Handle("/transactions", jsonhttp.MethodHandler{
"GET": http.HandlerFunc(s.transactionListHandler),
})
router.Handle("/transactions/{hash}", jsonhttp.MethodHandler{
"GET": http.HandlerFunc(s.transactionDetailHandler),
"POST": http.HandlerFunc(s.transactionResendHandler),
"DELETE": http.HandlerFunc(s.transactionCancelHandler),
})

router.Handle("/tags/{id}", jsonhttp.MethodHandler{
"GET": http.HandlerFunc(s.getTagHandler),
})
Expand Down
12 changes: 0 additions & 12 deletions pkg/node/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,6 @@ func InitChain(
return nil, common.Address{}, 0, nil, nil, fmt.Errorf("new transaction service: %w", err)
}

// Sync the with the given Ethereum backend:
isSynced, _, err := transaction.IsSynced(ctx, backend, maxDelay)
if err != nil {
return nil, common.Address{}, 0, nil, nil, fmt.Errorf("is synced: %w", err)
}
if !isSynced {
logger.Infof("waiting to sync with the Ethereum backend")
err := transaction.WaitSynced(logger, ctx, backend, maxDelay)
if err != nil {
return nil, common.Address{}, 0, nil, nil, fmt.Errorf("waiting backend sync: %w", err)
}
}
return backend, overlayEthAddress, chainID.Int64(), transactionMonitor, transactionService, nil
}

Expand Down
68 changes: 42 additions & 26 deletions pkg/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,43 @@ func NewBee(addr string, publicKey *ecdsa.PublicKey, signer crypto.Signer, netwo

addressbook := addressbook.New(stateStore)

var (
swapBackend *ethclient.Client
overlayEthAddress common.Address
chainID int64
transactionService transaction.Service
transactionMonitor transaction.Monitor
chequebookFactory chequebook.Factory
chequebookService chequebook.Service
chequeStore chequebook.ChequeStore
cashoutService chequebook.CashoutService
pollingInterval = time.Duration(o.BlockTime) * time.Second
)
if !o.Standalone {
swapBackend, overlayEthAddress, chainID, transactionMonitor, transactionService, err = InitChain(
p2pCtx,
logger,
stateStore,
o.SwapEndpoint,
signer,
pollingInterval,
)
if err != nil {
return nil, fmt.Errorf("init chain: %w", err)
}
b.ethClientCloser = swapBackend.Close
b.transactionCloser = tracerCloser
b.transactionMonitorCloser = transactionMonitor
}

var debugAPIService *debugapi.Service
if o.DebugAPIAddr != "" {
overlayEthAddress, err := signer.EthereumAddress()
if err != nil {
return nil, fmt.Errorf("eth address: %w", err)
}
// set up basic debug api endpoints for debugging and /health endpoint
debugAPIService = debugapi.New(*publicKey, pssPrivateKey.PublicKey, overlayEthAddress, logger, tracer, o.CORSAllowedOrigins)
debugAPIService = debugapi.New(*publicKey, pssPrivateKey.PublicKey, overlayEthAddress, logger, tracer, o.CORSAllowedOrigins, transactionService)

debugAPIListener, err := net.Listen("tcp", o.DebugAPIAddr)
if err != nil {
Expand All @@ -230,33 +259,20 @@ func NewBee(addr string, publicKey *ecdsa.PublicKey, signer crypto.Signer, netwo
b.debugAPIServer = debugAPIServer
}

var (
swapBackend *ethclient.Client
overlayEthAddress common.Address
chainID int64
transactionService transaction.Service
transactionMonitor transaction.Monitor
chequebookFactory chequebook.Factory
chequebookService chequebook.Service
chequeStore chequebook.ChequeStore
cashoutService chequebook.CashoutService
pollingInterval = time.Duration(o.BlockTime) * time.Second
)
if !o.Standalone {
swapBackend, overlayEthAddress, chainID, transactionMonitor, transactionService, err = InitChain(
p2pCtx,
logger,
stateStore,
o.SwapEndpoint,
signer,
pollingInterval,
)
// Sync the with the given Ethereum backend:
isSynced, _, err := transaction.IsSynced(p2pCtx, swapBackend, maxDelay)
if err != nil {
return nil, fmt.Errorf("init chain: %w", err)
return nil, fmt.Errorf("is synced: %w", err)
}
if !isSynced {
logger.Infof("waiting to sync with the Ethereum backend")

err := transaction.WaitSynced(logger, p2pCtx, swapBackend, maxDelay)
if err != nil {
return nil, fmt.Errorf("waiting backend sync: %w", err)
}
}
b.ethClientCloser = swapBackend.Close
b.transactionCloser = tracerCloser
b.transactionMonitorCloser = transactionMonitor
}

if o.SwapEnable {
Expand Down Expand Up @@ -751,7 +767,7 @@ func NewBee(addr string, publicKey *ecdsa.PublicKey, signer crypto.Signer, netwo
}

// inject dependencies and configure full debug api http path routes
debugAPIService.Configure(swarmAddress, p2ps, pingPong, kad, lightNodes, storer, tagService, acc, pseudosettleService, o.SwapEnable, swapService, chequebookService, batchStore, transactionService, post, postageContractService)
debugAPIService.Configure(swarmAddress, p2ps, pingPong, kad, lightNodes, storer, tagService, acc, pseudosettleService, o.SwapEnable, swapService, chequebookService, batchStore, post, postageContractService)
}

if err := kad.Start(p2pCtx); err != nil {
Expand Down

0 comments on commit 3d9bcfd

Please sign in to comment.