args_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. package rpc
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "math/big"
  6. "testing"
  7. )
  8. func TestSha3(t *testing.T) {
  9. input := `["0x68656c6c6f20776f726c64"]`
  10. expected := "0x68656c6c6f20776f726c64"
  11. args := new(Sha3Args)
  12. json.Unmarshal([]byte(input), &args)
  13. if args.Data != expected {
  14. t.Error("got %s expected %s", input, expected)
  15. }
  16. }
  17. func TestGetBalanceArgs(t *testing.T) {
  18. input := `["0x407d73d8a49eeb85d32cf465507dd71d507100c1", "0x1f"]`
  19. expected := new(GetBalanceArgs)
  20. expected.Address = "0x407d73d8a49eeb85d32cf465507dd71d507100c1"
  21. expected.BlockNumber = 31
  22. args := new(GetBalanceArgs)
  23. if err := json.Unmarshal([]byte(input), &args); err != nil {
  24. t.Error(err)
  25. }
  26. if err := args.requirements(); err != nil {
  27. t.Error(err)
  28. }
  29. if args.Address != expected.Address {
  30. t.Errorf("Address should be %v but is %v", expected.Address, args.Address)
  31. }
  32. if args.BlockNumber != expected.BlockNumber {
  33. t.Errorf("BlockNumber should be %v but is %v", expected.BlockNumber, args.BlockNumber)
  34. }
  35. }
  36. func TestGetBalanceEmptyArgs(t *testing.T) {
  37. input := `[]`
  38. args := new(GetBalanceArgs)
  39. err := json.Unmarshal([]byte(input), &args)
  40. if err == nil {
  41. t.Error("Expected error but didn't get one")
  42. }
  43. }
  44. func TestGetBlockByHashArgs(t *testing.T) {
  45. input := `["0xe670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d1527331", true]`
  46. expected := new(GetBlockByHashArgs)
  47. expected.BlockHash = "0xe670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d1527331"
  48. expected.Transactions = true
  49. args := new(GetBlockByHashArgs)
  50. if err := json.Unmarshal([]byte(input), &args); err != nil {
  51. t.Error(err)
  52. }
  53. if args.BlockHash != expected.BlockHash {
  54. t.Errorf("BlockHash should be %v but is %v", expected.BlockHash, args.BlockHash)
  55. }
  56. if args.Transactions != expected.Transactions {
  57. t.Errorf("Transactions should be %v but is %v", expected.Transactions, args.Transactions)
  58. }
  59. }
  60. func TestGetBlockByNumberArgs(t *testing.T) {
  61. input := `["0x1b4", false]`
  62. expected := new(GetBlockByNumberArgs)
  63. expected.BlockNumber = 436
  64. expected.Transactions = false
  65. args := new(GetBlockByNumberArgs)
  66. if err := json.Unmarshal([]byte(input), &args); err != nil {
  67. t.Error(err)
  68. }
  69. if args.BlockNumber != expected.BlockNumber {
  70. t.Errorf("BlockHash should be %v but is %v", expected.BlockNumber, args.BlockNumber)
  71. }
  72. if args.Transactions != expected.Transactions {
  73. t.Errorf("Transactions should be %v but is %v", expected.Transactions, args.Transactions)
  74. }
  75. }
  76. func TestNewTxArgs(t *testing.T) {
  77. input := `[{"from": "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
  78. "to": "0xd46e8dd67c5d32be8058bb8eb970870f072445675",
  79. "gas": "0x76c0",
  80. "gasPrice": "0x9184e72a000",
  81. "value": "0x9184e72a000",
  82. "data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"}]`
  83. expected := new(NewTxArgs)
  84. expected.From = "0xb60e8dd61c5d32be8058bb8eb970870f07233155"
  85. expected.To = "0xd46e8dd67c5d32be8058bb8eb970870f072445675"
  86. expected.Gas = big.NewInt(30400)
  87. expected.GasPrice = big.NewInt(10000000000000)
  88. expected.Value = big.NewInt(10000000000000)
  89. expected.Data = "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"
  90. args := new(NewTxArgs)
  91. if err := json.Unmarshal([]byte(input), &args); err != nil {
  92. t.Error(err)
  93. }
  94. if expected.From != args.From {
  95. t.Errorf("From shoud be %#v but is %#v", expected.From, args.From)
  96. }
  97. if expected.To != args.To {
  98. t.Errorf("To shoud be %#v but is %#v", expected.To, args.To)
  99. }
  100. if bytes.Compare(expected.Gas.Bytes(), args.Gas.Bytes()) != 0 {
  101. t.Errorf("Gas shoud be %#v but is %#v", expected.Gas.Bytes(), args.Gas.Bytes())
  102. }
  103. if bytes.Compare(expected.GasPrice.Bytes(), args.GasPrice.Bytes()) != 0 {
  104. t.Errorf("GasPrice shoud be %#v but is %#v", expected.GasPrice, args.GasPrice)
  105. }
  106. if bytes.Compare(expected.Value.Bytes(), args.Value.Bytes()) != 0 {
  107. t.Errorf("Value shoud be %#v but is %#v", expected.Value, args.Value)
  108. }
  109. if expected.Data != args.Data {
  110. t.Errorf("Data shoud be %#v but is %#v", expected.Data, args.Data)
  111. }
  112. }
  113. func TestGetStorageArgs(t *testing.T) {
  114. input := `["0x407d73d8a49eeb85d32cf465507dd71d507100c1", "latest"]`
  115. expected := new(GetStorageArgs)
  116. expected.Address = "0x407d73d8a49eeb85d32cf465507dd71d507100c1"
  117. expected.BlockNumber = -1
  118. args := new(GetStorageArgs)
  119. if err := json.Unmarshal([]byte(input), &args); err != nil {
  120. t.Error(err)
  121. }
  122. if err := args.requirements(); err != nil {
  123. t.Error(err)
  124. }
  125. if expected.Address != args.Address {
  126. t.Errorf("Address shoud be %#v but is %#v", expected.Address, args.Address)
  127. }
  128. if expected.BlockNumber != args.BlockNumber {
  129. t.Errorf("BlockNumber shoud be %#v but is %#v", expected.BlockNumber, args.BlockNumber)
  130. }
  131. }
  132. func TestGetStorageAtArgs(t *testing.T) {
  133. input := `["0x407d73d8a49eeb85d32cf465507dd71d507100c1", "0x0", "0x2"]`
  134. expected := new(GetStorageAtArgs)
  135. expected.Address = "0x407d73d8a49eeb85d32cf465507dd71d507100c1"
  136. expected.Key = "0x0"
  137. expected.BlockNumber = 2
  138. args := new(GetStorageAtArgs)
  139. if err := json.Unmarshal([]byte(input), &args); err != nil {
  140. t.Error(err)
  141. }
  142. if err := args.requirements(); err != nil {
  143. t.Error(err)
  144. }
  145. if expected.Address != args.Address {
  146. t.Errorf("Address shoud be %#v but is %#v", expected.Address, args.Address)
  147. }
  148. if expected.Key != args.Key {
  149. t.Errorf("Address shoud be %#v but is %#v", expected.Address, args.Address)
  150. }
  151. if expected.BlockNumber != args.BlockNumber {
  152. t.Errorf("BlockNumber shoud be %#v but is %#v", expected.BlockNumber, args.BlockNumber)
  153. }
  154. }
  155. func TestGetTxCountArgs(t *testing.T) {
  156. input := `["0x407d73d8a49eeb85d32cf465507dd71d507100c1", "latest"]`
  157. expected := new(GetTxCountArgs)
  158. expected.Address = "0x407d73d8a49eeb85d32cf465507dd71d507100c1"
  159. expected.BlockNumber = -1
  160. args := new(GetTxCountArgs)
  161. if err := json.Unmarshal([]byte(input), &args); err != nil {
  162. t.Error(err)
  163. }
  164. if err := args.requirements(); err != nil {
  165. t.Error(err)
  166. }
  167. if expected.Address != args.Address {
  168. t.Errorf("Address shoud be %#v but is %#v", expected.Address, args.Address)
  169. }
  170. if expected.BlockNumber != args.BlockNumber {
  171. t.Errorf("BlockNumber shoud be %#v but is %#v", expected.BlockNumber, args.BlockNumber)
  172. }
  173. }
  174. func TestGetDataArgs(t *testing.T) {
  175. input := `["0xd5677cf67b5aa051bb40496e68ad359eb97cfbf8", "latest"]`
  176. expected := new(GetDataArgs)
  177. expected.Address = "0xd5677cf67b5aa051bb40496e68ad359eb97cfbf8"
  178. expected.BlockNumber = -1
  179. args := new(GetDataArgs)
  180. if err := json.Unmarshal([]byte(input), &args); err != nil {
  181. t.Error(err)
  182. }
  183. if err := args.requirements(); err != nil {
  184. t.Error(err)
  185. }
  186. if expected.Address != args.Address {
  187. t.Errorf("Address shoud be %#v but is %#v", expected.Address, args.Address)
  188. }
  189. if expected.BlockNumber != args.BlockNumber {
  190. t.Errorf("BlockNumber shoud be %#v but is %#v", expected.BlockNumber, args.BlockNumber)
  191. }
  192. }
  193. func TestFilterOptions(t *testing.T) {
  194. input := `[{
  195. "fromBlock": "0x1",
  196. "toBlock": "0x2",
  197. "limit": "0x3",
  198. "offset": "0x0",
  199. "address": "0xd5677cf67b5aa051bb40496e68ad359eb97cfbf8",
  200. "topics": ["0x12341234"]}]`
  201. expected := new(FilterOptions)
  202. expected.Earliest = 1
  203. expected.Latest = 2
  204. expected.Max = 3
  205. expected.Skip = 0
  206. expected.Address = "0xd5677cf67b5aa051bb40496e68ad359eb97cfbf8"
  207. // expected.Topics = []string{"0x12341234"}
  208. args := new(FilterOptions)
  209. if err := json.Unmarshal([]byte(input), &args); err != nil {
  210. t.Error(err)
  211. }
  212. if expected.Earliest != args.Earliest {
  213. t.Errorf("Earliest shoud be %#v but is %#v", expected.Earliest, args.Earliest)
  214. }
  215. if expected.Latest != args.Latest {
  216. t.Errorf("Latest shoud be %#v but is %#v", expected.Latest, args.Latest)
  217. }
  218. if expected.Max != args.Max {
  219. t.Errorf("Max shoud be %#v but is %#v", expected.Max, args.Max)
  220. }
  221. if expected.Skip != args.Skip {
  222. t.Errorf("Skip shoud be %#v but is %#v", expected.Skip, args.Skip)
  223. }
  224. if expected.Address != args.Address {
  225. t.Errorf("Address shoud be %#v but is %#v", expected.Address, args.Address)
  226. }
  227. // if expected.Topics != args.Topics {
  228. // t.Errorf("Topic shoud be %#v but is %#v", expected.Topic, args.Topic)
  229. // }
  230. }
  231. func TestDbArgs(t *testing.T) {
  232. input := `["0x74657374","0x6b6579","0x6d79537472696e67"]`
  233. expected := new(DbArgs)
  234. expected.Database = "0x74657374"
  235. expected.Key = "0x6b6579"
  236. expected.Value = "0x6d79537472696e67"
  237. args := new(DbArgs)
  238. if err := json.Unmarshal([]byte(input), &args); err != nil {
  239. t.Error(err)
  240. }
  241. if err := args.requirements(); err != nil {
  242. t.Error(err)
  243. }
  244. if expected.Database != args.Database {
  245. t.Errorf("Database shoud be %#v but is %#v", expected.Database, args.Database)
  246. }
  247. if expected.Key != args.Key {
  248. t.Errorf("Key shoud be %#v but is %#v", expected.Key, args.Key)
  249. }
  250. if expected.Value != args.Value {
  251. t.Errorf("Value shoud be %#v but is %#v", expected.Value, args.Value)
  252. }
  253. }
  254. func TestWhisperMessageArgs(t *testing.T) {
  255. input := `[{"from":"0xc931d93e97ab07fe42d923478ba2465f2",
  256. "topics": ["0x68656c6c6f20776f726c64"],
  257. "payload":"0x68656c6c6f20776f726c64",
  258. "ttl": "0x64",
  259. "priority": "0x64"}]`
  260. expected := new(WhisperMessageArgs)
  261. expected.From = "0xc931d93e97ab07fe42d923478ba2465f2"
  262. expected.To = ""
  263. expected.Payload = "0x68656c6c6f20776f726c64"
  264. expected.Priority = 100
  265. expected.Ttl = 100
  266. expected.Topics = []string{"0x68656c6c6f20776f726c64"}
  267. args := new(WhisperMessageArgs)
  268. if err := json.Unmarshal([]byte(input), &args); err != nil {
  269. t.Error(err)
  270. }
  271. if expected.From != args.From {
  272. t.Errorf("From shoud be %#v but is %#v", expected.From, args.From)
  273. }
  274. if expected.To != args.To {
  275. t.Errorf("To shoud be %#v but is %#v", expected.To, args.To)
  276. }
  277. if expected.Payload != args.Payload {
  278. t.Errorf("Value shoud be %#v but is %#v", expected.Payload, args.Payload)
  279. }
  280. if expected.Ttl != args.Ttl {
  281. t.Errorf("Ttl shoud be %#v but is %#v", expected.Ttl, args.Ttl)
  282. }
  283. if expected.Priority != args.Priority {
  284. t.Errorf("Priority shoud be %#v but is %#v", expected.Priority, args.Priority)
  285. }
  286. // if expected.Topics != args.Topics {
  287. // t.Errorf("Topic shoud be %#v but is %#v", expected.Topic, args.Topic)
  288. // }
  289. }
  290. func TestFilterIdArgs(t *testing.T) {
  291. input := `["0x7"]`
  292. expected := new(FilterIdArgs)
  293. expected.Id = 7
  294. args := new(FilterIdArgs)
  295. if err := json.Unmarshal([]byte(input), &args); err != nil {
  296. t.Error(err)
  297. }
  298. if expected.Id != args.Id {
  299. t.Errorf("Id shoud be %#v but is %#v", expected.Id, args.Id)
  300. }
  301. }
  302. func TestWhsiperFilterArgs(t *testing.T) {
  303. input := `[{"topics": ["0x68656c6c6f20776f726c64"], "to": "0x34ag445g3455b34"}]`
  304. expected := new(WhisperFilterArgs)
  305. expected.From = ""
  306. expected.To = "0x34ag445g3455b34"
  307. expected.Topics = []string{"0x68656c6c6f20776f726c64"}
  308. args := new(WhisperFilterArgs)
  309. if err := json.Unmarshal([]byte(input), &args); err != nil {
  310. t.Error(err)
  311. }
  312. if expected.From != args.From {
  313. t.Errorf("From shoud be %#v but is %#v", expected.From, args.From)
  314. }
  315. if expected.To != args.To {
  316. t.Errorf("To shoud be %#v but is %#v", expected.To, args.To)
  317. }
  318. // if expected.Topics != args.Topics {
  319. // t.Errorf("Topics shoud be %#v but is %#v", expected.Topics, args.Topics)
  320. // }
  321. }
  322. func TestCompileArgs(t *testing.T) {
  323. input := `["contract test { function multiply(uint a) returns(uint d) { return a * 7; } }"]`
  324. expected := new(CompileArgs)
  325. expected.Source = `contract test { function multiply(uint a) returns(uint d) { return a * 7; } }`
  326. args := new(CompileArgs)
  327. if err := json.Unmarshal([]byte(input), &args); err != nil {
  328. t.Error(err)
  329. }
  330. if expected.Source != args.Source {
  331. t.Errorf("Source shoud be %#v but is %#v", expected.Source, args.Source)
  332. }
  333. }
  334. func TestFilterStringArgs(t *testing.T) {
  335. input := `["pending"]`
  336. expected := new(FilterStringArgs)
  337. expected.Word = "pending"
  338. args := new(FilterStringArgs)
  339. if err := json.Unmarshal([]byte(input), &args); err != nil {
  340. t.Error(err)
  341. }
  342. if expected.Word != args.Word {
  343. t.Errorf("Word shoud be %#v but is %#v", expected.Word, args.Word)
  344. }
  345. }
  346. func TestFilterStringEmptyArgs(t *testing.T) {
  347. input := `[]`
  348. args := new(FilterStringArgs)
  349. err := json.Unmarshal([]byte(input), &args)
  350. if err == nil {
  351. t.Error("Expected error but didn't get one")
  352. }
  353. }
  354. func TestWhisperIdentityArgs(t *testing.T) {
  355. input := `["0xc931d93e97ab07fe42d923478ba2465f283"]`
  356. expected := new(WhisperIdentityArgs)
  357. expected.Identity = "0xc931d93e97ab07fe42d923478ba2465f283"
  358. args := new(WhisperIdentityArgs)
  359. if err := json.Unmarshal([]byte(input), &args); err != nil {
  360. t.Error(err)
  361. }
  362. if expected.Identity != args.Identity {
  363. t.Errorf("Identity shoud be %#v but is %#v", expected.Identity, args.Identity)
  364. }
  365. }
  366. func TestBlockNumIndexArgs(t *testing.T) {
  367. input := `["0x29a", "0x0"]`
  368. expected := new(BlockNumIndexArgs)
  369. expected.BlockNumber = 666
  370. expected.Index = 0
  371. args := new(BlockNumIndexArgs)
  372. if err := json.Unmarshal([]byte(input), &args); err != nil {
  373. t.Error(err)
  374. }
  375. if expected.BlockNumber != args.BlockNumber {
  376. t.Errorf("BlockNumber shoud be %#v but is %#v", expected.BlockNumber, args.BlockNumber)
  377. }
  378. if expected.Index != args.Index {
  379. t.Errorf("Index shoud be %#v but is %#v", expected.Index, args.Index)
  380. }
  381. }
  382. func TestHashIndexArgs(t *testing.T) {
  383. input := `["0xc6ef2fc5426d6ad6fd9e2a26abeab0aa2411b7ab17f30a99d3cb96aed1d1055b", "0x1"]`
  384. expected := new(HashIndexArgs)
  385. expected.Hash = "0xc6ef2fc5426d6ad6fd9e2a26abeab0aa2411b7ab17f30a99d3cb96aed1d1055b"
  386. expected.Index = 1
  387. args := new(HashIndexArgs)
  388. if err := json.Unmarshal([]byte(input), &args); err != nil {
  389. t.Error(err)
  390. }
  391. if expected.Hash != args.Hash {
  392. t.Errorf("Hash shoud be %#v but is %#v", expected.Hash, args.Hash)
  393. }
  394. if expected.Index != args.Index {
  395. t.Errorf("Index shoud be %#v but is %#v", expected.Index, args.Index)
  396. }
  397. }