Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
3 changes: 3 additions & 0 deletions core/types/receipt.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ const (

// ReceiptStatusSuccessful is the status code of a transaction if execution succeeded.
ReceiptStatusSuccessful = uint64(1)

// DefaultEffectiveGasPrice is the default effective gas price for a transaction.
DefaultEffectiveGasPrice = 100 * 1e9
)

// Receipt represents the results of a transaction.
Expand Down
7 changes: 3 additions & 4 deletions core/types/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,15 @@ import (
"sync/atomic"
"time"

"github.com/harmony-one/harmony/internal/params"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/rlp"

"github.com/harmony-one/harmony/crypto/hash"
common2 "github.com/harmony-one/harmony/internal/common"
"github.com/harmony-one/harmony/internal/params"
staking "github.com/harmony-one/harmony/staking/types"
errs "github.com/pkg/errors"
)

// no go:generate gencodec -type txdata -field-override txdataMarshaling -out gen_tx_json.go
Expand Down Expand Up @@ -531,7 +530,7 @@ func (tx *Transaction) SenderAddress() (common.Address, error) {
}
addr, err := Sender(signer, tx)
if err != nil {
return common.Address{}, err
return common.Address{}, errs.WithMessage(err, "failed to extract sender address")
}
return addr, nil
}
Expand Down
24 changes: 16 additions & 8 deletions rosetta/services/tx_operation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,31 @@ import (
"reflect"
"testing"

"github.com/harmony-one/harmony/hmy/tracers"

"github.com/coinbase/rosetta-sdk-go/types"
ethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"

hmytypes "github.com/harmony-one/harmony/core/types"
"github.com/harmony-one/harmony/core/vm"
"github.com/harmony-one/harmony/hmy"
"github.com/harmony-one/harmony/hmy/tracers"
internalCommon "github.com/harmony-one/harmony/internal/common"
"github.com/harmony-one/harmony/internal/params"
"github.com/harmony-one/harmony/rosetta/common"
rpcV2 "github.com/harmony-one/harmony/rpc/harmony/v2"
"github.com/harmony-one/harmony/staking"
stakingTypes "github.com/harmony-one/harmony/staking/types"
"github.com/harmony-one/harmony/test/helpers"
)

// GetMessageFromStakingTx gets the staking message, as seen by the rpc layer
func GetMessageFromStakingTx(tx *stakingTypes.StakingTransaction) (map[string]interface{}, error) {
rpcStakingTx, err := rpcV2.NewStakingTransaction(tx, ethcommon.Hash{}, 0, 0, 0, true)
if err != nil {
return nil, err
}
return types.MarshalMap(rpcStakingTx.Msg)
}

