Skip to content

Commit

Permalink
fix : unuse generic
Browse files Browse the repository at this point in the history
  • Loading branch information
felix-shin-wt committed Jun 7, 2024
1 parent 5c3b855 commit eaee291
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 80 deletions.
18 changes: 6 additions & 12 deletions wemix/bind/backends/wemix_simulated.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,15 @@ type SimClient interface {
ethereum.TransactionReader
ethereum.TransactionSender
SuggestGasTipCap(ctx context.Context) (*big.Int, error) // GasPricer1559
GovContracts() *gov.GovContracts
Commit()
}

var ChainID = params.AllEthashProtocolChanges.ChainID

type wemixSimulatedBackend struct {
stack *node.Node
eth *eth.Ethereum
backend *backends.SimulatedBackend
governance *gov.GovContracts
stack *node.Node
eth *eth.Ethereum
backend *backends.SimulatedBackend
}

func NewWemixSimulatedBackend(pk *ecdsa.PrivateKey, datadir string, alloc core.GenesisAlloc, options ...OptionFn) (SimClient, error) {
Expand Down Expand Up @@ -168,10 +166,9 @@ func NewWemixSimulatedBackend(pk *ecdsa.PrivateKey, datadir string, alloc core.G
}

return &wemixSimulatedBackend{
stack: stack,
eth: backend,
backend: backends.NewSimulatedBackendWithEthereum(backend),
governance: contracts,
stack: stack,
eth: backend,
backend: backends.NewSimulatedBackendWithEthereum(backend),
}, nil
}

