Skip to content

Commit

Permalink
Merge pull request #39 from iotaledger/feat/delegation
Browse files Browse the repository at this point in the history
Delegate
  • Loading branch information
cyberphysic4l authored Jan 23, 2024
2 parents ac72e2c + a1d8639 commit b0df89b
Show file tree
Hide file tree
Showing 48 changed files with 1,553 additions and 601 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,29 @@ Possible commands:
Possible
Spam with scenario `tx`
```bash
./evil-tools spammer --eviltools.spammer.type tx --eviltools.spammer.rate 10 --eviltools.spammer.duration 100s
./evil-tools spammer --spammer.type tx --spammer.rate 10 --spammer.duration 100s
```
Infinite spam is enabled when no duration flag is provided.
```bash
./evil-tools spammer --eviltools.spammer.type tx --eviltools.spammer.rate 10
./evil-tools spammer --spammer.type tx --spammer.rate 10
```
You can provide urls for clients:
```bash
./evil-tools spammer --eviltools.spammer.urls "http://localhost:8050,http://localhost:8060" --eviltools.spammer.type tx --eviltools.spammer.rate 10
./evil-tools spammer --spammer.urls "http://localhost:8050,http://localhost:8060" --spammer.type tx --spammer.rate 10
```
Enable deep spam:
```bash
./evil-tools spammer --eviltools.spammer.type tx --eviltools.spammer.rate 10 --eviltools.spammer.duration 100s --eviltools.spammer.deep
./evil-tools spammer --spammer.type tx --spammer.rate 10 --spammer.duration 100s --spammer.deep
```

### Examples for the accounts
Create implicit account with alias `A`:
```bash
./evil-tools accounts create --eviltools.accounts.create.alias A --eviltools.accounts.create.implicit
./evil-tools accounts create --accounts.create.alias A --accounts.create.implicit
```
Create account with genesis account paying for creation transaction:
```bash
./evil-tools accounts create --eviltools.accounts.create.alias A
./evil-tools accounts create --accounts.create.alias A
```

### Scenario diagrams:
Expand Down
144 changes: 144 additions & 0 deletions components/accounts/component.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package accounts

import (
"context"
"os"

"go.uber.org/dig"

"github.com/iotaledger/evil-tools/pkg/accountwallet"
"github.com/iotaledger/hive.go/app"
"github.com/iotaledger/hive.go/ierrors"
)

const (
ScriptName = "accounts"
)

func init() {
Component = &app.Component{
Name: "Accounts",
Params: params,
DepsFunc: func(cDeps dependencies) { deps = cDeps },
Run: run,
Provide: func(c *dig.Container) error {
return c.Provide(provideWallet)
},
IsEnabled: func(_ *dig.Container) bool { return true },
}
}

var (
Component *app.Component
deps dependencies
)

type dependencies struct {
dig.In

AccountWallets *accountwallet.AccountWallets
}

func run() error {
Component.LogInfo("Starting evil-tools accounts ... done")
// save wallet state on shutdown
defer func() {
err := accountwallet.SaveState(deps.AccountWallets)
if err != nil {
Component.LogErrorf("Error while saving wallet state: %v", err)
}
}()

accountsSubcommandsFlags := parseAccountCommands(getCommands(os.Args[2:]), ParamsAccounts)
accountsSubcommands(
Component.Daemon().ContextStopped(),
deps.AccountWallets,
accountsSubcommandsFlags,
)

return nil
}

func provideWallet() *accountwallet.AccountWallets {
// load wallet
accWallet, err := accountwallet.Run(Component.Daemon().ContextStopped(), Component.Logger,
accountwallet.WithClientURL(ParamsAccounts.NodeURLs[0]),
accountwallet.WithFaucetURL(ParamsAccounts.FaucetURL),
accountwallet.WithAccountStatesFile(ParamsAccounts.AccountStatesFile),
accountwallet.WithFaucetAccountParams(&accountwallet.GenesisAccountParams{
FaucetPrivateKey: ParamsAccounts.BlockIssuerPrivateKey,
FaucetAccountID: ParamsAccounts.AccountID,
}),
)
if err != nil {
Component.LogPanic(err.Error())
}

return accWallet
}

func accountsSubcommands(ctx context.Context, wallets *accountwallet.AccountWallets, subcommands []accountwallet.AccountSubcommands) {
for _, sub := range subcommands {
err := accountsSubcommand(ctx, wallets, sub)
if err != nil {
Component.LogFatal(ierrors.Wrap(err, "failed to run subcommand").Error())

return
}
}
}

