Skip to content

Commit

Permalink
Update bridges between two spaces for CIP 1559 (#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
wanliqun authored Jun 13, 2024
1 parent 5a9d7fe commit 01a1f49
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 99 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ require (
github.com/mcuadros/go-defaults v1.2.0
github.com/montanaflynn/stats v0.6.6
github.com/openweb3/go-rpc-provider v0.3.3-0.20240314082803-8ee6dd0c7e4f
github.com/openweb3/web3go v0.2.8-0.20240611080531-f6a84f75d083
github.com/openweb3/web3go v0.2.8-0.20240613033628-df44b448f8a4
github.com/pkg/errors v0.9.1
github.com/royeo/dingrobot v1.0.1-0.20191230075228-c90a788ca8fd
github.com/rs/cors v1.7.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -600,8 +600,8 @@ github.com/openweb3/go-rpc-provider v0.3.3-0.20240314082803-8ee6dd0c7e4f/go.mod
github.com/openweb3/go-sdk-common v0.0.0-20220720074746-a7134e1d372c h1:BrPXZpkTdmZe5bNjSSnxWqL44X9FcZ3xftLcYNkIJ68=
github.com/openweb3/go-sdk-common v0.0.0-20220720074746-a7134e1d372c/go.mod h1:0WCVKMiLiYEaHhpQWQ3rgLti/Fv/+JPRiB0sEoovwk8=
github.com/openweb3/web3go v0.2.3/go.mod h1:oiiUgYd/A1D6ZX8PjVnWZGX4aUn/n8DlSLFyrsqQg1k=
github.com/openweb3/web3go v0.2.8-0.20240611080531-f6a84f75d083 h1:Lv9YSJLAvXiiAnbkwrJ8WckcEsCVyfWBDFUvHIe1U28=
github.com/openweb3/web3go v0.2.8-0.20240611080531-f6a84f75d083/go.mod h1:FKQRl5wjQE/in9GEiHFCJ9JNVhizKeJ7KLxhbhUA/cw=
github.com/openweb3/web3go v0.2.8-0.20240613033628-df44b448f8a4 h1:FBYV/425wN6U6ZyJlFVa86ZwB8laYQbzWcYH3+LflrU=
github.com/openweb3/web3go v0.2.8-0.20240613033628-df44b448f8a4/go.mod h1:FKQRl5wjQE/in9GEiHFCJ9JNVhizKeJ7KLxhbhUA/cw=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
Expand Down
73 changes: 55 additions & 18 deletions rpc/cfxbridge/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ import (
var (
txnStatusFailed = hexutil.Uint64(1) // conflux tx status `failed`
txnStatusSuccess = hexutil.Uint64(0) // conflux tx status `success`

// conflux txn type
txnTypeLegacy = hexutil.Uint64(types.TRANSACTION_TYPE_LEGACY)
txnType1559 = hexutil.Uint64(types.TRANSACTION_TYPE_1559)
txnType2930 = hexutil.Uint64(types.TRANSACTION_TYPE_2930)

// conflux space
spaceEVM = types.SPACE_EVM
)

func NormalizeBig(value *big.Int, err error) (*hexutil.Big, error) {
Expand Down Expand Up @@ -87,6 +95,18 @@ func ConvertTxStatusNullable(value *uint64) (status *hexutil.Uint64) {
return
}

// Deduce transaction type from other transaction fields but without furthur validation.
func DeduceTxnType(txn *ethTypes.TransactionDetail) *hexutil.Uint64 {
switch {
case txn.MaxFeePerGas != nil || txn.MaxPriorityFeePerGas != nil:
return &txnType1559
case txn.Accesses != nil:
return &txnType2930
default:
return &txnTypeLegacy
}
}

func ConvertTx(tx *ethTypes.TransactionDetail, ethNetworkId uint32) *types.Transaction {
if tx == nil {
return nil
Expand All @@ -97,25 +117,36 @@ func ConvertTx(tx *ethTypes.TransactionDetail, ethNetworkId uint32) *types.Trans
chainId = types.NewBigIntByRaw(tx.ChainID)
}

var acl *types.AccessList
if tx.Accesses != nil {
acl = &types.AccessList{}
acl.FromEthType(&tx.Accesses, ethNetworkId)
}

return &types.Transaction{
Hash: types.Hash(tx.Hash.Hex()),
Nonce: types.NewBigInt(tx.Nonce),
BlockHash: ConvertHashNullable(tx.BlockHash),
TransactionIndex: (*hexutil.Uint64)(tx.TransactionIndex),
From: ConvertAddress(tx.From, ethNetworkId),
To: ConvertAddressNullable(tx.To, ethNetworkId),
Value: types.NewBigIntByRaw(tx.Value),
GasPrice: types.NewBigIntByRaw(tx.GasPrice),
Gas: types.NewBigInt(tx.Gas),
ContractCreated: ConvertAddressNullable(tx.Creates, ethNetworkId),
Data: hexutil.Encode(tx.Input),
StorageLimit: HexBig0,
EpochHeight: HexBig0,
ChainID: chainId,
Status: ConvertTxStatusNullable(tx.Status),
V: types.NewBigIntByRaw(tx.V),
R: types.NewBigIntByRaw(tx.R),
S: types.NewBigIntByRaw(tx.S),
TransactionType: DeduceTxnType(tx),
Hash: types.Hash(tx.Hash.Hex()),
Nonce: types.NewBigInt(tx.Nonce),
BlockHash: ConvertHashNullable(tx.BlockHash),
TransactionIndex: (*hexutil.Uint64)(tx.TransactionIndex),
From: ConvertAddress(tx.From, ethNetworkId),
To: ConvertAddressNullable(tx.To, ethNetworkId),
Value: types.NewBigIntByRaw(tx.Value),
GasPrice: types.NewBigIntByRaw(tx.GasPrice),
Gas: types.NewBigInt(tx.Gas),
ContractCreated: ConvertAddressNullable(tx.Creates, ethNetworkId),
Data: hexutil.Encode(tx.Input),
StorageLimit: HexBig0,
EpochHeight: HexBig0,
ChainID: chainId,
Status: ConvertTxStatusNullable(tx.Status),
AccessList: acl,
MaxPriorityFeePerGas: types.NewBigIntByRaw(tx.MaxPriorityFeePerGas),
MaxFeePerGas: types.NewBigIntByRaw(tx.MaxFeePerGas),
V: types.NewBigIntByRaw(tx.V),
R: types.NewBigIntByRaw(tx.R),
S: types.NewBigIntByRaw(tx.S),
YParity: (*hexutil.Uint64)(tx.YParity),
}
}

Expand Down Expand Up @@ -148,6 +179,7 @@ func ConvertBlockHeader(block *ethTypes.Block, ethNetworkId uint32) *types.Block
BlockNumber: types.NewBigIntByRaw(block.Number),
GasLimit: types.NewBigInt(block.GasLimit),
GasUsed: types.NewBigInt(block.GasUsed),
BaseFeePerGas: types.NewBigIntByRaw(block.BaseFeePerGas),
Timestamp: types.NewBigInt(block.Timestamp),
Difficulty: types.NewBigIntByRaw(block.Difficulty),
PowQuality: HexBig0,
Expand Down Expand Up @@ -215,6 +247,7 @@ func ConvertLog(log *ethTypes.Log, ethNetworkId uint32) *types.Log {
TransactionIndex: types.NewBigInt(uint64(log.TxIndex)), // tx index in block
LogIndex: types.NewBigInt(uint64(log.Index)), // log index in block
TransactionLogIndex: types.NewBigInt(uint64(*log.TransactionLogIndex)), // log index in tx
Space: &spaceEVM,
}
}

Expand All @@ -241,14 +274,17 @@ func ConvertReceipt(receipt *ethTypes.Receipt, ethNetworkId uint32) *types.Trans
)

return &types.TransactionReceipt{
Type: (*hexutil.Uint64)(receipt.Type),
TransactionHash: types.Hash(receipt.TransactionHash.Hex()),
Index: hexutil.Uint64(receipt.TransactionIndex),
BlockHash: types.Hash(receipt.BlockHash.Hex()),
EpochNumber: types.NewUint64(receipt.BlockNumber),
From: ConvertAddress(receipt.From, ethNetworkId),
To: ConvertAddressNullable(receipt.To, ethNetworkId),
GasUsed: types.NewBigInt(receipt.GasUsed),
AccumulatedGasUsed: types.NewBigInt(receipt.CumulativeGasUsed),
GasFee: types.NewBigIntByRaw(gasFee),
EffectiveGasPrice: types.NewBigInt(receipt.EffectiveGasPrice),
ContractCreated: ConvertAddressNullable(receipt.ContractAddress, ethNetworkId),
Logs: logs,
LogsBloom: types.Bloom(hexutil.Encode(receipt.LogsBloom.Bytes())),
Expand All @@ -259,6 +295,7 @@ func ConvertReceipt(receipt *ethTypes.Receipt, ethNetworkId uint32) *types.Trans
StorageCoveredBySponsor: false,
StorageCollateralized: 0,
StorageReleased: emptyStorageChangeList,
BurntGasFee: types.NewBigIntByRaw(receipt.BurntGasFee),
}
}

Expand Down
31 changes: 23 additions & 8 deletions rpc/cfxbridge/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,18 @@ func (ea *EthAddress) UnmarshalJSON(data []byte) error {
}

// EthCallRequest is compatible with CFX CallRequest and accepts hex40 format address.
// Note, StorageLimit field is simply ignored.
// Note, `StorageLimit` and `TransactionType` fields are simply ignored.
type EthCallRequest struct {
From *EthAddress
To *EthAddress
GasPrice *hexutil.Big
Gas *hexutil.Uint64
Value *hexutil.Big
Nonce *hexutil.Uint64
Data *string
From *EthAddress
To *EthAddress
GasPrice *hexutil.Big
Gas *hexutil.Uint64
MaxFeePerGas *hexutil.Big
MaxPriorityFeePerGas *hexutil.Big
Value *hexutil.Big
Nonce *hexutil.Uint64
Data *string
AccessList types.AccessList
}

func (req *EthCallRequest) ToCallMsg() ethTypes.CallRequest {
Expand All @@ -186,6 +189,14 @@ func (req *EthCallRequest) ToCallMsg() ethTypes.CallRequest {
msg.Gas = (*uint64)(req.Gas)
}

if req.MaxFeePerGas != nil {
msg.MaxFeePerGas = req.MaxFeePerGas.ToInt()
}

if req.MaxPriorityFeePerGas != nil {
msg.MaxPriorityFeePerGas = req.MaxPriorityFeePerGas.ToInt()
}

if req.Value != nil {
msg.Value = req.Value.ToInt()
}
Expand All @@ -198,6 +209,10 @@ func (req *EthCallRequest) ToCallMsg() ethTypes.CallRequest {
msg.Data = hexutil.MustDecode(*req.Data)
}

if req.AccessList != nil {
msg.AccessList = req.AccessList.ToEthType()
}

return msg
}

Expand Down
100 changes: 53 additions & 47 deletions rpc/ethbridge/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,36 +67,42 @@ func ConvertTx(tx *cfxtypes.Transaction, txExt *store.TransactionExtra) *types.T
nonce = tx.Nonce.ToInt().Uint64()
}

