Skip to content

Commit

Permalink
Move utils.go to utils directory
Browse files Browse the repository at this point in the history
  • Loading branch information
jkrvivian committed Nov 1, 2023
1 parent 2d2396c commit da5ffc0
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 35 deletions.
28 changes: 3 additions & 25 deletions accountwallet/faucet.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/mr-tron/base58"

"github.com/iotaledger/evil-tools/models"
"github.com/iotaledger/evil-tools/utils"
"github.com/iotaledger/hive.go/ierrors"
"github.com/iotaledger/hive.go/lo"
"github.com/iotaledger/iota-core/pkg/blockhandler"
Expand Down Expand Up @@ -45,32 +46,9 @@ func (a *AccountWallet) RequestFaucetFunds(clt models.Client, receiveAddr iotago
return nil, ierrors.Wrap(err, "failed to request funds from faucet")
}

indexer, err := clt.Indexer()
if err != nil {
return nil, ierrors.Wrap(err, "failed to get indexer client")
}

addrBech := receiveAddr.Bech32(clt.CommittedAPI().ProtocolParameters().Bech32HRP())

time.Sleep(10 * time.Second)

res, err := indexer.Outputs(context.Background(), &apimodels.BasicOutputsQuery{
AddressBech32: addrBech,
})
outputID, outputStruct, err := utils.AwaitAddressUnspentOutputToBeAccepted(clt, receiveAddr, 10*time.Second)
if err != nil {
return nil, ierrors.Wrap(err, "indexer request failed in request faucet funds")
}

var outputStruct iotago.Output
var outputID iotago.OutputID
for res.Next() {
unspents, err := res.Outputs(context.TODO())
if err != nil {
return nil, ierrors.Wrap(err, "failed to get faucet unspent outputs")
}

outputStruct = unspents[0]
outputID = lo.Return1(res.Response.Items.OutputIDs())[0]
return nil, ierrors.Wrap(err, "failed to await faucet funds")
}