//nolint:all,forcetypassert
func accountsSubcommand(ctx context.Context, wallets *accountwallet.AccountWallets, subCommand accountwallet.AccountSubcommands) error {
Component.LogInfof("Run subcommand: %s, with parameter set: %v", subCommand.Type().String(), subCommand)

switch subCommand.Type() {
case accountwallet.OperationCreateAccount:
//nolint:forcetypassert // we can safely assume that the type is correct
accParams := subCommand.(*accountwallet.CreateAccountParams)

accountID, err := wallets.CreateAccount(ctx, accParams)
if err != nil {
return ierrors.Wrap(err, "failed to create account")
}

Component.LogInfof("Created account %s", accountID)

case accountwallet.OperationDestroyAccount:
//nolint:forcetypassert // we can safely assume that the type is correct
accParams := subCommand.(*accountwallet.DestroyAccountParams)

if err := wallets.DestroyAccount(ctx, accParams); err != nil {
return ierrors.Wrap(err, "failed to destroy account")
}

case accountwallet.OperationAllotAccount:
//nolint:forcetypassert // we can safely assume that the type is correct
accParams := subCommand.(*accountwallet.AllotAccountParams)

if err := wallets.AllotToAccount(accParams); err != nil {
return ierrors.Wrap(err, "failed to allot to account")
}

case accountwallet.OperationDelegateAccount:
//nolint:forcetypassert // we can safely assume that the type is correct
params := subCommand.(*accountwallet.DelegateAccountParams)

if err := wallets.DelegateToAccount(ctx, params); err != nil {
return ierrors.Wrap(err, "failed to delegate to account")
}

case accountwallet.OperationRewardsAccount:
//nolint:forcetypassert // we can safely assume that the type is correct
params := subCommand.(*accountwallet.RewardsAccountParams)

if err := wallets.Rewards(ctx, params); err != nil {
return ierrors.Wrap(err, "failed to get rewards")
}

default:
return ierrors.New("unknown subcommand")
}

return nil
}
40 changes: 36 additions & 4 deletions pkg/accountwallet/params.go → components/accounts/params.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
package accountwallet
package accounts

import (
"github.com/iotaledger/hive.go/app"
)

type (
ParametersAccountsCreate struct {
Expand Down Expand Up @@ -30,10 +34,15 @@ type (
EndEpoch int64 `default:"0" usage:"The end epoch of the account to stake"`
}

ParametersRewards struct {
Alias string `default:"" usage:"The alias name of the wallet to get rewards for"`
}

ParametersAccountsDelegate struct {
FromAccount string `default:"" usage:"The alias name of the account to delegate mana from"`
ToAccount string `default:"" usage:"The alias of the account to delegate mana to"`
Amount int64 `default:"100" usage:"The amount of mana to delegate"`
FromAlias string `default:"" usage:"The alias of the account to delegate IOTA tokens from"`
ToAddress string `default:"rms1pzg8cqhfxqhq7pt37y8cs4v5u4kcc48lquy2k73ehsdhf5ukhya3y5rx2w6" usage:"The account address of the account to delegate IOTA tokens to"`
Amount int64 `default:"100" usage:"The amount of mana to delegate"`
CheckPool bool `default:"false" usage:"Check if the delegation is added to pool stake when the start epoch is committed"`
}

ParametersAccountsUpdate struct {
Expand All @@ -44,7 +53,15 @@ type (
ExpirySlot int64 `default:"0" usage:"Update the expiry slot of the account"`
}

ParameterAccountsInfo struct {
Alias string `default:"" usage:"Alias name of the account to get info"`
Verbose bool `default:"false" usage:"Verbose output"`
}

ParametersAccounts struct {
NodeURLs []string `default:"http://localhost:8050" usage:"API URLs for clients used in test separated with commas"`
FaucetURL string `default:"http://localhost:8088" usage:"Faucet URL used in test"`

AccountStatesFile string `default:"wallet.dat" usage:"File to store account states in"`
BlockIssuerPrivateKey string `default:"db39d2fde6301d313b108dc9db1ee724d0f405f6fde966bd776365bc5f4a5fb31e4b21eb51dcddf65c20db1065e1f1514658b23a3ddbf48d30c0efc926a9a648" usage:"Block issuer private key (in hex) to use for genesis account"`
AccountID string `default:"0x6aee704f25558e8aa7630fed0121da53074188abc423b3c5810f80be4936eb6e" usage:"Account ID to use for genesis account"`
Expand All @@ -54,7 +71,22 @@ type (
Destroy ParametersAccountsDestroy
Allot ParametersAccountsAllot
Stake ParametersAccountsStake
Rewards ParametersRewards
Delegate ParametersAccountsDelegate
Update ParametersAccountsUpdate
Info ParameterAccountsInfo
}
)

var ParamsAccounts = &ParametersAccounts{}

var params = &app.ComponentParams{
Params: map[string]any{
"accounts": ParamsAccounts,
},
Masked: []string{
"profiling",
"logger",
"app",
},
}
Loading

0 comments on commit b0df89b

Please sign in to comment.