Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(staking/precompile): split into method and abi #785

Merged
merged 1 commit into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions x/staking/precompile/allowance_shares.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import (

type AllowanceSharesMethod struct {
*Keeper
abi.Method
AllowanceSharesABI
}

func NewAllowanceSharesMethod(keeper *Keeper) *AllowanceSharesMethod {
return &AllowanceSharesMethod{
Keeper: keeper,
Method: stakingABI.Methods["allowanceShares"],
Keeper: keeper,
AllowanceSharesABI: NewAllowanceSharesABI(),
}
}

Expand Down Expand Up @@ -46,25 +46,35 @@ func (m *AllowanceSharesMethod) Run(evm *vm.EVM, contract *vm.Contract) ([]byte,
return m.PackOutput(allowance)
}

func (m *AllowanceSharesMethod) PackInput(args fxstakingtypes.AllowanceSharesArgs) ([]byte, error) {
type AllowanceSharesABI struct {
abi.Method
}

func NewAllowanceSharesABI() AllowanceSharesABI {
return AllowanceSharesABI{
Method: stakingABI.Methods["allowanceShares"],
}
}

func (m AllowanceSharesABI) PackInput(args fxstakingtypes.AllowanceSharesArgs) ([]byte, error) {
arguments, err := m.Method.Inputs.Pack(args.Validator, args.Owner, args.Spender)
if err != nil {
return nil, err
}
return append(m.GetMethodId(), arguments...), nil
return append(m.Method.ID, arguments...), nil
}

func (m *AllowanceSharesMethod) UnpackInput(data []byte) (*fxstakingtypes.AllowanceSharesArgs, error) {
func (m AllowanceSharesABI) UnpackInput(data []byte) (*fxstakingtypes.AllowanceSharesArgs, error) {
args := new(fxstakingtypes.AllowanceSharesArgs)
err := types.ParseMethodArgs(m.Method, args, data[4:])
return args, err
}

func (m *AllowanceSharesMethod) PackOutput(amount *big.Int) ([]byte, error) {
func (m AllowanceSharesABI) PackOutput(amount *big.Int) ([]byte, error) {
return m.Method.Outputs.Pack(amount)
}

func (m *AllowanceSharesMethod) UnpackOutput(data []byte) (*big.Int, error) {
func (m AllowanceSharesABI) UnpackOutput(data []byte) (*big.Int, error) {
Comment on lines +59 to +77
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider using pointer receivers to avoid unnecessary copying

Currently, the methods of AllowanceSharesABI use value receivers. Since abi.Method may be a sizable struct, using pointer receivers can improve performance by preventing unnecessary copying of the struct when methods are called.

Apply this diff to change the method receivers to pointers:

-func (m AllowanceSharesABI) PackInput(args fxstakingtypes.AllowanceSharesArgs) ([]byte, error) {
+func (m *AllowanceSharesABI) PackInput(args fxstakingtypes.AllowanceSharesArgs) ([]byte, error) {

-func (m AllowanceSharesABI) UnpackInput(data []byte) (*fxstakingtypes.AllowanceSharesArgs, error) {
+func (m *AllowanceSharesABI) UnpackInput(data []byte) (*fxstakingtypes.AllowanceSharesArgs, error) {

-func (m AllowanceSharesABI) PackOutput(amount *big.Int) ([]byte, error) {
+func (m *AllowanceSharesABI) PackOutput(amount *big.Int) ([]byte, error) {

-func (m AllowanceSharesABI) UnpackOutput(data []byte) (*big.Int, error) {
+func (m *AllowanceSharesABI) UnpackOutput(data []byte) (*big.Int, error) {
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
func (m AllowanceSharesABI) PackInput(args fxstakingtypes.AllowanceSharesArgs) ([]byte, error) {
arguments, err := m.Method.Inputs.Pack(args.Validator, args.Owner, args.Spender)
if err != nil {
return nil, err
}
return append(m.GetMethodId(), arguments...), nil
return append(m.Method.ID, arguments...), nil
}
func (m *AllowanceSharesMethod) UnpackInput(data []byte) (*fxstakingtypes.AllowanceSharesArgs, error) {
func (m AllowanceSharesABI) UnpackInput(data []byte) (*fxstakingtypes.AllowanceSharesArgs, error) {
args := new(fxstakingtypes.AllowanceSharesArgs)
err := types.ParseMethodArgs(m.Method, args, data[4:])
return args, err
}
func (m *AllowanceSharesMethod) PackOutput(amount *big.Int) ([]byte, error) {
func (m AllowanceSharesABI) PackOutput(amount *big.Int) ([]byte, error) {
return m.Method.Outputs.Pack(amount)
}
func (m *AllowanceSharesMethod) UnpackOutput(data []byte) (*big.Int, error) {
func (m AllowanceSharesABI) UnpackOutput(data []byte) (*big.Int, error) {
func (m *AllowanceSharesABI) PackInput(args fxstakingtypes.AllowanceSharesArgs) ([]byte, error) {
arguments, err := m.Method.Inputs.Pack(args.Validator, args.Owner, args.Spender)
if err != nil {
return nil, err
}
return append(m.Method.ID, arguments...), nil
}
func (m *AllowanceSharesABI) UnpackInput(data []byte) (*fxstakingtypes.AllowanceSharesArgs, error) {
args := new(fxstakingtypes.AllowanceSharesArgs)
err := types.ParseMethodArgs(m.Method, args, data[4:])
return args, err
}
func (m *AllowanceSharesABI) PackOutput(amount *big.Int) ([]byte, error) {
return m.Method.Outputs.Pack(amount)
}
func (m *AllowanceSharesABI) UnpackOutput(data []byte) (*big.Int, error) {

amount, err := m.Method.Outputs.Unpack(data)
if err != nil {
return nil, err
Expand Down
34 changes: 22 additions & 12 deletions x/staking/precompile/approve_shares.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@ import (

type ApproveSharesMethod struct {
*Keeper
abi.Method
abi.Event
ApproveSharesABI
}

func NewApproveSharesMethod(keeper *Keeper) *ApproveSharesMethod {
return &ApproveSharesMethod{
Keeper: keeper,
Method: stakingABI.Methods["approveShares"],
Event: stakingABI.Events["ApproveShares"],
Keeper: keeper,
ApproveSharesABI: NewApproveSharesABI(),
}
}

Expand Down Expand Up @@ -66,41 +64,53 @@ func (m *ApproveSharesMethod) Run(evm *vm.EVM, contract *vm.Contract) ([]byte, e
return m.PackOutput(true)
}

func (m *ApproveSharesMethod) NewApproveSharesEvent(owner, spender common.Address, validator string, shares *big.Int) (data []byte, topic []common.Hash, err error) {
type ApproveSharesABI struct {
abi.Method
abi.Event
}

func NewApproveSharesABI() ApproveSharesABI {
return ApproveSharesABI{
Method: stakingABI.Methods["approveShares"],
Event: stakingABI.Events["ApproveShares"],
}
}

func (m ApproveSharesABI) NewApproveSharesEvent(owner, spender common.Address, validator string, shares *big.Int) (data []byte, topic []common.Hash, err error) {
data, topic, err = types.PackTopicData(m.Event, []common.Hash{owner.Hash(), spender.Hash()}, validator, shares)
if err != nil {
return nil, nil, err
}
return data, topic, nil
}

func (m *ApproveSharesMethod) PackInput(args fxstakingtypes.ApproveSharesArgs) ([]byte, error) {
func (m ApproveSharesABI) PackInput(args fxstakingtypes.ApproveSharesArgs) ([]byte, error) {
arguments, err := m.Method.Inputs.Pack(args.Validator, args.Spender, args.Shares)
if err != nil {
return nil, err
}
return append(m.GetMethodId(), arguments...), nil
return append(m.Method.ID, arguments...), nil
}

func (m *ApproveSharesMethod) UnpackInput(data []byte) (*fxstakingtypes.ApproveSharesArgs, error) {
func (m ApproveSharesABI) UnpackInput(data []byte) (*fxstakingtypes.ApproveSharesArgs, error) {
args := new(fxstakingtypes.ApproveSharesArgs)
err := types.ParseMethodArgs(m.Method, args, data[4:])
return args, err
}

func (m *ApproveSharesMethod) PackOutput(result bool) ([]byte, error) {
func (m ApproveSharesABI) PackOutput(result bool) ([]byte, error) {
return m.Method.Outputs.Pack(result)
}

func (m *ApproveSharesMethod) UnpackOutput(data []byte) (bool, error) {
func (m ApproveSharesABI) UnpackOutput(data []byte) (bool, error) {
amount, err := m.Method.Outputs.Unpack(data)
if err != nil {
return false, err
}
return amount[0].(bool), nil
}

func (m *ApproveSharesMethod) UnpackEvent(log *ethtypes.Log) (*fxcontract.IStakingApproveShares, error) {
func (m ApproveSharesABI) UnpackEvent(log *ethtypes.Log) (*fxcontract.IStakingApproveShares, error) {
if log == nil {
return nil, errors.New("empty log")
}
Expand Down
8 changes: 4 additions & 4 deletions x/staking/precompile/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ type PrecompileTestSuite struct {
delegateV2Method *precompile.DelegateV2Method
delegationMethod *precompile.DelegationMethod
delegationRewardsMethod *precompile.DelegationRewardsMethod
redelegateMethodV2 *precompile.RedelegateMethodV2
redelegateV2Method *precompile.RedelegateV2Method
// slashingInfoMethod *precompile.SlashingInfoMethod
transferSharesMethod *precompile.TransferShares
transferFromSharesMethod *precompile.TransferFromShares
transferSharesMethod *precompile.TransferSharesMethod
transferFromSharesMethod *precompile.TransferFromSharesMethod
undelegateV2Method *precompile.UndelegateV2Method
validatorListMethod *precompile.ValidatorListMethod
withdrawMethod *precompile.WithdrawMethod
Expand Down Expand Up @@ -85,7 +85,7 @@ func (suite *PrecompileTestSuite) SetupTest() {
suite.delegateV2Method = precompile.NewDelegateV2Method(nil)
suite.delegationMethod = precompile.NewDelegationMethod(nil)
suite.delegationRewardsMethod = precompile.NewDelegationRewardsMethod(nil)
suite.redelegateMethodV2 = precompile.NewRedelegateV2Method(nil)
suite.redelegateV2Method = precompile.NewRedelegateV2Method(nil)
// suite.slashingInfoMethod = precompile.NewSlashingInfoMethod(nil)
suite.transferSharesMethod = precompile.NewTransferSharesMethod(nil)
suite.transferFromSharesMethod = precompile.NewTransferFromSharesMethod(nil)
Expand Down
34 changes: 22 additions & 12 deletions x/staking/precompile/delegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@ import (

type DelegateV2Method struct {
*Keeper
abi.Method
abi.Event
DelegateV2ABI
}

func NewDelegateV2Method(keeper *Keeper) *DelegateV2Method {
return &DelegateV2Method{
Keeper: keeper,
Method: stakingABI.Methods["delegateV2"],
Event: stakingABI.Events["DelegateV2"],
Keeper: keeper,
DelegateV2ABI: NewDelegateV2ABI(),
}
}

Expand Down Expand Up @@ -71,41 +69,53 @@ func (m *DelegateV2Method) Run(evm *vm.EVM, contract *vm.Contract) ([]byte, erro
return m.PackOutput(true)
}

func (m *DelegateV2Method) NewDelegateEvent(sender common.Address, validator string, amount *big.Int) (data []byte, topic []common.Hash, err error) {
type DelegateV2ABI struct {
abi.Method
abi.Event
}

func NewDelegateV2ABI() DelegateV2ABI {
return DelegateV2ABI{
Method: stakingABI.Methods["delegateV2"],
Event: stakingABI.Events["DelegateV2"],
}
}

func (m DelegateV2ABI) NewDelegateEvent(sender common.Address, validator string, amount *big.Int) (data []byte, topic []common.Hash, err error) {
data, topic, err = types.PackTopicData(m.Event, []common.Hash{sender.Hash()}, validator, amount)
if err != nil {
return nil, nil, err
}
return data, topic, nil
}

func (m *DelegateV2Method) PackInput(args fxstakingtypes.DelegateV2Args) ([]byte, error) {
func (m DelegateV2ABI) PackInput(args fxstakingtypes.DelegateV2Args) ([]byte, error) {
arguments, err := m.Method.Inputs.Pack(args.Validator, args.Amount)
if err != nil {
return nil, err
}
return append(m.GetMethodId(), arguments...), nil
return append(m.Method.ID, arguments...), nil
}

func (m *DelegateV2Method) UnpackInput(data []byte) (*fxstakingtypes.DelegateV2Args, error) {
func (m DelegateV2ABI) UnpackInput(data []byte) (*fxstakingtypes.DelegateV2Args, error) {
args := new(fxstakingtypes.DelegateV2Args)
err := types.ParseMethodArgs(m.Method, args, data[4:])
return args, err
}

func (m *DelegateV2Method) PackOutput(result bool) ([]byte, error) {
func (m DelegateV2ABI) PackOutput(result bool) ([]byte, error) {
return m.Method.Outputs.Pack(result)
}

func (m *DelegateV2Method) UnpackOutput(data []byte) (bool, error) {
func (m DelegateV2ABI) UnpackOutput(data []byte) (bool, error) {
amount, err := m.Method.Outputs.Unpack(data)
if err != nil {
return false, err
}
return amount[0].(bool), nil
}
Comment on lines +110 to 116
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Add type assertion safety check in UnpackOutput.

The type assertion amount[0].(bool) could panic if the unpacked value is not a boolean. Consider adding a type safety check.

 func (m DelegateV2ABI) UnpackOutput(data []byte) (bool, error) {
 	amount, err := m.Method.Outputs.Unpack(data)
 	if err != nil {
 		return false, err
 	}
+	if len(amount) == 0 {
+		return false, errors.New("empty output data")
+	}
+	val, ok := amount[0].(bool)
+	if !ok {
+		return false, errors.New("invalid output type: expected bool")
+	}
-	return amount[0].(bool), nil
+	return val, nil
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
func (m DelegateV2ABI) UnpackOutput(data []byte) (bool, error) {
amount, err := m.Method.Outputs.Unpack(data)
if err != nil {
return false, err
}
return amount[0].(bool), nil
}
func (m DelegateV2ABI) UnpackOutput(data []byte) (bool, error) {
amount, err := m.Method.Outputs.Unpack(data)
if err != nil {
return false, err
}
if len(amount) == 0 {
return false, errors.New("empty output data")
}
val, ok := amount[0].(bool)
if !ok {
return false, errors.New("invalid output type: expected bool")
}
return val, nil
}


func (m *DelegateV2Method) UnpackEvent(log *ethtypes.Log) (*fxcontract.IStakingDelegateV2, error) {
func (m DelegateV2ABI) UnpackEvent(log *ethtypes.Log) (*fxcontract.IStakingDelegateV2, error) {
if log == nil {
return nil, errors.New("empty log")
}
Expand Down
26 changes: 18 additions & 8 deletions x/staking/precompile/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import (

type DelegationMethod struct {
*Keeper
abi.Method
DelegationABI
}

func NewDelegationMethod(keeper *Keeper) *DelegationMethod {
return &DelegationMethod{
Keeper: keeper,
Method: stakingABI.Methods["delegation"],
Keeper: keeper,
DelegationABI: NewDelegationABI(),
}
}

Expand Down Expand Up @@ -63,25 +63,35 @@ func (m *DelegationMethod) Run(evm *vm.EVM, contract *vm.Contract) ([]byte, erro
return m.PackOutput(delegation.GetShares().TruncateInt().BigInt(), delegationAmt.TruncateInt().BigInt())
}

func (m *DelegationMethod) PackInput(args fxstakingtypes.DelegationArgs) ([]byte, error) {
type DelegationABI struct {
abi.Method
}

func NewDelegationABI() DelegationABI {
return DelegationABI{
Method: stakingABI.Methods["delegation"],
}
}

func (m DelegationABI) PackInput(args fxstakingtypes.DelegationArgs) ([]byte, error) {
arguments, err := m.Method.Inputs.Pack(args.Validator, args.Delegator)
if err != nil {
return nil, err
}
return append(m.GetMethodId(), arguments...), nil
return append(m.Method.ID, arguments...), nil
}

func (m *DelegationMethod) UnpackInput(data []byte) (*fxstakingtypes.DelegationArgs, error) {
func (m DelegationABI) UnpackInput(data []byte) (*fxstakingtypes.DelegationArgs, error) {
args := new(fxstakingtypes.DelegationArgs)
err := types.ParseMethodArgs(m.Method, args, data[4:])
return args, err
}

func (m *DelegationMethod) PackOutput(shares, amount *big.Int) ([]byte, error) {
func (m DelegationABI) PackOutput(shares, amount *big.Int) ([]byte, error) {
return m.Method.Outputs.Pack(shares, amount)
}

func (m *DelegationMethod) UnpackOutput(data []byte) (*big.Int, *big.Int, error) {
func (m DelegationABI) UnpackOutput(data []byte) (*big.Int, *big.Int, error) {
unpack, err := m.Method.Outputs.Unpack(data)
if err != nil {
return nil, nil, err
Expand Down
26 changes: 18 additions & 8 deletions x/staking/precompile/delegation_rewards.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import (

type DelegationRewardsMethod struct {
*Keeper
abi.Method
DelegationRewardsABI
}

func NewDelegationRewardsMethod(keeper *Keeper) *DelegationRewardsMethod {
return &DelegationRewardsMethod{
Keeper: keeper,
Method: stakingABI.Methods["delegationRewards"],
Keeper: keeper,
DelegationRewardsABI: NewDelegationRewardsABI(),
}
}

Expand Down Expand Up @@ -69,25 +69,35 @@ func (m *DelegationRewardsMethod) Run(evm *vm.EVM, contract *vm.Contract) ([]byt
return m.PackOutput(rewards.AmountOf(m.stakingDenom).TruncateInt().BigInt())
}

func (m *DelegationRewardsMethod) PackInput(args fxstakingtypes.DelegationRewardsArgs) ([]byte, error) {
type DelegationRewardsABI struct {
abi.Method
}

func NewDelegationRewardsABI() DelegationRewardsABI {
return DelegationRewardsABI{
Method: stakingABI.Methods["delegationRewards"],
}
}

func (m DelegationRewardsABI) PackInput(args fxstakingtypes.DelegationRewardsArgs) ([]byte, error) {
arguments, err := m.Method.Inputs.Pack(args.Validator, args.Delegator)
if err != nil {
return nil, err
}
return append(m.GetMethodId(), arguments...), nil
return append(m.Method.ID, arguments...), nil
}

func (m *DelegationRewardsMethod) UnpackInput(data []byte) (*fxstakingtypes.DelegationRewardsArgs, error) {
func (m DelegationRewardsABI) UnpackInput(data []byte) (*fxstakingtypes.DelegationRewardsArgs, error) {
args := new(fxstakingtypes.DelegationRewardsArgs)
err := types.ParseMethodArgs(m.Method, args, data[4:])
return args, err
}

func (m *DelegationRewardsMethod) PackOutput(amount *big.Int) ([]byte, error) {
func (m DelegationRewardsABI) PackOutput(amount *big.Int) ([]byte, error) {
return m.Method.Outputs.Pack(amount)
}

func (m *DelegationRewardsMethod) UnpackOutput(data []byte) (*big.Int, error) {
func (m DelegationRewardsABI) UnpackOutput(data []byte) (*big.Int, error) {
amount, err := m.Method.Outputs.Unpack(data)
if err != nil {
return nil, err
Expand Down
Loading