Skip to content

Commit

Permalink
[action] Add grantReward ToEthTx() (iotexproject#4047)
Browse files Browse the repository at this point in the history
  • Loading branch information
millken authored Apr 15, 2024
1 parent 0291751 commit da2517d
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 1 deletion.
73 changes: 73 additions & 0 deletions action/grantreward.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,61 @@ package action

import (
"math/big"
"strings"

"google.golang.org/protobuf/proto"

"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/core/types"
"github.com/iotexproject/iotex-core/pkg/util/byteutil"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
)

var (
_grantRewardMethod abi.Method
_ EthCompatibleAction = (*GrantReward)(nil)
)

const (
// BlockReward indicates that the action is to grant block reward
BlockReward = iota
// EpochReward indicates that the action is to grant epoch reward
EpochReward

_grantrewardInterfaceABI = `[
{
"inputs": [
{
"internalType": "int8",
"name": "rewardType",
"type": "int8"
},
{
"internalType": "uint64",
"name": "height",
"type": "uint64"
}
],
"name": "grantReward",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
]`
)

func init() {
grantRewardInterface, err := abi.JSON(strings.NewReader(_grantrewardInterfaceABI))
if err != nil {
panic(err)
}
var ok bool
_grantRewardMethod, ok = grantRewardInterface.Methods["grantReward"]
if !ok {
panic("fail to load the method")
}
}

// GrantReward is the action to grant either block or epoch reward
type GrantReward struct {
AbstractAction
Expand Down Expand Up @@ -78,6 +119,38 @@ func (*GrantReward) Cost() (*big.Int, error) {
return big.NewInt(0), nil
}

// EncodeABIBinary encodes data in abi encoding
func (g *GrantReward) EncodeABIBinary() ([]byte, error) {
return g.encodeABIBinary()
}

func (g *GrantReward) encodeABIBinary() ([]byte, error) {
data, err := _grantRewardMethod.Inputs.Pack(
int8(g.rewardType),
g.height,
)
if err != nil {
return nil, err
}
return append(_grantRewardMethod.ID, data...), nil
}

// ToEthTx converts a grant reward action to an ethereum transaction
func (g *GrantReward) ToEthTx(_ uint32) (*types.Transaction, error) {
data, err := g.encodeABIBinary()
if err != nil {
return nil, err
}
return types.NewTx(&types.LegacyTx{
Nonce: g.Nonce(),
GasPrice: g.GasPrice(),
Gas: g.GasLimit(),
To: &_rewardingProtocolEthAddr,
Data: data,
Value: big.NewInt(0),
}), nil
}

// GrantRewardBuilder is the struct to build GrantReward
type GrantRewardBuilder struct {
Builder
Expand Down
44 changes: 44 additions & 0 deletions action/grantreward_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package action

import (
"math/big"
"testing"

"github.com/iotexproject/iotex-core/pkg/util/byteutil"
"github.com/stretchr/testify/require"
)

func TestGrandReward(t *testing.T) {
require := require.New(t)
tests := []struct {
rewardType int
height uint64
}{
{BlockReward, 100},
{EpochReward, 200},
}
for _, test := range tests {
g := &GrantReward{
rewardType: test.rewardType,
height: test.height,
}
require.Equal(test.rewardType, g.RewardType())
require.Equal(test.height, g.Height())
require.NoError(g.SanityCheck())
require.NoError(g.LoadProto(g.Proto()))
intrinsicGas, err := g.IntrinsicGas()
require.NoError(err)
require.Equal(uint64(0), intrinsicGas)
cost, err := g.Cost()
require.NoError(err)
require.Equal(big.NewInt(0), cost)
ethTx, err := g.ToEthTx(0)
require.NoError(err)
require.NotNil(ethTx)
require.Equal(byteutil.Must(g.EncodeABIBinary()), ethTx.Data())
require.Equal(big.NewInt(0), ethTx.GasPrice())
require.Equal(uint64(0), ethTx.Gas())
require.Equal(big.NewInt(0), ethTx.Value())
require.Equal(_rewardingProtocolEthAddr.Hex(), ethTx.To().Hex())
}
}
2 changes: 1 addition & 1 deletion api/web3server_integrity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func getBlockByNumber(t *testing.T, handler *hTTPHandler) {
params string
expected int
}{
{`["1", true]`, 1},
{`["1", true]`, 2},
{`["1", false]`, 2},
{`["10", false]`, 0},
} {
Expand Down

0 comments on commit da2517d

Please sign in to comment.