var access gethTypes.AccessList
if tx.AccessList != nil {
access = *tx.AccessList.ToEthType()
}

ethTxn := &types.TransactionDetail{
BlockHash: tx.BlockHash.ToCommonHash(),
ChainID: big.NewInt(int64(chainId)),
Creates: creates,
From: from,
Gas: gas,
GasPrice: tx.GasPrice.ToInt(),
Hash: ConvertHash(tx.Hash),
Input: input,
Nonce: nonce,
R: tx.R.ToInt(),
S: tx.S.ToInt(),
Status: ConvertTxStatus(tx.Status),
To: to,
TransactionIndex: (*uint64)(tx.TransactionIndex),
V: tx.V.ToInt(),
Value: tx.Value.ToInt(),
Type: (*uint64)(tx.TransactionType),
Accesses: access,
BlockHash: tx.BlockHash.ToCommonHash(),
ChainID: big.NewInt(int64(chainId)),
Creates: creates,
From: from,
Gas: gas,
GasPrice: tx.GasPrice.ToInt(),
Hash: ConvertHash(tx.Hash),
Input: input,
MaxFeePerGas: tx.MaxFeePerGas.ToInt(),
MaxPriorityFeePerGas: tx.MaxPriorityFeePerGas.ToInt(),
Nonce: nonce,
R: tx.R.ToInt(),
S: tx.S.ToInt(),
Status: ConvertTxStatus(tx.Status),
To: to,
TransactionIndex: (*uint64)(tx.TransactionIndex),
V: tx.V.ToInt(),
Value: tx.Value.ToInt(),
YParity: (*uint64)(tx.YParity),
}

