-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbase.go
More file actions
202 lines (166 loc) · 6.76 KB
/
base.go
File metadata and controls
202 lines (166 loc) · 6.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package scenarios
import (
"math/big"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/sei-protocol/sei-load/utils/seievmtx"
"github.com/sei-protocol/sei-load/config"
"github.com/sei-protocol/sei-load/generator/utils"
"github.com/sei-protocol/sei-load/types"
)
// bigOne is 1 in big.Int.
var bigOne = big.NewInt(1)
// TxGenerator defines the interface for generating transactions.
type TxGenerator interface {
Name() string
Generate(scenario *types.TxScenario) *types.LoadTx
Attach(config *config.LoadConfig, address common.Address) error
Deploy(config *config.LoadConfig, deployer *types.Account) common.Address
}
// ScenarioDeployer defines the interface for scenario-specific deployment logic
// This can be implemented by both contract and non-contract scenarios
type ScenarioDeployer interface {
// DeployScenario handles any setup required for the scenario
// For contracts: deploys the contract and returns its address
// For non-contracts: performs any initialization and returns zero address
DeployScenario(config *config.LoadConfig, deployer *types.Account) common.Address
// AttachScenario connects to an existing contract.
AttachScenario(config *config.LoadConfig, address common.Address) common.Address
// CreateTransaction creates a transaction for this scenario
CreateTransaction(config *config.LoadConfig, scenario *types.TxScenario) (*ethtypes.Transaction, error)
}
// ContractBindFunc defines a function that creates a contract instance from an address
type ContractBindFunc[T any] func(address common.Address, backend bind.ContractBackend) (*T, error)
// ContractDeployer defines the interface for contract-specific deployment logic
// This extends ScenarioDeployer with contract-specific methods
type ContractDeployer[T any] interface {
ScenarioDeployer
// DeployContract deploys the contract with specific constructor arguments
DeployContract(opts *bind.TransactOpts, client *ethclient.Client) (common.Address, *ethtypes.Transaction, error)
// GetBindFunc returns the function to bind the contract instance
GetBindFunc() ContractBindFunc[T]
// SetContract stores the bound contract instance
SetContract(contract *T)
// CreateContractTransaction creates a contract interaction transaction
CreateContractTransaction(auth *bind.TransactOpts, scenario *types.TxScenario) (*ethtypes.Transaction, error)
}
// ScenarioBase provides common functionality for all scenarios
type ScenarioBase struct {
config *config.LoadConfig
deployed bool
address common.Address
deployer ScenarioDeployer
scenarioConfig config.Scenario
}
// NewScenarioBase creates a new base scenario with the given deployer
func NewScenarioBase(deployer ScenarioDeployer, cfg config.Scenario) *ScenarioBase {
return &ScenarioBase{
deployer: deployer,
scenarioConfig: cfg,
}
}
// Deploy handles the common deployment flow
func (s *ScenarioBase) Deploy(config *config.LoadConfig, deployer *types.Account) common.Address {
s.config = config
s.address = s.deployer.DeployScenario(config, deployer)
s.deployed = true
return s.address
}
// Attach connects to an existing contract.
func (s *ScenarioBase) Attach(config *config.LoadConfig, address common.Address) error {
s.config = config
s.address = s.deployer.AttachScenario(config, address)
s.deployed = true
return nil
}
// Generate handles the common transaction generation flow
func (s *ScenarioBase) Generate(scenario *types.TxScenario) *types.LoadTx {
if !s.deployed {
panic("Scenario not deployed/initialized")
}
// Create transaction using scenario-specific logic
tx, err := s.deployer.CreateTransaction(s.config, scenario)
if err != nil {
panic("Failed to create transaction: " + err.Error())
}
cosmosTxBytes, _ := seievmtx.EncodeCosmosTxFromEthTx(tx)
res := types.CreateTxFromEthTx(tx, scenario)
res.CosmosTx = cosmosTxBytes
return res
}
// GetConfig returns the configuration
func (s *ScenarioBase) GetConfig() *config.LoadConfig {
return s.config
}
// GetAddress returns the deployed contract address (zero address for non-contract scenarios)
func (s *ScenarioBase) GetAddress() common.Address {
return s.address
}
// ContractScenarioBase provides common functionality for contract scenarios
type ContractScenarioBase[T any] struct {
*ScenarioBase
deployer ContractDeployer[T]
}
// NewContractScenarioBase creates a new base scenario with the given contract deployer
func NewContractScenarioBase[T any](deployer ContractDeployer[T], cfg config.Scenario) *ContractScenarioBase[T] {
base := &ContractScenarioBase[T]{
deployer: deployer,
}
base.ScenarioBase = NewScenarioBase(base, cfg)
return base
}
func dial(config *config.LoadConfig) (*ethclient.Client, error) {
if len(config.Endpoints) == 0 {
return ethclient.NewClient(nil), nil
}
return ethclient.Dial(config.Endpoints[0])
}
// AttachScenario implements AttachScenario interface for contract scenarios
func (c *ContractScenarioBase[T]) AttachScenario(config *config.LoadConfig, address common.Address) common.Address {
client, err := dial(config)
if err != nil {
panic("Failed to connect to Ethereum client: " + err.Error())
}
// Bind contract instance using the provided bind function
bindFunc := c.deployer.GetBindFunc()
contract, err := bindFunc(address, client)
if err != nil {
panic("Failed to bind contract: " + err.Error())
}
// Store the contract instance
c.deployer.SetContract(contract)
return address
}
// DeployScenario implements ScenarioDeployer interface for contract scenarios
func (c *ContractScenarioBase[T]) DeployScenario(config *config.LoadConfig, deployer *types.Account) common.Address {
client, err := dial(config)
if err != nil {
panic("Failed to connect to Ethereum client: " + err.Error())
}
// Create deployment options
auth, err := utils.CreateDeploymentOpts(config.GetChainID(), client, deployer)
if err != nil {
panic("Failed to create deployment options: " + err.Error())
}
// Deploy using contract-specific logic
address, _, err := c.deployer.DeployContract(auth, client)
if err != nil {
panic("Failed to deploy contract: " + err.Error())
}
// Bind contract instance using the provided bind function
bindFunc := c.deployer.GetBindFunc()
contract, err := bindFunc(address, client)
if err != nil {
panic("Failed to bind contract: " + err.Error())
}
// Store the contract instance
c.deployer.SetContract(contract)
return address
}
// CreateTransaction implements ScenarioDeployer interface for contract scenarios
func (c *ContractScenarioBase[T]) CreateTransaction(config *config.LoadConfig, scenario *types.TxScenario) (*ethtypes.Transaction, error) {
auth := utils.CreateTransactionOpts(config.GetChainID(), scenario)
return c.deployer.CreateContractTransaction(auth, scenario)
}