Skip to content

Commit f371e6c

Browse files
committed
Merge pull request #1156 from tgerring/issue1145
Differentiate between 0 and unspecified gas/gasprice
2 parents 3054fd4 + 1a96798 commit f371e6c

File tree

4 files changed

+73
-36
lines changed

4 files changed

+73
-36
lines changed

rpc/api.go

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,21 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
182182
nonce = args.Nonce.String()
183183
}
184184

185-
v, err := api.xeth().Transact(args.From, args.To, nonce, args.Value.String(), args.Gas.String(), args.GasPrice.String(), args.Data)
185+
var gas string
186+
if args.Gas == nil {
187+
gas = ""
188+
} else {
189+
gas = args.Gas.String()
190+
}
191+
192+
var gasprice string
193+
if args.GasPrice == nil {
194+
gasprice = ""
195+
} else {
196+
gasprice = args.GasPrice.String()
197+
}
198+
199+
v, err := api.xeth().Transact(args.From, args.To, nonce, args.Value.String(), gas, gasprice, args.Data)
186200
if err != nil {
187201
return err
188202
}
@@ -603,5 +617,19 @@ func (api *EthereumApi) doCall(params json.RawMessage) (string, string, error) {
603617
return "", "", err
604618
}
605619

606-
return api.xethAtStateNum(args.BlockNumber).Call(args.From, args.To, args.Value.String(), args.Gas.String(), args.GasPrice.String(), args.Data)
620+
var gas string
621+
if args.Gas == nil {
622+
gas = ""
623+
} else {
624+
gas = args.Gas.String()
625+
}
626+
627+
var gasprice string
628+
if args.GasPrice == nil {
629+
gasprice = ""
630+
} else {
631+
gasprice = args.GasPrice.String()
632+
}
633+
634+
return api.xethAtStateNum(args.BlockNumber).Call(args.From, args.To, args.Value.String(), gas, gasprice, args.Data)
607635
}