Expand Down Expand Up @@ -252,9 +249,6 @@ func (w *wemixSimulatedBackend) SendTransaction(ctx context.Context, tx *types.T
func (w *wemixSimulatedBackend) Commit() {
w.backend.Commit()
}
func (w *wemixSimulatedBackend) GovContracts() *gov.GovContracts {
return w.governance
}

func (w *wemixSimulatedBackend) PendingBalanceAt(ctx context.Context, account common.Address) (*big.Int, error) {
// return w.backend.PendingBalanceAt(ctx, account)
Expand Down
88 changes: 66 additions & 22 deletions wemix/bind/backends/wemix_simulated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,32 +32,71 @@ func TestWemixBackends(t *testing.T) {
},
backends.SetLogLevel(2),
)
require.NoError(t, err)
if err != nil {
return
}
require.NotNil(t, b)

t.Log(wemix_miner.HasMiningTokenFunc())
t.Log(wemix_miner.SuggestGasPriceFunc())
t.Log(wemix_miner.GetCoinbaseFunc(common.Big0))
defaultEnv := gov.DefaultInitEnvStorage

require.True(t, defaultEnv.MAX_PRIORITY_FEE_PER_GAS.Cmp(wemix_miner.SuggestGasPrice()) == 0)
coinBase, err := wemix_miner.GetCoinbase(common.Big0)
require.NoError(t, err)
require.Equal(t, opts.From, coinBase)

callOpts := new(bind.CallOpts)
contracts, err := gov.GetGovContractsByOwner(callOpts, b, opts.From)
require.NoError(t, err)
t.Log(contracts.Address().Registry)
t.Log(contracts.EnvStorageImp.GetBallotDurationMax(callOpts))
t.Log(contracts.EnvStorageImp.GetBallotDurationMin(callOpts))
t.Log(contracts.EnvStorageImp.GetBallotDurationMinMax(callOpts))
t.Log(contracts.EnvStorageImp.GetBlockCreationTime(callOpts))
t.Log(contracts.EnvStorageImp.GetBlockRewardAmount(callOpts))
t.Log(contracts.EnvStorageImp.GetBlockRewardDistributionMethod(callOpts))
t.Log(contracts.EnvStorageImp.GetBlocksPer(callOpts))
t.Log(contracts.EnvStorageImp.GetGasLimitAndBaseFee(callOpts))
t.Log(contracts.EnvStorageImp.GetMaxBaseFee(callOpts))
t.Log(contracts.EnvStorageImp.GetMaxIdleBlockInterval(callOpts))
t.Log(contracts.EnvStorageImp.GetMaxPriorityFeePerGas(callOpts))
t.Log(contracts.EnvStorageImp.GetStakingMax(callOpts))
t.Log(contracts.EnvStorageImp.GetStakingMin(callOpts))
t.Log(contracts.EnvStorageImp.GetStakingMinMax(callOpts))
t.Log(contracts.EnvStorageImp.GASTARGETPERCENTAGENAME(callOpts))

GetBallotDurationMax, err := contracts.EnvStorageImp.GetBallotDurationMax(callOpts)
require.NoError(t, err)
require.True(t, defaultEnv.BALLOT_DURATION_MAX.Cmp(GetBallotDurationMax) == 0)

GetBallotDurationMin, err := contracts.EnvStorageImp.GetBallotDurationMin(callOpts)
require.NoError(t, err)
require.True(t, defaultEnv.BALLOT_DURATION_MIN.Cmp(GetBallotDurationMin) == 0)

GetBlockCreationTime, err := contracts.EnvStorageImp.GetBlockCreationTime(callOpts)
require.NoError(t, err)
require.True(t, defaultEnv.BLOCK_CREATION_TIME.Cmp(GetBlockCreationTime) == 0)

GetBlockRewardAmount, err := contracts.EnvStorageImp.GetBlockRewardAmount(callOpts)
require.NoError(t, err)
require.True(t, defaultEnv.BLOCK_REWARD_AMOUNT.Cmp(GetBlockRewardAmount) == 0)

blockRewardDistributionBlockProducer,
blockRewardDistributionStakingReward, blockRewardDistributionEcosystem,
blockRewardDistributionMaintenance,
err := contracts.EnvStorageImp.GetBlockRewardDistributionMethod(callOpts)
require.NoError(t, err)
require.True(t, defaultEnv.BLOCK_REWARD_DISTRIBUTION_BLOCK_PRODUCER.Cmp(blockRewardDistributionBlockProducer) == 0)
require.True(t, defaultEnv.BLOCK_REWARD_DISTRIBUTION_STAKING_REWARD.Cmp(blockRewardDistributionStakingReward) == 0)
require.True(t, defaultEnv.BLOCK_REWARD_DISTRIBUTION_ECOSYSTEM.Cmp(blockRewardDistributionEcosystem) == 0)
require.True(t, defaultEnv.BLOCK_REWARD_DISTRIBUTION_MAINTENANCE.Cmp(blockRewardDistributionMaintenance) == 0)

GetBlocksPer, err := contracts.EnvStorageImp.GetBlocksPer(callOpts)
require.NoError(t, err)
require.True(t, defaultEnv.BLOCKS_PER.Cmp(GetBlocksPer) == 0)

GetMaxBaseFee, err := contracts.EnvStorageImp.GetMaxBaseFee(callOpts)
require.NoError(t, err)
require.True(t, defaultEnv.MAX_BASE_FEE.Cmp(GetMaxBaseFee) == 0)

GetMaxIdleBlockInterval, err := contracts.EnvStorageImp.GetMaxIdleBlockInterval(callOpts)
require.NoError(t, err)
require.True(t, defaultEnv.MAX_IDLE_BLOCK_INTERVAL.Cmp(GetMaxIdleBlockInterval) == 0)

GetMaxPriorityFeePerGas, err := contracts.EnvStorageImp.GetMaxPriorityFeePerGas(callOpts)
require.NoError(t, err)
require.True(t, defaultEnv.MAX_PRIORITY_FEE_PER_GAS.Cmp(GetMaxPriorityFeePerGas) == 0)

GetStakingMax, err := contracts.EnvStorageImp.GetStakingMax(callOpts)
require.NoError(t, err)
require.True(t, defaultEnv.STAKING_MAX.Cmp(GetStakingMax) == 0)

GetStakingMin, err := contracts.EnvStorageImp.GetStakingMin(callOpts)
require.NoError(t, err)
require.True(t, defaultEnv.STAKING_MIN.Cmp(GetStakingMin) == 0)

getNode, err := contracts.GovImp.GetNode(callOpts, common.Big1)
require.NoError(t, err)
Expand All @@ -67,7 +106,6 @@ func TestWemixBackends(t *testing.T) {
"\n Ip:", string(getNode.Ip),
"\n Port:", getNode.Port,
)
// /*
newVoterKey, err := crypto.GenerateKey()
require.NoError(t, err)
newVoter := crypto.PubkeyToAddress(newVoterKey.PublicKey)
Expand Down Expand Up @@ -114,12 +152,18 @@ func TestWemixBackends(t *testing.T) {
require.NoError(t, err)

ticker := time.NewTicker(10e9)
var headNumber *big.Int = nil
loop:
for {
b.Commit()
select {
case head := <-newHead:
t.Log("head.Number", head.Number)
if headNumber == nil {
headNumber = head.Number
} else {
require.Equal(t, new(big.Int).Add(headNumber, common.Big1), head.Number)
headNumber = head.Number
}
time.Sleep(0.2e9)
case err := <-sub.Err():
require.NoError(t, err)
Expand Down
158 changes: 112 additions & 46 deletions wemix/bind/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,61 @@ func NewGovContracts(backend bind.ContractBackend, registry common.Address) (*Go
}
)

if gov.address.Gov, gov.Gov, gov.GovImp, err = newUUPSContracts(cfg, "GovernanceContract", NewGov, NewGovImp); err != nil {
return nil, errors.Wrap(err, "GovernanceContract")
} else if gov.address.Staking, gov.Staking, gov.StakingImp, err = newUUPSContracts(cfg, "Staking", NewStaking, NewStakingImp); err != nil {
return nil, errors.Wrap(err, "Staking")
} else if gov.address.BallotStorage, gov.BallotStorage, gov.BallotStorageImp, err = newUUPSContracts(cfg, "BallotStorage", NewBallotStorage, NewBallotStorageImp); err != nil {
return nil, errors.Wrap(err, "BallotStorage")
} else if gov.address.EnvStorage, gov.EnvStorage, gov.EnvStorageImp, err = newUUPSContracts(cfg, "EnvStorage", NewEnvStorage, NewEnvStorageImp); err != nil {
return nil, errors.Wrap(err, "EnvStorage")
} else if gov.address.NCPExit, gov.NCPExit, gov.NCPExitImp, err = newUUPSContracts(cfg, "NCPExit", NewNCPExit, NewNCPExitImp); err != nil {
return nil, errors.Wrap(err, "NCPExit")
if err = newUUPSContracts(cfg, "GovernanceContract", func(address common.Address) error {
if Gov, err := NewGov(address, backend); err != nil {
return err
} else if GovImp, err := NewGovImp(address, backend); err != nil {
return err
} else {
gov.address.Gov, gov.Gov, gov.GovImp = address, Gov, GovImp
return nil
}
}); err != nil {
return nil, err
} else if err = newUUPSContracts(cfg, "Staking", func(address common.Address) error {
if Staking, err := NewStaking(address, backend); err != nil {
return err
} else if StakingImp, err := NewStakingImp(address, backend); err != nil {
return err
} else {
gov.address.Staking, gov.Staking, gov.StakingImp = address, Staking, StakingImp
return nil
}
}); err != nil {
return nil, err
} else if err = newUUPSContracts(cfg, "BallotStorage", func(address common.Address) error {
if BallotStorage, err := NewBallotStorage(address, backend); err != nil {
return err
} else if BallotStorageImp, err := NewBallotStorageImp(address, backend); err != nil {
return err
} else {
gov.address.BallotStorage, gov.BallotStorage, gov.BallotStorageImp = address, BallotStorage, BallotStorageImp
return nil
}
}); err != nil {
return nil, err
} else if err = newUUPSContracts(cfg, "EnvStorage", func(address common.Address) error {
if EnvStorage, err := NewEnvStorage(address, backend); err != nil {
return err
} else if EnvStorageImp, err := NewEnvStorageImp(address, backend); err != nil {
return err
} else {
gov.address.EnvStorage, gov.EnvStorage, gov.EnvStorageImp = address, EnvStorage, EnvStorageImp
return nil
}
}); err != nil {
return nil, err
} else if err = newUUPSContracts(cfg, "NCPExit", func(address common.Address) error {
if NCPExit, err := NewNCPExit(address, backend); err != nil {
return err
} else if NCPExitImp, err := NewNCPExitImp(address, backend); err != nil {
return err
} else {
gov.address.NCPExit, gov.NCPExit, gov.NCPExitImp = address, NCPExit, NCPExitImp
return nil
}
}); err != nil {
return nil, err
} else {
return gov, nil
}
Expand Down Expand Up @@ -119,33 +164,71 @@ func DeployGovContracts(opts *bind.TransactOpts, backend iBackend, optionDomains
gov.address.Registry, gov.Registry = address, contract
}

if err := txPool.AppendDeployTx(func(opts *bind.TransactOpts, backend bind.ContractBackend) (tx *types.Transaction, err error) {
gov.address.Registry, tx, gov.Registry, err = DeployRegistry(opts, backend)
logger.Info(fmt.Sprintf("Deploying Registry at %s...", gov.address.Registry))
return
}); err != nil {
return nil, errors.Wrap(err, "DeployRegistry")
}

// deploy imps
logger.Info("Deploy Logic Contracts...")
if impAddress.Gov, err = deployLogic(txPool, DeployGovImp); err != nil {
if err := txPool.AppendDeployTx(func(opts *bind.TransactOpts, backend bind.ContractBackend) (tx *types.Transaction, err error) {
impAddress.Gov, tx, _, err = DeployGovImp(opts, backend)
return
}); err != nil {
return nil, errors.Wrap(err, "DeployGovImp")
} else if impAddress.Staking, err = deployLogic(txPool, DeployStakingImp); err != nil {
} else if err := txPool.AppendDeployTx(func(opts *bind.TransactOpts, backend bind.ContractBackend) (tx *types.Transaction, err error) {
impAddress.Staking, tx, _, err = DeployStakingImp(opts, backend)
return tx, err
}); err != nil {
return nil, errors.Wrap(err, "DeployStakingImp")
} else if impAddress.BallotStorage, err = deployLogic(txPool, DeployBallotStorageImp); err != nil {
} else if err := txPool.AppendDeployTx(func(opts *bind.TransactOpts, backend bind.ContractBackend) (tx *types.Transaction, err error) {
impAddress.BallotStorage, tx, _, err = DeployBallotStorageImp(opts, backend)
return tx, err
}); err != nil {
return nil, errors.Wrap(err, "DeployBallotStorageImp")
} else if impAddress.EnvStorage, err = deployLogic(txPool, DeployEnvStorageImp); err != nil {
} else if err := txPool.AppendDeployTx(func(opts *bind.TransactOpts, backend bind.ContractBackend) (tx *types.Transaction, err error) {
impAddress.EnvStorage, tx, _, err = DeployEnvStorageImp(opts, backend)
return tx, err
}); err != nil {
return nil, errors.Wrap(err, "DeployEnvStorageImp")
} else if impAddress.NCPExit, err = deployLogic(txPool, DeployNCPExitImp); err != nil {
} else if err := txPool.AppendDeployTx(func(opts *bind.TransactOpts, backend bind.ContractBackend) (tx *types.Transaction, err error) {
impAddress.NCPExit, tx, _, err = DeployNCPExitImp(opts, backend)
return tx, err
}); err != nil {
return nil, errors.Wrap(err, "DeployNCPExitImp")
} else if err = txPool.WaitMined(); err != nil {
return nil, err
}

// deploy proxies
logger.Info("Deploy Governance Contracts...")
if gov.address.Gov, gov.Gov, err = deployProxy(txPool, impAddress.Gov, DeployGov); err != nil {
if err := txPool.AppendDeployTx(func(opts *bind.TransactOpts, backend bind.ContractBackend) (tx *types.Transaction, err error) {
gov.address.Gov, tx, gov.Gov, err = DeployGov(opts, backend, impAddress.Gov)
return
}); err != nil {
return nil, errors.Wrap(err, "DeployGov")
} else if gov.address.Staking, gov.Staking, err = deployProxy(txPool, impAddress.Staking, DeployStaking); err != nil {
} else if err := txPool.AppendDeployTx(func(opts *bind.TransactOpts, backend bind.ContractBackend) (tx *types.Transaction, err error) {
gov.address.Staking, tx, gov.Staking, err = DeployStaking(opts, backend, impAddress.Staking)
return
}); err != nil {
return nil, errors.Wrap(err, "DeployStaking")
} else if gov.address.BallotStorage, gov.BallotStorage, err = deployProxy(txPool, impAddress.BallotStorage, DeployBallotStorage); err != nil {
} else if err := txPool.AppendDeployTx(func(opts *bind.TransactOpts, backend bind.ContractBackend) (tx *types.Transaction, err error) {
gov.address.BallotStorage, tx, gov.BallotStorage, err = DeployBallotStorage(opts, backend, impAddress.BallotStorage)
return
}); err != nil {
return nil, errors.Wrap(err, "DeployBallotStorage")
} else if gov.address.EnvStorage, gov.EnvStorage, err = deployProxy(txPool, impAddress.EnvStorage, DeployEnvStorage); err != nil {
} else if err := txPool.AppendDeployTx(func(opts *bind.TransactOpts, backend bind.ContractBackend) (tx *types.Transaction, err error) {
gov.address.EnvStorage, tx, gov.EnvStorage, err = DeployEnvStorage(opts, backend, impAddress.EnvStorage)
return
}); err != nil {
return nil, errors.Wrap(err, "DeployEnvStorage")
} else if gov.address.NCPExit, gov.NCPExit, err = deployProxy(txPool, impAddress.NCPExit, DeployNCPExit); err != nil {
} else if err := txPool.AppendDeployTx(func(opts *bind.TransactOpts, backend bind.ContractBackend) (tx *types.Transaction, err error) {
gov.address.NCPExit, tx, gov.NCPExit, err = DeployNCPExit(opts, backend, impAddress.NCPExit)
return
}); err != nil {
return nil, errors.Wrap(err, "DeployNCPExit")
} else if err = txPool.WaitMined(); err != nil {
return nil, err
Expand Down Expand Up @@ -410,21 +493,16 @@ type uupsConfig struct {
registry *Registry
}

func newUUPSContracts[P, L any](
func newUUPSContracts(
cfg *uupsConfig,
name string,
newProxy func(common.Address, bind.ContractBackend) (*P, error),
newLogic func(common.Address, bind.ContractBackend) (*L, error),
) (common.Address, *P, *L, error) {
if address, err := cfg.registry.GetContractAddress(cfg.callOpts, metclient.ToBytes32(name)); err != nil {
return common.Address{}, nil, nil, errors.Wrap(err, "GetContractAddress")
} else if proxy, err := newProxy(address, cfg.backend); err != nil {
return common.Address{}, nil, nil, errors.Wrap(err, "newProxy")
} else if imp, err := newLogic(address, cfg.backend); err != nil {
return common.Address{}, nil, nil, errors.Wrap(err, "newLogic")
} else {
return address, proxy, imp, nil
callback func(common.Address) error,
) error {
address, err := cfg.registry.GetContractAddress(cfg.callOpts, metclient.ToBytes32(name))
if err != nil {
return err
}
return callback(address)
}

type iBackend interface {
Expand Down Expand Up @@ -464,18 +542,6 @@ func (pool *txPool) AppendTx(tx *types.Transaction, err error) error {
return err
}

func deployProxy[T any](pool *txPool, logic common.Address, deploy func(*bind.TransactOpts, bind.ContractBackend, common.Address) (common.Address, *types.Transaction, *T, error)) (common.Address, *T, error) {
address, tx, proxy, err := deploy(pool.opts, pool.backend, logic)
if tx != nil {
pool.txs = append(pool.txs, tx)
}
return address, proxy, err
}

func deployLogic[T any](pool *txPool, deploy func(*bind.TransactOpts, bind.ContractBackend) (common.Address, *types.Transaction, *T, error)) (common.Address, error) {
address, tx, _, err := deploy(pool.opts, pool.backend)
if tx != nil {
pool.txs = append(pool.txs, tx)
}
return address, err
func (pool *txPool) AppendDeployTx(deploy func(opts *bind.TransactOpts, backend bind.ContractBackend) (*types.Transaction, error)) error {
return pool.AppendTx(deploy(pool.opts, pool.backend))
}

0 comments on commit eaee291

Please sign in to comment.