-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: precompile staking validator info
- Loading branch information
1 parent
8fa9a08
commit 80a2e34
Showing
14 changed files
with
715 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package precompile | ||
|
||
import ( | ||
"fmt" | ||
"math/big" | ||
|
||
"github.com/ethereum/go-ethereum/accounts/abi" | ||
"github.com/ethereum/go-ethereum/core/vm" | ||
|
||
"github.com/functionx/fx-core/v7/x/evm/types" | ||
fxstakingtypes "github.com/functionx/fx-core/v7/x/staking/types" | ||
) | ||
|
||
type SlashingInfoMethod struct { | ||
*Keeper | ||
abi.Method | ||
} | ||
|
||
func NewSlashingInfoMethod(keeper *Keeper) *SlashingInfoMethod { | ||
return &SlashingInfoMethod{ | ||
Keeper: keeper, | ||
Method: fxstakingtypes.GetABI().Methods["slashingInfo"], | ||
} | ||
} | ||
|
||
func (m *SlashingInfoMethod) IsReadonly() bool { | ||
return true | ||
} | ||
|
||
func (m *SlashingInfoMethod) GetMethodId() []byte { | ||
return m.Method.ID | ||
} | ||
|
||
func (m *SlashingInfoMethod) RequiredGas() uint64 { | ||
return 1_000 | ||
} | ||
|
||
func (m *SlashingInfoMethod) Run(evm *vm.EVM, contract *vm.Contract) ([]byte, error) { | ||
args, err := m.UnpackInput(contract.Input) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
stateDB := evm.StateDB.(types.ExtStateDB) | ||
cacheCtx := stateDB.CacheContext() | ||
|
||
validator, found := m.Keeper.stakingKeeper.GetValidator(cacheCtx, args.GetValidator()) | ||
if !found { | ||
return nil, fmt.Errorf("validator %s not found", args.Validator) | ||
} | ||
|
||
consAddr, err := validator.GetConsAddr() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
signingInfo, found := m.Keeper.slashingKeeper.GetValidatorSigningInfo(cacheCtx, consAddr) | ||
if !found { | ||
return nil, fmt.Errorf("signing info %s not found", consAddr.String()) | ||
} | ||
return m.PackOutput(validator.Jailed, signingInfo.MissedBlocksCounter) | ||
} | ||
|
||
func (m *SlashingInfoMethod) PackInput(args fxstakingtypes.SlashingInfoArgs) ([]byte, error) { | ||
arguments, err := m.Method.Inputs.Pack(args.Validator) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return append(m.GetMethodId(), arguments...), nil | ||
} | ||
|
||
func (m *SlashingInfoMethod) UnpackInput(data []byte) (*fxstakingtypes.SlashingInfoArgs, error) { | ||
args := new(fxstakingtypes.SlashingInfoArgs) | ||
err := types.ParseMethodArgs(m.Method, args, data[4:]) | ||
return args, err | ||
} | ||
|
||
func (m *SlashingInfoMethod) PackOutput(jailed bool, missed int64) ([]byte, error) { | ||
return m.Method.Outputs.Pack(jailed, big.NewInt(missed)) | ||
} | ||
|
||
func (m *SlashingInfoMethod) UnpackOutput(data []byte) (bool, *big.Int, error) { | ||
unpack, err := m.Method.Outputs.Unpack(data) | ||
if err != nil { | ||
return false, nil, err | ||
} | ||
return unpack[0].(bool), unpack[1].(*big.Int), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package precompile_test | ||
|
||
import ( | ||
"fmt" | ||
"math/big" | ||
"strings" | ||
"testing" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/functionx/fx-core/v7/contract" | ||
testscontract "github.com/functionx/fx-core/v7/tests/contract" | ||
"github.com/functionx/fx-core/v7/testutil/helpers" | ||
"github.com/functionx/fx-core/v7/x/staking/precompile" | ||
"github.com/functionx/fx-core/v7/x/staking/types" | ||
) | ||
|
||
func TestSlashingInfoABI(t *testing.T) { | ||
slashingInfoMethod := precompile.NewSlashingInfoMethod(nil) | ||
|
||
require.Equal(t, 1, len(slashingInfoMethod.Method.Inputs)) | ||
require.Equal(t, 2, len(slashingInfoMethod.Method.Outputs)) | ||
} | ||
|
||
func (suite *PrecompileTestSuite) TestSlashingInfo() { | ||
slashingInfoMethod := precompile.NewSlashingInfoMethod(nil) | ||
testCases := []struct { | ||
name string | ||
malleate func(val sdk.ValAddress) (types.SlashingInfoArgs, error) | ||
result bool | ||
}{ | ||
{ | ||
name: "ok", | ||
malleate: func(val sdk.ValAddress) (types.SlashingInfoArgs, error) { | ||
return types.SlashingInfoArgs{ | ||
Validator: val.String(), | ||
}, nil | ||
}, | ||
result: true, | ||
}, | ||
{ | ||
name: "failed - invalid validator address", | ||
malleate: func(val sdk.ValAddress) (types.SlashingInfoArgs, error) { | ||
valStr := val.String() + "1" | ||
return types.SlashingInfoArgs{ | ||
Validator: valStr, | ||
}, fmt.Errorf("invalid validator address: %s", valStr) | ||
}, | ||
result: false, | ||
}, | ||
|
||
{ | ||
name: "contract - ok", | ||
malleate: func(val sdk.ValAddress) (types.SlashingInfoArgs, error) { | ||
return types.SlashingInfoArgs{ | ||
Validator: val.String(), | ||
}, nil | ||
}, | ||
result: true, | ||
}, | ||
{ | ||
name: "contract - failed - invalid validator address", | ||
malleate: func(val sdk.ValAddress) (types.SlashingInfoArgs, error) { | ||
valStr := val.String() + "1" | ||
return types.SlashingInfoArgs{ | ||
Validator: valStr, | ||
}, fmt.Errorf("invalid validator address: %s", valStr) | ||
}, | ||
result: false, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
suite.Run(fmt.Sprintf("Case %s", tc.name), func() { | ||
val := suite.GetFirstValidator() | ||
owner := suite.RandSigner() | ||
spender := suite.RandSigner() | ||
allowanceAmt := helpers.NewRandAmount() | ||
|
||
// set allowance | ||
suite.App.StakingKeeper.SetAllowance(suite.Ctx, val.GetOperator(), owner.AccAddress(), spender.AccAddress(), allowanceAmt.BigInt()) | ||
|
||
args, errResult := tc.malleate(val.GetOperator()) | ||
|
||
packData, err := slashingInfoMethod.PackInput(args) | ||
suite.Require().NoError(err) | ||
stakingContract := precompile.GetAddress() | ||
|
||
if strings.HasPrefix(tc.name, "contract") { | ||
stakingContract = suite.staking | ||
packData, err = contract.MustABIJson(testscontract.StakingTestMetaData.ABI).Pack(TestSlashingInfoName, args.Validator) | ||
suite.Require().NoError(err) | ||
} | ||
|
||
res := suite.EthereumTx(owner, stakingContract, big.NewInt(0), packData) | ||
|
||
if tc.result { | ||
suite.Require().False(res.Failed(), res.VmError) | ||
jailed, missed, err := slashingInfoMethod.UnpackOutput(res.Ret) | ||
suite.Require().NoError(err) | ||
validator, found := suite.App.StakingKeeper.GetValidator(suite.Ctx, val.GetOperator()) | ||
suite.True(found) | ||
suite.Equal(validator.Jailed, jailed) | ||
consAddr, err := validator.GetConsAddr() | ||
suite.NoError(err) | ||
signingInfo, found := suite.App.SlashingKeeper.GetValidatorSigningInfo(suite.Ctx, consAddr) | ||
suite.True(found) | ||
suite.Equal(signingInfo.MissedBlocksCounter, missed.Int64()) | ||
} else { | ||
suite.Error(res, errResult) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.