From 1b159cc5b8d73501c69e629b01f055b7bace6de8 Mon Sep 17 00:00:00 2001 From: muXxer Date: Mon, 23 Oct 2023 22:56:18 +0200 Subject: [PATCH] Several cleanups --- components/faucet/component.go | 17 +++----------- components/faucet/routes.go | 2 +- components/faucet/types.go | 7 ------ go.mod | 6 ++--- go.sum | 12 +++++----- pkg/faucet/faucet.go | 22 ++++++++++++------ tools/faucetspam/main.go | 41 +++++++++++----------------------- tools/gendoc/go.mod | 6 ++--- tools/gendoc/go.sum | 12 +++++----- 9 files changed, 50 insertions(+), 75 deletions(-) delete mode 100644 components/faucet/types.go diff --git a/components/faucet/component.go b/components/faucet/component.go index 60493c2..a767ece 100644 --- a/components/faucet/component.go +++ b/components/faucet/component.go @@ -143,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) @@ -172,6 +160,7 @@ func provide(c *dig.Container) error { if err != nil { return nil, err } + outputIDs := result.Response.Items.MustOutputIDs() for i := range outputs { diff --git a/components/faucet/routes.go b/components/faucet/routes.go index 54da1a0..3980b18 100644 --- a/components/faucet/routes.go +++ b/components/faucet/routes.go @@ -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) } diff --git a/components/faucet/types.go b/components/faucet/types.go deleted file mode 100644 index 47e9e6b..0000000 --- a/components/faucet/types.go +++ /dev/null @@ -1,7 +0,0 @@ -package faucet - -// faucetEnqueueRequest defines the request for a POST RouteFaucetEnqueue REST API call. -type faucetEnqueueRequest struct { - // The bech32 address. - Address string `json:"address"` -} diff --git a/go.mod b/go.mod index 3b506d2..8a56b41 100644 --- a/go.mod +++ b/go.mod @@ -10,9 +10,9 @@ require ( 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.20231023074100-348fe7fe4ec3 - github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231021205014-392b75609cc2 - github.com/iotaledger/iota.go/v4 v4.0.0-20231023073721-97f39c627159 + 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 diff --git a/go.sum b/go.sum index 7522692..a24759b 100644 --- a/go.sum +++ b/go.sum @@ -206,12 +206,12 @@ github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1.0.20231020115340-13da292 github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1.0.20231020115340-13da292c580b/go.mod h1:SdK26z8/VhWtxaqCuQrufm80SELgowQPmu9T/8eUQ8g= github.com/iotaledger/hive.go/stringify v0.0.0-20231020115340-13da292c580b h1:MDZhTZTVDiydXcW5j4TA7HixVCyAdToIMPhHfJee7cE= github.com/iotaledger/hive.go/stringify v0.0.0-20231020115340-13da292c580b/go.mod h1:FTo/UWzNYgnQ082GI9QVM9HFDERqf9rw9RivNpqrnTs= -github.com/iotaledger/inx-app v1.0.0-rc.3.0.20231023074100-348fe7fe4ec3 h1:7+fCfCWDtt2Ejs0F/hatDh+BUHiiM0a+ZSBWuVRUknQ= -github.com/iotaledger/inx-app v1.0.0-rc.3.0.20231023074100-348fe7fe4ec3/go.mod h1:9MAg2H+eUQZoTAnnYa7wBHtPQS+ahOXsEqYexp2vAYY= -github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231021205014-392b75609cc2 h1:xC5hFWN5wSGvEFjPWUWdmlj6UuYJOo72T0r0AfbzRUs= -github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231021205014-392b75609cc2/go.mod h1:8CiEU9nM9q37wtYBnElactUR6RiZ2IGXX7RRtswRuVI= -github.com/iotaledger/iota.go/v4 v4.0.0-20231023073721-97f39c627159 h1:Ef2eDsbsIAtcAr4SwGkV3RUkAdyCMbn0rhFSiGHLXIk= -github.com/iotaledger/iota.go/v4 v4.0.0-20231023073721-97f39c627159/go.mod h1:jqbLYq4a/FwuiPBqFfkAwwxU8vs3+kReRq2/tyX5qRA= +github.com/iotaledger/inx-app v1.0.0-rc.3.0.20231023191159-38919c4705e0 h1:/8pbFXhTSroJvjJMfJqfHjzoT9N8B4LUY3SbKruD5MM= +github.com/iotaledger/inx-app v1.0.0-rc.3.0.20231023191159-38919c4705e0/go.mod h1:My1SB4vZj42EgTDNJ/dgW8lUpLNmvtzu8f89J5y2kP0= +github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231023190837-6e7b2cdfd4fd h1:hh5mAnnaZHOYAi4CIqR9K/mv786ex9AQgpisbJ4ZMow= +github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231023190837-6e7b2cdfd4fd/go.mod h1:MK0SHfNicBmcaZb3qS3tA8NEJIWKNbcNtNNKuSDKqXY= +github.com/iotaledger/iota.go/v4 v4.0.0-20231023205010-58a0b5c7fe6d h1:vGfZmcCCloTzim6TysS3fXxc9xsTfXoVB6bsedZgp7A= +github.com/iotaledger/iota.go/v4 v4.0.0-20231023205010-58a0b5c7fe6d/go.mod h1:jqbLYq4a/FwuiPBqFfkAwwxU8vs3+kReRq2/tyX5qRA= github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc= diff --git a/pkg/faucet/faucet.go b/pkg/faucet/faucet.go index 82923a4..d3b0363 100644 --- a/pkg/faucet/faucet.go +++ b/pkg/faucet/faucet.go @@ -105,6 +105,12 @@ type InfoResponse struct { Bech32HRP iotago.NetworkPrefix `json:"bech32Hrp"` } +// EnqueueRequest defines the request for a POST RouteFaucetEnqueue REST API call. +type EnqueueRequest struct { + // The bech32 address. + Address string `json:"address"` +} + // EnqueueResponse defines the response of a POST RouteFaucetEnqueue REST API call. type EnqueueResponse struct { // The bech32 address. @@ -127,7 +133,8 @@ type Faucet struct { // used to fetch metadata of a transaction from the node. fetchTransactionMetadataFunc FetchTransactionMetadataFunc // used to collect the unlockable outputs and the balance of the faucet. - collectUnlockableFaucetOutputsAndBalanceFunc CollectUnlockableFaucetOutputsAndBalanceFunc + // write lock must be acquired outside. + collectUnlockableFaucetOutputsAndBalanceFuncWithoutLocking CollectUnlockableFaucetOutputsAndBalanceFunc // used to compute the unlockable balance of an address. computeUnlockableAddressBalanceFunc ComputeUnlockableAddressBalanceFunc // used to create a signed transaction payload and send it to a block issuer. @@ -297,7 +304,8 @@ func New( }, } - faucet.collectUnlockableFaucetOutputsAndBalanceFunc = func() ([]UTXOBasicOutput, iotago.BaseToken, error) { + // write lock must be acquired outside. + faucet.collectUnlockableFaucetOutputsAndBalanceFuncWithoutLocking = func() ([]UTXOBasicOutput, iotago.BaseToken, error) { // get all outputs of the faucet unspentOutputs, err := collectUnlockableFaucetOutputsFunc() if err != nil { @@ -698,14 +706,14 @@ func (f *Faucet) sendFaucetBlock(ctx context.Context, unspentOutputs []UTXOBasic // computeAndSetFaucetBalance computes the faucet balance minus the storage deposit for a single basic output. func (f *Faucet) computeAndSetFaucetBalance() error { - _, balance, err := f.collectUnlockableFaucetOutputsAndBalanceFunc() + f.Lock() + defer f.Unlock() + + _, balance, err := f.collectUnlockableFaucetOutputsAndBalanceFuncWithoutLocking() if err != nil { return err } - f.Lock() - defer f.Unlock() - f.faucetBalance = balance return nil @@ -751,7 +759,7 @@ func (f *Faucet) collectRequestsAndSendFaucetBlock(ctx context.Context) error { f.LogDebugf("collected %d requests", len(batchedRequests)) processRequests := func() ([]UTXOBasicOutput, []*queueItem, error) { - unspentOutputs, balance, err := f.collectUnlockableFaucetOutputsAndBalanceFunc() + unspentOutputs, balance, err := f.collectUnlockableFaucetOutputsAndBalanceFuncWithoutLocking() if err != nil { return nil, nil, err } diff --git a/tools/faucetspam/main.go b/tools/faucetspam/main.go index b3bd783..4cb8b1e 100755 --- a/tools/faucetspam/main.go +++ b/tools/faucetspam/main.go @@ -7,48 +7,33 @@ import ( "encoding/hex" "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" + "github.com/iotaledger/inx-faucet/pkg/faucet" iotago "github.com/iotaledger/iota.go/v4" + "github.com/iotaledger/iota.go/v4/tpkg" ) const ( - url = "https://faucet.tanglekit.de/api/enqueue" - //url = "http://localhost:14265/api/plugins/faucet/enqueue" -) - -// faucetEnqueueRequest defines the request for a POST RouteFaucetEnqueue REST API call. -type faucetEnqueueRequest struct { - // The bech32 address. - Address string `json:"address"` -} + url = "http://localhost:8088/api/enqueue" -// FaucetEnqueueResponse defines the response of a POST RouteFaucetEnqueue REST API call. -type FaucetEnqueueResponse struct { - // The bech32 address. - Address string `json:"address"` - // The number of waiting requests in the queue. - WaitingRequests int `json:"waitingRequests"` -} + network = iotago.PrefixTestnet +) func main() { - for i := 0; i < 1; i++ { - pubKey, privKey, err := ed25519.GenerateKey(nil) - if err != nil { - panic(err) - } + for i := 0; i < 1000; i++ { + pubKey := ed25519.PublicKey(tpkg.RandBytes(32)) addr := iotago.Ed25519AddressFromPubKey(pubKey) - fmt.Println("Your ed25519 private key: ", hex.EncodeToString(privKey)) fmt.Println("Your ed25519 public key: ", hex.EncodeToString(pubKey)) fmt.Println("Your ed25519 address: ", hex.EncodeToString(addr[:])) - fmt.Println("Your bech32 address: ", addr.Bech32("atoi")) + fmt.Println("Your bech32 address: ", addr.Bech32(network)) - jsonValue, _ := json.Marshal(&faucetEnqueueRequest{ - Address: addr.Bech32("atoi"), + jsonValue, _ := json.Marshal(&faucet.EnqueueRequest{ + Address: addr.Bech32(network), }) resp, err := http.Post(url, "application/json", bytes.NewBuffer(jsonValue)) @@ -61,12 +46,12 @@ func main() { panic(fmt.Errorf("http status code: %d", resp.StatusCode)) } - responseBytes, err := ioutil.ReadAll(resp.Body) + responseBytes, err := io.ReadAll(resp.Body) if err != nil { panic(fmt.Errorf("unable to read response: %w", err)) } - res := FaucetEnqueueResponse{} + res := faucet.EnqueueResponse{} err = json.Unmarshal(responseBytes, &res) if err != nil { panic(fmt.Errorf("unable to unmarshal response: %w", err)) diff --git a/tools/gendoc/go.mod b/tools/gendoc/go.mod index 3c2bcdb..9637ed0 100644 --- a/tools/gendoc/go.mod +++ b/tools/gendoc/go.mod @@ -44,9 +44,9 @@ require ( github.com/iotaledger/hive.go/runtime v0.0.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/iotaledger/inx-app v1.0.0-rc.3.0.20231023074100-348fe7fe4ec3 // indirect - github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231021205014-392b75609cc2 // indirect - github.com/iotaledger/iota.go/v4 v4.0.0-20231023073721-97f39c627159 // indirect + github.com/iotaledger/inx-app v1.0.0-rc.3.0.20231023191159-38919c4705e0 // indirect + github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231023190837-6e7b2cdfd4fd // indirect + github.com/iotaledger/iota.go/v4 v4.0.0-20231023205010-58a0b5c7fe6d // indirect github.com/knadh/koanf v1.5.0 // indirect github.com/kr/text v0.2.0 // indirect github.com/labstack/echo/v4 v4.11.2 // indirect diff --git a/tools/gendoc/go.sum b/tools/gendoc/go.sum index 5591c00..1df2a77 100644 --- a/tools/gendoc/go.sum +++ b/tools/gendoc/go.sum @@ -210,12 +210,12 @@ github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1.0.20231020115340-13da292 github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1.0.20231020115340-13da292c580b/go.mod h1:SdK26z8/VhWtxaqCuQrufm80SELgowQPmu9T/8eUQ8g= github.com/iotaledger/hive.go/stringify v0.0.0-20231020115340-13da292c580b h1:MDZhTZTVDiydXcW5j4TA7HixVCyAdToIMPhHfJee7cE= github.com/iotaledger/hive.go/stringify v0.0.0-20231020115340-13da292c580b/go.mod h1:FTo/UWzNYgnQ082GI9QVM9HFDERqf9rw9RivNpqrnTs= -github.com/iotaledger/inx-app v1.0.0-rc.3.0.20231023074100-348fe7fe4ec3 h1:7+fCfCWDtt2Ejs0F/hatDh+BUHiiM0a+ZSBWuVRUknQ= -github.com/iotaledger/inx-app v1.0.0-rc.3.0.20231023074100-348fe7fe4ec3/go.mod h1:9MAg2H+eUQZoTAnnYa7wBHtPQS+ahOXsEqYexp2vAYY= -github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231021205014-392b75609cc2 h1:xC5hFWN5wSGvEFjPWUWdmlj6UuYJOo72T0r0AfbzRUs= -github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231021205014-392b75609cc2/go.mod h1:8CiEU9nM9q37wtYBnElactUR6RiZ2IGXX7RRtswRuVI= -github.com/iotaledger/iota.go/v4 v4.0.0-20231023073721-97f39c627159 h1:Ef2eDsbsIAtcAr4SwGkV3RUkAdyCMbn0rhFSiGHLXIk= -github.com/iotaledger/iota.go/v4 v4.0.0-20231023073721-97f39c627159/go.mod h1:jqbLYq4a/FwuiPBqFfkAwwxU8vs3+kReRq2/tyX5qRA= +github.com/iotaledger/inx-app v1.0.0-rc.3.0.20231023191159-38919c4705e0 h1:/8pbFXhTSroJvjJMfJqfHjzoT9N8B4LUY3SbKruD5MM= +github.com/iotaledger/inx-app v1.0.0-rc.3.0.20231023191159-38919c4705e0/go.mod h1:My1SB4vZj42EgTDNJ/dgW8lUpLNmvtzu8f89J5y2kP0= +github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231023190837-6e7b2cdfd4fd h1:hh5mAnnaZHOYAi4CIqR9K/mv786ex9AQgpisbJ4ZMow= +github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231023190837-6e7b2cdfd4fd/go.mod h1:MK0SHfNicBmcaZb3qS3tA8NEJIWKNbcNtNNKuSDKqXY= +github.com/iotaledger/iota.go/v4 v4.0.0-20231023205010-58a0b5c7fe6d h1:vGfZmcCCloTzim6TysS3fXxc9xsTfXoVB6bsedZgp7A= +github.com/iotaledger/iota.go/v4 v4.0.0-20231023205010-58a0b5c7fe6d/go.mod h1:jqbLYq4a/FwuiPBqFfkAwwxU8vs3+kReRq2/tyX5qRA= github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=