if !util.IsEip155Tx(ethTxn) { // only return chainID for EIP155 tx
tx.ChainID = nil
}

// fill missed data field `Accesses`, `BlockNumber`, `MaxFeePerGas`, `MaxPriorityFeePerGas`, `type`, `StandardV`
// fill missed data field `BlockNumber` and `StandardV`
if txExt != nil {
ethTxn.Accesses = txExt.Accesses
ethTxn.BlockNumber = txExt.BlockNumber.ToInt()
ethTxn.MaxFeePerGas = txExt.MaxFeePerGas.ToInt()
ethTxn.MaxPriorityFeePerGas = txExt.MaxPriorityFeePerGas.ToInt()
ethTxn.Type = txExt.Type
ethTxn.StandardV = txExt.StandardV.ToInt()
}

Expand Down Expand Up @@ -149,6 +155,7 @@ func ConvertBlockHeader(value *cfxtypes.BlockHeader, blockExt *store.BlockExtra)

ethBlock := &types.Block{
Author: &minerAddr,
BaseFeePerGas: value.BaseFeePerGas.ToInt(),
Difficulty: (*big.Int)(value.Difficulty),
ExtraData: extraData,
GasLimit: value.GasLimit.ToInt().Uint64(),
Expand All @@ -167,9 +174,8 @@ func ConvertBlockHeader(value *cfxtypes.BlockHeader, blockExt *store.BlockExtra)
Uncles: uncleHashes,
}

