Skip to content

Commit 3cf7d2e

Browse files
authored
internal/ethapi: add CallArgs.ToMessage method (#20854)
ToMessage is used to convert between ethapi.CallArgs and types.Message. It reduces the length of the DoCall method by about half by abstracting out the conversion between the CallArgs and the Message. This should improve the code's maintainability and reusability.
1 parent be6078a commit 3cf7d2e

File tree

1 file changed

+37
-32
lines changed

1 file changed

+37
-32
lines changed

internal/ethapi/api.go

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,42 @@ type CallArgs struct {
739739
Data *hexutil.Bytes `json:"data"`
740740
}
741741

742+
// ToMessage converts CallArgs to the Message type used by the core evm
743+
func (args *CallArgs) ToMessage(globalGasCap *big.Int) types.Message {
744+
// Set sender address or use zero address if none specified.
745+
var addr common.Address
746+
if args.From != nil {
747+
addr = *args.From
748+
}
749+
750+
// Set default gas & gas price if none were set
751+
gas := uint64(math.MaxUint64 / 2)
752+
if args.Gas != nil {
753+
gas = uint64(*args.Gas)
754+
}
755+
if globalGasCap != nil && globalGasCap.Uint64() < gas {
756+
log.Warn("Caller gas above allowance, capping", "requested", gas, "cap", globalGasCap)
757+
gas = globalGasCap.Uint64()
758+
}
759+
gasPrice := new(big.Int)
760+
if args.GasPrice != nil {
761+
gasPrice = args.GasPrice.ToInt()
762+
}
763+
764+
value := new(big.Int)
765+
if args.Value != nil {
766+
value = args.Value.ToInt()
767+
}
768+
769+
var data []byte
770+
if args.Data != nil {
771+
data = []byte(*args.Data)
772+
}
773+
774+
msg := types.NewMessage(addr, args.To, 0, value, gas, gasPrice, data, false)
775+
return msg
776+
}
777+
742778
// account indicates the overriding fields of account during the execution of
743779
// a message call.
744780
// Note, state and stateDiff can't be specified at the same time. If state is
@@ -761,12 +797,6 @@ func DoCall(ctx context.Context, b Backend, args CallArgs, blockNrOrHash rpc.Blo
761797
return nil, 0, false, err
762798
}
763799

764-
// Set sender address or use zero address if none specified.
765-
var addr common.Address
766-
if args.From != nil {
767-
addr = *args.From
768-
}
769-
770800
// Override the fields of specified contracts before execution.
771801
for addr, account := range overrides {
772802
// Override account nonce.
@@ -795,32 +825,6 @@ func DoCall(ctx context.Context, b Backend, args CallArgs, blockNrOrHash rpc.Blo
795825
}
796826
}
797827
}
798-
// Set default gas & gas price if none were set
799-
gas := uint64(math.MaxUint64 / 2)
800-
if args.Gas != nil {
801-
gas = uint64(*args.Gas)
802-
}
803-
if globalGasCap != nil && globalGasCap.Uint64() < gas {
804-
log.Warn("Caller gas above allowance, capping", "requested", gas, "cap", globalGasCap)
805-
gas = globalGasCap.Uint64()
806-
}
807-
gasPrice := new(big.Int)
808-
if args.GasPrice != nil {
809-
gasPrice = args.GasPrice.ToInt()
810-
}
811-
812-
value := new(big.Int)
813-
if args.Value != nil {
814-
value = args.Value.ToInt()
815-
}
816-
817-
var data []byte
818-
if args.Data != nil {
819-
data = []byte(*args.Data)
820-
}
821-
822-
// Create new call message
823-
msg := types.NewMessage(addr, args.To, 0, value, gas, gasPrice, data, false)
824828

825829
// Setup context so it may be cancelled the call has completed
826830
// or, in case of unmetered gas, setup a context with a timeout.
@@ -835,6 +839,7 @@ func DoCall(ctx context.Context, b Backend, args CallArgs, blockNrOrHash rpc.Blo
835839
defer cancel()
836840

837841
// Get a new instance of the EVM.
842+
msg := args.ToMessage(globalGasCap)
838843
evm, vmError, err := b.GetEVM(ctx, msg, state, header)
839844
if err != nil {
840845
return nil, 0, false, err

0 commit comments

Comments
 (0)