block_test_util.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser 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. // The go-ethereum library 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 Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package tests
  17. import (
  18. "bytes"
  19. "encoding/hex"
  20. "fmt"
  21. "io"
  22. "math/big"
  23. "path/filepath"
  24. "runtime"
  25. "strconv"
  26. "strings"
  27. "time"
  28. "github.com/ethereum/go-ethereum/accounts"
  29. "github.com/ethereum/go-ethereum/common"
  30. "github.com/ethereum/go-ethereum/core"
  31. "github.com/ethereum/go-ethereum/core/state"
  32. "github.com/ethereum/go-ethereum/core/types"
  33. "github.com/ethereum/go-ethereum/crypto"
  34. "github.com/ethereum/go-ethereum/eth"
  35. "github.com/ethereum/go-ethereum/ethdb"
  36. "github.com/ethereum/go-ethereum/event"
  37. "github.com/ethereum/go-ethereum/logger/glog"
  38. "github.com/ethereum/go-ethereum/node"
  39. "github.com/ethereum/go-ethereum/rlp"
  40. )
  41. // Block Test JSON Format
  42. type BlockTest struct {
  43. Genesis *types.Block
  44. Json *btJSON
  45. preAccounts map[string]btAccount
  46. postAccounts map[string]btAccount
  47. lastblockhash string
  48. }
  49. type btJSON struct {
  50. Blocks []btBlock
  51. GenesisBlockHeader btHeader
  52. Pre map[string]btAccount
  53. PostState map[string]btAccount
  54. Lastblockhash string
  55. }
  56. type btBlock struct {
  57. BlockHeader *btHeader
  58. Rlp string
  59. Transactions []btTransaction
  60. UncleHeaders []*btHeader
  61. }
  62. type btAccount struct {
  63. Balance string
  64. Code string
  65. Nonce string
  66. Storage map[string]string
  67. PrivateKey string
  68. }
  69. type btHeader struct {
  70. Bloom string
  71. Coinbase string
  72. MixHash string
  73. Nonce string
  74. Number string
  75. Hash string
  76. ParentHash string
  77. ReceiptTrie string
  78. SeedHash string
  79. StateRoot string
  80. TransactionsTrie string
  81. UncleHash string
  82. ExtraData string
  83. Difficulty string
  84. GasLimit string
  85. GasUsed string
  86. Timestamp string
  87. }
  88. type btTransaction struct {
  89. Data string
  90. GasLimit string
  91. GasPrice string
  92. Nonce string
  93. R string
  94. S string
  95. To string
  96. V string
  97. Value string
  98. }
  99. func RunBlockTestWithReader(homesteadBlock *big.Int, r io.Reader, skipTests []string) error {
  100. btjs := make(map[string]*btJSON)
  101. if err := readJson(r, &btjs); err != nil {
  102. return err
  103. }
  104. bt, err := convertBlockTests(btjs)
  105. if err != nil {
  106. return err
  107. }
  108. if err := runBlockTests(homesteadBlock, bt, skipTests); err != nil {
  109. return err
  110. }
  111. return nil
  112. }
  113. func RunBlockTest(homesteadBlock *big.Int, file string, skipTests []string) error {
  114. btjs := make(map[string]*btJSON)
  115. if err := readJsonFile(file, &btjs); err != nil {
  116. return err
  117. }
  118. bt, err := convertBlockTests(btjs)
  119. if err != nil {
  120. return err
  121. }
  122. if err := runBlockTests(homesteadBlock, bt, skipTests); err != nil {
  123. return err
  124. }
  125. return nil
  126. }
  127. func runBlockTests(homesteadBlock *big.Int, bt map[string]*BlockTest, skipTests []string) error {
  128. skipTest := make(map[string]bool, len(skipTests))
  129. for _, name := range skipTests {
  130. skipTest[name] = true
  131. }
  132. for name, test := range bt {
  133. if skipTest[name] {
  134. glog.Infoln("Skipping block test", name)
  135. continue
  136. }
  137. // test the block
  138. if err := runBlockTest(homesteadBlock, test); err != nil {
  139. return fmt.Errorf("%s: %v", name, err)
  140. }
  141. glog.Infoln("Block test passed: ", name)
  142. }
  143. return nil
  144. }
  145. func runBlockTest(homesteadBlock *big.Int, test *BlockTest) error {
  146. ks := crypto.NewKeyStorePassphrase(filepath.Join(common.DefaultDataDir(), "keystore"), crypto.StandardScryptN, crypto.StandardScryptP)
  147. am := accounts.NewManager(ks)
  148. db, _ := ethdb.NewMemDatabase()
  149. // import pre accounts & construct test genesis block & state root
  150. _, err := test.InsertPreState(db, am)
  151. if err != nil {
  152. return fmt.Errorf("InsertPreState: %v", err)
  153. }
  154. cfg := &eth.Config{
  155. ChainConfig: &core.ChainConfig{HomesteadBlock: homesteadBlock},
  156. TestGenesisState: db,
  157. TestGenesisBlock: test.Genesis,
  158. Etherbase: common.Address{},
  159. AccountManager: am,
  160. PowShared: true,
  161. }
  162. ethereum, err := eth.New(&node.ServiceContext{EventMux: new(event.TypeMux)}, cfg)
  163. if err != nil {
  164. return err
  165. }
  166. cm := ethereum.BlockChain()
  167. //vm.Debug = true
  168. validBlocks, err := test.TryBlocksInsert(cm)
  169. if err != nil {
  170. return err
  171. }
  172. lastblockhash := common.HexToHash(test.lastblockhash)
  173. cmlast := cm.LastBlockHash()
  174. if lastblockhash != cmlast {
  175. return fmt.Errorf("lastblockhash validation mismatch: want: %x, have: %x", lastblockhash, cmlast)
  176. }
  177. newDB, err := cm.State()
  178. if err != nil {
  179. return err
  180. }
  181. if err = test.ValidatePostState(newDB); err != nil {
  182. return fmt.Errorf("post state validation failed: %v", err)
  183. }
  184. return test.ValidateImportedHeaders(cm, validBlocks)
  185. }
  186. // InsertPreState populates the given database with the genesis
  187. // accounts defined by the test.
  188. func (t *BlockTest) InsertPreState(db ethdb.Database, am *accounts.Manager) (*state.StateDB, error) {
  189. statedb, err := state.New(common.Hash{}, db)
  190. if err != nil {
  191. return nil, err
  192. }
  193. for addrString, acct := range t.preAccounts {
  194. addr, err := hex.DecodeString(addrString)
  195. if err != nil {
  196. return nil, err
  197. }
  198. code, err := hex.DecodeString(strings.TrimPrefix(acct.Code, "0x"))
  199. if err != nil {
  200. return nil, err
  201. }
  202. balance, ok := new(big.Int).SetString(acct.Balance, 0)
  203. if !ok {
  204. return nil, err
  205. }
  206. nonce, err := strconv.ParseUint(prepInt(16, acct.Nonce), 16, 64)
  207. if err != nil {
  208. return nil, err
  209. }
  210. if acct.PrivateKey != "" {
  211. privkey, err := hex.DecodeString(strings.TrimPrefix(acct.PrivateKey, "0x"))
  212. err = crypto.ImportBlockTestKey(privkey)
  213. err = am.TimedUnlock(common.BytesToAddress(addr), "", 999999*time.Second)
  214. if err != nil {
  215. return nil, err
  216. }
  217. }
  218. obj := statedb.CreateAccount(common.HexToAddress(addrString))
  219. obj.SetCode(code)
  220. obj.SetBalance(balance)
  221. obj.SetNonce(nonce)
  222. for k, v := range acct.Storage {
  223. statedb.SetState(common.HexToAddress(addrString), common.HexToHash(k), common.HexToHash(v))
  224. }
  225. }
  226. root, err := statedb.Commit()
  227. if err != nil {
  228. return nil, fmt.Errorf("error writing state: %v", err)
  229. }
  230. if t.Genesis.Root() != root {
  231. return nil, fmt.Errorf("computed state root does not match genesis block: genesis=%x computed=%x", t.Genesis.Root().Bytes()[:4], root.Bytes()[:4])
  232. }
  233. return statedb, nil
  234. }
  235. /* See https://github.com/ethereum/tests/wiki/Blockchain-Tests-II
  236. Whether a block is valid or not is a bit subtle, it's defined by presence of
  237. blockHeader, transactions and uncleHeaders fields. If they are missing, the block is
  238. invalid and we must verify that we do not accept it.
  239. Since some tests mix valid and invalid blocks we need to check this for every block.
  240. If a block is invalid it does not necessarily fail the test, if it's invalidness is
  241. expected we are expected to ignore it and continue processing and then validate the
  242. post state.
  243. */
  244. func (t *BlockTest) TryBlocksInsert(blockchain *core.BlockChain) ([]btBlock, error) {
  245. validBlocks := make([]btBlock, 0)
  246. // insert the test blocks, which will execute all transactions
  247. for _, b := range t.Json.Blocks {
  248. cb, err := mustConvertBlock(b)
  249. if err != nil {
  250. if b.BlockHeader == nil {
  251. continue // OK - block is supposed to be invalid, continue with next block
  252. } else {
  253. return nil, fmt.Errorf("Block RLP decoding failed when expected to succeed: %v", err)
  254. }
  255. }
  256. // RLP decoding worked, try to insert into chain:
  257. blocks := types.Blocks{cb}
  258. i, err := blockchain.InsertChain(blocks)
  259. if err != nil {
  260. if b.BlockHeader == nil {
  261. continue // OK - block is supposed to be invalid, continue with next block
  262. } else {
  263. return nil, fmt.Errorf("Block #%v insertion into chain failed: %v", blocks[i].Number(), err)
  264. }
  265. }
  266. if b.BlockHeader == nil {
  267. return nil, fmt.Errorf("Block insertion should have failed")
  268. }
  269. // validate RLP decoding by checking all values against test file JSON
  270. if err = validateHeader(b.BlockHeader, cb.Header()); err != nil {
  271. return nil, fmt.Errorf("Deserialised block header validation failed: %v", err)
  272. }
  273. validBlocks = append(validBlocks, b)
  274. }
  275. return validBlocks, nil
  276. }
  277. func validateHeader(h *btHeader, h2 *types.Header) error {
  278. expectedBloom := mustConvertBytes(h.Bloom)
  279. if !bytes.Equal(expectedBloom, h2.Bloom.Bytes()) {
  280. return fmt.Errorf("Bloom: want: %x have: %x", expectedBloom, h2.Bloom.Bytes())
  281. }
  282. expectedCoinbase := mustConvertBytes(h.Coinbase)
  283. if !bytes.Equal(expectedCoinbase, h2.Coinbase.Bytes()) {
  284. return fmt.Errorf("Coinbase: want: %x have: %x", expectedCoinbase, h2.Coinbase.Bytes())
  285. }
  286. expectedMixHashBytes := mustConvertBytes(h.MixHash)
  287. if !bytes.Equal(expectedMixHashBytes, h2.MixDigest.Bytes()) {
  288. return fmt.Errorf("MixHash: want: %x have: %x", expectedMixHashBytes, h2.MixDigest.Bytes())
  289. }
  290. expectedNonce := mustConvertBytes(h.Nonce)
  291. if !bytes.Equal(expectedNonce, h2.Nonce[:]) {
  292. return fmt.Errorf("Nonce: want: %x have: %x", expectedNonce, h2.Nonce)
  293. }
  294. expectedNumber := mustConvertBigInt(h.Number, 16)
  295. if expectedNumber.Cmp(h2.Number) != 0 {
  296. return fmt.Errorf("Number: want: %v have: %v", expectedNumber, h2.Number)
  297. }
  298. expectedParentHash := mustConvertBytes(h.ParentHash)
  299. if !bytes.Equal(expectedParentHash, h2.ParentHash.Bytes()) {
  300. return fmt.Errorf("Parent hash: want: %x have: %x", expectedParentHash, h2.ParentHash.Bytes())
  301. }
  302. expectedReceiptHash := mustConvertBytes(h.ReceiptTrie)
  303. if !bytes.Equal(expectedReceiptHash, h2.ReceiptHash.Bytes()) {
  304. return fmt.Errorf("Receipt hash: want: %x have: %x", expectedReceiptHash, h2.ReceiptHash.Bytes())
  305. }
  306. expectedTxHash := mustConvertBytes(h.TransactionsTrie)
  307. if !bytes.Equal(expectedTxHash, h2.TxHash.Bytes()) {
  308. return fmt.Errorf("Tx hash: want: %x have: %x", expectedTxHash, h2.TxHash.Bytes())
  309. }
  310. expectedStateHash := mustConvertBytes(h.StateRoot)
  311. if !bytes.Equal(expectedStateHash, h2.Root.Bytes()) {
  312. return fmt.Errorf("State hash: want: %x have: %x", expectedStateHash, h2.Root.Bytes())
  313. }
  314. expectedUncleHash := mustConvertBytes(h.UncleHash)
  315. if !bytes.Equal(expectedUncleHash, h2.UncleHash.Bytes()) {
  316. return fmt.Errorf("Uncle hash: want: %x have: %x", expectedUncleHash, h2.UncleHash.Bytes())
  317. }
  318. expectedExtraData := mustConvertBytes(h.ExtraData)
  319. if !bytes.Equal(expectedExtraData, h2.Extra) {
  320. return fmt.Errorf("Extra data: want: %x have: %x", expectedExtraData, h2.Extra)
  321. }
  322. expectedDifficulty := mustConvertBigInt(h.Difficulty, 16)
  323. if expectedDifficulty.Cmp(h2.Difficulty) != 0 {
  324. return fmt.Errorf("Difficulty: want: %v have: %v", expectedDifficulty, h2.Difficulty)
  325. }
  326. expectedGasLimit := mustConvertBigInt(h.GasLimit, 16)
  327. if expectedGasLimit.Cmp(h2.GasLimit) != 0 {
  328. return fmt.Errorf("GasLimit: want: %v have: %v", expectedGasLimit, h2.GasLimit)
  329. }
  330. expectedGasUsed := mustConvertBigInt(h.GasUsed, 16)
  331. if expectedGasUsed.Cmp(h2.GasUsed) != 0 {
  332. return fmt.Errorf("GasUsed: want: %v have: %v", expectedGasUsed, h2.GasUsed)
  333. }
  334. expectedTimestamp := mustConvertBigInt(h.Timestamp, 16)
  335. if expectedTimestamp.Cmp(h2.Time) != 0 {
  336. return fmt.Errorf("Timestamp: want: %v have: %v", expectedTimestamp, h2.Time)
  337. }
  338. return nil
  339. }
  340. func (t *BlockTest) ValidatePostState(statedb *state.StateDB) error {
  341. // validate post state accounts in test file against what we have in state db
  342. for addrString, acct := range t.postAccounts {
  343. // XXX: is is worth it checking for errors here?
  344. addr, err := hex.DecodeString(addrString)
  345. if err != nil {
  346. return err
  347. }
  348. code, err := hex.DecodeString(strings.TrimPrefix(acct.Code, "0x"))
  349. if err != nil {
  350. return err
  351. }
  352. balance, ok := new(big.Int).SetString(acct.Balance, 0)
  353. if !ok {
  354. return err
  355. }
  356. nonce, err := strconv.ParseUint(prepInt(16, acct.Nonce), 16, 64)
  357. if err != nil {
  358. return err
  359. }
  360. // address is indirectly verified by the other fields, as it's the db key
  361. code2 := statedb.GetCode(common.BytesToAddress(addr))
  362. balance2 := statedb.GetBalance(common.BytesToAddress(addr))
  363. nonce2 := statedb.GetNonce(common.BytesToAddress(addr))
  364. if !bytes.Equal(code2, code) {
  365. return fmt.Errorf("account code mismatch for addr: %s want: %s have: %s", addrString, hex.EncodeToString(code), hex.EncodeToString(code2))
  366. }
  367. if balance2.Cmp(balance) != 0 {
  368. return fmt.Errorf("account balance mismatch for addr: %s, want: %d, have: %d", addrString, balance, balance2)
  369. }
  370. if nonce2 != nonce {
  371. return fmt.Errorf("account nonce mismatch for addr: %s want: %d have: %d", addrString, nonce, nonce2)
  372. }
  373. }
  374. return nil
  375. }
  376. func (test *BlockTest) ValidateImportedHeaders(cm *core.BlockChain, validBlocks []btBlock) error {
  377. // to get constant lookup when verifying block headers by hash (some tests have many blocks)
  378. bmap := make(map[string]btBlock, len(test.Json.Blocks))
  379. for _, b := range validBlocks {
  380. bmap[b.BlockHeader.Hash] = b
  381. }
  382. // iterate over blocks backwards from HEAD and validate imported
  383. // headers vs test file. some tests have reorgs, and we import
  384. // block-by-block, so we can only validate imported headers after
  385. // all blocks have been processed by ChainManager, as they may not
  386. // be part of the longest chain until last block is imported.
  387. for b := cm.CurrentBlock(); b != nil && b.NumberU64() != 0; b = cm.GetBlock(b.Header().ParentHash) {
  388. bHash := common.Bytes2Hex(b.Hash().Bytes()) // hex without 0x prefix
  389. if err := validateHeader(bmap[bHash].BlockHeader, b.Header()); err != nil {
  390. return fmt.Errorf("Imported block header validation failed: %v", err)
  391. }
  392. }
  393. return nil
  394. }
  395. func convertBlockTests(in map[string]*btJSON) (map[string]*BlockTest, error) {
  396. out := make(map[string]*BlockTest)
  397. for name, test := range in {
  398. var err error
  399. if out[name], err = convertBlockTest(test); err != nil {
  400. return out, fmt.Errorf("bad test %q: %v", name, err)
  401. }
  402. }
  403. return out, nil
  404. }
  405. func convertBlockTest(in *btJSON) (out *BlockTest, err error) {
  406. // the conversion handles errors by catching panics.
  407. // you might consider this ugly, but the alternative (passing errors)
  408. // would be much harder to read.
  409. defer func() {
  410. if recovered := recover(); recovered != nil {
  411. buf := make([]byte, 64<<10)
  412. buf = buf[:runtime.Stack(buf, false)]
  413. err = fmt.Errorf("%v\n%s", recovered, buf)
  414. }
  415. }()
  416. out = &BlockTest{preAccounts: in.Pre, postAccounts: in.PostState, Json: in, lastblockhash: in.Lastblockhash}
  417. out.Genesis = mustConvertGenesis(in.GenesisBlockHeader)
  418. return out, err
  419. }
  420. func mustConvertGenesis(testGenesis btHeader) *types.Block {
  421. hdr := mustConvertHeader(testGenesis)
  422. hdr.Number = big.NewInt(0)
  423. return types.NewBlockWithHeader(hdr)
  424. }
  425. func mustConvertHeader(in btHeader) *types.Header {
  426. // hex decode these fields
  427. header := &types.Header{
  428. //SeedHash: mustConvertBytes(in.SeedHash),
  429. MixDigest: mustConvertHash(in.MixHash),
  430. Bloom: mustConvertBloom(in.Bloom),
  431. ReceiptHash: mustConvertHash(in.ReceiptTrie),
  432. TxHash: mustConvertHash(in.TransactionsTrie),
  433. Root: mustConvertHash(in.StateRoot),
  434. Coinbase: mustConvertAddress(in.Coinbase),
  435. UncleHash: mustConvertHash(in.UncleHash),
  436. ParentHash: mustConvertHash(in.ParentHash),
  437. Extra: mustConvertBytes(in.ExtraData),
  438. GasUsed: mustConvertBigInt(in.GasUsed, 16),
  439. GasLimit: mustConvertBigInt(in.GasLimit, 16),
  440. Difficulty: mustConvertBigInt(in.Difficulty, 16),
  441. Time: mustConvertBigInt(in.Timestamp, 16),
  442. Nonce: types.EncodeNonce(mustConvertUint(in.Nonce, 16)),
  443. }
  444. return header
  445. }
  446. func mustConvertBlock(testBlock btBlock) (*types.Block, error) {
  447. var b types.Block
  448. r := bytes.NewReader(mustConvertBytes(testBlock.Rlp))
  449. err := rlp.Decode(r, &b)
  450. return &b, err
  451. }
  452. func mustConvertBytes(in string) []byte {
  453. if in == "0x" {
  454. return []byte{}
  455. }
  456. h := unfuckFuckedHex(strings.TrimPrefix(in, "0x"))
  457. out, err := hex.DecodeString(h)
  458. if err != nil {
  459. panic(fmt.Errorf("invalid hex: %q: ", h, err))
  460. }
  461. return out
  462. }
  463. func mustConvertHash(in string) common.Hash {
  464. out, err := hex.DecodeString(strings.TrimPrefix(in, "0x"))
  465. if err != nil {
  466. panic(fmt.Errorf("invalid hex: %q", in))
  467. }
  468. return common.BytesToHash(out)
  469. }
  470. func mustConvertAddress(in string) common.Address {
  471. out, err := hex.DecodeString(strings.TrimPrefix(in, "0x"))
  472. if err != nil {
  473. panic(fmt.Errorf("invalid hex: %q", in))
  474. }
  475. return common.BytesToAddress(out)
  476. }
  477. func mustConvertBloom(in string) types.Bloom {
  478. out, err := hex.DecodeString(strings.TrimPrefix(in, "0x"))
  479. if err != nil {
  480. panic(fmt.Errorf("invalid hex: %q", in))
  481. }
  482. return types.BytesToBloom(out)
  483. }
  484. func mustConvertBigInt(in string, base int) *big.Int {
  485. in = prepInt(base, in)
  486. out, ok := new(big.Int).SetString(in, base)
  487. if !ok {
  488. panic(fmt.Errorf("invalid integer: %q", in))
  489. }
  490. return out
  491. }
  492. func mustConvertUint(in string, base int) uint64 {
  493. in = prepInt(base, in)
  494. out, err := strconv.ParseUint(in, base, 64)
  495. if err != nil {
  496. panic(fmt.Errorf("invalid integer: %q", in))
  497. }
  498. return out
  499. }
  500. func LoadBlockTests(file string) (map[string]*BlockTest, error) {
  501. btjs := make(map[string]*btJSON)
  502. if err := readJsonFile(file, &btjs); err != nil {
  503. return nil, err
  504. }
  505. return convertBlockTests(btjs)
  506. }
  507. // Nothing to see here, please move along...
  508. func prepInt(base int, s string) string {
  509. if base == 16 {
  510. if strings.HasPrefix(s, "0x") {
  511. s = s[2:]
  512. }
  513. if len(s) == 0 {
  514. s = "00"
  515. }
  516. s = nibbleFix(s)
  517. }
  518. return s
  519. }
  520. // don't ask
  521. func unfuckFuckedHex(almostHex string) string {
  522. return nibbleFix(strings.Replace(almostHex, "v", "", -1))
  523. }
  524. func nibbleFix(s string) string {
  525. if len(s)%2 != 0 {
  526. s = "0" + s
  527. }
  528. return s
  529. }