Skip to content

Commit 2ecb728

Browse files
authored
Merge branch 'master' into master
2 parents 9f0e857 + 262dc4e commit 2ecb728

File tree

2 files changed

+50
-32
lines changed

2 files changed

+50
-32
lines changed

api/web3server.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/ethereum/go-ethereum/core/types"
1414
"github.com/ethereum/go-ethereum/eth/tracers"
1515
"github.com/ethereum/go-ethereum/eth/tracers/logger"
16+
"github.com/ethereum/go-ethereum/rpc"
1617
"github.com/iotexproject/go-pkgs/crypto"
1718
"github.com/iotexproject/go-pkgs/hash"
1819
"github.com/iotexproject/go-pkgs/util"
@@ -418,9 +419,12 @@ func (svr *web3Handler) getBalance(in *gjson.Result) (interface{}, error) {
418419
return nil, err
419420
}
420421
var (
421-
accountMeta *iotextypes.AccountMeta
422-
height, archive = blockNumberToHeight(bn)
422+
accountMeta *iotextypes.AccountMeta
423423
)
424+
height, archive, err := svr.blockNumberOrHashToHeight(rpc.BlockNumberOrHashWithNumber(bn))
425+
if err != nil {
426+
return nil, err
427+
}
424428
if !archive {
425429
accountMeta, _, err = svr.coreService.Account(ioAddr)
426430
} else {
@@ -496,10 +500,13 @@ func (svr *web3Handler) call(ctx context.Context, in *gjson.Result) (interface{}
496500
var (
497501
elp = (&action.EnvelopeBuilder{}).SetAction(action.NewExecution(to, callMsg.Value, data)).
498502
SetGasLimit(callMsg.Gas).Build()
499-
ret string
500-
receipt *iotextypes.Receipt
501-
height, archive = blockNumberToHeight(callMsg.BlockNumber)
503+
ret string
504+
receipt *iotextypes.Receipt
502505
)
506+
height, archive, err := svr.blockNumberOrHashToHeight(callMsg.BlockNumberOrHash)
507+
if err != nil {
508+
return nil, err
509+
}
503510
if !archive {
504511
ret, receipt, err = svr.coreService.ReadContract(context.Background(), callMsg.From, elp)
505512
} else {
@@ -1153,6 +1160,7 @@ func (svr *web3Handler) traceCall(ctx context.Context, in *gjson.Result) (interf
11531160
err error
11541161
callMsg *callMsg
11551162
)
1163+
// TODO: refactor blkNumOrHashObj to avoid code duplication
11561164
blkNumOrHashObj, options := in.Get("params.1"), in.Get("params.2")
11571165
callMsg, err = parseCallObject(in)
11581166
if err != nil {

api/web3server_utils.go

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -284,16 +284,16 @@ func parseLogRequest(in gjson.Result) (*filterObject, error) {
284284
}
285285

286286
type callMsg struct {
287-
From address.Address // the sender of the 'transaction'
288-
To string // the destination contract (empty for contract creation)
289-
Gas uint64 // if 0, the call executes with near-infinite gas
290-
GasPrice *big.Int // wei <-> gas exchange ratio
291-
GasFeeCap *big.Int // EIP-1559 fee cap per gas.
292-
GasTipCap *big.Int // EIP-1559 tip per gas.
293-
Value *big.Int // amount of wei sent along with the call
294-
Data []byte // input data, usually an ABI-encoded contract method invocation
295-
AccessList types.AccessList // EIP-2930 access list.
296-
BlockNumber rpc.BlockNumber
287+
From address.Address // the sender of the 'transaction'
288+
To string // the destination contract (empty for contract creation)
289+
Gas uint64 // if 0, the call executes with near-infinite gas
290+
GasPrice *big.Int // wei <-> gas exchange ratio
291+
GasFeeCap *big.Int // EIP-1559 fee cap per gas.
292+
GasTipCap *big.Int // EIP-1559 tip per gas.
293+
Value *big.Int // amount of wei sent along with the call
294+
Data []byte // input data, usually an ABI-encoded contract method invocation
295+
AccessList types.AccessList // EIP-2930 access list.
296+
BlockNumberOrHash rpc.BlockNumberOrHash // EIP-1898
297297
}
298298

299299
func parseCallObject(in *gjson.Result) (*callMsg, error) {
@@ -307,7 +307,7 @@ func parseCallObject(in *gjson.Result) (*callMsg, error) {
307307
value *big.Int = big.NewInt(0)
308308
data []byte
309309
acl types.AccessList
310-
bn = rpc.LatestBlockNumber
310+
bn = rpc.BlockNumberOrHashWithNumber(rpc.LatestBlockNumber)
311311
err error
312312
)
313313
fromStr := in.Get("params.0.from").String()
@@ -378,24 +378,25 @@ func parseCallObject(in *gjson.Result) (*callMsg, error) {
378378
}
379379
}
380380
if bnParam := in.Get("params.1"); bnParam.Exists() {
381-
if err = bn.UnmarshalJSON([]byte(bnParam.String())); err != nil {
381+
if err = bn.UnmarshalJSON([]byte(bnParam.Raw)); err != nil {
382382
return nil, errors.Wrapf(err, "failed to unmarshal height %s", bnParam.String())
383383
}
384384
}
385385
return &callMsg{
386-
From: from,
387-
To: to,
388-
Gas: gasLimit,
389-
GasPrice: gasPrice,
390-
GasFeeCap: gasFeeCap,
391-
GasTipCap: gasTipCap,
392-
Value: value,
393-
Data: data,
394-
AccessList: acl,
395-
BlockNumber: bn,
386+
From: from,
387+
To: to,
388+
Gas: gasLimit,
389+
GasPrice: gasPrice,
390+
GasFeeCap: gasFeeCap,
391+
GasTipCap: gasTipCap,
392+
Value: value,
393+
Data: data,
394+
AccessList: acl,
395+
BlockNumberOrHash: bn,
396396
}, nil
397397
}
398398

399+
// TODO: fix this to support eip 1898
399400
func parseBlockNumber(in *gjson.Result) (rpc.BlockNumber, error) {
400401
if !in.Exists() {
401402
return rpc.LatestBlockNumber, nil
@@ -407,14 +408,23 @@ func parseBlockNumber(in *gjson.Result) (rpc.BlockNumber, error) {
407408
return height, nil
408409
}
409410

410-
func blockNumberToHeight(bn rpc.BlockNumber) (uint64, bool) {
411-
switch bn {
411+
func (svr *web3Handler) blockNumberOrHashToHeight(bn rpc.BlockNumberOrHash) (uint64, bool, error) {
412+
if bn.BlockHash != nil {
413+
bh := (*bn.BlockHash).String()
414+
blk, err := svr.coreService.BlockByHash(util.Remove0xPrefix(bh))
415+
if err != nil {
416+
return 0, false, errors.Wrapf(err, "failed to get block height by hash %s", bh)
417+
}
418+
return uint64(blk.Block.Height()), true, nil
419+
}
420+
421+
switch *bn.BlockNumber {
412422
case rpc.SafeBlockNumber, rpc.FinalizedBlockNumber, rpc.LatestBlockNumber, rpc.PendingBlockNumber:
413-
return 0, false
423+
return 0, false, nil
414424
case rpc.EarliestBlockNumber:
415-
return 1, true
425+
return 1, true, nil
416426
default:
417-
return uint64(bn), true
427+
return uint64(*bn.BlockNumber), true, nil
418428
}
419429
}
420430

0 commit comments

Comments
 (0)