0g-chain/precompiles/dasigners/query.go

125 lines
4.2 KiB
Go
Raw Normal View History

2024-05-09 18:54:47 +00:00
package dasigners
import (
2024-05-18 15:01:28 +00:00
"fmt"
2024-05-09 18:54:47 +00:00
"math/big"
2024-05-18 15:01:28 +00:00
precopmiles_common "github.com/0glabs/0g-chain/precompiles/common"
2024-05-09 18:54:47 +00:00
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/accounts/abi"
2024-05-16 14:49:29 +00:00
"github.com/ethereum/go-ethereum/common"
2024-05-09 18:54:47 +00:00
"github.com/ethereum/go-ethereum/core/vm"
)
func (d *DASignersPrecompile) Params(ctx sdk.Context, _ *vm.EVM, method *abi.Method, _ []interface{}) ([]byte, error) {
params := d.dasignersKeeper.GetParams(ctx)
return method.Outputs.Pack(IDASignersParams{
TokensPerVote: big.NewInt(int64(params.TokensPerVote)),
MaxVotesPerSigner: big.NewInt(int64(params.MaxVotesPerSigner)),
MaxQuorums: big.NewInt(int64(params.MaxQuorums)),
EpochBlocks: big.NewInt(int64(params.EpochBlocks)),
EncodedSlices: big.NewInt(int64(params.EncodedSlices)),
})
}
2024-05-09 18:54:47 +00:00
func (d *DASignersPrecompile) EpochNumber(ctx sdk.Context, _ *vm.EVM, method *abi.Method, _ []interface{}) ([]byte, error) {
epochNumber, err := d.dasignersKeeper.GetEpochNumber(ctx)
if err != nil {
return nil, err
}
return method.Outputs.Pack(big.NewInt(int64(epochNumber)))
}
2024-05-16 14:49:29 +00:00
func (d *DASignersPrecompile) QuorumCount(ctx sdk.Context, _ *vm.EVM, method *abi.Method, args []interface{}) ([]byte, error) {
req, err := NewQueryQuorumCountRequest(args)
2024-05-18 15:01:28 +00:00
if err != nil {
return nil, err
}
2024-05-16 14:49:29 +00:00
response, err := d.dasignersKeeper.QuorumCount(ctx, req)
if err != nil {
return nil, err
}
return method.Outputs.Pack(big.NewInt(int64(response.QuorumCount)))
}
2024-05-09 18:54:47 +00:00
func (d *DASignersPrecompile) GetSigner(ctx sdk.Context, _ *vm.EVM, method *abi.Method, args []interface{}) ([]byte, error) {
req, err := NewQuerySignerRequest(args)
if err != nil {
return nil, err
}
response, err := d.dasignersKeeper.Signer(sdk.WrapSDKContext(ctx), req)
if err != nil {
return nil, err
}
2024-05-16 14:49:29 +00:00
signers := make([]IDASignersSignerDetail, len(response.Signer))
for i, signer := range response.Signer {
signers[i] = NewIDASignersSignerDetail(signer)
}
return method.Outputs.Pack(signers)
2024-05-09 18:54:47 +00:00
}
2024-05-18 15:01:28 +00:00
func (d *DASignersPrecompile) IsSigner(ctx sdk.Context, _ *vm.EVM, method *abi.Method, args []interface{}) ([]byte, error) {
if len(args) != 1 {
return nil, fmt.Errorf(precopmiles_common.ErrInvalidNumberOfArgs, 1, len(args))
}
account := ToLowerHexWithoutPrefix(args[0].(common.Address))
_, found, err := d.dasignersKeeper.GetSigner(ctx, account)
if err != nil {
return nil, err
}
return method.Outputs.Pack(found)
}
func (d *DASignersPrecompile) RegisteredEpoch(ctx sdk.Context, _ *vm.EVM, method *abi.Method, args []interface{}) ([]byte, error) {
if len(args) != 2 {
return nil, fmt.Errorf(precopmiles_common.ErrInvalidNumberOfArgs, 2, len(args))
}
account := ToLowerHexWithoutPrefix(args[0].(common.Address))
epoch := args[1].(*big.Int).Uint64()
_, found, err := d.dasignersKeeper.GetRegistration(ctx, epoch, account)
if err != nil {
return nil, err
}
return method.Outputs.Pack(found)
}
2024-05-16 14:49:29 +00:00
func (d *DASignersPrecompile) GetQuorum(ctx sdk.Context, _ *vm.EVM, method *abi.Method, args []interface{}) ([]byte, error) {
req, err := NewQueryEpochQuorumRequest(args)
2024-05-09 18:54:47 +00:00
if err != nil {
return nil, err
}
2024-05-16 14:49:29 +00:00
response, err := d.dasignersKeeper.EpochQuorum(sdk.WrapSDKContext(ctx), req)
2024-05-09 18:54:47 +00:00
if err != nil {
return nil, err
}
2024-05-16 14:49:29 +00:00
signers := make([]common.Address, len(response.Quorum.Signers))
for i, signer := range response.Quorum.Signers {
signers[i] = common.HexToAddress(signer)
2024-05-09 18:54:47 +00:00
}
return method.Outputs.Pack(signers)
}
2024-06-12 05:27:48 +00:00
func (d *DASignersPrecompile) GetQuorumRow(ctx sdk.Context, _ *vm.EVM, method *abi.Method, args []interface{}) ([]byte, error) {
req, err := NewQueryEpochQuorumRowRequest(args)
if err != nil {
return nil, err
}
response, err := d.dasignersKeeper.EpochQuorumRow(sdk.WrapSDKContext(ctx), req)
if err != nil {
return nil, err
}
return method.Outputs.Pack(common.HexToAddress(response.Signer))
}
2024-05-09 18:54:47 +00:00
func (d *DASignersPrecompile) GetAggPkG1(ctx sdk.Context, _ *vm.EVM, method *abi.Method, args []interface{}) ([]byte, error) {
req, err := NewQueryAggregatePubkeyG1Request(args)
if err != nil {
return nil, err
}
response, err := d.dasignersKeeper.AggregatePubkeyG1(sdk.WrapSDKContext(ctx), req)
if err != nil {
return nil, err
}
2024-05-11 09:13:54 +00:00
return method.Outputs.Pack(NewBN254G1Point(response.AggregatePubkeyG1), big.NewInt(int64(response.Total)), big.NewInt(int64(response.Hit)))
2024-05-09 18:54:47 +00:00
}