rpc/args.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -261,22 +261,22 @@ func (args *NewTxArgs) UnmarshalJSON(b []byte) (err error) {
261261
args.Value = num
262262

263263
num = nil
264-
if ext.Gas == nil {
265-
num = big.NewInt(0)
266-
} else {
264+
if ext.Gas != nil {
267265
if num, err = numString(ext.Gas); err != nil {
268266
return err
269267
}
268+
} else {
269+
num = nil
270270
}
271271
args.Gas = num
272272

273273
num = nil
274-
if ext.GasPrice == nil {
275-
num = big.NewInt(0)
276-
} else {
274+
if ext.GasPrice != nil {
277275
if num, err = numString(ext.GasPrice); err != nil {
278276
return err
279277
}
278+
} else {
279+
num = nil
280280
}
281281
args.GasPrice = num
282282

@@ -346,21 +346,21 @@ func (args *CallArgs) UnmarshalJSON(b []byte) (err error) {
346346
}
347347
args.Value = num
348348

349-
if ext.Gas == nil {
350-
num = big.NewInt(0)
351-
} else {
349+
if ext.Gas != nil {
352350
if num, err = numString(ext.Gas); err != nil {
353351
return err
354352
}
353+
} else {
354+
num = nil
355355
}
356356
args.Gas = num
357357

358-
if ext.GasPrice == nil {
359-
num = big.NewInt(0)
360-
} else {
358+
if ext.GasPrice != nil {
361359
if num, err = numString(ext.GasPrice); err != nil {
362360
return err
363361
}
362+
} else {
363+
num = nil
364364
}
365365
args.GasPrice = num
366366

rpc/args_test.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -573,14 +573,15 @@ func TestNewTxArgsGasMissing(t *testing.T) {
573573
"data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"
574574
}]`
575575
expected := new(NewTxArgs)
576-
expected.Gas = big.NewInt(0)
576+
expected.Gas = nil
577577

578578
args := new(NewTxArgs)
579579
if err := json.Unmarshal([]byte(input), &args); err != nil {
580580
t.Error(err)
581581
}
582582

583-
if bytes.Compare(expected.Gas.Bytes(), args.Gas.Bytes()) != 0 {
583+
if args.Gas != expected.Gas {
584+
// if bytes.Compare(expected.Gas.Bytes(), args.Gas.Bytes()) != 0 {
584585
t.Errorf("Gas shoud be %v but is %v", expected.Gas, args.Gas)
585586
}
586587
}
@@ -594,14 +595,15 @@ func TestNewTxArgsBlockGaspriceMissing(t *testing.T) {
594595
"data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"
595596
}]`
596597
expected := new(NewTxArgs)
597-
expected.GasPrice = big.NewInt(0)
598+
expected.GasPrice = nil
598599

599600
args := new(NewTxArgs)
600601
if err := json.Unmarshal([]byte(input), &args); err != nil {
601602
t.Error(err)
602603
}
603604

604-
if bytes.Compare(expected.GasPrice.Bytes(), args.GasPrice.Bytes()) != 0 {
605+
if args.GasPrice != expected.GasPrice {
606+
// if bytes.Compare(expected.GasPrice.Bytes(), args.GasPrice.Bytes()) != 0 {
605607
t.Errorf("GasPrice shoud be %v but is %v", expected.GasPrice, args.GasPrice)
606608
}
607609

@@ -829,9 +831,10 @@ func TestCallArgsGasMissing(t *testing.T) {
829831
}
830832

831833
expected := new(CallArgs)
832-
expected.Gas = big.NewInt(0)
834+
expected.Gas = nil
833835

834-
if bytes.Compare(expected.Gas.Bytes(), args.Gas.Bytes()) != 0 {
836+
if args.Gas != expected.Gas {
837+
// if bytes.Compare(expected.Gas.Bytes(), args.Gas.Bytes()) != 0 {
835838
t.Errorf("Gas shoud be %v but is %v", expected.Gas, args.Gas)
836839
}
837840

@@ -852,9 +855,10 @@ func TestCallArgsBlockGaspriceMissing(t *testing.T) {
852855
}
853856

854857
expected := new(CallArgs)
855-
expected.GasPrice = big.NewInt(0)
858+
expected.GasPrice = nil
856859

857-
if bytes.Compare(expected.GasPrice.Bytes(), args.GasPrice.Bytes()) != 0 {
860+
if args.GasPrice != expected.GasPrice {
861+
// if bytes.Compare(expected.GasPrice.Bytes(), args.GasPrice.Bytes()) != 0 {
858862
t.Errorf("GasPrice shoud be %v but is %v", expected.GasPrice, args.GasPrice)
859863
}
860864
}

xeth/xeth.go

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -885,12 +885,29 @@ func (self *XEth) Transact(fromStr, toStr, nonceStr, valueStr, gasStr, gasPriceS
885885
from = common.HexToAddress(fromStr)
886886
to = common.HexToAddress(toStr)
887887
value = common.Big(valueStr)
888-
gas = common.Big(gasStr)
889-
price = common.Big(gasPriceStr)
888+
gas *big.Int
889+
price *big.Int
890890
data []byte
891891
contractCreation bool
892892
)
893893

894+
if len(gasStr) == 0 {
895+
gas = DefaultGas()
896+
} else {
897+
gas = common.Big(gasStr)
898+
}
899+
900+
if len(gasPriceStr) == 0 {
901+
price = DefaultGasPrice()
902+
} else {
903+
price = common.Big(gasPriceStr)
904+
}
905+
906+
data = common.FromHex(codeStr)
907+
if len(toStr) == 0 {
908+
contractCreation = true
909+
}
910+
894911
// 2015-05-18 Is this still needed?
895912
// TODO if no_private_key then
896913
//if _, exists := p.register[args.From]; exists {
@@ -916,18 +933,6 @@ func (self *XEth) Transact(fromStr, toStr, nonceStr, valueStr, gasStr, gasPriceS
916933

917934
// TODO: align default values to have the same type, e.g. not depend on
918935
// common.Value conversions later on
919-
if gas.Cmp(big.NewInt(0)) == 0 {
920-
gas = DefaultGas()
921-
}
922-
923-
if price.Cmp(big.NewInt(0)) == 0 {
924-
price = DefaultGasPrice()
925-
}
926-
927-
data = common.FromHex(codeStr)
928-
if len(toStr) == 0 {
929-
contractCreation = true
930-
}
931936

932937
var tx *types.Transaction
933938
if contractCreation {

0 commit comments

Comments
 (0)