eth_args.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  1. package api
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "math/big"
  6. "github.com/ethereum/go-ethereum/common"
  7. "github.com/ethereum/go-ethereum/core/state"
  8. "github.com/ethereum/go-ethereum/rpc/shared"
  9. )
  10. const (
  11. defaultLogLimit = 100
  12. defaultLogOffset = 0
  13. )
  14. type GetBalanceArgs struct {
  15. Address string
  16. BlockNumber int64
  17. }
  18. func (args *GetBalanceArgs) UnmarshalJSON(b []byte) (err error) {
  19. var obj []interface{}
  20. if err := json.Unmarshal(b, &obj); err != nil {
  21. return shared.NewDecodeParamError(err.Error())
  22. }
  23. if len(obj) < 1 {
  24. return shared.NewInsufficientParamsError(len(obj), 1)
  25. }
  26. addstr, ok := obj[0].(string)
  27. if !ok {
  28. return shared.NewInvalidTypeError("address", "not a string")
  29. }
  30. args.Address = addstr
  31. if len(obj) > 1 {
  32. if err := blockHeight(obj[1], &args.BlockNumber); err != nil {
  33. return err
  34. }
  35. } else {
  36. args.BlockNumber = -1
  37. }
  38. return nil
  39. }
  40. type GetStorageArgs struct {
  41. Address string
  42. BlockNumber int64
  43. }
  44. func (args *GetStorageArgs) UnmarshalJSON(b []byte) (err error) {
  45. var obj []interface{}
  46. if err := json.Unmarshal(b, &obj); err != nil {
  47. return shared.NewDecodeParamError(err.Error())
  48. }
  49. if len(obj) < 1 {
  50. return shared.NewInsufficientParamsError(len(obj), 1)
  51. }
  52. addstr, ok := obj[0].(string)
  53. if !ok {
  54. return shared.NewInvalidTypeError("address", "not a string")
  55. }
  56. args.Address = addstr
  57. if len(obj) > 1 {
  58. if err := blockHeight(obj[1], &args.BlockNumber); err != nil {
  59. return err
  60. }
  61. } else {
  62. args.BlockNumber = -1
  63. }
  64. return nil
  65. }
  66. type GetStorageAtArgs struct {
  67. Address string
  68. BlockNumber int64
  69. Key string
  70. }
  71. func (args *GetStorageAtArgs) UnmarshalJSON(b []byte) (err error) {
  72. var obj []interface{}
  73. if err := json.Unmarshal(b, &obj); err != nil {
  74. return shared.NewDecodeParamError(err.Error())
  75. }
  76. if len(obj) < 2 {
  77. return shared.NewInsufficientParamsError(len(obj), 2)
  78. }
  79. addstr, ok := obj[0].(string)
  80. if !ok {
  81. return shared.NewInvalidTypeError("address", "not a string")
  82. }
  83. args.Address = addstr
  84. keystr, ok := obj[1].(string)
  85. if !ok {
  86. return shared.NewInvalidTypeError("key", "not a string")
  87. }
  88. args.Key = keystr
  89. if len(obj) > 2 {
  90. if err := blockHeight(obj[2], &args.BlockNumber); err != nil {
  91. return err
  92. }
  93. } else {
  94. args.BlockNumber = -1
  95. }
  96. return nil
  97. }
  98. type GetTxCountArgs struct {
  99. Address string
  100. BlockNumber int64
  101. }
  102. func (args *GetTxCountArgs) UnmarshalJSON(b []byte) (err error) {
  103. var obj []interface{}
  104. if err := json.Unmarshal(b, &obj); err != nil {
  105. return shared.NewDecodeParamError(err.Error())
  106. }
  107. if len(obj) < 1 {
  108. return shared.NewInsufficientParamsError(len(obj), 1)
  109. }
  110. addstr, ok := obj[0].(string)
  111. if !ok {
  112. return shared.NewInvalidTypeError("address", "not a string")
  113. }
  114. args.Address = addstr
  115. if len(obj) > 1 {
  116. if err := blockHeight(obj[1], &args.BlockNumber); err != nil {
  117. return err
  118. }
  119. } else {
  120. args.BlockNumber = -1
  121. }
  122. return nil
  123. }
  124. type HashArgs struct {
  125. Hash string
  126. }
  127. func (args *HashArgs) UnmarshalJSON(b []byte) (err error) {
  128. var obj []interface{}
  129. if err := json.Unmarshal(b, &obj); err != nil {
  130. return shared.NewDecodeParamError(err.Error())
  131. }
  132. if len(obj) < 1 {
  133. return shared.NewInsufficientParamsError(len(obj), 1)
  134. }
  135. arg0, ok := obj[0].(string)
  136. if !ok {
  137. return shared.NewInvalidTypeError("hash", "not a string")
  138. }
  139. args.Hash = arg0
  140. return nil
  141. }
  142. type BlockNumArg struct {
  143. BlockNumber int64
  144. }
  145. func (args *BlockNumArg) UnmarshalJSON(b []byte) (err error) {
  146. var obj []interface{}
  147. if err := json.Unmarshal(b, &obj); err != nil {
  148. return shared.NewDecodeParamError(err.Error())
  149. }
  150. if len(obj) < 1 {
  151. return shared.NewInsufficientParamsError(len(obj), 1)
  152. }
  153. if err := blockHeight(obj[0], &args.BlockNumber); err != nil {
  154. return err
  155. }
  156. return nil
  157. }
  158. type GetDataArgs struct {
  159. Address string
  160. BlockNumber int64
  161. }
  162. func (args *GetDataArgs) UnmarshalJSON(b []byte) (err error) {
  163. var obj []interface{}
  164. if err := json.Unmarshal(b, &obj); err != nil {
  165. return shared.NewDecodeParamError(err.Error())
  166. }
  167. if len(obj) < 1 {
  168. return shared.NewInsufficientParamsError(len(obj), 1)
  169. }
  170. addstr, ok := obj[0].(string)
  171. if !ok {
  172. return shared.NewInvalidTypeError("address", "not a string")
  173. }
  174. args.Address = addstr
  175. if len(obj) > 1 {
  176. if err := blockHeight(obj[1], &args.BlockNumber); err != nil {
  177. return err
  178. }
  179. } else {
  180. args.BlockNumber = -1
  181. }
  182. return nil
  183. }
  184. type NewDataArgs struct {
  185. Data string
  186. }
  187. func (args *NewDataArgs) UnmarshalJSON(b []byte) (err error) {
  188. var obj []interface{}
  189. if err := json.Unmarshal(b, &obj); err != nil {
  190. return shared.NewDecodeParamError(err.Error())
  191. }
  192. // Check for sufficient params
  193. if len(obj) < 1 {
  194. return shared.NewInsufficientParamsError(len(obj), 1)
  195. }
  196. data, ok := obj[0].(string)
  197. if !ok {
  198. return shared.NewInvalidTypeError("data", "not a string")
  199. }
  200. args.Data = data
  201. if len(args.Data) == 0 {
  202. return shared.NewValidationError("data", "is required")
  203. }
  204. return nil
  205. }
  206. type NewSigArgs struct {
  207. From string
  208. Data string
  209. }
  210. func (args *NewSigArgs) UnmarshalJSON(b []byte) (err error) {
  211. var obj []interface{}
  212. if err := json.Unmarshal(b, &obj); err != nil {
  213. return shared.NewDecodeParamError(err.Error())
  214. }
  215. // Check for sufficient params
  216. if len(obj) < 1 {
  217. return shared.NewInsufficientParamsError(len(obj), 1)
  218. }
  219. from, ok := obj[0].(string)
  220. if !ok {
  221. return shared.NewInvalidTypeError("from", "not a string")
  222. }
  223. args.From = from
  224. if len(args.From) == 0 {
  225. return shared.NewValidationError("from", "is required")
  226. }
  227. data, ok := obj[1].(string)
  228. if !ok {
  229. return shared.NewInvalidTypeError("data", "not a string")
  230. }
  231. args.Data = data
  232. if len(args.Data) == 0 {
  233. return shared.NewValidationError("data", "is required")
  234. }
  235. return nil
  236. }
  237. type NewTxArgs struct {
  238. From string
  239. To string
  240. Nonce *big.Int
  241. Value *big.Int
  242. Gas *big.Int
  243. GasPrice *big.Int
  244. Data string
  245. BlockNumber int64
  246. }
  247. func (args *NewTxArgs) UnmarshalJSON(b []byte) (err error) {
  248. var obj []json.RawMessage
  249. var ext struct {
  250. From string
  251. To string
  252. Nonce interface{}
  253. Value interface{}
  254. Gas interface{}
  255. GasPrice interface{}
  256. Data string
  257. }
  258. // Decode byte slice to array of RawMessages
  259. if err := json.Unmarshal(b, &obj); err != nil {
  260. return shared.NewDecodeParamError(err.Error())
  261. }
  262. // Check for sufficient params
  263. if len(obj) < 1 {
  264. return shared.NewInsufficientParamsError(len(obj), 1)
  265. }
  266. // Decode 0th RawMessage to temporary struct
  267. if err := json.Unmarshal(obj[0], &ext); err != nil {
  268. return shared.NewDecodeParamError(err.Error())
  269. }
  270. if len(ext.From) == 0 {
  271. return shared.NewValidationError("from", "is required")
  272. }
  273. args.From = ext.From
  274. args.To = ext.To
  275. args.Data = ext.Data
  276. var num *big.Int
  277. if ext.Nonce != nil {
  278. num, err = numString(ext.Nonce)
  279. if err != nil {
  280. return err
  281. }
  282. }
  283. args.Nonce = num
  284. if ext.Value == nil {
  285. num = big.NewInt(0)
  286. } else {
  287. num, err = numString(ext.Value)
  288. if err != nil {
  289. return err
  290. }
  291. }
  292. args.Value = num
  293. num = nil
  294. if ext.Gas != nil {
  295. if num, err = numString(ext.Gas); err != nil {
  296. return err
  297. }
  298. }
  299. args.Gas = num
  300. num = nil
  301. if ext.GasPrice != nil {
  302. if num, err = numString(ext.GasPrice); err != nil {
  303. return err
  304. }
  305. }
  306. args.GasPrice = num
  307. // Check for optional BlockNumber param
  308. if len(obj) > 1 {
  309. if err := blockHeightFromJson(obj[1], &args.BlockNumber); err != nil {
  310. return err
  311. }
  312. } else {
  313. args.BlockNumber = -1
  314. }
  315. return nil
  316. }
  317. type SourceArgs struct {
  318. Source string
  319. }
  320. func (args *SourceArgs) UnmarshalJSON(b []byte) (err error) {
  321. var obj []interface{}
  322. if err := json.Unmarshal(b, &obj); err != nil {
  323. return shared.NewDecodeParamError(err.Error())
  324. }
  325. if len(obj) < 1 {
  326. return shared.NewInsufficientParamsError(len(obj), 1)
  327. }
  328. arg0, ok := obj[0].(string)
  329. if !ok {
  330. return shared.NewInvalidTypeError("source code", "not a string")
  331. }
  332. args.Source = arg0
  333. return nil
  334. }
  335. type CallArgs struct {
  336. From string
  337. To string
  338. Value *big.Int
  339. Gas *big.Int
  340. GasPrice *big.Int
  341. Data string
  342. BlockNumber int64
  343. }
  344. func (args *CallArgs) UnmarshalJSON(b []byte) (err error) {
  345. var obj []json.RawMessage
  346. var ext struct {
  347. From string
  348. To string
  349. Value interface{}
  350. Gas interface{}
  351. GasPrice interface{}
  352. Data string
  353. }
  354. // Decode byte slice to array of RawMessages
  355. if err := json.Unmarshal(b, &obj); err != nil {
  356. return shared.NewDecodeParamError(err.Error())
  357. }
  358. // Check for sufficient params
  359. if len(obj) < 1 {
  360. return shared.NewInsufficientParamsError(len(obj), 1)
  361. }
  362. // Decode 0th RawMessage to temporary struct
  363. if err := json.Unmarshal(obj[0], &ext); err != nil {
  364. return shared.NewDecodeParamError(err.Error())
  365. }
  366. args.From = ext.From
  367. if len(ext.To) == 0 {
  368. return shared.NewValidationError("to", "is required")
  369. }
  370. args.To = ext.To
  371. var num *big.Int
  372. if ext.Value == nil {
  373. num = big.NewInt(0)
  374. } else {
  375. if num, err = numString(ext.Value); err != nil {
  376. return err
  377. }
  378. }
  379. args.Value = num
  380. if ext.Gas == nil {
  381. num = big.NewInt(0)
  382. } else {
  383. if num, err = numString(ext.Gas); err != nil {
  384. return err
  385. }
  386. }
  387. args.Gas = num
  388. if ext.GasPrice == nil {
  389. num = big.NewInt(0)
  390. } else {
  391. if num, err = numString(ext.GasPrice); err != nil {
  392. return err
  393. }
  394. }
  395. args.GasPrice = num
  396. args.Data = ext.Data
  397. // Check for optional BlockNumber param
  398. if len(obj) > 1 {
  399. if err := blockHeightFromJson(obj[1], &args.BlockNumber); err != nil {
  400. return err
  401. }
  402. } else {
  403. args.BlockNumber = -1
  404. }
  405. return nil
  406. }
  407. type HashIndexArgs struct {
  408. Hash string
  409. Index int64
  410. }
  411. func (args *HashIndexArgs) UnmarshalJSON(b []byte) (err error) {
  412. var obj []interface{}
  413. if err := json.Unmarshal(b, &obj); err != nil {
  414. return shared.NewDecodeParamError(err.Error())
  415. }
  416. if len(obj) < 2 {
  417. return shared.NewInsufficientParamsError(len(obj), 2)
  418. }
  419. arg0, ok := obj[0].(string)
  420. if !ok {
  421. return shared.NewInvalidTypeError("hash", "not a string")
  422. }
  423. args.Hash = arg0
  424. arg1, ok := obj[1].(string)
  425. if !ok {
  426. return shared.NewInvalidTypeError("index", "not a string")
  427. }
  428. args.Index = common.Big(arg1).Int64()
  429. return nil
  430. }
  431. type BlockNumIndexArgs struct {
  432. BlockNumber int64
  433. Index int64
  434. }
  435. func (args *BlockNumIndexArgs) UnmarshalJSON(b []byte) (err error) {
  436. var obj []interface{}
  437. if err := json.Unmarshal(b, &obj); err != nil {
  438. return shared.NewDecodeParamError(err.Error())
  439. }
  440. if len(obj) < 2 {
  441. return shared.NewInsufficientParamsError(len(obj), 2)
  442. }
  443. if err := blockHeight(obj[0], &args.BlockNumber); err != nil {
  444. return err
  445. }
  446. var arg1 *big.Int
  447. if arg1, err = numString(obj[1]); err != nil {
  448. return err
  449. }
  450. args.Index = arg1.Int64()
  451. return nil
  452. }
  453. type GetBlockByHashArgs struct {
  454. BlockHash string
  455. IncludeTxs bool
  456. }
  457. func (args *GetBlockByHashArgs) UnmarshalJSON(b []byte) (err error) {
  458. var obj []interface{}
  459. if err := json.Unmarshal(b, &obj); err != nil {
  460. return shared.NewDecodeParamError(err.Error())
  461. }
  462. if len(obj) < 2 {
  463. return shared.NewInsufficientParamsError(len(obj), 2)
  464. }
  465. argstr, ok := obj[0].(string)
  466. if !ok {
  467. return shared.NewInvalidTypeError("blockHash", "not a string")
  468. }
  469. args.BlockHash = argstr
  470. args.IncludeTxs = obj[1].(bool)
  471. return nil
  472. }
  473. type GetBlockByNumberArgs struct {
  474. BlockNumber int64
  475. IncludeTxs bool
  476. }
  477. func (args *GetBlockByNumberArgs) UnmarshalJSON(b []byte) (err error) {
  478. var obj []interface{}
  479. if err := json.Unmarshal(b, &obj); err != nil {
  480. return shared.NewDecodeParamError(err.Error())
  481. }
  482. if len(obj) < 2 {
  483. return shared.NewInsufficientParamsError(len(obj), 2)
  484. }
  485. if err := blockHeight(obj[0], &args.BlockNumber); err != nil {
  486. return err
  487. }
  488. args.IncludeTxs = obj[1].(bool)
  489. return nil
  490. }
  491. type BlockFilterArgs struct {
  492. Earliest int64
  493. Latest int64
  494. Address []string
  495. Topics [][]string
  496. Skip int
  497. Max int
  498. }
  499. func (args *BlockFilterArgs) UnmarshalJSON(b []byte) (err error) {
  500. var obj []struct {
  501. FromBlock interface{} `json:"fromBlock"`
  502. ToBlock interface{} `json:"toBlock"`
  503. Limit interface{} `json:"limit"`
  504. Offset interface{} `json:"offset"`
  505. Address interface{} `json:"address"`
  506. Topics interface{} `json:"topics"`
  507. }
  508. if err = json.Unmarshal(b, &obj); err != nil {
  509. return shared.NewDecodeParamError(err.Error())
  510. }
  511. if len(obj) < 1 {
  512. return shared.NewInsufficientParamsError(len(obj), 1)
  513. }
  514. // args.Earliest, err = toNumber(obj[0].ToBlock)
  515. // if err != nil {
  516. // return shared.NewDecodeParamError(fmt.Sprintf("FromBlock %v", err))
  517. // }
  518. // args.Latest, err = toNumber(obj[0].FromBlock)
  519. // if err != nil {
  520. // return shared.NewDecodeParamError(fmt.Sprintf("ToBlock %v", err))
  521. var num int64
  522. var numBig *big.Int
  523. // if blank then latest
  524. if obj[0].FromBlock == nil {
  525. num = -1
  526. } else {
  527. if err := blockHeight(obj[0].FromBlock, &num); err != nil {
  528. return err
  529. }
  530. }
  531. // if -2 or other "silly" number, use latest
  532. if num < 0 {
  533. args.Earliest = -1 //latest block
  534. } else {
  535. args.Earliest = num
  536. }
  537. // if blank than latest
  538. if obj[0].ToBlock == nil {
  539. num = -1
  540. } else {
  541. if err := blockHeight(obj[0].ToBlock, &num); err != nil {
  542. return err
  543. }
  544. }
  545. args.Latest = num
  546. if obj[0].Limit == nil {
  547. numBig = big.NewInt(defaultLogLimit)
  548. } else {
  549. if numBig, err = numString(obj[0].Limit); err != nil {
  550. return err
  551. }
  552. }
  553. args.Max = int(numBig.Int64())
  554. if obj[0].Offset == nil {
  555. numBig = big.NewInt(defaultLogOffset)
  556. } else {
  557. if numBig, err = numString(obj[0].Offset); err != nil {
  558. return err
  559. }
  560. }
  561. args.Skip = int(numBig.Int64())
  562. if obj[0].Address != nil {
  563. marg, ok := obj[0].Address.([]interface{})
  564. if ok {
  565. v := make([]string, len(marg))
  566. for i, arg := range marg {
  567. argstr, ok := arg.(string)
  568. if !ok {
  569. return shared.NewInvalidTypeError(fmt.Sprintf("address[%d]", i), "is not a string")
  570. }
  571. v[i] = argstr
  572. }
  573. args.Address = v
  574. } else {
  575. argstr, ok := obj[0].Address.(string)
  576. if ok {
  577. v := make([]string, 1)
  578. v[0] = argstr
  579. args.Address = v
  580. } else {
  581. return shared.NewInvalidTypeError("address", "is not a string or array")
  582. }
  583. }
  584. }
  585. if obj[0].Topics != nil {
  586. other, ok := obj[0].Topics.([]interface{})
  587. if ok {
  588. topicdbl := make([][]string, len(other))
  589. for i, iv := range other {
  590. if argstr, ok := iv.(string); ok {
  591. // Found a string, push into first element of array
  592. topicsgl := make([]string, 1)
  593. topicsgl[0] = argstr
  594. topicdbl[i] = topicsgl
  595. } else if argarray, ok := iv.([]interface{}); ok {
  596. // Found an array of other
  597. topicdbl[i] = make([]string, len(argarray))
  598. for j, jv := range argarray {
  599. if v, ok := jv.(string); ok {
  600. topicdbl[i][j] = v
  601. } else if jv == nil {
  602. topicdbl[i][j] = ""
  603. } else {
  604. return shared.NewInvalidTypeError(fmt.Sprintf("topic[%d][%d]", i, j), "is not a string")
  605. }
  606. }
  607. } else if iv == nil {
  608. topicdbl[i] = []string{""}
  609. } else {
  610. return shared.NewInvalidTypeError(fmt.Sprintf("topic[%d]", i), "not a string or array")
  611. }
  612. }
  613. args.Topics = topicdbl
  614. return nil
  615. } else {
  616. return shared.NewInvalidTypeError("topic", "is not a string or array")
  617. }
  618. }
  619. return nil
  620. }
  621. type FilterIdArgs struct {
  622. Id int
  623. }
  624. func (args *FilterIdArgs) UnmarshalJSON(b []byte) (err error) {
  625. var obj []interface{}
  626. if err := json.Unmarshal(b, &obj); err != nil {
  627. return shared.NewDecodeParamError(err.Error())
  628. }
  629. if len(obj) < 1 {
  630. return shared.NewInsufficientParamsError(len(obj), 1)
  631. }
  632. var num *big.Int
  633. if num, err = numString(obj[0]); err != nil {
  634. return err
  635. }
  636. args.Id = int(num.Int64())
  637. return nil
  638. }
  639. type LogRes struct {
  640. Address *hexdata `json:"address"`
  641. Topics []*hexdata `json:"topics"`
  642. Data *hexdata `json:"data"`
  643. BlockNumber *hexnum `json:"blockNumber"`
  644. LogIndex *hexnum `json:"logIndex"`
  645. BlockHash *hexdata `json:"blockHash"`
  646. TransactionHash *hexdata `json:"transactionHash"`
  647. TransactionIndex *hexnum `json:"transactionIndex"`
  648. }
  649. func NewLogRes(log *state.Log) LogRes {
  650. var l LogRes
  651. l.Topics = make([]*hexdata, len(log.Topics))
  652. for j, topic := range log.Topics {
  653. l.Topics[j] = newHexData(topic)
  654. }
  655. l.Address = newHexData(log.Address)
  656. l.Data = newHexData(log.Data)
  657. l.BlockNumber = newHexNum(log.Number)
  658. l.LogIndex = newHexNum(log.Index)
  659. l.TransactionHash = newHexData(log.TxHash)
  660. l.TransactionIndex = newHexNum(log.TxIndex)
  661. l.BlockHash = newHexData(log.BlockHash)
  662. return l
  663. }
  664. func NewLogsRes(logs state.Logs) (ls []LogRes) {
  665. ls = make([]LogRes, len(logs))
  666. for i, log := range logs {
  667. ls[i] = NewLogRes(log)
  668. }
  669. return
  670. }
  671. func NewHashesRes(hs []common.Hash) []string {
  672. hashes := make([]string, len(hs))
  673. for i, hash := range hs {
  674. hashes[i] = hash.Hex()
  675. }
  676. return hashes
  677. }
  678. type SubmitWorkArgs struct {
  679. Nonce uint64
  680. Header string
  681. Digest string
  682. }
  683. func (args *SubmitWorkArgs) UnmarshalJSON(b []byte) (err error) {
  684. var obj []interface{}
  685. if err = json.Unmarshal(b, &obj); err != nil {
  686. return shared.NewDecodeParamError(err.Error())
  687. }
  688. if len(obj) < 3 {
  689. return shared.NewInsufficientParamsError(len(obj), 3)
  690. }
  691. var objstr string
  692. var ok bool
  693. if objstr, ok = obj[0].(string); !ok {
  694. return shared.NewInvalidTypeError("nonce", "not a string")
  695. }
  696. args.Nonce = common.String2Big(objstr).Uint64()
  697. if objstr, ok = obj[1].(string); !ok {
  698. return shared.NewInvalidTypeError("header", "not a string")
  699. }
  700. args.Header = objstr
  701. if objstr, ok = obj[2].(string); !ok {
  702. return shared.NewInvalidTypeError("digest", "not a string")
  703. }
  704. args.Digest = objstr
  705. return nil
  706. }