Skip to content

Commit b7cf41e

Browse files
Magickingfjl
authored andcommitted
accounts/abi: fix method constant flag for solidity 6.0 (#20482)
1 parent 3bb6815 commit b7cf41e

2 files changed

Lines changed: 64 additions & 7 deletions

File tree

‎accounts/abi/abi.go‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,13 @@ func (abi ABI) UnpackIntoMap(v map[string]interface{}, name string, data []byte)
108108
// UnmarshalJSON implements json.Unmarshaler interface
109109
func (abi *ABI) UnmarshalJSON(data []byte) error {
110110
var fields []struct {
111-
Type string
112-
Name string
113-
Constant bool
114-
Anonymous bool
115-
Inputs []Argument
116-
Outputs []Argument
111+
Type string
112+
Name string
113+
Constant bool
114+
StateMutability string
115+
Anonymous bool
116+
Inputs []Argument
117+
Outputs []Argument
117118
}
118119
if err := json.Unmarshal(data, &fields); err != nil {
119120
return err
@@ -134,10 +135,11 @@ func (abi *ABI) UnmarshalJSON(data []byte) error {
134135
name = fmt.Sprintf("%s%d", field.Name, idx)
135136
_, ok = abi.Methods[name]
136137
}
138+
isConst := field.Constant || field.StateMutability == "pure" || field.StateMutability == "view"
137139
abi.Methods[name] = Method{
138140
Name: name,
139141
RawName: field.Name,
140-
Const: field.Constant,
142+
Const: isConst,
141143
Inputs: field.Inputs,
142144
Outputs: field.Outputs,
143145
}

‎accounts/abi/bind/bind_test.go‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1530,6 +1530,61 @@ var bindTests = []struct {
15301530
nil,
15311531
[]string{"ContractOne", "ContractTwo", "ExternalLib"},
15321532
},
1533+
// Test the existence of the free retrieval calls
1534+
{
1535+
`PureAndView`,
1536+
`pragma solidity >=0.6.0;
1537+
contract PureAndView {
1538+
function PureFunc() public pure returns (uint) {
1539+
return 42;
1540+
}
1541+
function ViewFunc() public view returns (uint) {
1542+
return block.number;
1543+
}
1544+
}
1545+
`,
1546+
[]string{`608060405234801561001057600080fd5b5060b68061001f6000396000f3fe6080604052348015600f57600080fd5b506004361060325760003560e01c806376b5686a146037578063bb38c66c146053575b600080fd5b603d606f565b6040518082815260200191505060405180910390f35b60596077565b6040518082815260200191505060405180910390f35b600043905090565b6000602a90509056fea2646970667358221220d158c2ab7fdfce366a7998ec79ab84edd43b9815630bbaede2c760ea77f29f7f64736f6c63430006000033`},
1547+
[]string{`[{"inputs": [],"name": "PureFunc","outputs": [{"internalType": "uint256","name": "","type": "uint256"}],"stateMutability": "pure","type": "function"},{"inputs": [],"name": "ViewFunc","outputs": [{"internalType": "uint256","name": "","type": "uint256"}],"stateMutability": "view","type": "function"}]`},
1548+
`
1549+
"math/big"
1550+
1551+
"github.com/ethereum/go-ethereum/accounts/abi/bind"
1552+
"github.com/ethereum/go-ethereum/accounts/abi/bind/backends"
1553+
"github.com/ethereum/go-ethereum/core"
1554+
"github.com/ethereum/go-ethereum/crypto"
1555+
`,
1556+
`
1557+
// Generate a new random account and a funded simulator
1558+
key, _ := crypto.GenerateKey()
1559+
auth := bind.NewKeyedTransactor(key)
1560+
1561+
sim := backends.NewSimulatedBackend(core.GenesisAlloc{auth.From: {Balance: big.NewInt(10000000000)}}, 10000000)
1562+
defer sim.Close()
1563+
1564+
// Deploy a tester contract and execute a structured call on it
1565+
_, _, pav, err := DeployPureAndView(auth, sim)
1566+
if err != nil {
1567+
t.Fatalf("Failed to deploy PureAndView contract: %v", err)
1568+
}
1569+
sim.Commit()
1570+
1571+
// This test the existence of the free retreiver call for view and pure functions
1572+
if num, err := pav.PureFunc(nil); err != nil {
1573+
t.Fatalf("Failed to call anonymous field retriever: %v", err)
1574+
} else if num.Cmp(big.NewInt(42)) != 0 {
1575+
t.Fatalf("Retrieved value mismatch: have %v, want %v", num, 42)
1576+
}
1577+
if num, err := pav.ViewFunc(nil); err != nil {
1578+
t.Fatalf("Failed to call anonymous field retriever: %v", err)
1579+
} else if num.Cmp(big.NewInt(1)) != 0 {
1580+
t.Fatalf("Retrieved value mismatch: have %v, want %v", num, 1)
1581+
}
1582+
`,
1583+
nil,
1584+
nil,
1585+
nil,
1586+
nil,
1587+
},
15331588
}
15341589

15351590
// Tests that packages generated by the binder can be successfully compiled and

0 commit comments

Comments
 (0)