return &models.Output{
Expand Down
7 changes: 4 additions & 3 deletions evilwallet/evilwallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/iotaledger/evil-tools/accountwallet"
evillogger "github.com/iotaledger/evil-tools/logger"
"github.com/iotaledger/evil-tools/models"
"github.com/iotaledger/evil-tools/utils"
"github.com/iotaledger/hive.go/ds/types"
"github.com/iotaledger/hive.go/ierrors"
"github.com/iotaledger/hive.go/lo"
Expand Down Expand Up @@ -285,7 +286,7 @@ func (e *EvilWallet) requestFaucetFunds(wallet *Wallet) (output *models.Output,
return nil, ierrors.Wrap(err, "failed to request funds from faucet")
}

outputID, iotaOutput, err := AwaitAddressUnspentOutputToBeAccepted(clt, receiveAddr, 10*time.Second)
outputID, iotaOutput, err := utils.AwaitAddressUnspentOutputToBeAccepted(clt, receiveAddr, 10*time.Second)
if err != nil {
return nil, ierrors.Wrap(err, "failed to await faucet output acceptance")
}
Expand Down Expand Up @@ -371,7 +372,7 @@ func (e *EvilWallet) splitOutputs(inputWallet, outputWallet *Wallet) ([]iotago.T
}

func (e *EvilWallet) createSplitOutputs(input *models.Output, splitNumber int, receiveWallet *Wallet) []*OutputOption {
balances := SplitBalanceEqually(splitNumber, input.Balance)
balances := utils.SplitBalanceEqually(splitNumber, input.Balance)
outputs := make([]*OutputOption, splitNumber)
for i, bal := range balances {
outputs[i] = &OutputOption{amount: bal, address: receiveWallet.Address(), outputType: iotago.OutputBasic}
Expand Down Expand Up @@ -744,7 +745,7 @@ func (e *EvilWallet) updateOutputBalances(buildOptions *Options) (err error) {
totalBalance += in.Balance
}
}
balances := SplitBalanceEqually(len(buildOptions.outputs)+len(buildOptions.aliasOutputs), totalBalance)
balances := utils.SplitBalanceEqually(len(buildOptions.outputs)+len(buildOptions.aliasOutputs), totalBalance)
i := 0
for out, output := range buildOptions.aliasOutputs {
switch output.Type() {
Expand Down
5 changes: 3 additions & 2 deletions evilwallet/output_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"go.uber.org/atomic"

"github.com/iotaledger/evil-tools/models"
"github.com/iotaledger/evil-tools/utils"
"github.com/iotaledger/hive.go/ds/types"
"github.com/iotaledger/hive.go/logger"
"github.com/iotaledger/hive.go/runtime/syncutils"
Expand Down Expand Up @@ -116,7 +117,7 @@ func (o *OutputManager) Track(outputIDs ...iotago.OutputID) (allConfirmed bool)
go func(id iotago.OutputID, clt models.Client) {
defer wg.Done()

if !AwaitOutputToBeAccepted(clt, id, awaitOutputToBeConfirmed) {
if !utils.AwaitOutputToBeAccepted(clt, id, awaitOutputToBeConfirmed) {
unconfirmedOutputFound.Store(true)
}
}(ID, o.connector.GetClient())
Expand Down Expand Up @@ -238,7 +239,7 @@ func (o *OutputManager) AwaitTransactionsAcceptance(txIDs ...iotago.TransactionI
defer func() {
<-semaphore
}()
err := AwaitTransactionToBeAccepted(clt, txID, waitForAcceptance, txLeft)
err := utils.AwaitTransactionToBeAccepted(clt, txID, waitForAcceptance, txLeft)
txLeft.Dec()
if err != nil {
o.log.Errorf("Error awaiting transaction %s to be accepted: %s", txID.String(), err)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ require (
github.com/AlecAivazis/survey/v2 v2.3.7
github.com/google/martian v2.1.0+incompatible
github.com/iotaledger/hive.go/app v0.0.0-20231020115340-13da292c580b
github.com/iotaledger/hive.go/core v1.0.0-rc.3.0.20231020115340-13da292c580b
github.com/iotaledger/hive.go/ds v0.0.0-20231020115340-13da292c580b
github.com/iotaledger/hive.go/ierrors v0.0.0-20231024193930-47c6046e38a8
github.com/iotaledger/hive.go/lo v0.0.0-20231020115340-13da292c580b
Expand Down Expand Up @@ -40,6 +39,7 @@ require (
github.com/iotaledger/grocksdb v1.7.5-0.20230220105546-5162e18885c7 // indirect
github.com/iotaledger/hive.go/ads v0.0.0-20231020115340-13da292c580b // 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.20231020115340-13da292c580b // indirect
github.com/iotaledger/hive.go/crypto v0.0.0-20231020115340-13da292c580b // indirect
github.com/iotaledger/hive.go/kvstore v0.0.0-20231020115340-13da292c580b // indirect
github.com/iotaledger/hive.go/log v0.0.0-20231020115340-13da292c580b // indirect
Expand Down
4 changes: 2 additions & 2 deletions spammer/spamming_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"sync"
"time"

"github.com/iotaledger/evil-tools/evilwallet"
"github.com/iotaledger/evil-tools/models"
"github.com/iotaledger/evil-tools/utils"
"github.com/iotaledger/hive.go/ierrors"
"github.com/iotaledger/hive.go/lo"
iotago "github.com/iotaledger/iota.go/v4"
Expand Down Expand Up @@ -138,7 +138,7 @@ func createBlowBallCenter(s *Spammer) (iotago.BlockID, error) {
},
}, s.IssuerAlias, clt)

err := evilwallet.AwaitBlockToBeConfirmed(clt, centerID, 30*time.Second)
err := utils.AwaitBlockToBeConfirmed(clt, centerID, 30*time.Second)

return centerID, err
}
Expand Down
9 changes: 7 additions & 2 deletions evilwallet/utils.go → utils/utils.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package evilwallet
package utils

import (
"context"
Expand All @@ -15,6 +15,11 @@ import (

var UtilsLogger = evillogger.New("Utils")

const (
waitForAcceptance = 20 * time.Second
awaitAcceptationSleep = 1 * time.Second
)

// SplitBalanceEqually splits the balance equally between `splitNumber` outputs.
func SplitBalanceEqually(splitNumber int, balance iotago.BaseToken) []iotago.BaseToken {
outputBalances := make([]iotago.BaseToken, 0)
Expand Down Expand Up @@ -92,7 +97,7 @@ func AwaitTransactionToBeAccepted(clt models.Client, txID iotago.TransactionID,
return nil
}

func AwaitAddressUnspentOutputToBeAccepted(clt models.Client, addr *iotago.Ed25519Address, waitFor time.Duration) (outputID iotago.OutputID, output iotago.Output, err error) {
func AwaitAddressUnspentOutputToBeAccepted(clt models.Client, addr iotago.Address, waitFor time.Duration) (outputID iotago.OutputID, output iotago.Output, err error) {
indexer, err := clt.Indexer()
if err != nil {
return iotago.EmptyOutputID, nil, ierrors.Wrap(err, "failed to get indexer client")
Expand Down

0 comments on commit da5ffc0

Please sign in to comment.