Skip to content

Commit dfca42e

Browse files
committed
added generate_child_wallet task
1 parent 94aba8d commit dfca42e

File tree

5 files changed

+210
-2
lines changed

5 files changed

+210
-2
lines changed

pkg/coordinator/tasks/generate_blob_transactions/task.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,14 @@ func (t *Task) LoadConfig() error {
133133

134134
func (t *Task) Execute(ctx context.Context) error {
135135
if t.walletPool != nil {
136+
err := t.walletPool.GetRootWallet().AwaitReady(ctx)
137+
if err != nil {
138+
return err
139+
}
140+
136141
t.logger.Infof("funding wallet: %v [nonce: %v] %v ETH", t.walletPool.GetRootWallet().GetAddress().Hex(), t.walletPool.GetRootWallet().GetNonce(), t.walletPool.GetRootWallet().GetReadableBalance(18, 0, 4, false, false))
137142

138-
err := t.ensureChildWalletFunding(ctx)
143+
err = t.ensureChildWalletFunding(ctx)
139144
if err != nil {
140145
t.logger.Infof("failed ensuring child wallet funding: %v", err)
141146
return err
@@ -147,6 +152,11 @@ func (t *Task) Execute(ctx context.Context) error {
147152

148153
go t.runChildWalletFundingRoutine(ctx)
149154
} else {
155+
err := t.wallet.AwaitReady(ctx)
156+
if err != nil {
157+
return err
158+
}
159+
150160
t.logger.Infof("wallet: %v [nonce: %v] %v ETH", t.wallet.GetAddress().Hex(), t.wallet.GetNonce(), t.wallet.GetReadableBalance(18, 0, 4, false, false))
151161
}
152162

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package generatechildwallet
2+
3+
import (
4+
"errors"
5+
"math/big"
6+
)
7+
8+
type Config struct {
9+
PrivateKey string `yaml:"privateKey" json:"privateKey"`
10+
WalletSeed string `yaml:"walletSeed" json:"walletSeed"`
11+
RandomSeed bool `yaml:"randomSeed" json:"randomSeed"`
12+
13+
PrefundFeeCap *big.Int `yaml:"prefundFeeCap" json:"prefundFeeCap"`
14+
PrefundTipCap *big.Int `yaml:"prefundTipCap" json:"prefundTipCap"`
15+
PrefundAmount *big.Int `yaml:"prefundAmount" json:"prefundAmount"`
16+
PrefundMinBalance *big.Int `yaml:"prefundMinBalance" json:"prefundMinBalance"`
17+
18+
WalletAddressResultVar string `yaml:"walletAddressResultVar" json:"walletAddressResultVar"`
19+
WalletPrivateKeyResultVar string `yaml:"walletPrivateKeyResultVar" json:"walletPrivateKeyResultVar"`
20+
}
21+
22+
func DefaultConfig() Config {
23+
return Config{
24+
PrefundFeeCap: big.NewInt(500000000000), // 500 Gwei
25+
PrefundTipCap: big.NewInt(1000000000), // 1 Gwei
26+
PrefundAmount: big.NewInt(1000000000000000000), // 1 ETH
27+
PrefundMinBalance: big.NewInt(500000000000000000), // 0.5 ETH
28+
}
29+
}
30+
31+
func (c *Config) Validate() error {
32+
if c.PrivateKey == "" {
33+
return errors.New("privateKey must be set")
34+
}
35+
36+
return nil
37+
}
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
package generatechildwallet
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"math/rand"
7+
"time"
8+
9+
"github.com/ethereum/go-ethereum/crypto"
10+
"github.com/ethpandaops/assertoor/pkg/coordinator/types"
11+
"github.com/ethpandaops/assertoor/pkg/coordinator/wallet"
12+
"github.com/sirupsen/logrus"
13+
)
14+
15+
var (
16+
TaskName = "generate_child_wallet"
17+
TaskDescriptor = &types.TaskDescriptor{
18+
Name: TaskName,
19+
Description: "Generates a funded child wallet.",
20+
Config: DefaultConfig(),
21+
NewTask: NewTask,
22+
}
23+
)
24+
25+
type Task struct {
26+
ctx *types.TaskContext
27+
options *types.TaskOptions
28+
config Config
29+
logger logrus.FieldLogger
30+
wallet *wallet.Wallet
31+
}
32+
33+
func NewTask(ctx *types.TaskContext, options *types.TaskOptions) (types.Task, error) {
34+
return &Task{
35+
ctx: ctx,
36+
options: options,
37+
logger: ctx.Logger.GetLogger(),
38+
}, nil
39+
}
40+
41+
func (t *Task) Name() string {
42+
return TaskDescriptor.Name
43+
}
44+
45+
func (t *Task) Title() string {
46+
return t.ctx.Vars.ResolvePlaceholders(t.options.Title)
47+
}
48+
49+
func (t *Task) Description() string {
50+
return TaskDescriptor.Description
51+
}
52+
53+
func (t *Task) Config() interface{} {
54+
return t.config
55+
}
56+
57+
func (t *Task) Logger() logrus.FieldLogger {
58+
return t.logger
59+
}
60+
61+
func (t *Task) Timeout() time.Duration {
62+
return t.options.Timeout.Duration
63+
}
64+
65+
func (t *Task) LoadConfig() error {
66+
config := DefaultConfig()
67+
68+
// parse static config
69+
if t.options.Config != nil {
70+
if err := t.options.Config.Unmarshal(&config); err != nil {
71+
return fmt.Errorf("error parsing task config for %v: %w", TaskName, err)
72+
}
73+
}
74+
75+
// load dynamic vars
76+
err := t.ctx.Vars.ConsumeVars(&config, t.options.ConfigVars)
77+
if err != nil {
78+
return err
79+
}
80+
81+
// validate config
82+
if err2 := config.Validate(); err2 != nil {
83+
return err2
84+
}
85+
86+
// Load root wallet
87+
privKey, err := crypto.HexToECDSA(config.PrivateKey)
88+
if err != nil {
89+
return err
90+
}
91+
92+
t.wallet, err = t.ctx.Scheduler.GetCoordinator().WalletManager().GetWalletByPrivkey(privKey)
93+
if err != nil {
94+
return fmt.Errorf("cannot initialize wallet: %w", err)
95+
}
96+
97+
t.config = config
98+
99+
return nil
100+
}
101+
102+
func (t *Task) Execute(ctx context.Context) error {
103+
err := t.wallet.AwaitReady(ctx)
104+
if err != nil {
105+
return err
106+
}
107+
108+
t.logger.Infof("root wallet: %v [nonce: %v] %v ETH", t.wallet.GetAddress().Hex(), t.wallet.GetNonce(), t.wallet.GetReadableBalance(18, 0, 4, false, false))
109+
110+
walletSeed := t.config.WalletSeed
111+
if t.config.RandomSeed {
112+
walletSeed = t.randStringBytes(20)
113+
}
114+
115+
walletPool, err := t.ctx.Scheduler.GetCoordinator().WalletManager().GetWalletPoolByPrivkey(t.wallet.GetPrivateKey(), 1, walletSeed)
116+
if err != nil {
117+
return err
118+
}
119+
120+
err = walletPool.EnsureFunding(ctx, t.config.PrefundMinBalance, t.config.PrefundAmount, t.config.PrefundFeeCap, t.config.PrefundTipCap, 1)
121+
if err != nil {
122+
return err
123+
}
124+
125+
childWallet := walletPool.GetNextChildWallet()
126+
t.logger.Infof("child wallet: %v [nonce: %v] %v ETH", childWallet.GetAddress().Hex(), childWallet.GetNonce(), childWallet.GetReadableBalance(18, 0, 4, false, false))
127+
128+
if t.config.WalletAddressResultVar != "" {
129+
t.ctx.Vars.SetVar(t.config.WalletAddressResultVar, childWallet.GetAddress().Hex())
130+
}
131+
132+
if t.config.WalletPrivateKeyResultVar != "" {
133+
t.ctx.Vars.SetVar(t.config.WalletPrivateKeyResultVar, fmt.Sprintf("%x", crypto.FromECDSA(childWallet.GetPrivateKey())))
134+
}
135+
136+
return ctx.Err()
137+
}
138+
139+
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
140+
141+
func (t *Task) randStringBytes(n int) string {
142+
b := make([]byte, n)
143+
for i := range b {
144+
//nolint:gosec // ignore
145+
b[i] = letterBytes[rand.Intn(len(letterBytes))]
146+
}
147+
148+
return string(b)
149+
}

pkg/coordinator/tasks/generate_eoa_transactions/task.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,14 @@ func (t *Task) LoadConfig() error {
132132

133133
func (t *Task) Execute(ctx context.Context) error {
134134
if t.walletPool != nil {
135+
err := t.walletPool.GetRootWallet().AwaitReady(ctx)
136+
if err != nil {
137+
return err
138+
}
139+
135140
t.logger.Infof("funding wallet: %v [nonce: %v] %v ETH", t.walletPool.GetRootWallet().GetAddress().Hex(), t.walletPool.GetRootWallet().GetNonce(), t.walletPool.GetRootWallet().GetReadableBalance(18, 0, 4, false, false))
136141

137-
err := t.ensureChildWalletFunding(ctx)
142+
err = t.ensureChildWalletFunding(ctx)
138143
if err != nil {
139144
t.logger.Infof("failed ensuring child wallet funding: %v", err)
140145
return err
@@ -146,6 +151,11 @@ func (t *Task) Execute(ctx context.Context) error {
146151

147152
go t.runChildWalletFundingRoutine(ctx)
148153
} else {
154+
err := t.wallet.AwaitReady(ctx)
155+
if err != nil {
156+
return err
157+
}
158+
149159
t.logger.Infof("wallet: %v [nonce: %v] %v ETH", t.wallet.GetAddress().Hex(), t.wallet.GetNonce(), t.wallet.GetReadableBalance(18, 0, 4, false, false))
150160
}
151161

pkg/coordinator/tasks/tasks.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
checkexecutionsyncstatus "github.com/ethpandaops/assertoor/pkg/coordinator/tasks/check_execution_sync_status"
1717
generateblobtransactions "github.com/ethpandaops/assertoor/pkg/coordinator/tasks/generate_blob_transactions"
1818
generateblschanges "github.com/ethpandaops/assertoor/pkg/coordinator/tasks/generate_bls_changes"
19+
generatechildwallet "github.com/ethpandaops/assertoor/pkg/coordinator/tasks/generate_child_wallet"
1920
generatedeposits "github.com/ethpandaops/assertoor/pkg/coordinator/tasks/generate_deposits"
2021
generateeoatransactions "github.com/ethpandaops/assertoor/pkg/coordinator/tasks/generate_eoa_transactions"
2122
generateexits "github.com/ethpandaops/assertoor/pkg/coordinator/tasks/generate_exits"
@@ -45,6 +46,7 @@ var AvailableTaskDescriptors = []*types.TaskDescriptor{
4546
checkexecutionsyncstatus.TaskDescriptor,
4647
generateblobtransactions.TaskDescriptor,
4748
generateblschanges.TaskDescriptor,
49+
generatechildwallet.TaskDescriptor,
4850
generateeoatransactions.TaskDescriptor,
4951
generatedeposits.TaskDescriptor,
5052
generateexits.TaskDescriptor,

0 commit comments

Comments
 (0)