func TestGetStakingOperationsFromCreateValidator(t *testing.T) {
gasLimit := uint64(1e18)
createValidatorTxDescription := stakingTypes.Description{
Expand All @@ -46,7 +54,7 @@ func TestGetStakingOperationsFromCreateValidator(t *testing.T) {
if err != nil {
t.Fatal(err.Error())
}
metadata, err := helpers.GetMessageFromStakingTx(tx)
metadata, err := GetMessageFromStakingTx(tx)
if err != nil {
t.Fatal(err.Error())
}
Expand Down Expand Up @@ -187,7 +195,7 @@ func TestGetStakingOperationsFromDelegate(t *testing.T) {
if err != nil {
t.Fatal(err.Error())
}
metadata, err := helpers.GetMessageFromStakingTx(tx)
metadata, err := GetMessageFromStakingTx(tx)
if err != nil {
t.Fatal(err.Error())
}
Expand Down Expand Up @@ -323,7 +331,7 @@ func TestGetStakingOperationsFromUndelegate(t *testing.T) {
if err != nil {
t.Fatal(err.Error())
}
metadata, err := helpers.GetMessageFromStakingTx(tx)
metadata, err := GetMessageFromStakingTx(tx)
if err != nil {
t.Fatal(err.Error())
}
Expand Down Expand Up @@ -397,7 +405,7 @@ func TestGetStakingOperationsFromCollectRewards(t *testing.T) {
if err != nil {
t.Fatal(err.Error())
}
metadata, err := helpers.GetMessageFromStakingTx(tx)
metadata, err := GetMessageFromStakingTx(tx)
if err != nil {
t.Fatal(err.Error())
}
Expand Down Expand Up @@ -458,7 +466,7 @@ func TestGetStakingOperationsFromEditValidator(t *testing.T) {
if err != nil {
t.Fatal(err.Error())
}
metadata, err := helpers.GetMessageFromStakingTx(tx)
metadata, err := GetMessageFromStakingTx(tx)
if err != nil {
t.Fatal(err.Error())
}
Expand Down
10 changes: 9 additions & 1 deletion rpc/harmony/eth/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,14 @@ func NewReceipt(senderAddr common.Address, tx *types.EthTransaction, blockHash c
receipt.Logs[i].TxHash = ethTxHash
}

var effectiveGasPrice hexutil.Big = hexutil.Big(*big.NewInt(types.DefaultEffectiveGasPrice))
if receipt.EffectiveGasPrice != nil {
//e := hexutil.Big(*new(big.Int).Set(receipt.EffectiveGasPrice))
//effectiveGasPrice = &e
//effectiveGasPrice = hexutil.Big(*receipt.EffectiveGasPrice)
effectiveGasPrice = hexutil.Big(*receipt.EffectiveGasPrice)
}

fields := map[string]interface{}{
"blockHash": blockHash,
"blockNumber": hexutil.Uint64(blockNumber),
Expand All @@ -152,7 +160,7 @@ func NewReceipt(senderAddr common.Address, tx *types.EthTransaction, blockHash c
"contractAddress": nil,
"logs": receipt.Logs,
"logsBloom": receipt.Bloom,
"effectiveGasPrice": hexutil.Big(*receipt.EffectiveGasPrice),
"effectiveGasPrice": effectiveGasPrice,
}

// Assign receipt status or post state.
Expand Down
45 changes: 45 additions & 0 deletions rpc/harmony/eth/types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package eth

import (
"math/big"
"testing"

common2 "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/harmony-one/harmony/core/types"
"github.com/harmony-one/harmony/internal/common"
"github.com/stretchr/testify/require"
)

func asInt(i any) *big.Int {
if s, ok := i.(hexutil.Big); ok {
return s.ToInt()
}
panic("<failed to convert>")
}

func TestNewReceipt(t *testing.T) {
var (
senderAddr = common.Address{}
tx = &types.EthTransaction{}
blockHash = common2.Hash{}
blockNumber uint64 = 1
blockIndex uint64 = 1
receipt = &types.Receipt{}
r = make(map[string]interface{})
err error
)

t.Run("effectiveGasPrice", func(t *testing.T) {
r, err = NewReceipt(common2.Address(senderAddr), tx, blockHash, blockNumber, blockIndex, receipt)
require.NoError(t, err)
require.EqualValues(t, big.NewInt(types.DefaultEffectiveGasPrice), asInt(r["effectiveGasPrice"]))

rec := &types.Receipt{
EffectiveGasPrice: big.NewInt(1),
}
r, err = NewReceipt(common2.Address(senderAddr), tx, blockHash, blockNumber, blockIndex, rec)
require.NoError(t, err)
require.EqualValues(t, big.NewInt(1), asInt(r["effectiveGasPrice"]))
})
}
60 changes: 57 additions & 3 deletions rpc/harmony/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,16 @@ type TxReceipt struct {
EffectiveGasPrice hexutil.Big `json:"effectiveGasPrice"`
}

// GetEffectiveGasPrice returns the effective gas price of the tx receipt
func (s TxReceipt) GetEffectiveGasPrice() hexutil.Big {
return s.EffectiveGasPrice
}

// GetContractAddress returns the contract address of the tx receipt
func (s TxReceipt) GetContractAddress() common.Address {
return s.ContractAddress
}

// StakingTxReceipt represents a staking transaction receipt that will serialize to the RPC representation.
type StakingTxReceipt struct {
BlockHash common.Hash `json:"blockHash"`
Expand All @@ -209,6 +219,16 @@ type StakingTxReceipt struct {
EffectiveGasPrice hexutil.Big `json:"effectiveGasPrice"`
}

// GetEffectiveGasPrice returns the effective gas price of the staking tx receipt
func (s StakingTxReceipt) GetEffectiveGasPrice() hexutil.Big {
return s.EffectiveGasPrice
}

// GetContractAddress returns the contract address of the staking tx receipt
func (s StakingTxReceipt) GetContractAddress() common.Address {
return s.ContractAddress
}

// CxReceipt represents a CxReceipt that will serialize to the RPC representation of a CxReceipt
type CxReceipt struct {
BlockHash common.Hash `json:"blockHash"`
Expand Down Expand Up @@ -304,11 +324,13 @@ func NewTransaction(
return result, nil
}

type Receipt interface{}

// NewReceipt returns a transaction OR staking transaction that will serialize to the RPC
// representation.
func NewReceipt(
tx interface{}, blockHash common.Hash, blockNumber, blockIndex uint64, receipt *types.Receipt,
) (interface{}, error) {
) (Receipt, error) {
plainTx, ok := tx.(*types.Transaction)
if ok {
return NewTxReceipt(plainTx, blockHash, blockNumber, blockIndex, receipt)
Expand Down Expand Up @@ -346,6 +368,11 @@ func NewTxReceipt(
}
}

var effectiveGasPrice = hexutil.Big(*big.NewInt(types.DefaultEffectiveGasPrice))
if receipt.EffectiveGasPrice != nil {
effectiveGasPrice = hexutil.Big(*receipt.EffectiveGasPrice)
}

// Declare receipt
txReceipt := &TxReceipt{
BlockHash: blockHash,
Expand All @@ -361,7 +388,7 @@ func NewTxReceipt(
To: receiver,
Root: receipt.PostState,
Status: hexutil.Uint(receipt.Status),
EffectiveGasPrice: hexutil.Big(*receipt.EffectiveGasPrice),
EffectiveGasPrice: effectiveGasPrice,
}

// Set empty array for empty logs
Expand Down Expand Up @@ -390,6 +417,11 @@ func NewStakingTxReceipt(
return nil, err
}

var effectiveGasPrice hexutil.Big = hexutil.Big(*big.NewInt(types.DefaultEffectiveGasPrice))
if receipt.EffectiveGasPrice != nil {
effectiveGasPrice = hexutil.Big(*receipt.EffectiveGasPrice)
}

// Declare receipt
txReceipt := &StakingTxReceipt{
BlockHash: blockHash,
Expand All @@ -404,7 +436,7 @@ func NewStakingTxReceipt(
Type: hexutil.Uint64(tx.StakingType()),
Root: receipt.PostState,
Status: hexutil.Uint(receipt.Status),
EffectiveGasPrice: hexutil.Big(*receipt.EffectiveGasPrice),
EffectiveGasPrice: effectiveGasPrice,
}

// Set empty array for empty logs
Expand Down Expand Up @@ -721,3 +753,25 @@ func NewStakingTransactionFromBlockIndex(b *types.Block, index uint64) (*Staking
}
return NewStakingTransaction(txs[index], b.Hash(), b.NumberU64(), b.Time().Uint64(), index)
}

type getEffectiveGasPrice interface {
GetEffectiveGasPrice() hexutil.Big
}

type getContractAddress interface {
GetContractAddress() common.Address
}

func MustReceiptEffectivePrice(receipt Receipt) hexutil.Big {
if s, ok := receipt.(getEffectiveGasPrice); ok {
return s.GetEffectiveGasPrice()
}
panic(fmt.Sprintf("<failed to convert, i(%T) does not support GetEffectiveGasPrice interface>", receipt))
}

func MustContractAddress(receipt Receipt) common.Address {
if s, ok := receipt.(getContractAddress); ok {
return s.GetContractAddress()
}
panic(fmt.Sprintf("<failed to convert, i(%T) does not support getContractAddress interface>", receipt))
}
Loading