abihelper_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // Copyright 2018 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // go-ethereum is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package core
  17. import (
  18. "fmt"
  19. "strings"
  20. "testing"
  21. "io/ioutil"
  22. "math/big"
  23. "reflect"
  24. "github.com/ethereum/go-ethereum/accounts/abi"
  25. "github.com/ethereum/go-ethereum/common"
  26. )
  27. func verify(t *testing.T, jsondata, calldata string, exp []interface{}) {
  28. abispec, err := abi.JSON(strings.NewReader(jsondata))
  29. if err != nil {
  30. t.Fatal(err)
  31. }
  32. cd := common.Hex2Bytes(calldata)
  33. sigdata, argdata := cd[:4], cd[4:]
  34. method, err := abispec.MethodById(sigdata)
  35. if err != nil {
  36. t.Fatal(err)
  37. }
  38. data, err := method.Inputs.UnpackValues(argdata)
  39. if len(data) != len(exp) {
  40. t.Fatalf("Mismatched length, expected %d, got %d", len(exp), len(data))
  41. }
  42. for i, elem := range data {
  43. if !reflect.DeepEqual(elem, exp[i]) {
  44. t.Fatalf("Unpack error, arg %d, got %v, want %v", i, elem, exp[i])
  45. }
  46. }
  47. }
  48. func TestNewUnpacker(t *testing.T) {
  49. type unpackTest struct {
  50. jsondata string
  51. calldata string
  52. exp []interface{}
  53. }
  54. testcases := []unpackTest{
  55. { // https://solidity.readthedocs.io/en/develop/abi-spec.html#use-of-dynamic-types
  56. `[{"type":"function","name":"f", "inputs":[{"type":"uint256"},{"type":"uint32[]"},{"type":"bytes10"},{"type":"bytes"}]}]`,
  57. // 0x123, [0x456, 0x789], "1234567890", "Hello, world!"
  58. "8be65246" + "00000000000000000000000000000000000000000000000000000000000001230000000000000000000000000000000000000000000000000000000000000080313233343536373839300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000004560000000000000000000000000000000000000000000000000000000000000789000000000000000000000000000000000000000000000000000000000000000d48656c6c6f2c20776f726c642100000000000000000000000000000000000000",
  59. []interface{}{
  60. big.NewInt(0x123),
  61. []uint32{0x456, 0x789},
  62. [10]byte{49, 50, 51, 52, 53, 54, 55, 56, 57, 48},
  63. common.Hex2Bytes("48656c6c6f2c20776f726c6421"),
  64. },
  65. }, { // https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI#examples
  66. `[{"type":"function","name":"sam","inputs":[{"type":"bytes"},{"type":"bool"},{"type":"uint256[]"}]}]`,
  67. // "dave", true and [1,2,3]
  68. "a5643bf20000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000000464617665000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003",
  69. []interface{}{
  70. []byte{0x64, 0x61, 0x76, 0x65},
  71. true,
  72. []*big.Int{big.NewInt(1), big.NewInt(2), big.NewInt(3)},
  73. },
  74. }, {
  75. `[{"type":"function","name":"send","inputs":[{"type":"uint256"}]}]`,
  76. "a52c101e0000000000000000000000000000000000000000000000000000000000000012",
  77. []interface{}{big.NewInt(0x12)},
  78. }, {
  79. `[{"type":"function","name":"compareAndApprove","inputs":[{"type":"address"},{"type":"uint256"},{"type":"uint256"}]}]`,
  80. "751e107900000000000000000000000000000133700000deadbeef00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001",
  81. []interface{}{
  82. common.HexToAddress("0x00000133700000deadbeef000000000000000000"),
  83. new(big.Int).SetBytes([]byte{0x00}),
  84. big.NewInt(0x1),
  85. },
  86. },
  87. }
  88. for _, c := range testcases {
  89. verify(t, c.jsondata, c.calldata, c.exp)
  90. }
  91. }
  92. /*
  93. func TestReflect(t *testing.T) {
  94. a := big.NewInt(0)
  95. b := new(big.Int).SetBytes([]byte{0x00})
  96. if !reflect.DeepEqual(a, b) {
  97. t.Fatalf("Nope, %v != %v", a, b)
  98. }
  99. }
  100. */
  101. func TestCalldataDecoding(t *testing.T) {
  102. // send(uint256) : a52c101e
  103. // compareAndApprove(address,uint256,uint256) : 751e1079
  104. // issue(address[],uint256) : 42958b54
  105. jsondata := `
  106. [
  107. {"type":"function","name":"send","inputs":[{"name":"a","type":"uint256"}]},
  108. {"type":"function","name":"compareAndApprove","inputs":[{"name":"a","type":"address"},{"name":"a","type":"uint256"},{"name":"a","type":"uint256"}]},
  109. {"type":"function","name":"issue","inputs":[{"name":"a","type":"address[]"},{"name":"a","type":"uint256"}]},
  110. {"type":"function","name":"sam","inputs":[{"name":"a","type":"bytes"},{"name":"a","type":"bool"},{"name":"a","type":"uint256[]"}]}
  111. ]`
  112. //Expected failures
  113. for _, hexdata := range []string{
  114. "a52c101e00000000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000000000000000042",
  115. "a52c101e000000000000000000000000000000000000000000000000000000000000001200",
  116. "a52c101e00000000000000000000000000000000000000000000000000000000000000",
  117. "a52c101e",
  118. "a52c10",
  119. "",
  120. // Too short
  121. "751e10790000000000000000000000000000000000000000000000000000000000000012",
  122. "751e1079FFffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
  123. //Not valid multiple of 32
  124. "deadbeef00000000000000000000000000000000000000000000000000000000000000",
  125. //Too short 'issue'
  126. "42958b5400000000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000000000000000042",
  127. // Too short compareAndApprove
  128. "a52c101e00ff0000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000000000000000042",
  129. // From https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI
  130. // contains a bool with illegal values
  131. "a5643bf20000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000001100000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000000464617665000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003",
  132. } {
  133. _, err := parseCallData(common.Hex2Bytes(hexdata), jsondata)
  134. if err == nil {
  135. t.Errorf("Expected decoding to fail: %s", hexdata)
  136. }
  137. }
  138. //Expected success
  139. for _, hexdata := range []string{
  140. // From https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI
  141. "a5643bf20000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000000464617665000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003",
  142. "a52c101e0000000000000000000000000000000000000000000000000000000000000012",
  143. "a52c101eFFffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
  144. "751e1079000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  145. "42958b54" +
  146. // start of dynamic type
  147. "0000000000000000000000000000000000000000000000000000000000000040" +
  148. //uint256
  149. "0000000000000000000000000000000000000000000000000000000000000001" +
  150. // length of array
  151. "0000000000000000000000000000000000000000000000000000000000000002" +
  152. // array values
  153. "000000000000000000000000000000000000000000000000000000000000dead" +
  154. "000000000000000000000000000000000000000000000000000000000000beef",
  155. } {
  156. _, err := parseCallData(common.Hex2Bytes(hexdata), jsondata)
  157. if err != nil {
  158. t.Errorf("Unexpected failure on input %s:\n %v (%d bytes) ", hexdata, err, len(common.Hex2Bytes(hexdata)))
  159. }
  160. }
  161. }
  162. func TestSelectorUnmarshalling(t *testing.T) {
  163. var (
  164. db *AbiDb
  165. err error
  166. abistring []byte
  167. abistruct abi.ABI
  168. )
  169. db, err = NewAbiDBFromFile("../../cmd/clef/4byte.json")
  170. if err != nil {
  171. t.Fatal(err)
  172. }
  173. fmt.Printf("DB size %v\n", db.Size())
  174. for id, selector := range db.db {
  175. abistring, err = MethodSelectorToAbi(selector)
  176. if err != nil {
  177. t.Error(err)
  178. return
  179. }
  180. abistruct, err = abi.JSON(strings.NewReader(string(abistring)))
  181. if err != nil {
  182. t.Error(err)
  183. return
  184. }
  185. m, err := abistruct.MethodById(common.Hex2Bytes(id[2:]))
  186. if err != nil {
  187. t.Error(err)
  188. return
  189. }
  190. if m.Sig() != selector {
  191. t.Errorf("Expected equality: %v != %v", m.Sig(), selector)
  192. }
  193. }
  194. }
  195. func TestCustomABI(t *testing.T) {
  196. d, err := ioutil.TempDir("", "signer-4byte-test")
  197. if err != nil {
  198. t.Fatal(err)
  199. }
  200. filename := fmt.Sprintf("%s/4byte_custom.json", d)
  201. abidb, err := NewAbiDBFromFiles("../../cmd/clef/4byte.json", filename)
  202. if err != nil {
  203. t.Fatal(err)
  204. }
  205. // Now we'll remove all existing signatures
  206. abidb.db = make(map[string]string)
  207. calldata := common.Hex2Bytes("a52c101edeadbeef")
  208. _, err = abidb.LookupMethodSelector(calldata)
  209. if err == nil {
  210. t.Fatalf("Should not find a match on empty db")
  211. }
  212. if err = abidb.AddSignature("send(uint256)", calldata); err != nil {
  213. t.Fatalf("Failed to save file: %v", err)
  214. }
  215. _, err = abidb.LookupMethodSelector(calldata)
  216. if err != nil {
  217. t.Fatalf("Should find a match for abi signature, got: %v", err)
  218. }
  219. //Check that it wrote to file
  220. abidb2, err := NewAbiDBFromFile(filename)
  221. if err != nil {
  222. t.Fatalf("Failed to create new abidb: %v", err)
  223. }
  224. _, err = abidb2.LookupMethodSelector(calldata)
  225. if err != nil {
  226. t.Fatalf("Save failed: should find a match for abi signature after loading from disk")
  227. }
  228. }