Skip to content

Commit

Permalink
Merge pull request #97 from iotaledger/feat/listen-to-accepted-transa…
Browse files Browse the repository at this point in the history
…ctions

Listen to accepted transactions instead of ledger updates
  • Loading branch information
muXxer authored Oct 23, 2023
2 parents 5fd7d93 + 1b159cc commit 935b512
Show file tree
Hide file tree
Showing 11 changed files with 238 additions and 212 deletions.
2 changes: 1 addition & 1 deletion components/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var (
Name = "inx-faucet"

// Version of the app.
Version = "2.0.0-alpha.2"
Version = "2.0.0-alpha.3"
)

func App() *app.App {
Expand Down
54 changes: 25 additions & 29 deletions components/faucet/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ import (
"github.com/iotaledger/hive.go/app"
"github.com/iotaledger/hive.go/app/shutdown"
"github.com/iotaledger/hive.go/crypto"
"github.com/iotaledger/hive.go/ds/types"
"github.com/iotaledger/hive.go/ierrors"
"github.com/iotaledger/hive.go/lo"
"github.com/iotaledger/inx-app/pkg/httpserver"
"github.com/iotaledger/inx-app/pkg/nodebridge"
"github.com/iotaledger/inx-faucet/pkg/daemon"
Expand Down Expand Up @@ -120,8 +122,8 @@ func provide(c *dig.Container) error {
}

return &faucet.TransactionMetadata{
State: metadata.GetTxState(),
FailureReason: metadata.GetTxFailureReason(),
State: metadata.GetTransactionState(),
FailureReason: metadata.GetTransactionFailureReason(),
}, nil
}

Expand All @@ -141,22 +143,10 @@ func provide(c *dig.Container) error {
ctxRequest, cancelRequest := context.WithTimeout(Component.Daemon().ContextStopped(), inxRequestTimeout)
defer cancelRequest()

// simple outputs are basic outputs without timelocks, expiration, native tokens, storage deposit return unlocks conditions.
falseCondition := false
// the restricted address only returns simple outputs, which are basic outputs without timelocks,
// expiration, native tokens, storage deposit return unlocks conditions.
query := &apimodels.BasicOutputsQuery{
AddressBech32: faucetAddressRestricted.Bech32(deps.NodeBridge.APIProvider().CommittedAPI().ProtocolParameters().Bech32HRP()),
IndexerTimelockParams: apimodels.IndexerTimelockParams{
HasTimelock: &falseCondition,
},
IndexerExpirationParams: apimodels.IndexerExpirationParams{
HasExpiration: &falseCondition,
},
IndexerStorageDepositParams: apimodels.IndexerStorageDepositParams{
HasStorageDepositReturn: &falseCondition,
},
IndexerNativeTokenParams: apimodels.IndexerNativeTokenParams{
HasNativeToken: &falseCondition,
},
}

result, err := indexer.Outputs(ctxRequest, query)
Expand All @@ -170,6 +160,7 @@ func provide(c *dig.Container) error {
if err != nil {
return nil, err
}

outputIDs := result.Response.Items.MustOutputIDs()

for i := range outputs {
Expand Down Expand Up @@ -259,10 +250,13 @@ func provide(c *dig.Container) error {
}

submitTransactionPayload := func(ctx context.Context, builder *builder.TransactionBuilder, signer iotago.AddressSigner, storedManaOutputIndex int, numPoWWorkers ...int) (iotago.BlockPayload, iotago.BlockID, error) {
Component.LogDebug("sending transaction payload...")
signedTx, blockCreatedResponse, err := deps.BlockIssuerClient.SendPayloadWithTransactionBuilder(ctx, builder, signer, storedManaOutputIndex, numPoWWorkers...)
if err != nil {
return nil, iotago.EmptyBlockID, err
}
//nolint:forcetypeassert // we can safely assume that this is a SignedTransaction
Component.LogDebugf("sent transaction payload, blockID: %s, txID: %s", blockCreatedResponse.BlockID, lo.Return1(signedTx.(*iotago.SignedTransaction).ID()))

return signedTx, blockCreatedResponse.BlockID, nil
}
Expand Down Expand Up @@ -302,28 +296,30 @@ func provide(c *dig.Container) error {

func run() error {

// create a background worker that handles the ledger updates
if err := Component.Daemon().BackgroundWorker("Faucet[LedgerUpdates]", func(ctx context.Context) {
if err := deps.NodeBridge.ListenToLedgerUpdates(ctx, 0, 0, func(update *nodebridge.LedgerUpdate) error {
createdOutputs := iotago.OutputIDs{}
for _, output := range update.Created {
createdOutputs = append(createdOutputs, output.GetOutputId().Unwrap())
// create a background worker that handles the accepted transactions
if err := Component.Daemon().BackgroundWorker("Faucet[ListenToAcceptedTransactions]", func(ctx context.Context) {
if err := deps.NodeBridge.ListenToAcceptedTransactions(ctx, func(tx *nodebridge.AcceptedTransaction) error {
// create maps for faster lookup.
// outputs that are created and consumed in the same update exist in both maps.
createdOutputs := make(map[iotago.OutputID]struct{})
for _, output := range tx.Created {
createdOutputs[output.UnwrapOutputID()] = types.Void
}
consumedOutputs := iotago.OutputIDs{}
for _, spent := range update.Consumed {
consumedOutputs = append(consumedOutputs, spent.GetOutput().GetOutputId().Unwrap())
consumedOutputs := make(map[iotago.OutputID]struct{})
for _, spent := range tx.Consumed {
consumedOutputs[spent.GetOutput().UnwrapOutputID()] = types.Void
}

err := deps.Faucet.ApplyNewLedgerUpdate(createdOutputs, consumedOutputs)
err := deps.Faucet.ApplyAcceptedTransaction(createdOutputs, consumedOutputs)
if err != nil {
deps.ShutdownHandler.SelfShutdown(fmt.Sprintf("faucet plugin hit a critical error while applying new ledger update: %s", err.Error()), true)
deps.ShutdownHandler.SelfShutdown(fmt.Sprintf("faucet plugin hit a critical error while applying new accepted transaction: %s", err.Error()), true)
}

return err
}); err != nil {
deps.ShutdownHandler.SelfShutdown(fmt.Sprintf("Listening to LedgerUpdates failed, error: %s", err), false)
deps.ShutdownHandler.SelfShutdown(fmt.Sprintf("Listening to AcceptedTransactions failed, error: %s", err), false)
}
}, daemon.PriorityStopFaucetLedgerUpdates); err != nil {
}, daemon.PriorityStopFaucetAcceptedTransactions); err != nil {
Component.LogPanicf("failed to start worker: %s", err)
}

Expand Down
2 changes: 1 addition & 1 deletion components/faucet/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func enforceMaxOneDotPerURL(next echo.HandlerFunc) echo.HandlerFunc {
}

func addFaucetOutputToQueue(c echo.Context) (*faucet.EnqueueResponse, error) {
request := &faucetEnqueueRequest{}
request := &faucet.EnqueueRequest{}
if err := c.Bind(request); err != nil {
return nil, ierrors.Wrapf(httpserver.ErrInvalidParameter, "Invalid Request! Error: %s", err)
}
Expand Down
7 changes: 0 additions & 7 deletions components/faucet/types.go

This file was deleted.

28 changes: 14 additions & 14 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ go 1.21
require (
github.com/iotaledger/hive.go/app v0.0.0-20231020115340-13da292c580b
github.com/iotaledger/hive.go/crypto v0.0.0-20231020115340-13da292c580b
github.com/iotaledger/hive.go/ds v0.0.0-20231020115340-13da292c580b
github.com/iotaledger/hive.go/ierrors v0.0.0-20231020115340-13da292c580b
github.com/iotaledger/hive.go/lo v0.0.0-20231020115340-13da292c580b
github.com/iotaledger/hive.go/logger v0.0.0-20231020115340-13da292c580b
github.com/iotaledger/hive.go/runtime v0.0.0-20231020115340-13da292c580b
github.com/iotaledger/inx-app v1.0.0-rc.3.0.20231020152103-b6ea7ff7a4af
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231020151337-569450d5bf7d
github.com/iotaledger/iota.go/v4 v4.0.0-20231019174124-aa2290512bcd
github.com/iotaledger/inx-app v1.0.0-rc.3.0.20231023191159-38919c4705e0
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231023190837-6e7b2cdfd4fd
github.com/iotaledger/iota.go/v4 v4.0.0-20231023205010-58a0b5c7fe6d
github.com/labstack/echo/v4 v4.11.2
go.uber.org/dig v1.17.1
golang.org/x/time v0.3.0
Expand All @@ -33,36 +35,34 @@ require (
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-github v17.0.0+incompatible // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/pprof v0.0.0-20230821062121-407c9e7a662f // indirect
github.com/google/pprof v0.0.0-20230926050212-f7f687d19a98 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 // indirect
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
github.com/hashicorp/go-version v1.6.0 // indirect
github.com/holiman/uint256 v1.2.3 // indirect
github.com/iancoleman/orderedmap v0.3.0 // indirect
github.com/iotaledger/hive.go/constraints v0.0.0-20231020115340-13da292c580b // indirect
github.com/iotaledger/hive.go/core v1.0.0-rc.3.0.20231005142627-86973b2edb3b // indirect
github.com/iotaledger/hive.go/ds v0.0.0-20231019081410-32f61b05bebe // indirect
github.com/iotaledger/hive.go/lo v0.0.0-20231020115340-13da292c580b // indirect
github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1.0.20231019081410-32f61b05bebe // indirect
github.com/iotaledger/hive.go/core v1.0.0-rc.3.0.20231020115340-13da292c580b // indirect
github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1.0.20231020115340-13da292c580b // indirect
github.com/iotaledger/hive.go/stringify v0.0.0-20231020115340-13da292c580b // indirect
github.com/knadh/koanf v1.5.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/labstack/gommon v0.4.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/mr-tron/base58 v1.2.0 // indirect
github.com/pasztorpisti/qs v0.0.0-20171216220353-8d6c33ee906c // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/petermattis/goid v0.0.0-20230904192822-1876fd5063bc // indirect
github.com/prometheus/client_golang v1.16.0 // indirect
github.com/prometheus/client_model v0.4.0 // indirect
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.11.1 // indirect
github.com/prometheus/client_golang v1.17.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.45.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/sasha-s/go-deadlock v0.3.1 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
Expand Down
Loading

0 comments on commit 935b512

Please sign in to comment.