// fill missed data fields `BaseFeePerGas`, `MixHash`, `TotalDifficulty`, `Sha3Uncles`
// fill missed data fields `MixHash`, `TotalDifficulty` and `Sha3Uncles`
if blockExt != nil {
ethBlock.BaseFeePerGas = blockExt.BaseFeePerGas.ToInt()
ethBlock.MixHash = blockExt.MixHash
ethBlock.TotalDifficulty = blockExt.TotalDifficulty.ToInt()

Expand Down Expand Up @@ -240,33 +246,33 @@ func ConvertReceipt(value *cfxtypes.TransactionReceipt, rcptExtra *store.Receipt
root, _ = hexutil.Decode(string(value.StateRoot))
}

receipt := &types.Receipt{
BlockHash: ConvertHash(value.BlockHash),
BlockNumber: uint64(*value.EpochNumber),
ContractAddress: contractAddr,
From: from,
GasUsed: value.GasUsed.ToInt().Uint64(),
Logs: logs,
LogsBloom: logsBloom,
Root: root,
Status: ConvertTxStatus(&value.OutcomeStatus),
To: to,
TransactionHash: ConvertHash(value.TransactionHash),
TransactionIndex: uint64(value.Index),
TxExecErrorMsg: value.TxExecErrorMsg,
var cumulativeGasUsed uint64
if value.AccumulatedGasUsed != nil {
cumulativeGasUsed = value.AccumulatedGasUsed.ToInt().Uint64()
}

// fill missed data field `CumulativeGasUsed`, `EffectiveGasPrice`, `Type`
if rcptExtra != nil {
if rcptExtra.CumulativeGasUsed != nil {
receipt.CumulativeGasUsed = *rcptExtra.CumulativeGasUsed
}

if rcptExtra.EffectiveGasPrice != nil {
receipt.EffectiveGasPrice = *rcptExtra.EffectiveGasPrice
}
var effectiveGasPrice uint64
if value.EffectiveGasPrice != nil {
effectiveGasPrice = value.EffectiveGasPrice.ToInt().Uint64()
}

receipt.Type = rcptExtra.Type
receipt := &types.Receipt{
BlockHash: ConvertHash(value.BlockHash),
BlockNumber: uint64(*value.EpochNumber),
ContractAddress: contractAddr,
CumulativeGasUsed: cumulativeGasUsed,
EffectiveGasPrice: effectiveGasPrice,
From: from,
GasUsed: value.GasUsed.ToInt().Uint64(),
Logs: logs,
LogsBloom: logsBloom,
Root: root,
Status: ConvertTxStatus(&value.OutcomeStatus),
To: to,
TransactionHash: ConvertHash(value.TransactionHash),
TransactionIndex: uint64(value.Index),
TxExecErrorMsg: value.TxExecErrorMsg,
Type: (*uint64)(value.Type),
}

return receipt
Expand Down
Loading

0 comments on commit 01a1f49

Please sign in to comment.