github.com/halybang/go-ethereum@v1.0.5-0.20180325041310-3b262bc1367c/core/vm/ota_storage_test.go (about)

     1  // Copyright 2018 Wanchain Foundation Ltd
     2  
     3  package vm
     4  
     5  import (
     6  	"bytes"
     7  	"github.com/wanchain/go-wanchain/common"
     8  	"github.com/wanchain/go-wanchain/core/state"
     9  	"github.com/wanchain/go-wanchain/crypto"
    10  	"github.com/wanchain/go-wanchain/ethdb"
    11  	"math/big"
    12  	"testing"
    13  )
    14  
    15  var (
    16  	otaShortAddrs = []string{
    17  		"0x022c849aefd10287bb1fb831524a83403ecefc9d546fbf73ef5e95b79c3cb5ae7602ca02565436af262a4cc9197145278d355aee79140e201e35879c5ac72f5dbd2f",
    18  		"0x0348cc8f64f14085eb24e100db9dbd46d217a44451c571f3ebbb8a1b387e2a613c03ef64a43cc2f4498a6641dcee5afe317654d72f61971c03821a1f1b06a32a58db",
    19  		"0x02864c100e06bcfc53ad86aecd0d14b126bc90268b5a64e267556244281d7c0288032f82c8055f947a1509885f5551804fcfb6fa084c2b0915a286747a892cdaba54",
    20  		"0x03bfdf88c14bda519d7d348be2b3a04e9ea7888e064707ffd9bba9dc264e6d8c9f03d7ea3d3d10f39115ff00c70606cae16e9ef7dbcb533f907d3d05e88983023e5e",
    21  		"0x02850cbb0c4b8e3930e5dd79eb7b736c38e24514f89168f87a25496658713a90a4029eccc7471db606ed4a279b4571e4a4ea2f0158ebf53e20071c85d0b2d1ec5fab",
    22  		"0x02483128152168625de2b21b4d7ba1f8e98a160ea78361b3225695517385fc3218023f1f8f4079be98200f882cbdaabbc6cc18ceae48b44f6bf9053de09d024de9be",
    23  		"0x0305246565268865843190a09ece7cce28c9295d11f79930ca1787f2f044e413fd02e87f1b3c3103f028a000c7bda3e09d82f56e63ac1edf157f8955c61a059aa8a8",
    24  		"0x027037ad331a3028d9005f1eb2b78b288fcece677c380142ea5b9919f1302ed00b032a5e555c0bbb29c42b5f5e7402f35bc22bc34d0d008dac41b00ad43fdb39f6d5",
    25  		"0x039d89050b5981bcb6de8c47cdee5365b8676698cba82ccc244cea33ff4da814d6026c94b7fa6b5ce6bb67449d2db032271081abb1dde056de4a2f31130a979e9479",
    26  	}
    27  
    28  	otaMixSetAddrs = []string{
    29  		"0345677dd5c14406945bd22e5b03af2db518690649523877aac5d3438cde4234370354a50db18be990a93c245883271e5875263e5cc614aecef0ccd2e6cecdae46d6",
    30  		"02296efc8293f6d4b488a687a07f16e9716d09ccf226e5d6132297c5fbcc732edd034344965cee2dd786831c5d2887ad62b6cc6363c53df7e61023503e5e22b01ff1",
    31  		"02613f9ea642a4e17bc59c6b806776534b3e4d1fa9d06b977ba0e31003fc6b775802a3b0c239cd195292f110212bdef9cd2f4a87f7146ebdaa37f07155b78fd8aa32",
    32  		"0275c5c0632ee0961f4e3575e5f4bf70bbc0d4e87752672d9cbbaf17892c38e1ff026b6eda7eb9dee482188f139359fd02db6632b75f7e5abed400df9bebad67cf05",
    33  		"024e4f0e02a1306cbb84051717538ca372c540111c388f0a9e1a37bb51da14e5b502e8dc38ae31f0f762d20f61cf2c6197a85a4c7771141e614b8b866892061708aa",
    34  		"023f684bb0b634d265a60a64e3c0acb58527b710ea47dd438ba4e9bb684654fb9703369e4cb790612e5ef16ef0993f91e62ee9cec63c802b02ab8923b129b4b4f33b",
    35  		"026cc75d1d2b0bbfaa6f88092e2c440860b501a50060ed41afe70ae21c6609e4c503420c38f7107bd674e6f08c6d055c979e5c4b12add96fa160cde72fab41e091d0",
    36  		"035f4996d104565103438ee5ade3ba34dad12a4035e2ba7232b75ebd711ec203830313404fadcb529e9a3729cde2b3ee4da9dc6920389a03c0954235d786f240244d",
    37  		"02443b23d45855fe44add2252786f329f973d85d268b44dc9a8c49caecda249c9203102b240b49d5a4351d5497beac94b6e7b18f532ad8cf25e0f7cbdcf522ca80e4",
    38  		"027767754af7cbd056fd3da7e4684d378244d6476910e5a175a1d8f66b396c4c72030f8d056dd1926cf5dbb0761c2c8209b0564948d7274bfd62083f936dd0b30360",
    39  		"03bcc152a969015950efdc6caa9fe3da7fafa554d2da20d873224857b5defe65740271ef5117002351c79bdf7ceba0b2dc3e5fcbc19201dfd75e52d43ae171481c64",
    40  		"03031a913d6d3d65b20ea782c083838776515e8339778cb27b0554a8c2f507926f0223930620c9340e3b407469f0214c7ef424216c94b631d5ca45c2fecd84bf6fc4",
    41  		"03e38622e56e8d1481fccdd9b10f089e738c1654f479252e88251459a26a49485e02536ff9c6b860e9067acf3df277f0a88832848910dba90eacb0caccd26db2fcec",
    42  		"02026cbbdfa8e285a723dc7ab96b0fc59a6bc90b00d3dba2c7e49e70735f434b0702acdf7017c148e457cc3df1e1344743bf973d72c7a07a5e90bed34875f0c457af",
    43  		"0292b449f4b95b1365f586304b0d812bf9f89d3969bb871d4cfd2442d434c1408b03326b9d547559d8193105097718f1b6104375059281f32d1499b697be5e1ecbef",
    44  		"03a2389ad24bb2fdad626dcfb5c708ab6f6488e49f32a429c3cb5585e897b20fd5021fca8ea16f7ddbbeb599cb52a7992c0f2cf27d464dee882061b1f174ff46518f",
    45  		"0369028a62bc80e3ab5c32e4462b6b2436f535718967730f92a7b4d8c692a3543d0306ea0188604a287a5076a4c8f80681141301b1f6b9ae62c1a2b448853d1595b3",
    46  		"02b62e18d6765426b10d5678b668996305b9e71bef0143a6c487a0abd0ade2551c02f9b1cb397dbf98045fca68b222a4c9ba3bc50591eda80c77ed60748ad6099b0f",
    47  		"0253e4190bc7000fb94e72551c3c995c66c29773b0925d54829843a8b0cf74fdd5025877aff7ffb92dd2aba4e0a39d1a72450168e8d2ca0e0e7ec47b6c9933f06f16",
    48  		"03eedb35d5f3680fa6e535bd166b28e5ebdb1aac444b31f92b6a30cff1bf0b826503152f3f221a4df963503a8dddaba122c5574602307e6f8ca8627d948478a2ffa3",
    49  		"035424c0fa7725680a40798dc1e848a6d5419092f30beab9bd7101f607376446e202aab80e8a1a3b6a84192074b64a29d7eb4ac416c4b18dfe3ac5f5c53ffbb5d344",
    50  		"02246c1e5807b503c869555c72c8e0696eeadeda94ddf2a916dd44cf09f9248be7027b785216c5c803e076e0b16c32278fd02ab3fe0b4d86368a791099339aa23dff",
    51  		"0301a8dc84e6994fd73bed91603ae9db80592a1548e0338526171a31f2593a2a6d03ac85f09660dff31180bdf60db3ce7821474a8a2a1f596946f27419e2435ea665",
    52  		"02b3c0ccdd78b2bb7658fba741580ffa269b76b1066403883758cca640827ef17703f6a86fab023c81a6919ef7b5655f2b99aa5af9fceaa706ebfb3c9cc417bcdc44",
    53  		"03d12c9ea6c92079606941a38bea695182fd9bb1a97c52df67fdaac4e20549c1f203a2696274405b6ca045548b21685dd30b86b01caaaf9acd8d0f07c0895ba93010",
    54  		"036f10d5f5a72f1eb7022dee80f73c56a3d98762695fd8d4d9215179192887fdd1029c51c2699c858eb96d42172dd80002b2b2f88a177aa8f5713190c76a2dd7dcd1",
    55  		"02f493719e11903da6ac39e64c9ec669fe49221ebb42091c10c150d4b097842dfa0323cf1a6f3d6e7458c335b066510de4105d3bd1930b070b9fb95a8980f1309c95",
    56  		"0294b5df5a3473fad47a83ba5160198f618a18f7aa16945cc8a6fe6fb826f6c2a8038a06f5d02cf4da43806f8d1451db84dbe63e139c4329a872105a97c6c61b311c",
    57  		"039db56bc204f1631e768766f4f329657b4961c8db6011685ac193dfcc6ecafb8202143b3398c1b203dad209ae4fde8272c6ff2ea0203d364390d5c6c6605c15ccd8",
    58  		"02d5158e0841b6a8c1f90fa5847194654d7a805369d41a5411a762625995bc2a6a0346c2000172bfa0f2d34248b1375cc30e25724b3728041b31791d101ab757cb2b",
    59  		"02db10e1f27001f60fe4fd9335b1f8f0f0ff35c2cac1466de3203f743a6a0c000f027af6c98f4af8d89db22d87d5c6f9c1c96350518e193f1531d629577baef26b3e",
    60  		"03a62717a7e0ee6563550fa42c3075bad24bcbc4e696c3c57216faa2d678c70cb803086dd9fcf1972952bfcf67ba96fb022cdc59f55dd61045a7e79f61ab7259dfee",
    61  		"03ba22fd5c70faa32654b4727d717ecc705bbaebd4418d327b149c1b26c3cf089b022c0ad4422dfd354c291a5205b14d6574711abbef376f6e0bd666fc848fc136c6",
    62  		"02a302740b26c7cc60eb4b9dbd0a772e4bb99f9681b7527f980282d25a3e6f228203d20a8ff046d214b4a717f21bad6fd412654dc574846e1531aac50aabd4030682",
    63  		"02b3c5f09e8adc7b3bd767d3fec7b149395e3a8a0a02460d869ec1b5c9e0c22a8a036d6b496d371790a7a9e0739fa85e29a9dfef05235d14ac888d1d3b04e983b8d1",
    64  		"03d2d155adb26c7a69cde34a98a1a3e90c345b19ef053afdf130f6157e7b5c849202c5c1a6a3fab02e3e68b6045102b552f6dde13c734f7a833838cf821d6e848792",
    65  		"02d310d351eb694001b9cd170c65a48a0afa9b0d3d40910af0da9fc97c8232051e027ff42b3f0a76deff74c06d0f31c8f3fe2489f7af05e7194ef5daf39057bc0672",
    66  		"0228678d008cf51fd67a8409c1bb6a83832673bcb6bda98a0b3b481c2db59f32ae03045ce1f57c0ee72277f47605d9340bec9bf00a0c01504c8a942b225320460f02",
    67  		"02808cc9ef0f20fa1dd88d3b49c95e03a5d5eb372c42ad0ef25a01363530a725a10364af6b5ae595dcca7ffb3c70b6f9ef6a8891396c018304c7beb6d0ffde494c9f",
    68  		"0246321b86689fdb44522328c80ff260bd37411dad5a595cb06b6a78c0094afb4c03f00592ca8dd745491a0e0b40d686dd4c029bf820d22126d6ea8982b854055db9",
    69  		"032f39386e45e009f8cc6872bfc485e4dd2e9e276f18366485c00b2a1d13df0b3a036a98241a67e24efb2d18c0eaf7fc89b3e9f78a36dc5b1683b014efa185314761",
    70  		"03f39fe9d263c664a1f1eacdf5d0718c75b25b520b881f13ed2dd583cd8edcc88302a3623c3c72e44b39545d898a3d4fa61d100985a0de513ffd3dd86152875c2cb7",
    71  		"02e3266ad9cad4a53cb2b49cf384ea6dac649d6595f6410a4b13e7b42e5346273003600ca1862a4b42ad2f70fc30cbbe477f7c1c15cf9dab07209b3fe1db4c0e5035",
    72  		"030043f261253b63a61235be84daf1b9a976d5b4bd4de885fece6d8a185517bd400359c81205db387a185172bc4f7293a23a7a2ea4f37bb04a36f22ab381aec97198",
    73  		"03cc230e682c1a1758f755be2ea5c513e1b5f23d8e29aff0f5c9c87f4e247f78f002fc32f147d6229e5561c20ea7837391b6aa1f6b98d333027f65687658b8dd0d9c",
    74  		"02bce836983d53579aa9d9318090443e79d46ae59439349efac40801685f78ec3f022515ed0945198e136908e8eb4bb4433e2b69ff388c1815786d842117c025c055",
    75  		"02e5c98c4cdc9e3e1e3a76aff69378c7715b0f3c22f6af71b06d878ce6730e8b5d030f184ffa5c1f190fea374a13a8d867dc337e1bf23fb73d8740c2b972705a31fc",
    76  		"03189cff12f5e82d840de1177b65f748f67cad7490222b54b23879c80a153829ca03987dbc75cf17c04c73e7b03489a103fafb350ab633051e52403c9c82ec625547",
    77  		"03204de59afcca1fe6cc7e546077be8a989ae0c3ce4b137255dc34438f6b6072ed0203ed4c5a4b13d03ff55e9c76ddaf8a8ca33958e8ff5b620325b81a36005de39c",
    78  		"02ec5e3c4035f4ddf14a05b6b01d8ae978b8b5322316016012e50f62d94e12b508027ba6c309c302696d39b3c301a9af7a332240019871c733410ab0e2f0471b4030",
    79  		"0331cd68127f683467c971bfe6f580d0ed7c3983b261c2e637d10e02e5552a40d103edc7839a03fecca40d1d8c832d70a94e23b4bf905769b4390aab4aa184fbd349",
    80  		"02fdd74baf355caa20c803e20e6aab9f116f9f07352c028a39fc9817a31e7d283703ec758b11ba083133c939b8faab78162ac21371d56ba394a65fb464c2cf70dfe7",
    81  		"02d0e74c5a576e9644ff16f1268ac17ae4940cfab8a91401bc86e69da52170769f035b6441f07bf9c05bf4e26c070f106abbb49ccae2cafcc16d236ff2facca16685",
    82  		"031a46331b0c0c6eb5a378eaedf063be498048a1a15e50c397752dd078d7ab1dc7032bad37342169ee779c3110972a0b77d2907f0d148f8b4e803644696861f0c4d2",
    83  		"0259b0fbf936bea8c46c2d2e5065b49da0812aaf6546b2d294bcfccdfb0f37bbb203897b491179291210050925cb4c0f99830c84688f5476a9aae0d2ef71ec0af42c",
    84  		"02a681409a2ca8ffcc054abdce551b8c5e8fc49384119f1881eab8c6851073386e03ef40d9fbf4e0f5d5a21f213e1faa1b2ca5eb321a94da1a41abbf9d6d6da03b7f",
    85  		"03062d24a85db1b9346c7b4b5fed7f58f98943a69402f55825d4f39cfe3e2e6eaa027c934d8a458bdd5476b16998f33e2980a4f048997c22239be9902c401d6bdbf3",
    86  		"027c89e76924902afdac486d9d3f9b7677679cfce06d33e419cfc3132ae63bcff1021cb2d8bb2ca809fea308a2c1d18f74cefb8dd4002d3316c732fc824a4de89f0b",
    87  		"022f4d6174ae3f35f779c5d62fb1a3735ab073a12a9dafecf4514eb058032bb10c03b03c0f83f127ce1b31b06a99d7f6ee0f13bbf16bd47f86ce70dcddda4ab3a418",
    88  		"0351929f675e6e3a8c31256e553c57ca0ff15a49a191f409e7eff3e58fc3f3ef3003076fde3fabc12367e5a79f8e01a2fd7ee3addc6568c14e8e8adddfb36a0be59c",
    89  		"0356fbe24ffbeab869d3c4973e03765b55050719f70cc189f6ec1731a5271943b1038d4db476dd5545b54220f3da53dc20e8b10cbf74b24a1bb72ba28b1231e8dfcd",
    90  		"03ff9bcbb7bb824fc459d4ba2f4a8ec203e5c0c5bfa35eed9a42871a7c8ec8b136037e3f2f4576657b63d32b6df207e184e649af8d19c59eedd4bf2d381d00c8d34c",
    91  		"0256b94edf885ae93756b52b9e90dfc0cc368982cf43907faabc42572b795d784c03ec82ef2d6c47e6b8a9117f8497c4ff24fbf29c68f1e16d9e6e18af0acc512cba",
    92  		"0214544f30858022be597d9a6135155a897e419aae590ea6c241480ec8817d7e5a03028ececa6308092f8a6fda2e4a170422618c2f02fb78ce105cacecde7c99b166",
    93  		"031ea7b51d749e07da3f85f0f44746ff5b059197d8514d2a9204a02e7355734e6f02203b307d63f87c5edf5292a82886c745f3640f2883afb4af6d03139a4fbb8940",
    94  		"02ea5cdbf7b40275f822de5a124f749b73dfcbb7305e28e5d168a93f2abff51b6f031fabb5ce9a75d7ff264f0665df232fe705f6fc233c20d4ab1fae353714046295",
    95  		"03e72ddc3add58c0ce2926f9c45c2173e3e960e10cd6f4db36c5a4e23221890ce003d0527941902ac1e84138ebc631a3cd2643031f61c5fc8b7443eeb92a913bed90",
    96  		"02598d8cf21adbeb7d838e5eaa565fabcd1a23a0602f0f881387e991dbda15304d02e679601f6b95ca5557b0754961e4e94f0ca047e11e76b60deafede3c5b6334a6",
    97  		"03d818f4d359d7868b1c23f2a1c182dd75a66753fa50226957b54cc8e049ae78810274abc42ab5dd5a925ad3130d45eac887a962c275a119cbe5940d8b00846ab1ad",
    98  		"033350f1f023949fe863b7039b030eb27e819f074fcf7cab729a11a805575ff00f02e9070e812301301ca4d411ce2c8850f8b69bf343f3becd54b5284f6f4f2aebdb",
    99  		"0207d3f70f99c68c693c436a9aaa10585ab15efe7f752489386c9dc1df6f45c70c035dc2f9a2ae8431f937d3de0aae7cecd7f6a3ce689fdb53ffdd684c240b32c5d8",
   100  		"030a02107f4e105465347bbf87ab4994708f75430eae979eb6343e6b93bdc9c3160286ffb56634a3dcb1543f194ee9cec2799fb77f93699b8e98992ad8670958aaf8",
   101  		"0200d6e7fd6c5f907fd586c0da75f2d0da408be183957f6eef5433ed24307a781d038d366fb426c2d78ea18195195a96f0d9ae25bf641b874a26bdf5600109a2db68",
   102  		"03b105e4f4ea87408dfa183f322b35f85b8f0272e913ee04109341911840ec731d02b47a64f2a9228e0846e66679f518e5260e94309188c6f7d12173b3e4003ed04c",
   103  		"025884e9eee012d2642da839d09f0a1f0d3d03ac398a0c30ec790e2e2c1c0226ef0329b7cb3590d2bbbbb24753c01965ad193f23c4eb204d7f83ee9b7b822666045f",
   104  		"028bced3ed46d83c5d8a73695d9ee21b699e3162c0eddefa73ad573c74b291c7ab02b6b6a263f2180fdf00cba8f9c9c2c211c90c993c1d3325ca14c6c434a66ba4e0",
   105  		"02b4b41b2bc6a5177debc6aaead1a4259582d92b289d08c19b00c94078f4d09953031d9b6003d8124b6ea586d36ac1092c8e1a18b729440adad40163d300390500c8",
   106  		"03363c202ab84582cb7c5d48613581c713796bdde2ba9f8f3ca086ff7cb46dc3a5036ffd7507d3c7452683352c106d71ff94f0a4dabc14ceaa86e7bc257a8362912e",
   107  		"02101d266a899ce1d120752bf9be105693c098c6f1561c5af204a04556ad15b75003b5cec201bfc00364479d8f8572f74aaf6fb9b049b29d063c30a05590200d9e2a",
   108  		"034ba0172a11e0bf3b04d2200b4ab2fa1f55e51d2f404e30f697a34a604304f381024a7340e5297c8e280cb78000bc6caf7d53371534d2a64554027ee76f606fcd14",
   109  		"0244e4ef42951845073c7bbdc14f1087ef3c790efc0690ca1f2fd0a6eada524652031ca519d19ad62b8e4ac6dbf6bb44543d7c565c673982640bea32e02dea6edad9",
   110  		"0207584426460218a04ae021b187dc689f7966e190ac4717e3c9f9db8e9b0dee21035d3aad0ae9c379294b91126f3496ee675779b8978b524dd506dd256156d4d368",
   111  		"026d7b49ab33327724652f03d127775e06e413ea6f9908045ad01a80d07bfdf7c203e3c6f4b6a60fd6661ac561254e1e11a61bf0300a37a41428f7b82368b76ae296",
   112  		"02fb177eb8779cbac2e78711433413b3057ad4c20cf4c059d3145f56889a1887410274f7909057f4f1aa964b00a1e29695bc16d69135f2d3c1705ff9d2b0b6d1d657",
   113  		"0262a2a03160b9c8cae206eb11bb8d11d38f6b2b6c7df2ed102e52decb597ccd7a02833ff4593e48788fe145cabe0c604b6638efd1645fd0bfc1fe5e0a62b08d65a5",
   114  		"02a5600908dbaffc21d476e1f525e5615e35e1201f3a303dc4aab0f1bb50fd8dfd0376d9f9d8fbfd281f264e40ac1a1e06b015445e2e068931111a878eaf14322d12",
   115  		"03f783adfb17ff6a3159e4064803e7b243c8cf6d1cf450ba7677b30bf78e339cac026d80012216c4b287d2c324370217ddb55ebae09f12d6762db9e8270c388e1a46",
   116  		"03f27ab925a2df5bca077db2e7b707ae02da1fc321acf48ee93b439c5d972e620d0360920390ff3c054aad64b9335ae2c3553a2b12e27dcc9289c1ae546c7e25e470",
   117  	}
   118  )
   119  
   120  func TestOTABalance2ContractAddr(t *testing.T) {
   121  
   122  	{
   123  		addr := OTABalance2ContractAddr(nil)
   124  		if (addr != common.Address{}) {
   125  			t.Error("expect empoty address!")
   126  		}
   127  	}
   128  
   129  	{
   130  		addr := OTABalance2ContractAddr(big.NewInt(10))
   131  		if (addr != common.Address{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16}) {
   132  			t.Error("unexpect address!")
   133  		}
   134  	}
   135  
   136  }
   137  
   138  func TestGetOtaBalance(t *testing.T) {
   139  	var (
   140  		db, _      = ethdb.NewMemDatabase()
   141  		statedb, _ = state.New(common.Hash{}, state.NewDatabase(db))
   142  
   143  		otaShortAddr = common.FromHex(otaShortAddrs[0])
   144  	)
   145  
   146  	t.Logf("otaShortAddr len:%d", len(otaShortAddr))
   147  	otaAX := otaShortAddr[1:common.HashLength]
   148  	balance, err := GetOtaBalanceFromAX(statedb, otaAX)
   149  	if err == nil || balance != nil {
   150  		t.Errorf("otaAX len:%d, err:%s", len(otaAX), err.Error())
   151  	}
   152  
   153  	otaAX = otaShortAddr[1 : 1+common.HashLength]
   154  	balance, err = GetOtaBalanceFromAX(statedb, otaAX)
   155  	if err != nil {
   156  		t.Errorf("err:%s", err.Error())
   157  	}
   158  
   159  	if balance != nil && balance.Cmp(big.NewInt(0)) != 0 {
   160  		t.Errorf("balance:%v", balance)
   161  	}
   162  
   163  	err = setOTA(statedb, big.NewInt(10), otaShortAddr)
   164  	if err != nil {
   165  		t.Errorf("SetOTA err:%s", err.Error())
   166  		return
   167  	}
   168  
   169  	balance, err = GetOtaBalanceFromAX(statedb, otaAX)
   170  	if err != nil {
   171  		t.Errorf("GetOtaBalanceFromAX err:%s", err.Error())
   172  	}
   173  
   174  	if balance == nil || balance.Cmp(big.NewInt(10)) != 0 {
   175  		t.Errorf("GetOtaBalanceFromAX balance:%v", balance)
   176  	}
   177  }
   178  
   179  func TestCheckOTAExist(t *testing.T) {
   180  	var (
   181  		db, _      = ethdb.NewMemDatabase()
   182  		statedb, _ = state.New(common.Hash{}, state.NewDatabase(db))
   183  
   184  		otaShortAddr = common.FromHex(otaShortAddrs[1])
   185  		otaAX        = otaShortAddr[1 : 1+common.HashLength]
   186  		balanceSet   = big.NewInt(10)
   187  	)
   188  
   189  	_, _, err := CheckOTAAXExist(nil, otaAX)
   190  	if err == nil {
   191  		t.Error("expect err: invalid input param!")
   192  	}
   193  
   194  	_, _, err = CheckOTAAXExist(statedb, otaAX[1:])
   195  	if err == nil {
   196  		t.Error("expect err: invalid input param!")
   197  	}
   198  
   199  	exist, balanceGet, err := CheckOTAAXExist(statedb, otaAX)
   200  	if err != nil {
   201  		t.Errorf("CheckOTAExist, err:%s", err.Error())
   202  	}
   203  
   204  	if exist || (balanceGet != nil && balanceGet.Cmp(common.Big0) != 0) {
   205  		t.Errorf("exis:%t, balance:%v", exist, balanceGet)
   206  	}
   207  
   208  	err = setOTA(statedb, balanceSet, otaShortAddr)
   209  	if err != nil {
   210  		t.Errorf("SetOTA err:%s", err.Error())
   211  	}
   212  
   213  	exist, balanceGet, err = CheckOTAAXExist(statedb, otaAX)
   214  	if err != nil {
   215  		t.Errorf("CheckOTAExist, err:%s", err.Error())
   216  	}
   217  	if !exist || balanceGet == nil || balanceGet.Cmp(big.NewInt(10)) != 0 {
   218  		t.Errorf("ChechOTAExist, exis:%t, balanceGet:%v", exist, balanceGet)
   219  	}
   220  }
   221  
   222  func TestBatCheckOTAExist(t *testing.T) {
   223  
   224  	{
   225  		var (
   226  			db, _      = ethdb.NewMemDatabase()
   227  			statedb, _ = state.New(common.Hash{}, state.NewDatabase(db))
   228  
   229  			otaShortAddrBytes = [][]byte{
   230  				common.FromHex(otaShortAddrs[1]),
   231  				common.FromHex(otaShortAddrs[2]),
   232  				common.FromHex(otaShortAddrs[3]),
   233  				common.FromHex(otaShortAddrs[4]),
   234  			}
   235  		)
   236  
   237  		otaAXs := make([][]byte, 0, 4)
   238  		for _, otaShortAddr := range otaShortAddrBytes {
   239  			otaAXs = append(otaAXs, otaShortAddr[0:1+common.HashLength])
   240  		}
   241  
   242  		_, _, _, err := BatCheckOTAExist(nil, otaAXs)
   243  		if err == nil {
   244  			t.Error("expect err: invalid input param!")
   245  		}
   246  
   247  		_, _, _, err = BatCheckOTAExist(statedb, nil)
   248  		if err == nil {
   249  			t.Error("expect err: invalid input param!")
   250  		}
   251  
   252  		otaAXs = append(otaAXs, otaAXs[0][1:])
   253  		_, _, _, err = BatCheckOTAExist(statedb, nil)
   254  		if err == nil {
   255  			t.Error("expect err: invalid input ota AX!")
   256  		}
   257  
   258  	}
   259  
   260  	{
   261  		var (
   262  			db, _      = ethdb.NewMemDatabase()
   263  			statedb, _ = state.New(common.Hash{}, state.NewDatabase(db))
   264  
   265  			otaShortAddrBytes = [][]byte{
   266  				common.FromHex(otaShortAddrs[1]),
   267  				common.FromHex(otaShortAddrs[2]),
   268  				common.FromHex(otaShortAddrs[3]),
   269  				common.FromHex(otaShortAddrs[4]),
   270  			}
   271  
   272  			balanceSet = big.NewInt(10)
   273  		)
   274  
   275  		otaAXs := make([][]byte, 0, 4)
   276  		for _, otaShortAddr := range otaShortAddrBytes {
   277  			otaAXs = append(otaAXs, otaShortAddr[0:1+common.HashLength])
   278  		}
   279  
   280  		exist, balanceGet, unexisotaAx, err := BatCheckOTAExist(statedb, otaAXs)
   281  		if exist || (balanceGet != nil && balanceGet.Cmp(big.NewInt(0)) != 0) {
   282  			t.Errorf("exis:%t, balanceGet:%v", exist, balanceGet)
   283  		}
   284  
   285  		if unexisotaAx == nil {
   286  			t.Errorf("unexisotaAX is nil!")
   287  		}
   288  
   289  		if common.ToHex(unexisotaAx) != common.ToHex(otaAXs[0]) {
   290  			t.Errorf("unexisotaAx:%s, expect:%s", common.ToHex(unexisotaAx), common.ToHex(otaAXs[0]))
   291  		}
   292  
   293  		if err != nil {
   294  			t.Logf("err:%s", err.Error())
   295  		}
   296  
   297  		for _, otaShortAddr := range otaShortAddrBytes {
   298  			err = setOTA(statedb, balanceSet, otaShortAddr)
   299  			if err != nil {
   300  				t.Errorf("err:%s", err.Error())
   301  			}
   302  		}
   303  
   304  		exist, balanceGet, unexisotaAx, err = BatCheckOTAExist(statedb, otaAXs)
   305  		if !exist || (balanceGet != nil && balanceSet.Cmp(balanceGet) != 0) {
   306  			t.Errorf("exis:%t, balanceGet:%v", exist, balanceGet)
   307  		}
   308  
   309  		if unexisotaAx != nil {
   310  			t.Errorf("unexisota:%s", common.ToHex(unexisotaAx))
   311  		}
   312  
   313  		if err != nil {
   314  			t.Errorf("err:%s", err.Error())
   315  		}
   316  
   317  		unexisotaShortAddr := common.FromHex(otaShortAddrs[5])
   318  		unexisotaAXSet := unexisotaShortAddr[0 : 1+common.HashLength]
   319  		otaAXs = append(otaAXs, unexisotaAXSet)
   320  		exist, balanceGet, unexisotaAx, err = BatCheckOTAExist(statedb, otaAXs)
   321  		if exist || (balanceGet != nil && balanceSet.Cmp(balanceGet) == 0) {
   322  			t.Errorf("exis:%t, balanceGet:%v", exist, balanceGet)
   323  		}
   324  
   325  		if unexisotaAx != nil {
   326  			t.Logf("unexisotaAx:%s", common.ToHex(unexisotaAx))
   327  		}
   328  		if err != nil {
   329  			t.Logf("err:%s", err.Error())
   330  		}
   331  
   332  		err = setOTA(statedb, big.NewInt(0).Add(balanceSet, big.NewInt(10)), unexisotaShortAddr)
   333  		if err != nil {
   334  			t.Errorf("err:%s", err.Error())
   335  		}
   336  
   337  		exist, balanceGet, unexisotaAx, err = BatCheckOTAExist(statedb, otaAXs)
   338  		if exist || (balanceGet != nil && balanceSet.Cmp(balanceGet) == 0) {
   339  			t.Errorf("exis:%t, balanceGet:%v", exist, balanceGet)
   340  		}
   341  
   342  		if exist || (balanceGet != nil && balanceSet.Cmp(balanceGet) == 0) {
   343  			t.Errorf("exis:%t, balanceGet:%v", exist, balanceGet)
   344  		}
   345  
   346  		if err != nil {
   347  			t.Logf("err:%s", err.Error())
   348  		}
   349  
   350  		if unexisotaAx == nil {
   351  			t.Errorf("unexisota is nil!")
   352  		}
   353  
   354  		if common.ToHex(unexisotaAx) != common.ToHex(unexisotaAXSet) {
   355  			t.Errorf("unexisota:%s, expect:%s", common.ToHex(unexisotaAx), common.ToHex(unexisotaAXSet))
   356  		}
   357  	}
   358  
   359  }
   360  
   361  func TestSetOTA(t *testing.T) {
   362  	var (
   363  		db, _      = ethdb.NewMemDatabase()
   364  		statedb, _ = state.New(common.Hash{}, state.NewDatabase(db))
   365  
   366  		otaShortAddr = common.FromHex(otaShortAddrs[3])
   367  		otaAX        = otaShortAddr[1 : 1+common.HashLength]
   368  		balanceSet   = big.NewInt(10)
   369  	)
   370  
   371  	t.Logf("otaShortAddr len:%d", len(otaShortAddr))
   372  
   373  	err := setOTA(statedb, balanceSet, otaShortAddr)
   374  	if err != nil {
   375  		t.Errorf("err:%s", err.Error())
   376  	}
   377  
   378  	balance, err := GetOtaBalanceFromAX(statedb, otaAX)
   379  	if err != nil {
   380  		t.Errorf("err:%s", err.Error())
   381  	}
   382  
   383  	if balance == nil || balance.Cmp(balanceSet) != 0 {
   384  		t.Errorf("balance:%v", balance)
   385  	}
   386  }
   387  
   388  func TestAddOTAIfNotExist(t *testing.T) {
   389  	var (
   390  		db, _      = ethdb.NewMemDatabase()
   391  		statedb, _ = state.New(common.Hash{}, state.NewDatabase(db))
   392  
   393  		otaShortAddr = common.FromHex(otaShortAddrs[4])
   394  		otaAX        = otaShortAddr[1 : 1+common.HashLength]
   395  		balanceSet   = big.NewInt(10)
   396  	)
   397  
   398  	add, err := AddOTAIfNotExist(statedb, balanceSet, otaShortAddr)
   399  	if err != nil {
   400  		t.Errorf("err:%s", err.Error())
   401  	}
   402  
   403  	if !add {
   404  		t.Errorf("add is false!")
   405  	}
   406  
   407  	add, err = AddOTAIfNotExist(statedb, balanceSet, otaShortAddr)
   408  	if err == nil {
   409  		t.Errorf("expect err: ota exist already!")
   410  	}
   411  
   412  	if add {
   413  		t.Errorf("add is true!")
   414  	}
   415  
   416  	balance, err := GetOtaBalanceFromAX(statedb, otaAX)
   417  	if err != nil {
   418  		t.Errorf("err:%s", err.Error())
   419  	}
   420  
   421  	if balance == nil || balance.Cmp(balanceSet) != 0 {
   422  		t.Errorf("balance:%v", balance)
   423  	}
   424  }
   425  
   426  func TestSetOtaBalanceToAX(t *testing.T) {
   427  	{
   428  		err := SetOtaBalanceToAX(nil, make([]byte, common.HashLength), big1)
   429  		if err == nil {
   430  			t.Error("expect err: invalid input param!")
   431  		}
   432  	}
   433  
   434  	{
   435  		var (
   436  			db, _      = ethdb.NewMemDatabase()
   437  			statedb, _ = state.New(common.Hash{}, state.NewDatabase(db))
   438  		)
   439  
   440  		err := SetOtaBalanceToAX(statedb, make([]byte, common.HashLength-1), big1)
   441  		if err == nil {
   442  			t.Error("expect err: invalid input param!")
   443  		}
   444  	}
   445  
   446  	{
   447  		var (
   448  			db, _      = ethdb.NewMemDatabase()
   449  			statedb, _ = state.New(common.Hash{}, state.NewDatabase(db))
   450  		)
   451  
   452  		err := SetOtaBalanceToAX(statedb, make([]byte, common.HashLength), nil)
   453  		if err == nil {
   454  			t.Error("expect err: invalid input param!")
   455  		}
   456  	}
   457  }
   458  
   459  func TestGetOTAInfoFromAX(t *testing.T) {
   460  	var (
   461  		db, _      = ethdb.NewMemDatabase()
   462  		statedb, _ = state.New(common.Hash{}, state.NewDatabase(db))
   463  
   464  		otaShortAddr = common.FromHex(otaShortAddrs[4])
   465  		otaAX        = otaShortAddr[0 : 1+common.HashLength]
   466  		balanceSet   = big.NewInt(10)
   467  	)
   468  
   469  	otaShortAddrGet, balanceGet, err := GetOTAInfoFromAX(statedb, otaAX)
   470  	if otaShortAddrGet != nil {
   471  		t.Errorf("otaShortAddrGet is not nil.")
   472  	}
   473  
   474  	if balanceGet != nil && balanceGet.Cmp(big.NewInt(0)) != 0 {
   475  		t.Errorf("balance is not 0! balance:%s", balanceGet.String())
   476  	}
   477  
   478  	if err == nil {
   479  		t.Errorf("err is nil!")
   480  	}
   481  
   482  	err = setOTA(statedb, balanceSet, otaShortAddr)
   483  	if err != nil {
   484  		t.Errorf("err:%s", err.Error())
   485  	}
   486  
   487  	otaShortAddrGet, balanceGet, err = GetOTAInfoFromAX(statedb, otaAX)
   488  	if otaShortAddrGet == nil {
   489  		t.Errorf("otaShortAddrGet is nil!")
   490  	}
   491  
   492  	if common.ToHex(otaShortAddrGet) != common.ToHex(otaShortAddr) {
   493  		t.Errorf("otaShortAddrGet:%s, expect:%s", common.ToHex(otaShortAddrGet), common.ToHex(otaShortAddr))
   494  	}
   495  
   496  	if balanceGet == nil {
   497  		t.Errorf("balanceGet is nil!")
   498  	}
   499  
   500  	if balanceSet.Cmp(balanceGet) != 0 {
   501  		t.Errorf("balanceGet:%v, expect:%v", balanceGet, balanceSet)
   502  	}
   503  
   504  }
   505  
   506  //func TestGetOTASet(t *testing.T) {
   507  //	{
   508  //		var (
   509  //			db, _      = ethdb.NewMemDatabase()
   510  //			statedb, _ = state.New(common.Hash{}, state.NewDatabase(db))
   511  //
   512  //			otaWanAddr = common.FromHex(otaShortAddrs[6])
   513  //			otaAX      = otaWanAddr[1 : 1+common.HashLength]
   514  //		)
   515  //
   516  //		setLen := 3
   517  //		_, _, err := GetOTASet(statedb, otaAX, setLen)
   518  //		if err == nil {
   519  //			t.Error("err is nil! expect err: can't find ota address balance!")
   520  //		}
   521  //	}
   522  //
   523  //	{
   524  //		var (
   525  //			db, _      = ethdb.NewMemDatabase()
   526  //			statedb, _ = state.New(common.Hash{}, state.NewDatabase(db))
   527  //
   528  //			otaWanAddr = common.FromHex(otaShortAddrs[6])
   529  //			otaAX      = otaWanAddr[1 : 1+common.HashLength]
   530  //			balanceSet = big.NewInt(10)
   531  //
   532  //			setLen = 3
   533  //		)
   534  //
   535  //		err := SetOtaBalanceToAX(statedb, otaAX, balanceSet)
   536  //		if err != nil {
   537  //			t.Error("set ota balance fail. err:", err.Error())
   538  //		}
   539  //
   540  //		_, _, err = GetOTASet(statedb, otaAX, setLen)
   541  //		if err == nil {
   542  //			t.Error("err is nil! expect err: no ota address exist! balance:10")
   543  //		}
   544  //
   545  //	}
   546  //
   547  //	for i := 0; i < 100; i++ {
   548  //		var (
   549  //			db, _      = ethdb.NewMemDatabase()
   550  //			statedb, _ = state.New(common.Hash{}, state.NewDatabase(db))
   551  //
   552  //			otaWanAddr = common.FromHex(otaShortAddrs[6])
   553  //			otaAX      = otaWanAddr[1 : 1+common.HashLength]
   554  //			balanceSet = big.NewInt(10)
   555  //
   556  //			setLen = 1
   557  //		)
   558  //
   559  //		err := setOTA(statedb, balanceSet, otaWanAddr)
   560  //		if err != nil {
   561  //			t.Error("set ota balance fail. err:", err.Error())
   562  //		}
   563  //
   564  //		otaSet, balanceGet, err := GetOTASet(statedb, otaAX, setLen)
   565  //		if err != nil {
   566  //			t.Error("get ota set fail! err: ", err.Error())
   567  //		}
   568  //
   569  //		if otaSet == nil {
   570  //			t.Error("otaSet is nil")
   571  //		}
   572  //
   573  //		if len(otaSet) != setLen {
   574  //			t.Error("otaSet len wrong! expect:", setLen, ", actual:", len(otaSet))
   575  //		}
   576  //
   577  //		for _, otaGet := range otaSet {
   578  //			if !bytes.Equal(otaGet, otaWanAddr) {
   579  //				t.Error("ota addr in set is wrong! expect:", common.ToHex(otaWanAddr), ", actual:", common.ToHex(otaGet))
   580  //			}
   581  //		}
   582  //
   583  //		if balanceGet == nil {
   584  //			t.Error("balance from GetOTASet is nil! expect:", balanceSet.Uint64())
   585  //		}
   586  //
   587  //		if balanceSet.Cmp(balanceGet) != 0 {
   588  //			t.Error("balance from GetOTASet is nul! expect:", balanceSet.Uint64(), ", actual:", balanceGet.Uint64())
   589  //		}
   590  //
   591  //	}
   592  //
   593  //	for i := 0; i < 100; i++ {
   594  //		var (
   595  //			db, _      = ethdb.NewMemDatabase()
   596  //			statedb, _ = state.New(common.Hash{}, state.NewDatabase(db))
   597  //
   598  //			otaWanAddr = common.FromHex(otaShortAddrs[6])
   599  //			otaAX      = otaWanAddr[1 : 1+common.HashLength]
   600  //			balanceSet = big.NewInt(10)
   601  //
   602  //			setLen = 2
   603  //		)
   604  //
   605  //		err := setOTA(statedb, balanceSet, otaWanAddr)
   606  //		if err != nil {
   607  //			t.Error("set ota balance fail. err:", err.Error())
   608  //		}
   609  //
   610  //		otaSet, balanceGet, err := GetOTASet(statedb, otaAX, setLen)
   611  //		if err != nil {
   612  //			t.Error("get ota set fail! err: ", err.Error())
   613  //		}
   614  //
   615  //		if otaSet == nil {
   616  //			t.Error("otaSet is nil")
   617  //		}
   618  //
   619  //		if len(otaSet) != setLen {
   620  //			t.Error("otaSet len wrong! expect:", setLen, ", actual:", len(otaSet))
   621  //		}
   622  //
   623  //		for _, otaGet := range otaSet {
   624  //			if !bytes.Equal(otaGet, otaWanAddr) {
   625  //				t.Error("ota addr in set is wrong! expect:", common.ToHex(otaWanAddr), ", actual:", common.ToHex(otaGet))
   626  //			}
   627  //		}
   628  //
   629  //		if balanceGet == nil {
   630  //			t.Error("balance from GetOTASet is nil! expect:", balanceSet.Uint64())
   631  //		}
   632  //
   633  //		if balanceSet.Cmp(balanceGet) != 0 {
   634  //			t.Error("balance from GetOTASet is nul! expect:", balanceSet.Uint64(), ", actual:", balanceGet.Uint64())
   635  //		}
   636  //
   637  //	}
   638  //
   639  //	for i := 0; i < 100; i++ {
   640  //		var (
   641  //			db, _      = ethdb.NewMemDatabase()
   642  //			statedb, _ = state.New(common.Hash{}, state.NewDatabase(db))
   643  //
   644  //			otaWanAddr = common.FromHex(otaShortAddrs[6])
   645  //			otaAX      = otaWanAddr[1 : 1+common.HashLength]
   646  //			balanceSet = big.NewInt(10)
   647  //
   648  //			setLen = 3
   649  //		)
   650  //
   651  //		err := setOTA(statedb, balanceSet, otaWanAddr)
   652  //		if err != nil {
   653  //			t.Error("set ota balance fail. err:", err.Error())
   654  //		}
   655  //
   656  //		otaSet, balanceGet, err := GetOTASet(statedb, otaAX, setLen)
   657  //		if err != nil {
   658  //			t.Error("get ota set fail! err: ", err.Error())
   659  //		}
   660  //
   661  //		if otaSet == nil {
   662  //			t.Error("otaSet is nil")
   663  //		}
   664  //
   665  //		if len(otaSet) != setLen {
   666  //			t.Error("otaSet len wrong! expect:", setLen, ", actual:", len(otaSet))
   667  //		}
   668  //
   669  //		for _, otaGet := range otaSet {
   670  //			if !bytes.Equal(otaGet, otaWanAddr) {
   671  //				t.Error("ota addr in set is wrong! expect:", common.ToHex(otaWanAddr), ", actual:", common.ToHex(otaGet))
   672  //			}
   673  //		}
   674  //
   675  //		if balanceGet == nil {
   676  //			t.Error("balance from GetOTASet is nil! expect:", balanceSet.Uint64())
   677  //		}
   678  //
   679  //		if balanceSet.Cmp(balanceGet) != 0 {
   680  //			t.Error("balance from GetOTASet is nul! expect:", balanceSet.Uint64(), ", actual:", balanceGet.Uint64())
   681  //		}
   682  //
   683  //	}
   684  //
   685  //	for i := 0; i < 100; i++ {
   686  //		var (
   687  //			db, _      = ethdb.NewMemDatabase()
   688  //			statedb, _ = state.New(common.Hash{}, state.NewDatabase(db))
   689  //
   690  //			otaWanAddr         = common.FromHex(otaShortAddrs[6])
   691  //			otaMixSetAddrBytes = make([][]byte, 0, 100)
   692  //			otaAX              = otaWanAddr[1 : 1+common.HashLength]
   693  //			balanceSet         = big.NewInt(10)
   694  //
   695  //			setLen = 1
   696  //		)
   697  //
   698  //		for _, otaWanAddr := range otaMixSetAddrs {
   699  //			otaMixSetAddrBytes = append(otaMixSetAddrBytes, common.FromHex(otaWanAddr))
   700  //		}
   701  //
   702  //		err := setOTA(statedb, balanceSet, otaWanAddr)
   703  //		if err != nil {
   704  //			t.Error("set ota balance fail. err:", err.Error())
   705  //		}
   706  //
   707  //		err = setOTA(statedb, balanceSet, otaMixSetAddrBytes[0])
   708  //		if err != nil {
   709  //			t.Error("set ota balance fail. err:", err.Error())
   710  //		}
   711  //
   712  //		otaSet, balanceGet, err := GetOTASet(statedb, otaAX, setLen)
   713  //		if err != nil {
   714  //			t.Error("get ota set fail! err: ", err.Error())
   715  //		}
   716  //
   717  //		if otaSet == nil {
   718  //			t.Error("otaSet is nil")
   719  //		}
   720  //
   721  //		if len(otaSet) != setLen {
   722  //			t.Error("otaSet len wrong! expect:", setLen, ", actual:", len(otaSet))
   723  //		}
   724  //
   725  //		for _, otaGet := range otaSet {
   726  //			if !bytes.Equal(otaGet, otaMixSetAddrBytes[0]) {
   727  //				t.Error("ota addr in set is wrong! expect:", common.ToHex(otaMixSetAddrBytes[0]), ", actual:", common.ToHex(otaGet))
   728  //			}
   729  //		}
   730  //
   731  //		if balanceGet == nil {
   732  //			t.Error("balance from GetOTASet is nil! expect:", balanceSet.Uint64())
   733  //		}
   734  //
   735  //		if balanceSet.Cmp(balanceGet) != 0 {
   736  //			t.Error("balance from GetOTASet is nul! expect:", balanceSet.Uint64(), ", actual:", balanceGet.Uint64())
   737  //		}
   738  //
   739  //	}
   740  //
   741  //	for i := 0; i < 30; i++ {
   742  //		var (
   743  //			db, _      = ethdb.NewMemDatabase()
   744  //			statedb, _ = state.New(common.Hash{}, state.NewDatabase(db))
   745  //
   746  //			otaWanAddr         = common.FromHex(otaShortAddrs[6])
   747  //			otaMixSetAddrBytes = make([][]byte, 0, 100)
   748  //			otaAX              = otaWanAddr[1 : 1+common.HashLength]
   749  //			balanceSet         = big.NewInt(10)
   750  //
   751  //			setLen = 2
   752  //		)
   753  //
   754  //		for _, otaWanAddr := range otaMixSetAddrs {
   755  //			otaMixSetAddrBytes = append(otaMixSetAddrBytes, common.FromHex(otaWanAddr))
   756  //		}
   757  //
   758  //		err := setOTA(statedb, balanceSet, otaWanAddr)
   759  //		if err != nil {
   760  //			t.Error("set ota balance fail. err:", err.Error())
   761  //		}
   762  //
   763  //		err = setOTA(statedb, balanceSet, otaMixSetAddrBytes[0])
   764  //		if err != nil {
   765  //			t.Error("set ota balance fail. err:", err.Error())
   766  //		}
   767  //
   768  //		otaSet, balanceGet, err := GetOTASet(statedb, otaAX, setLen)
   769  //		if err != nil {
   770  //			t.Error("get ota set fail! err: ", err.Error())
   771  //		}
   772  //
   773  //		if otaSet == nil {
   774  //			t.Error("otaSet is nil")
   775  //		}
   776  //
   777  //		if len(otaSet) != setLen {
   778  //			t.Error("otaSet len wrong! expect:", setLen, ", actual:", len(otaSet))
   779  //		}
   780  //
   781  //		var otaGetAX [common.HashLength]byte
   782  //		otaAXMap := make(map[[common.HashLength]byte]bool)
   783  //		for _, otaGet := range otaSet {
   784  //			AXGet, _ := GetAXFromWanAddr(otaGet)
   785  //			copy(otaGetAX[:], AXGet)
   786  //			otaAXMap[otaGetAX] = true
   787  //		}
   788  //
   789  //		if len(otaAXMap) != 2 {
   790  //			t.Error("otaSet's non repeating ele is wrong. expect: ", setLen, ", actual:", len(otaAXMap))
   791  //		}
   792  //
   793  //		copy(otaGetAX[:], otaAX)
   794  //		_, ok := otaAXMap[otaGetAX]
   795  //		if !ok {
   796  //			t.Error("otaSet wrong, don't contain self!")
   797  //		}
   798  //
   799  //		if balanceGet == nil {
   800  //			t.Error("balance from GetOTASet is nil! expect:", balanceSet.Uint64())
   801  //		}
   802  //
   803  //		if balanceSet.Cmp(balanceGet) != 0 {
   804  //			t.Error("balance from GetOTASet is nul! expect:", balanceSet.Uint64(), ", actual:", balanceGet.Uint64())
   805  //		}
   806  //
   807  //	}
   808  //
   809  //	for i := 0; i < 30; i++ {
   810  //		var (
   811  //			db, _      = ethdb.NewMemDatabase()
   812  //			statedb, _ = state.New(common.Hash{}, state.NewDatabase(db))
   813  //
   814  //			otaWanAddr         = common.FromHex(otaShortAddrs[6])
   815  //			otaMixSetAddrBytes = make([][]byte, 0, 100)
   816  //			otaAX              = otaWanAddr[1 : 1+common.HashLength]
   817  //			balanceSet         = big.NewInt(10)
   818  //
   819  //			setLen = 3
   820  //		)
   821  //
   822  //		for _, otaWanAddr := range otaMixSetAddrs {
   823  //			otaMixSetAddrBytes = append(otaMixSetAddrBytes, common.FromHex(otaWanAddr))
   824  //		}
   825  //
   826  //		err := setOTA(statedb, balanceSet, otaWanAddr)
   827  //		if err != nil {
   828  //			t.Error("set ota balance fail. err:", err.Error())
   829  //		}
   830  //
   831  //		err = setOTA(statedb, balanceSet, otaMixSetAddrBytes[0])
   832  //		if err != nil {
   833  //			t.Error("set ota balance fail. err:", err.Error())
   834  //		}
   835  //
   836  //		otaSet, balanceGet, err := GetOTASet(statedb, otaAX, setLen)
   837  //		if err != nil {
   838  //			t.Error("get ota set fail! err: ", err.Error())
   839  //		}
   840  //
   841  //		if otaSet == nil {
   842  //			t.Error("otaSet is nil")
   843  //		}
   844  //
   845  //		if len(otaSet) != setLen {
   846  //			t.Error("otaSet len wrong! expect:", setLen, ", actual:", len(otaSet))
   847  //		}
   848  //
   849  //		var otaGetAX [common.HashLength]byte
   850  //		otaAXMap := make(map[[common.HashLength]byte]bool)
   851  //		for _, otaGet := range otaSet {
   852  //			AXGet, _ := GetAXFromWanAddr(otaGet)
   853  //			copy(otaGetAX[:], AXGet)
   854  //			otaAXMap[otaGetAX] = true
   855  //		}
   856  //
   857  //		if len(otaAXMap) != 2 {
   858  //			t.Error("otaSet's non repeating ele is wrong. expect: ", setLen, ", actual:", len(otaAXMap))
   859  //		}
   860  //
   861  //		copy(otaGetAX[:], otaAX)
   862  //		_, ok := otaAXMap[otaGetAX]
   863  //		if !ok {
   864  //			t.Error("otaSet wrong, don't contain self!")
   865  //		}
   866  //
   867  //		if balanceGet == nil {
   868  //			t.Error("balance from GetOTASet is nil! expect:", balanceSet.Uint64())
   869  //		}
   870  //
   871  //		if balanceSet.Cmp(balanceGet) != 0 {
   872  //			t.Error("balance from GetOTASet is nul! expect:", balanceSet.Uint64(), ", actual:", balanceGet.Uint64())
   873  //		}
   874  //
   875  //	}
   876  //
   877  //	for i := 0; i < 30; i++ {
   878  //		var (
   879  //			db, _      = ethdb.NewMemDatabase()
   880  //			statedb, _ = state.New(common.Hash{}, state.NewDatabase(db))
   881  //
   882  //			otaWanAddr         = common.FromHex(otaShortAddrs[6])
   883  //			otaMixSetAddrBytes = make([][]byte, 0, 100)
   884  //			otaAX              = otaWanAddr[1 : 1+common.HashLength]
   885  //			balanceSet         = big.NewInt(10)
   886  //
   887  //			setLen = 10
   888  //		)
   889  //
   890  //		for _, otaWanAddr := range otaMixSetAddrs {
   891  //			otaMixSetAddrBytes = append(otaMixSetAddrBytes, common.FromHex(otaWanAddr))
   892  //		}
   893  //
   894  //		err := setOTA(statedb, balanceSet, otaWanAddr)
   895  //		if err != nil {
   896  //			t.Error("set ota balance fail. err:", err.Error())
   897  //		}
   898  //
   899  //		err = setOTA(statedb, balanceSet, otaMixSetAddrBytes[0])
   900  //		if err != nil {
   901  //			t.Error("set ota balance fail. err:", err.Error())
   902  //		}
   903  //
   904  //		otaSet, balanceGet, err := GetOTASet(statedb, otaAX, setLen)
   905  //		if err != nil {
   906  //			t.Error("get ota set fail! err: ", err.Error())
   907  //		}
   908  //
   909  //		if otaSet == nil {
   910  //			t.Error("otaSet is nil")
   911  //		}
   912  //
   913  //		if len(otaSet) != setLen {
   914  //			t.Error("otaSet len wrong! expect:", setLen, ", actual:", len(otaSet))
   915  //		}
   916  //
   917  //		var otaGetAX [common.HashLength]byte
   918  //		otaAXMap := make(map[[common.HashLength]byte]bool)
   919  //		for _, otaGet := range otaSet {
   920  //			AXGet, _ := GetAXFromWanAddr(otaGet)
   921  //			copy(otaGetAX[:], AXGet)
   922  //			otaAXMap[otaGetAX] = true
   923  //		}
   924  //
   925  //		if len(otaAXMap) != 2 {
   926  //			t.Error("otaSet's non repeating ele is wrong. expect: ", setLen, ", actual:", len(otaAXMap))
   927  //		}
   928  //
   929  //		copy(otaGetAX[:], otaAX)
   930  //		_, ok := otaAXMap[otaGetAX]
   931  //		if !ok {
   932  //			t.Error("otaSet wrong, don't contain self!")
   933  //		}
   934  //
   935  //		if balanceGet == nil {
   936  //			t.Error("balance from GetOTASet is nil! expect:", balanceSet.Uint64())
   937  //		}
   938  //
   939  //		if balanceSet.Cmp(balanceGet) != 0 {
   940  //			t.Error("balance from GetOTASet is nul! expect:", balanceSet.Uint64(), ", actual:", balanceGet.Uint64())
   941  //		}
   942  //
   943  //	}
   944  //
   945  //	for i := 0; i < 30; i++ {
   946  //		var (
   947  //			db, _      = ethdb.NewMemDatabase()
   948  //			statedb, _ = state.New(common.Hash{}, state.NewDatabase(db))
   949  //
   950  //			otaWanAddr         = common.FromHex(otaShortAddrs[6])
   951  //			otaMixSetAddrBytes = make([][]byte, 0, 100)
   952  //			otaAX              = otaWanAddr[1 : 1+common.HashLength]
   953  //			balanceSet         = big.NewInt(10)
   954  //
   955  //			setLen = 10
   956  //		)
   957  //
   958  //		for _, otaWanAddr := range otaMixSetAddrs {
   959  //			otaMixSetAddrBytes = append(otaMixSetAddrBytes, common.FromHex(otaWanAddr))
   960  //		}
   961  //
   962  //		err := setOTA(statedb, balanceSet, otaWanAddr)
   963  //		if err != nil {
   964  //			t.Error("set ota balance fail. err:", err.Error())
   965  //		}
   966  //
   967  //		for _, addrByte := range otaMixSetAddrBytes {
   968  //			err = setOTA(statedb, balanceSet, addrByte)
   969  //			if err != nil {
   970  //				t.Error("set ota balance fail. err:", err.Error())
   971  //			}
   972  //		}
   973  //
   974  //		otaSet, balanceGet, err := GetOTASet(statedb, otaAX, setLen)
   975  //		if err != nil {
   976  //			t.Error("get ota set fail! err: ", err.Error())
   977  //		}
   978  //
   979  //		if otaSet == nil {
   980  //			t.Error("otaSet is nil")
   981  //		}
   982  //
   983  //		if len(otaSet) != setLen {
   984  //			t.Error("otaSet len wrong! expect:", setLen, ", actual:", len(otaSet))
   985  //		}
   986  //
   987  //		var otaGetAX [common.HashLength]byte
   988  //		otaAXMap := make(map[[common.HashLength]byte]bool)
   989  //		for _, otaGet := range otaSet {
   990  //			AXGet, _ := GetAXFromWanAddr(otaGet)
   991  //			copy(otaGetAX[:], AXGet)
   992  //			otaAXMap[otaGetAX] = true
   993  //		}
   994  //
   995  //		if len(otaAXMap) != setLen {
   996  //			t.Error("otaSet's non repeating ele is wrong. expect: ", setLen, ", actual:", len(otaAXMap))
   997  //		}
   998  //
   999  //		copy(otaGetAX[:], otaAX)
  1000  //		_, ok := otaAXMap[otaGetAX]
  1001  //		if ok {
  1002  //			t.Error("otaSet wrong, contain self!")
  1003  //		}
  1004  //
  1005  //		if balanceGet == nil {
  1006  //			t.Error("balance from GetOTASet is nil! expect:", balanceSet.Uint64())
  1007  //		}
  1008  //
  1009  //		if balanceSet.Cmp(balanceGet) != 0 {
  1010  //			t.Error("balance from GetOTASet is nul! expect:", balanceSet.Uint64(), ", actual:", balanceGet.Uint64())
  1011  //		}
  1012  //
  1013  //	}
  1014  //
  1015  //	{
  1016  //		var (
  1017  //			db, _      = ethdb.NewMemDatabase()
  1018  //			statedb, _ = state.New(common.Hash{}, state.NewDatabase(db))
  1019  //
  1020  //			otaWanAddr         = common.FromHex(otaShortAddrs[6])
  1021  //			otaMixSetAddrBytes = make([][]byte, 0, 100)
  1022  //			otaAX              = otaWanAddr[1 : 1+common.HashLength]
  1023  //			balanceSet         = big.NewInt(10)
  1024  //		)
  1025  //
  1026  //		for _, otaWanAddr := range otaMixSetAddrs {
  1027  //			otaMixSetAddrBytes = append(otaMixSetAddrBytes, common.FromHex(otaWanAddr))
  1028  //		}
  1029  //
  1030  //		setLen := 3
  1031  //		otaShortAddrBytesGet, balanceGet, err := GetOTASet(statedb, otaAX, setLen)
  1032  //		if err == nil {
  1033  //			t.Errorf("err is nil!")
  1034  //		}
  1035  //
  1036  //		if otaShortAddrBytesGet != nil {
  1037  //			t.Errorf("otaShortAddrBytesGet is not nil!")
  1038  //		}
  1039  //
  1040  //		if balanceGet != nil && balanceGet.Cmp(big.NewInt(0)) != 0 {
  1041  //			t.Errorf("balanceGet is not 0! balanceGet:%s", balanceGet.String())
  1042  //		}
  1043  //
  1044  //		err = setOTA(statedb, balanceSet, otaWanAddr)
  1045  //		if err != nil {
  1046  //			t.Errorf("err:%s", err.Error())
  1047  //		}
  1048  //
  1049  //		for _, otaShortAddrTmp := range otaMixSetAddrBytes {
  1050  //			err = setOTA(statedb, balanceSet, otaShortAddrTmp)
  1051  //			if err != nil {
  1052  //				t.Errorf("err:%s", err.Error())
  1053  //			}
  1054  //		}
  1055  //
  1056  //		// mem database Iterator doesnt work. unit test alwayse fail!!
  1057  //		otaShortAddrBytesGet, balanceGet, err = GetOTASet(statedb, otaAX, setLen)
  1058  //		if err != nil {
  1059  //			t.Errorf("err:%s", err.Error())
  1060  //		}
  1061  //
  1062  //		if otaShortAddrBytesGet == nil {
  1063  //			t.Errorf("otaShortAddrBytesGet is nil!")
  1064  //		}
  1065  //
  1066  //		if len(otaShortAddrBytesGet) != setLen {
  1067  //			t.Errorf("otaShortAddrBytesGet len is wrong! len:%d, expect:%d", len(otaShortAddrBytesGet), setLen)
  1068  //		}
  1069  //
  1070  //		for _, otaShortAddrGet := range otaShortAddrBytesGet {
  1071  //			otaAXGet := otaShortAddrGet[1 : 1+common.HashLength]
  1072  //			otaShortAddrReGet, balanceReGet, err := GetOTAInfoFromAX(statedb, otaAXGet)
  1073  //			if err != nil {
  1074  //				t.Errorf("err:%s", err.Error())
  1075  //			}
  1076  //
  1077  //			if common.ToHex(otaShortAddrReGet) != common.ToHex(otaShortAddrGet) {
  1078  //				t.Errorf("otaShortAddrReGet:%s, expect:%s", common.ToHex(otaShortAddrReGet), common.ToHex(otaShortAddrGet))
  1079  //			}
  1080  //
  1081  //			if balanceReGet == nil {
  1082  //				t.Errorf("balanceReGet is nil!")
  1083  //			}
  1084  //
  1085  //			if balanceReGet.Cmp(balanceSet) != 0 {
  1086  //				t.Errorf("balanceReGet:%s, expect:%s", balanceReGet.String(), balanceSet.String())
  1087  //			}
  1088  //		}
  1089  //
  1090  //	}
  1091  //}
  1092  
  1093  func TestGetOTASet(t *testing.T) {
  1094  	{
  1095  		var (
  1096  			db, _      = ethdb.NewMemDatabase()
  1097  			statedb, _ = state.New(common.Hash{}, state.NewDatabase(db))
  1098  
  1099  			otaWanAddr = common.FromHex(otaShortAddrs[6])
  1100  			otaAX      = otaWanAddr[1 : 1+common.HashLength]
  1101  		)
  1102  
  1103  		setLen := 3
  1104  		_, _, err := GetOTASet(statedb, otaAX, setLen)
  1105  		expectErr := "can't find ota address balance!"
  1106  		if err.Error() != expectErr {
  1107  			t.Error("err is nil! expect err: ", expectErr)
  1108  		}
  1109  	}
  1110  
  1111  	{
  1112  		var (
  1113  			db, _      = ethdb.NewMemDatabase()
  1114  			statedb, _ = state.New(common.Hash{}, state.NewDatabase(db))
  1115  
  1116  			otaWanAddr = common.FromHex(otaShortAddrs[6])
  1117  			otaAX      = otaWanAddr[1 : 1+common.HashLength]
  1118  			balanceSet = big.NewInt(10)
  1119  
  1120  			setLen = 3
  1121  		)
  1122  
  1123  		err := SetOtaBalanceToAX(statedb, otaAX, balanceSet)
  1124  		if err != nil {
  1125  			t.Error("set ota balance fail. err:", err.Error())
  1126  		}
  1127  
  1128  		_, _, err = GetOTASet(statedb, otaAX, setLen)
  1129  		expectErr := "no ota exist! balance:10"
  1130  		if err.Error() != expectErr {
  1131  			t.Error("err is nil! expect err: no ota exist! balance:10")
  1132  		}
  1133  	}
  1134  
  1135  	{
  1136  		var (
  1137  			db, _      = ethdb.NewMemDatabase()
  1138  			statedb, _ = state.New(common.Hash{}, state.NewDatabase(db))
  1139  
  1140  			otaWanAddr = common.FromHex(otaShortAddrs[6])
  1141  			otaAX      = otaWanAddr[1 : 1+common.HashLength]
  1142  			balanceSet = big.NewInt(10)
  1143  
  1144  			setLen = 1
  1145  		)
  1146  
  1147  		err := setOTA(statedb, balanceSet, otaWanAddr)
  1148  		if err != nil {
  1149  			t.Error("set ota balance fail. err:", err.Error())
  1150  		}
  1151  
  1152  		_, _, err = GetOTASet(statedb, otaAX, setLen)
  1153  		expectErr := "too more required ota number! balance:10, exist count:1"
  1154  		if err.Error() != expectErr {
  1155  			t.Error("get ota set fail! err: ", err.Error(), ", expected:", expectErr)
  1156  		}
  1157  	}
  1158  
  1159  	for i := 0; i < 100; i++ {
  1160  		var (
  1161  			db, _      = ethdb.NewMemDatabase()
  1162  			statedb, _ = state.New(common.Hash{}, state.NewDatabase(db))
  1163  
  1164  			otaWanAddr = common.FromHex(otaShortAddrs[6])
  1165  			otaAX      = otaWanAddr[1 : 1+common.HashLength]
  1166  			balanceSet = big.NewInt(10)
  1167  
  1168  			setLen = 1
  1169  		)
  1170  
  1171  		err := setOTA(statedb, balanceSet, otaWanAddr)
  1172  		err = setOTA(statedb, balanceSet, common.FromHex(otaShortAddrs[7]))
  1173  
  1174  		otaSet, balanceGet, err := GetOTASet(statedb, otaAX, setLen)
  1175  		if err != nil {
  1176  			t.Error("get ota set fail! err: ", err.Error())
  1177  		}
  1178  
  1179  		if otaSet == nil {
  1180  			t.Error("otaSet is nil")
  1181  		}
  1182  
  1183  		if len(otaSet) != setLen {
  1184  			t.Error("otaSet len wrong! expect:", setLen, ", actual:", len(otaSet))
  1185  		}
  1186  
  1187  		if !bytes.Equal(otaSet[0], common.FromHex(otaShortAddrs[7])) {
  1188  			t.Error("otaSet value wrong!, contain unexpected ota")
  1189  		}
  1190  
  1191  		if balanceGet == nil {
  1192  			t.Error("balance from GetOTASet is nil! expect:", balanceSet.Uint64())
  1193  		}
  1194  
  1195  		if balanceSet.Cmp(balanceGet) != 0 {
  1196  			t.Error("balance from GetOTASet is nul! expect:", balanceSet.Uint64(), ", actual:", balanceGet.Uint64())
  1197  		}
  1198  	}
  1199  
  1200  	for i := 0; i < 10; i++ {
  1201  		var (
  1202  			db, _      = ethdb.NewMemDatabase()
  1203  			statedb, _ = state.New(common.Hash{}, state.NewDatabase(db))
  1204  
  1205  			otaWanAddr = common.FromHex(otaShortAddrs[6])
  1206  			otaAX      = otaWanAddr[1 : 1+common.HashLength]
  1207  			balanceSet = big.NewInt(10)
  1208  
  1209  			setLen = 2
  1210  		)
  1211  
  1212  		err := setOTA(statedb, balanceSet, otaWanAddr)
  1213  		err = setOTA(statedb, balanceSet, common.FromHex(otaShortAddrs[7]))
  1214  
  1215  		_, _, err = GetOTASet(statedb, otaAX, setLen)
  1216  		expectErr := "too more required ota number! balance:10, exist count:2"
  1217  		if err.Error() != expectErr {
  1218  			t.Error("get ota set fail! err: ", err.Error(), ", expected:", expectErr)
  1219  		}
  1220  	}
  1221  
  1222  	for i := 0; i < 100; i++ {
  1223  		var (
  1224  			db, _      = ethdb.NewMemDatabase()
  1225  			statedb, _ = state.New(common.Hash{}, state.NewDatabase(db))
  1226  
  1227  			otaWanAddr = common.FromHex(otaShortAddrs[6])
  1228  			otaAX      = otaWanAddr[1 : 1+common.HashLength]
  1229  			balanceSet = big.NewInt(10)
  1230  
  1231  			setLen = 1
  1232  		)
  1233  
  1234  		err := setOTA(statedb, balanceSet, otaWanAddr)
  1235  		err = setOTA(statedb, balanceSet, common.FromHex(otaShortAddrs[7]))
  1236  		err = setOTA(statedb, balanceSet, common.FromHex(otaShortAddrs[8]))
  1237  
  1238  		otaSet, balanceGet, err := GetOTASet(statedb, otaAX, setLen)
  1239  		if err != nil {
  1240  			t.Error("get ota set fail! err: ", err.Error())
  1241  		}
  1242  
  1243  		if otaSet == nil {
  1244  			t.Error("otaSet is nil")
  1245  		}
  1246  
  1247  		if len(otaSet) != setLen {
  1248  			t.Error("otaSet len wrong! expect:", setLen, ", actual:", len(otaSet))
  1249  		}
  1250  
  1251  		if bytes.Equal(otaSet[0], otaWanAddr) {
  1252  			t.Error("otaSet value wrong!, contain unexpected ota")
  1253  		}
  1254  
  1255  		if balanceGet == nil {
  1256  			t.Error("balance from GetOTASet is nil! expect:", balanceSet.Uint64())
  1257  		}
  1258  
  1259  		if balanceSet.Cmp(balanceGet) != 0 {
  1260  			t.Error("balance from GetOTASet is nul! expect:", balanceSet.Uint64(), ", actual:", balanceGet.Uint64())
  1261  		}
  1262  	}
  1263  
  1264  	for i := 0; i < 100; i++ {
  1265  		var (
  1266  			db, _      = ethdb.NewMemDatabase()
  1267  			statedb, _ = state.New(common.Hash{}, state.NewDatabase(db))
  1268  
  1269  			otaWanAddr = common.FromHex(otaShortAddrs[6])
  1270  			otaAX      = otaWanAddr[1 : 1+common.HashLength]
  1271  			balanceSet = big.NewInt(10)
  1272  
  1273  			setLen = 2
  1274  		)
  1275  
  1276  		err := setOTA(statedb, balanceSet, otaWanAddr)
  1277  		err = setOTA(statedb, balanceSet, common.FromHex(otaShortAddrs[7]))
  1278  		err = setOTA(statedb, balanceSet, common.FromHex(otaShortAddrs[8]))
  1279  
  1280  		otaSet, balanceGet, err := GetOTASet(statedb, otaAX, setLen)
  1281  		if err != nil {
  1282  			t.Error("get ota set fail! err: ", err.Error())
  1283  		}
  1284  
  1285  		if otaSet == nil {
  1286  			t.Error("otaSet is nil")
  1287  		}
  1288  
  1289  		if len(otaSet) != setLen {
  1290  			t.Error("otaSet len wrong! expect:", setLen, ", actual:", len(otaSet))
  1291  		}
  1292  
  1293  		var otaGetAX [common.HashLength]byte
  1294  		otaAXMap := make(map[[common.HashLength]byte]bool)
  1295  		for _, otaGet := range otaSet {
  1296  			if bytes.Equal(otaGet, otaWanAddr) {
  1297  				t.Error("otaSet value wrong!, contain unexpected ota")
  1298  			}
  1299  
  1300  			AXGet, _ := GetAXFromWanAddr(otaGet)
  1301  			copy(otaGetAX[:], AXGet)
  1302  			otaAXMap[otaGetAX] = true
  1303  		}
  1304  
  1305  		if len(otaAXMap) != setLen {
  1306  			t.Error("otaSet's non repeating ele is wrong. expect: ", setLen, ", actual:", len(otaAXMap))
  1307  		}
  1308  
  1309  		if balanceGet == nil {
  1310  			t.Error("balance from GetOTASet is nil! expect:", balanceSet.Uint64())
  1311  		}
  1312  
  1313  		if balanceSet.Cmp(balanceGet) != 0 {
  1314  			t.Error("balance from GetOTASet is nul! expect:", balanceSet.Uint64(), ", actual:", balanceGet.Uint64())
  1315  		}
  1316  	}
  1317  
  1318  	for i := 0; i < 10; i++ {
  1319  		var (
  1320  			db, _      = ethdb.NewMemDatabase()
  1321  			statedb, _ = state.New(common.Hash{}, state.NewDatabase(db))
  1322  
  1323  			otaWanAddr = common.FromHex(otaShortAddrs[6])
  1324  			otaAX      = otaWanAddr[1 : 1+common.HashLength]
  1325  			balanceSet = big.NewInt(10)
  1326  
  1327  			setLen = 3
  1328  		)
  1329  
  1330  		err := setOTA(statedb, balanceSet, otaWanAddr)
  1331  		err = setOTA(statedb, balanceSet, common.FromHex(otaShortAddrs[7]))
  1332  		err = setOTA(statedb, balanceSet, common.FromHex(otaShortAddrs[8]))
  1333  
  1334  		_, _, err = GetOTASet(statedb, otaAX, setLen)
  1335  		expectErr := "too more required ota number! balance:10, exist count:3"
  1336  		if err.Error() != expectErr {
  1337  			t.Error("get ota set fail! err: ", err.Error(), ", expected:", expectErr)
  1338  		}
  1339  	}
  1340  
  1341  	for i := 0; i < 10; i++ {
  1342  		var (
  1343  			db, _      = ethdb.NewMemDatabase()
  1344  			statedb, _ = state.New(common.Hash{}, state.NewDatabase(db))
  1345  
  1346  			otaWanAddr = common.FromHex(otaShortAddrs[6])
  1347  			otaAX      = otaWanAddr[1 : 1+common.HashLength]
  1348  			balanceSet = big.NewInt(10)
  1349  
  1350  			setLen = 4
  1351  		)
  1352  
  1353  		err := setOTA(statedb, balanceSet, otaWanAddr)
  1354  		err = setOTA(statedb, balanceSet, common.FromHex(otaShortAddrs[7]))
  1355  		err = setOTA(statedb, balanceSet, common.FromHex(otaShortAddrs[8]))
  1356  
  1357  		_, _, err = GetOTASet(statedb, otaAX, setLen)
  1358  		expectErr := "too more required ota number! balance:10, exist count:3"
  1359  		if err.Error() != expectErr {
  1360  			t.Error("get ota set fail! err: ", err.Error(), ", expected:", expectErr)
  1361  		}
  1362  	}
  1363  
  1364  	for i := 0; i < 100; i++ {
  1365  		var (
  1366  			db, _      = ethdb.NewMemDatabase()
  1367  			statedb, _ = state.New(common.Hash{}, state.NewDatabase(db))
  1368  
  1369  			otaWanAddr         = common.FromHex(otaShortAddrs[6])
  1370  			otaMixSetAddrBytes = make([][]byte, 0, 100)
  1371  			otaAX              = otaWanAddr[1 : 1+common.HashLength]
  1372  			balanceSet         = big.NewInt(10)
  1373  
  1374  			setLen = 10
  1375  		)
  1376  
  1377  		for _, otaWanAddr := range otaMixSetAddrs {
  1378  			otaMixSetAddrBytes = append(otaMixSetAddrBytes, common.FromHex(otaWanAddr))
  1379  		}
  1380  
  1381  		err := setOTA(statedb, balanceSet, otaWanAddr)
  1382  		if err != nil {
  1383  			t.Error("set ota balance fail. err:", err.Error())
  1384  		}
  1385  
  1386  		for _, addrByte := range otaMixSetAddrBytes {
  1387  			err = setOTA(statedb, balanceSet, addrByte)
  1388  			if err != nil {
  1389  				t.Error("set ota balance fail. err:", err.Error())
  1390  			}
  1391  		}
  1392  
  1393  		otaSet, balanceGet, err := GetOTASet(statedb, otaAX, setLen)
  1394  		if err != nil {
  1395  			t.Error("get ota set fail! err: ", err.Error())
  1396  		}
  1397  
  1398  		if otaSet == nil {
  1399  			t.Error("otaSet is nil")
  1400  		}
  1401  
  1402  		if len(otaSet) != setLen {
  1403  			t.Error("otaSet len wrong! expect:", setLen, ", actual:", len(otaSet))
  1404  		}
  1405  
  1406  		var otaGetAX [common.HashLength]byte
  1407  		otaAXMap := make(map[[common.HashLength]byte]bool)
  1408  		for _, otaGet := range otaSet {
  1409  			AXGet, _ := GetAXFromWanAddr(otaGet)
  1410  			copy(otaGetAX[:], AXGet)
  1411  			otaAXMap[otaGetAX] = true
  1412  		}
  1413  
  1414  		if len(otaAXMap) != setLen {
  1415  			t.Error("otaSet's non repeating ele is wrong. expect: ", setLen, ", actual:", len(otaAXMap))
  1416  		}
  1417  
  1418  		copy(otaGetAX[:], otaAX)
  1419  		_, ok := otaAXMap[otaGetAX]
  1420  		if ok {
  1421  			t.Error("otaSet wrong, contain self!")
  1422  		}
  1423  
  1424  		if balanceGet == nil {
  1425  			t.Error("balance from GetOTASet is nil! expect:", balanceSet.Uint64())
  1426  		}
  1427  
  1428  		if balanceSet.Cmp(balanceGet) != 0 {
  1429  			t.Error("balance from GetOTASet is nul! expect:", balanceSet.Uint64(), ", actual:", balanceGet.Uint64())
  1430  		}
  1431  	}
  1432  
  1433  	for i := 0; i < 30; i++ {
  1434  		var (
  1435  			db, _      = ethdb.NewMemDatabase()
  1436  			statedb, _ = state.New(common.Hash{}, state.NewDatabase(db))
  1437  
  1438  			otaWanAddr         = common.FromHex(otaShortAddrs[6])
  1439  			otaMixSetAddrBytes = make([][]byte, 0, 100)
  1440  			otaAX              = otaWanAddr[1 : 1+common.HashLength]
  1441  			balanceSet         = big.NewInt(10)
  1442  		)
  1443  
  1444  		for _, otaWanAddr := range otaMixSetAddrs {
  1445  			otaMixSetAddrBytes = append(otaMixSetAddrBytes, common.FromHex(otaWanAddr))
  1446  		}
  1447  
  1448  		setLen := 3
  1449  		otaShortAddrBytesGet, balanceGet, err := GetOTASet(statedb, otaAX, setLen)
  1450  		expectErr := "can't find ota address balance!"
  1451  		if err.Error() != expectErr {
  1452  			t.Error("err is nil! expect err: ", expectErr)
  1453  		}
  1454  
  1455  		if otaShortAddrBytesGet != nil {
  1456  			t.Errorf("otaShortAddrBytesGet is not nil!")
  1457  		}
  1458  
  1459  		if balanceGet != nil && balanceGet.Cmp(big.NewInt(0)) != 0 {
  1460  			t.Errorf("balanceGet is not 0! balanceGet:%s", balanceGet.String())
  1461  		}
  1462  
  1463  		err = setOTA(statedb, balanceSet, otaWanAddr)
  1464  		if err != nil {
  1465  			t.Errorf("err:%s", err.Error())
  1466  		}
  1467  
  1468  		for _, otaShortAddrTmp := range otaMixSetAddrBytes {
  1469  			err = setOTA(statedb, balanceSet, otaShortAddrTmp)
  1470  			if err != nil {
  1471  				t.Errorf("err:%s", err.Error())
  1472  			}
  1473  		}
  1474  
  1475  		// mem database Iterator doesnt work. unit test alwayse fail!!
  1476  		otaShortAddrBytesGet, balanceGet, err = GetOTASet(statedb, otaAX, setLen)
  1477  		if err != nil {
  1478  			t.Errorf("err:%s", err.Error())
  1479  		}
  1480  
  1481  		if otaShortAddrBytesGet == nil {
  1482  			t.Errorf("otaShortAddrBytesGet is nil!")
  1483  		}
  1484  
  1485  		if len(otaShortAddrBytesGet) != setLen {
  1486  			t.Errorf("otaShortAddrBytesGet len is wrong! len:%d, expect:%d", len(otaShortAddrBytesGet), setLen)
  1487  		}
  1488  
  1489  		var otaGetAX [common.HashLength]byte
  1490  		otaAXMap := make(map[[common.HashLength]byte]bool)
  1491  		for _, otaGet := range otaShortAddrBytesGet {
  1492  			AXGet, _ := GetAXFromWanAddr(otaGet)
  1493  			copy(otaGetAX[:], AXGet)
  1494  			otaAXMap[otaGetAX] = true
  1495  		}
  1496  
  1497  		if len(otaAXMap) != setLen {
  1498  			t.Error("otaSet's non repeating ele is wrong. expect: ", setLen, ", actual:", len(otaAXMap))
  1499  		}
  1500  
  1501  		copy(otaGetAX[:], otaAX)
  1502  		_, ok := otaAXMap[otaGetAX]
  1503  		if ok {
  1504  			t.Error("otaSet wrong, contain self!")
  1505  		}
  1506  
  1507  		if balanceGet == nil {
  1508  			t.Error("balance from GetOTASet is nil! expect:", balanceSet.Uint64())
  1509  		}
  1510  
  1511  		if balanceSet.Cmp(balanceGet) != 0 {
  1512  			t.Error("balance from GetOTASet is nul! expect:", balanceSet.Uint64(), ", actual:", balanceGet.Uint64())
  1513  		}
  1514  
  1515  		for _, otaShortAddrGet := range otaShortAddrBytesGet {
  1516  			otaAXGet := otaShortAddrGet[1 : 1+common.HashLength]
  1517  			otaShortAddrReGet, balanceReGet, err := GetOTAInfoFromAX(statedb, otaAXGet)
  1518  			if err != nil {
  1519  				t.Errorf("err:%s", err.Error())
  1520  			}
  1521  
  1522  			if !bytes.Equal(otaShortAddrReGet, otaShortAddrGet) {
  1523  				t.Errorf("otaShortAddrReGet:%s, expect:%s", common.ToHex(otaShortAddrReGet), common.ToHex(otaShortAddrGet))
  1524  			}
  1525  
  1526  			if balanceReGet == nil {
  1527  				t.Errorf("balanceReGet is nil!")
  1528  			}
  1529  
  1530  			if balanceReGet.Cmp(balanceSet) != 0 {
  1531  				t.Errorf("balanceReGet:%s, expect:%s", balanceReGet.String(), balanceSet.String())
  1532  			}
  1533  		}
  1534  	}
  1535  }
  1536  
  1537  func TestCheckOTAImageExist(t *testing.T) {
  1538  	var (
  1539  		db, _      = ethdb.NewMemDatabase()
  1540  		statedb, _ = state.New(common.Hash{}, state.NewDatabase(db))
  1541  
  1542  		otaWanAddr = common.FromHex(otaShortAddrs[7])
  1543  		balanceSet = big.NewInt(10)
  1544  	)
  1545  
  1546  	otaImage := crypto.Keccak256(otaWanAddr)
  1547  	otaImageValue := balanceSet.Bytes()
  1548  
  1549  	exist, otaImageValueGet, err := CheckOTAImageExist(statedb, otaImage)
  1550  	if err != nil {
  1551  		t.Errorf("err:%s", err.Error())
  1552  	}
  1553  
  1554  	if exist {
  1555  		t.Errorf("exist is true!")
  1556  	}
  1557  
  1558  	if otaImageValueGet != nil && len(otaImageValueGet) != 0 {
  1559  		t.Errorf("otaImageValueGet is not empoty!")
  1560  	}
  1561  
  1562  	err = AddOTAImage(statedb, otaImage, otaImageValue)
  1563  	if err != nil {
  1564  		t.Errorf("err:%s", err.Error())
  1565  	}
  1566  
  1567  	exist, otaImageValueGet, err = CheckOTAImageExist(statedb, otaImage)
  1568  	if err != nil {
  1569  		t.Errorf("err:%s", err.Error())
  1570  	}
  1571  
  1572  	if err != nil {
  1573  		t.Errorf("err:%s", err.Error())
  1574  	}
  1575  
  1576  	if !exist {
  1577  		t.Errorf("exist is false!")
  1578  	}
  1579  
  1580  	if otaImageValueGet == nil || common.ToHex(otaImageValueGet) != common.ToHex(otaImageValue) {
  1581  		t.Errorf("otaImageValueGet:%s, expect:%s", common.ToHex(otaImageValueGet), common.ToHex(otaImageValue))
  1582  	}
  1583  }
  1584  
  1585  func TestAddOTAImage(t *testing.T) {
  1586  	var (
  1587  		db, _      = ethdb.NewMemDatabase()
  1588  		statedb, _ = state.New(common.Hash{}, state.NewDatabase(db))
  1589  
  1590  		otaWanAddr = common.FromHex(otaShortAddrs[7])
  1591  		balanceSet = big.NewInt(10)
  1592  	)
  1593  
  1594  	otaImage := crypto.Keccak256(otaWanAddr)
  1595  	otaImageValue := balanceSet.Bytes()
  1596  
  1597  	err := AddOTAImage(statedb, otaImage, otaImageValue)
  1598  	if err != nil {
  1599  		t.Errorf("err:%s", err.Error())
  1600  	}
  1601  
  1602  	err = AddOTAImage(statedb, otaImage, otaImageValue)
  1603  	if err != nil {
  1604  		t.Errorf("err:%s", err.Error())
  1605  	}
  1606  }