diff --git a/blockchain/chain/Cargo.toml b/blockchain/chain/Cargo.toml index ada47e0299f..038b8750b62 100644 --- a/blockchain/chain/Cargo.toml +++ b/blockchain/chain/Cargo.toml @@ -9,7 +9,7 @@ blocks = { path = "../blocks" } network = { path = "../../node/network" } cid = { package = "ferret_cid", path = "../../ipld/cid" } clock = { path = "../../node/clock" } -num-bigint = "0.2.3" +num-bigint = { git = "https://github.com/austinabell/num-bigint", rev = "f7084a9ed5a2b08d9bfb67790cb4ce9212193f31" } [dev-dependencies] address = { path = "../../vm/address" } diff --git a/tests/cbor/unsigned_message_vectors.json b/tests/cbor/unsigned_message_vectors.json new file mode 100644 index 00000000000..f0d0b504f8e --- /dev/null +++ b/tests/cbor/unsigned_message_vectors.json @@ -0,0 +1,275 @@ +[ + { + "message": { + "to": "t3aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaagazosta", + "from": "t2ch7krq7l35i74rebqbjdsp3ucl47t24e3juxjfa", + "nonce": 0, + "value": 0, + "gas_price": 0, + "gas_limit": 0, + "method": 0, + "params": "" + }, + "hex_cbor": "88583103000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000550211fea8c3ebdf51fe44818052393f7412f9f9eb84004040400040" + }, + { + "message": { + "to": "t0894385949183117216", + "from": "t1pyfq7dg6sq65acyomqvzvbgwni4zllglqffw5dy", + "nonce": 11998794077335055257, + "value": 4751997750760398084, + "gas_price": 7504504064263669287, + "gas_limit": 11199607447739267382, + "method": 3510942875414458836, + "params": "52fdfc072182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2" + }, + "hex_cbor": "884a00a08fa4c983e9dfb40c55017e0b0f8cde943dd00b0e642b9a84d66a3995accb1ba68447a4189deb99490041f27cc6f3875d04490068255aaf95e9462749009b6cffa2ba5179361b30b95ff183c471d4581e52fdfc072182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2" + }, + { + "message": { + "to": "t03328451335138149956", + "from": "t1cyg66djxytxhzdq7ynoqfxk7xinp6xsejbeufli", + "nonce": 14486903973548550719, + "value": 7955079406183515637, + "gas_price": 11926873763676642186, + "gas_limit": 2740103009342231109, + "method": 6941261091797652072, + "params": "d2c422acd2080badb37c5821b6d95526a41a9504680b4e7c8b763a1b1d49" + }, + "hex_cbor": "884a00c48cd6d8ab9bc2982e5501160def0d37c4ee7c8e1fc35d02dd5fba1aff5e441bc90bd268b68e6a3f49006e661e92759805f54900a584c47f2cdf5b8a49002606cd2b57d292451b6054502fc5d6d268581ed2c422acd2080badb37c5821b6d95526a41a9504680b4e7c8b763a1b1d49" + }, + { + "message": { + "to": "t018218388313430417611", + "from": "t16n7vrq5humzoqll7zg4yw6dta645tuakcoalp6y", + "nonce": 9956202364908137547, + "value": 5486140987150761883, + "gas_price": 9768663798983814715, + "gas_limit": 6382800227808658932, + "method": 2781055864473387780, + "params": "9c160f0702e2d0836bf84c7174cb7476364cc3dbd968b0f7172ed85794bb" + }, + "hex_cbor": "884b00cbc197d1d5f4adeafc015501f37f58c3a7a332e82d7fc9b98b787307b9d9d00a1b8a2b894cf840ec4b49004c22b02936d4ff9b4900879143f7f4a5ee3b4900589442fd5ad145f41b26984b92f6740304581e9c160f0702e2d0836bf84c7174cb7476364cc3dbd968b0f7172ed85794bb" + }, + { + "message": { + "to": "t02601737961087659062", + "from": "t1awsiuji4wpbxpzslg36f3wnfxzi4o5gq67tz2mi", + "nonce": 7273596521315663110, + "value": 3337066551442961397, + "gas_price": 17344948852394588913, + "gas_limit": 11963748953446345529, + "method": 8249030965139585917, + "params": "9d0f7bbacbf8713f8d962d7c8d019192c24224e2cafccae3a61fb586b143" + }, + "hex_cbor": "884a00b6d89dcac1dcce8d24550105a48a251cb3c377e64b36fc5dd9a5be51c774d01b64f1017fbc897d0649002e4fa459169873f54900f0b5a315724c7af14900a607c649581eeb391b727a71f52257bb7d581e9d0f7bbacbf8713f8d962d7c8d019192c24224e2cafccae3a61fb586b143" + }, + { + "message": { + "to": "t08603989663476771718", + "from": "t14mb3j32uuwajy5b2mliz63isp6zl5xkppzyuhfy", + "nonce": 6842348953158377901, + "value": 7388428680384065704, + "gas_price": 6735196588112087610, + "gas_limit": 1687184559264975024, + "method": 13174268766980400525, + "params": "de0374289a266f97647981998ebea89c0b4b373970115e82ed6f4125c8fa" + }, + "hex_cbor": "884a0086d7a9b5b5e0e0b3775501e303b4ef54a5809c743a62d19f6d127fb2bedd4f1b5ef4e81ede4561ad49006688f8bd3e99b0a849005d78399cbed80a3a4900176a156ae58348b01bb6d467a4af63e58d581ede0374289a266f97647981998ebea89c0b4b373970115e82ed6f4125c8fa" + }, + { + "message": { + "to": "t08807817071862113702", + "from": "t1dzdmyzzdy6q5elobj63eokzv2xnwsp4vm5l6aka", + "nonce": 12432680895096110463, + "value": 15595235597337683065, + "gas_price": 6556961545928831643, + "gas_limit": 5199948958991797301, + "method": 15213854965919594827, + "params": "f7df9dec6a40e9a1d007f033c2823061bdd0eaa59f8e4da6430105220d0b" + }, + "hex_cbor": "884a00a6cbd9b7e0dee99d7a55011e46cc6723c7a1d22dc14fb6472b35d5db693f951bac89c16725fd3d7f4900d86d68260fd6e47949005aff01c926fbf29b49004829ee0716de4c351bd322787c2bf3394b581ef7df9dec6a40e9a1d007f033c2823061bdd0eaa59f8e4da6430105220d0b" + }, + { + "message": { + "to": "t06651414131918424343", + "from": "t1svd45rkcfpsyqedvvhuv77yvllvu5ygmygjlvka", + "nonce": 5944830206637008055, + "value": 788787457839692041, + "gas_price": 15399114114227588261, + "gas_limit": 14967026985784794439, + "method": 3409814636252858217, + "params": "a7ba64f84ab43ca0c6e6b91c1fd3be8990434179d3af4491a369012db92d" + }, + "hex_cbor": "884a0097928bd898bda4a75c55019547cec5422be5881075a9e95fff155aeb4ee0cc1b528047936c9c64b749000af2560383d179094900d5b4a4b2ea3d4ca54900cfb58fbeaf635d471b2f5218587fc78769581ea7ba64f84ab43ca0c6e6b91c1fd3be8990434179d3af4491a369012db92d" + }, + { + "message": { + "to": "t013234731587024579193", + "from": "t1mrret5liwh46qde6qhaxrmcwil7jawjeqdijwfq", + "nonce": 14988856041258832631, + "value": 14297581759627478249, + "gas_price": 5751776211841778805, + "gas_limit": 6725505124774569258, + "method": 16883695360970880573, + "params": "6f1814be823350eab13935f31d84484517e924aef78ae151c00755925836" + }, + "hex_cbor": "884b00f9d4f7f19fc8cdd5b7015501646249f568b1f9e80c9e81c178b05642fe9059241bd0031d27b8b352f74900c66b36dbc9b344e949004fd269fd8e5f047549005d55cb471941e52a1bea4eef7a2694763d581e6f1814be823350eab13935f31d84484517e924aef78ae151c00755925836" + }, + { + "message": { + "to": "t09240932364278507230", + "from": "t1ly3ynedw74p4q3ytdnb4stjdkiodrl54moeyxea", + "nonce": 9249594463326629931, + "value": 8446960703956728189, + "gas_price": 14663632165210175172, + "gas_limit": 14382856709244076395, + "method": 16744157289148322445, + "params": "5deda77e7585bcea406b32d6108bd68584f57e37caac6e33feaa3263a399" + }, + "hex_cbor": "884b00decdecb1e5e0989f800155015e37869076ff1fc86f131b43c94d23521c38afbc1b805d29286f00f02b49007539a2019f06397d4900cb7fafc3545836c44900c79a2bf931d6416b1be85f325712f4128d581e5deda77e7585bcea406b32d6108bd68584f57e37caac6e33feaa3263a399" + }, + { + "message": { + "to": "t02282476590775666788", + "from": "t1uqexvn66gj4lxkbvmrgposwrlxbyd655o2nayyi", + "nonce": 10825064499110513322, + "value": 14689361390610371514, + "gas_price": 16734835965210899604, + "gas_limit": 6399527266456256611, + "method": 279676139769146943, + "params": "5f5abf44ccff332f7576b0620556304a3e3eae14c28d0cea39d2901a5272" + }, + "hex_cbor": "884a00e490d08adc8bbfd61f5501a4097ab7de3278bba835644cf74ad15dc381fbbd1b963a5aceec2c8aaa4900cbdb185b70ab53ba4900e83e14a538d3b494490058cfb024878d40631b03e19bf7a317ae3f581e5f5abf44ccff332f7576b0620556304a3e3eae14c28d0cea39d2901a5272" + }, + { + "message": { + "to": "t07673207765878545335", + "from": "t1dwwjod7vw62jzw2eva7gtxohaidjhgh6w2rofui", + "nonce": 11361626762965614966, + "value": 15014124176381749710, + "gas_price": 4596876061716608039, + "gas_limit": 828591673584457147, + "method": 9455745301124654205, + "params": "09d6fc25072fb63c35d6042c4160f38ee9e2a9f3fb4ffb0019b454d522b5" + }, + "hex_cbor": "884a00b7b7d6a7f0b9adbe6a55011dac970ff5b7b49cdb44a83e69ddc702069398fe1b9dac9b582d2301764900d05ce263e2d6a9ce49003fcb626c3f1d742749000b7fbfbcafd915bb1b83398e40b01aa47d581e09d6fc25072fb63c35d6042c4160f38ee9e2a9f3fb4ffb0019b454d522b5" + }, + { + "message": { + "to": "t04534277910591376951", + "from": "t1slswisymmkfulmvl3jynrnwqi27tkvmsgzhztvy", + "nonce": 8835565338717500304, + "value": 16576323000633271029, + "gas_price": 990415953277272574, + "gas_limit": 16445594914354785247, + "method": 3627100269752912500, + "params": "20c88969c2e2cdcf233438bf1774ace7709a4f091e9a83fdeae0ec55eb23" + }, + "hex_cbor": "884a00b79cccedc6b7bff63e550192e5644b0c628b45b2abda70d8b6d046bf3555921b7a9e3bdcdc02b3904900e60aedf1a6e222f549000dbeaa0fe2f8c1fe4900e43a7d712e166bdf1b32560c7a67588a74581e20c88969c2e2cdcf233438bf1774ace7709a4f091e9a83fdeae0ec55eb23" + }, + { + "message": { + "to": "t01400508188743108884", + "from": "t1e3vymxcdqfkqwz6e6wnxxx6ayuml3vxi5gef4xa", + "nonce": 9926103171667860567, + "value": 2907281439932170679, + "gas_price": 1472519844697410736, + "gas_limit": 10494390165361325183, + "method": 2975558351153467687, + "params": "f4ba348f8921a266b11d0f334c62fe52ba53af19779cb2948b6570ffa0b7" + }, + "hex_cbor": "884a009492bcb8f0d8e6b713550126eb865c4381550b67c4f59b7bdfc0c518bdd6e81b89c09a3e6f241c5749002858bd10f13e41b74900146f70ff3be70cb0490091a39040f4b6f47f1b294b4e8e20f31127581ef4ba348f8921a266b11d0f334c62fe52ba53af19779cb2948b6570ffa0b7" + }, + { + "message": { + "to": "t06607332037155172840", + "from": "t1bgqopgk64ywpprka4citgi62aldclyaegvwvx6y", + "nonce": 15095378478039910236, + "value": 118298131398851786, + "gas_price": 9506365343507173044, + "gas_limit": 10127547266291660615, + "method": 12627826653636866797, + "params": "c389cb5165ce64002cbd9c2887aa113df2468928d5a23b9ca740f80c9382" + }, + "hex_cbor": "884a00e8e3c4ddb0affdd85b550109a0e7995ee62cf7c540e0913323da02c625e0041bd17d8ebf3da5475c490001a44786139efcca490083ed64e9bcd44eb449008c8c4694a54af7471baf3f0d6fb73c32ed581ec389cb5165ce64002cbd9c2887aa113df2468928d5a23b9ca740f80c9382" + }, + { + "message": { + "to": "t010779167372955628948", + "from": "t1aizqgl2klzkzffwu35rufyuzefke2i6ndbewuhi", + "nonce": 3056332746016649150, + "value": 16859660887416251990, + "gas_price": 15682387623776390888, + "gas_limit": 9757647480442399021, + "method": 9215619702456294450, + "params": "ac476c9fb03fc9228fbae88fd580663a0454b68312207f0a3b584c623164" + }, + "hex_cbor": "884b0094eb80fcd7f3d2cb950155010233032f4a5e559296d4df6342e29921544d23cd1b2a6a467079b76fbe4900e9f98c4033fe26564900d9a30874792c8ee84900876a20af6b41292d1b7fe4754afdff9c32581eac476c9fb03fc9228fbae88fd580663a0454b68312207f0a3b584c623164" + }, + { + "message": { + "to": "t017050629189067344208", + "from": "t1mzposcnsd2tc66yu5i3kajtrh5pvwohdjvitcey", + "nonce": 18205846881357943473, + "value": 1960528713598030433, + "gas_price": 198614094973075395, + "gas_limit": 17941254959206722521, + "method": 857498332500047840, + "params": "bf4f0152e5d498320982c85aad70384859c05a4b13a1d5b2f5bfef5a6ed9" + }, + "hex_cbor": "884b00d09290da8c9d80d0ec015501665ee909b21ea62f7b14ea36a026713f5f5b38e31bfca82947a67e52b149001b35327a49f6d261490002c19e7792417fc34900f8fc24541c3b6bd91b0be67230b027b7e0581ebf4f0152e5d498320982c85aad70384859c05a4b13a1d5b2f5bfef5a6ed9" + }, + { + "message": { + "to": "t017526944655607742924", + "from": "t1x7xvs6oorrrlefyzn6wlbvaibzj3a2fyt4hsmvq", + "nonce": 8761126201432260190, + "value": 12168683120995651260, + "gas_price": 4606018198686923411, + "gas_limit": 12916708273433536175, + "method": 9406074772821824226, + "params": "f9046efa18415a761f03abaa40abc9448fddeb2191d945c04767af847afd" + }, + "hex_cbor": "884b00cc83b99bd7e98d9ef3015501bfef5979ce8c62b217196facb0d4080e53b068b81b7995c5dc9cbcbe5e4900a8dfd8d37b3ccebc49003febdd25e1b7fa934900b3415dbd315ae6af1b8289172b9cced2e2581ef9046efa18415a761f03abaa40abc9448fddeb2191d945c04767af847afd" + }, + { + "message": { + "to": "t015934087754879685594", + "from": "t1ez743nvc4j7qfirwnmxbh4qdqwha3iyalnq4rya", + "nonce": 14995696081579861508, + "value": 13955768992965067384, + "gas_price": 9891590185009426703, + "gas_limit": 15903206082924732236, + "method": 8115136352186866059, + "params": "a8af5e392f0fea1931a290220777a93143dfdcbfa68406e877073ff08834" + }, + "hex_cbor": "884b00daa7858cc08cd090dd015501267fcdb6a2e27f02a2366b2e13f203858e0da3001bd01b6a22b648e6044900c1acd9f55118027849008945fcdd893a310f4900dcb389ac728f5f4c1b709ec18437f5198b581ea8af5e392f0fea1931a290220777a93143dfdcbfa68406e877073ff08834" + }, + { + "message": { + "to": "t05804560326627778270", + "from": "t17dvtgkop7cqgi6myjne5kzvrnsbg5wnowjphhwy", + "nonce": 6933583034365165052, + "value": 15052574835258590597, + "gas_price": 894060311800635659, + "gas_limit": 9360130382033933288, + "method": 5384925032239196514, + "params": "b89b939beac03c875a27db029de37ae37a42318813487685929359ca8c5e" + }, + "hex_cbor": "884a00debde5cfcf9bfcc6505501f8eb3329cff8a06479984b49d566b16c826ed9ae1b60390908b802bdfc4900d0e57d0f8a92858549000c68571ddca6e10b490081e5dcfd887953e81b4abb18c948b9e962581eb89b939beac03c875a27db029de37ae37a42318813487685929359ca8c5e" + }, + { + "message": { + "to": "t018041105950862327045", + "from": "t1kvar5z3q7dwrfxjqsnuqpq5qsd7mvh2xypblwta", + "nonce": 12087323835742844336, + "value": 10924076007769315270, + "gas_price": 18143242942063673131, + "gas_limit": 7685299261280486864, + "method": 12078452559500257731, + "params": "aee4a3e933e5c400cde5e60c5ead6fc7ae77ba1d259b188a4b21c86fbc23" + }, + "hex_cbor": "884b0085fad6a2ecc8b8affa01550155411ee770f8ed12dd30936907c3b090feca9f571ba7beccfcba9075b04900979a1d40367517c64900fbc9bf542b517f2b49006aa7aaf5ad3205d01ba79f489bc56ba9c3581eaee4a3e933e5c400cde5e60c5ead6fc7ae77ba1d259b188a4b21c86fbc23" + } +] \ No newline at end of file diff --git a/vm/Cargo.toml b/vm/Cargo.toml index f4678d9de1f..c3ee80aedfb 100644 --- a/vm/Cargo.toml +++ b/vm/Cargo.toml @@ -5,7 +5,7 @@ authors = ["ChainSafe Systems "] edition = "2018" [dependencies] -num-bigint = "0.2.3" -address = {path = "./address"} -encoding = {path = "../encoding"} -serde = {version = "1.0", features = ["derive"]} +num-bigint = { git = "https://github.com/austinabell/num-bigint", rev = "f7084a9ed5a2b08d9bfb67790cb4ce9212193f31", features = ["serde_derive"] } +address = { path = "./address" } +encoding = { path = "../encoding" } +serde = { version = "1.0", features = ["derive"] } diff --git a/vm/actor/Cargo.toml b/vm/actor/Cargo.toml index f347519f17d..f431f4b01e3 100644 --- a/vm/actor/Cargo.toml +++ b/vm/actor/Cargo.toml @@ -8,7 +8,7 @@ edition = "2018" vm = { path = "../../vm" } address = { path = "../address" } runtime = { path = "../runtime" } -num-bigint = "0.2.3" +num-bigint = { git = "https://github.com/austinabell/num-bigint", rev = "f7084a9ed5a2b08d9bfb67790cb4ce9212193f31" } encoding = { path = "../../encoding" } num-traits = "0.2" num-derive = "0.2" diff --git a/vm/actor/src/builtin/account.rs b/vm/actor/src/builtin/account.rs index 4276c15fae1..7153ac49ceb 100644 --- a/vm/actor/src/builtin/account.rs +++ b/vm/actor/src/builtin/account.rs @@ -20,7 +20,7 @@ pub enum AccountMethod { impl AccountMethod { /// from_method_num converts a method number into an AccountMethod enum fn from_method_num(m: MethodNum) -> Option { - FromPrimitive::from_i32(m.into()) + FromPrimitive::from_u64(u64::from(m)) } } diff --git a/vm/actor/src/builtin/cron.rs b/vm/actor/src/builtin/cron.rs index 9cf31e15161..70461b42308 100644 --- a/vm/actor/src/builtin/cron.rs +++ b/vm/actor/src/builtin/cron.rs @@ -30,7 +30,7 @@ pub enum CronMethod { impl CronMethod { /// from_method_num converts a method number into an CronMethod enum fn from_method_num(m: MethodNum) -> Option { - FromPrimitive::from_i32(m.into()) + FromPrimitive::from_u64(u64::from(m)) } } diff --git a/vm/actor/src/builtin/init.rs b/vm/actor/src/builtin/init.rs index 676b1e90fc6..509760a2caf 100644 --- a/vm/actor/src/builtin/init.rs +++ b/vm/actor/src/builtin/init.rs @@ -40,7 +40,7 @@ pub enum InitMethod { impl InitMethod { /// from_method_num converts a method number into an InitMethod enum fn from_method_num(m: MethodNum) -> Option { - FromPrimitive::from_i32(m.into()) + FromPrimitive::from_u64(u64::from(m)) } } diff --git a/vm/actor/src/builtin/reward.rs b/vm/actor/src/builtin/reward.rs index 6aa4c8b20b5..c57190a1411 100644 --- a/vm/actor/src/builtin/reward.rs +++ b/vm/actor/src/builtin/reward.rs @@ -41,7 +41,7 @@ pub enum RewardMethod { impl RewardMethod { /// from_method_num converts a method number into an RewardMethod enum fn from_method_num(m: MethodNum) -> Option { - FromPrimitive::from_i32(m.into()) + FromPrimitive::from_u64(u64::from(m)) } } diff --git a/vm/actor/src/builtin/storage_power.rs b/vm/actor/src/builtin/storage_power.rs index 01feaad1dfb..e21504fdfa1 100644 --- a/vm/actor/src/builtin/storage_power.rs +++ b/vm/actor/src/builtin/storage_power.rs @@ -26,7 +26,7 @@ pub enum StoragePowerMethod { impl StoragePowerMethod { /// from_method_num converts a method number into an StoragePowerMethod enum fn from_method_num(m: MethodNum) -> Option { - FromPrimitive::from_i32(m.into()) + FromPrimitive::from_u64(u64::from(m)) } } diff --git a/vm/message/Cargo.toml b/vm/message/Cargo.toml index b492f6f622f..6afa9886322 100644 --- a/vm/message/Cargo.toml +++ b/vm/message/Cargo.toml @@ -7,8 +7,13 @@ edition = "2018" [dependencies] vm = { path = "../../vm" } address = { path = "../address" } -num-bigint = { version = "0.2.3", features = ["serde"] } +num-bigint = { git = "https://github.com/austinabell/num-bigint", rev = "f7084a9ed5a2b08d9bfb67790cb4ce9212193f31", features = ["serde_derive"] } encoding = { path = "../../encoding" } crypto = { path = "../../crypto" } derive_builder = "0.9" serde = { version = "1.0", features = ["derive"] } + +[dev-dependencies] +serde_json = "1.0" +serde = { version = "1.0", features = ["derive"] } +hex = "0.4.0" diff --git a/vm/message/src/unsigned_message.rs b/vm/message/src/unsigned_message.rs index 94cf0c3839e..046ef6012fa 100644 --- a/vm/message/src/unsigned_message.rs +++ b/vm/message/src/unsigned_message.rs @@ -2,14 +2,12 @@ // SPDX-License-Identifier: Apache-2.0 use super::Message; -use crate::TokenAmount; -use crate::{MethodNum, Serialized}; - use address::Address; use derive_builder::Builder; -use encoding::Cbor; +use encoding::{de, ser, Cbor}; use num_bigint::BigUint; -use serde::{Deserialize, Serialize}; +use serde::Deserialize; +use vm::{MethodNum, Serialized, TokenAmount}; /// Default Unsigned VM message type which includes all data needed for a state transition /// @@ -41,7 +39,7 @@ use serde::{Deserialize, Serialize}; /// let msg = message_builder.build().unwrap(); /// assert_eq!(msg.sequence(), 1); /// ``` -#[derive(PartialEq, Clone, Debug, Builder, Serialize, Deserialize)] +#[derive(PartialEq, Clone, Debug, Builder)] #[builder(name = "MessageBuilder")] pub struct UnsignedMessage { from: Address, @@ -60,15 +58,51 @@ pub struct UnsignedMessage { gas_limit: BigUint, } -// TODO verify format or implement custom serialize/deserialize function (if necessary): -// https://github.com/ChainSafe/ferret/issues/143 - impl UnsignedMessage { pub fn builder() -> MessageBuilder { MessageBuilder::default() } } +impl ser::Serialize for UnsignedMessage { + fn serialize(&self, s: S) -> Result + where + S: ser::Serializer, + { + ( + self.to.clone(), + self.from.clone(), + self.sequence, + self.value.clone(), + self.gas_price.clone(), + self.gas_limit.clone(), + self.method_num, + self.params.clone(), + ) + .serialize(s) + } +} + +impl<'de> de::Deserialize<'de> for UnsignedMessage { + fn deserialize(deserializer: D) -> Result + where + D: de::Deserializer<'de>, + { + let (to, from, sequence, value, gas_price, gas_limit, method_num, params) = + Deserialize::deserialize(deserializer)?; + Ok(Self { + to, + from, + sequence, + value, + gas_price, + gas_limit, + method_num, + params, + }) + } +} + impl Message for UnsignedMessage { fn from(&self) -> &Address { &self.from diff --git a/vm/message/tests/u_message_cbor.rs b/vm/message/tests/u_message_cbor.rs new file mode 100644 index 00000000000..8ffc2009eb6 --- /dev/null +++ b/vm/message/tests/u_message_cbor.rs @@ -0,0 +1,75 @@ +// Copyright 2020 ChainSafe Systems +// SPDX-License-Identifier: Apache-2.0 + +use address::Address; +use encoding::{from_slice, to_vec}; +use hex::decode; +use message::UnsignedMessage; +use num_bigint::BigUint; +use serde::Deserialize; +use std::fs::File; +use std::io::prelude::*; +use std::str::FromStr; +use vm::{MethodNum, Serialized, TokenAmount}; + +#[derive(Debug, Deserialize)] +struct MessageVector { + to: String, + from: String, + nonce: u64, + value: u64, + gas_price: u128, + gas_limit: u128, + method: u64, + params: String, +} + +impl From for UnsignedMessage { + fn from(vector: MessageVector) -> UnsignedMessage { + UnsignedMessage::builder() + .to(Address::from_str(&vector.to).unwrap()) + .from(Address::from_str(&vector.from).unwrap()) + .sequence(vector.nonce) + .value(TokenAmount::new(vector.value)) + .method_num(MethodNum::new(vector.method)) + .params(Serialized::new(decode(vector.params).unwrap())) + .gas_limit(BigUint::from(vector.gas_limit)) + .gas_price(BigUint::from(vector.gas_price)) + .build() + .unwrap() + } +} + +#[derive(Deserialize)] +struct TestVector { + message: MessageVector, + hex_cbor: String, +} + +fn encode_assert_cbor(message: &UnsignedMessage, expected: &[u8]) { + let enc_bz: Vec = to_vec(message).expect("Cbor serialization failed"); + + // Assert the message is encoded in same format + assert_eq!(enc_bz.as_slice(), expected); + // Assert decoding from those bytes goes back to unsigned message + assert_eq!( + &from_slice::(expected).expect("Should be able to deserialize cbor bytes"), + message + ); +} + +#[test] +fn unsigned_message_cbor_vectors() { + let mut file = File::open("../../tests/cbor/unsigned_message_vectors.json").unwrap(); + let mut string = String::new(); + file.read_to_string(&mut string).unwrap(); + + let vectors: Vec = + serde_json::from_str(&string).expect("Test vector deserialization failed"); + for tv in vectors { + encode_assert_cbor( + &UnsignedMessage::from(tv.message), + &decode(tv.hex_cbor).expect("Decoding cbor bytes failed"), + ) + } +} diff --git a/vm/src/method.rs b/vm/src/method.rs index 50a2d09805e..4221d8d007f 100644 --- a/vm/src/method.rs +++ b/vm/src/method.rs @@ -7,20 +7,20 @@ use std::ops::Deref; /// Method number indicator for calling actor methods #[derive(Default, Clone, Copy, PartialEq, Debug, Serialize, Deserialize)] -pub struct MethodNum(i32); // TODO: add constraints to this +pub struct MethodNum(u64); // TODO: add constraints to this // TODO verify format or implement custom serialize/deserialize function (if necessary): // https://github.com/ChainSafe/ferret/issues/143 impl MethodNum { /// Constructor for new MethodNum - pub fn new(num: i32) -> Self { + pub fn new(num: u64) -> Self { Self(num) } } -impl From for i32 { - fn from(method_num: MethodNum) -> i32 { +impl From for u64 { + fn from(method_num: MethodNum) -> u64 { method_num.0 } } diff --git a/vm/src/token.rs b/vm/src/token.rs index 1df6ec64076..532784447ba 100644 --- a/vm/src/token.rs +++ b/vm/src/token.rs @@ -1,7 +1,7 @@ // Copyright 2020 ChainSafe Systems // SPDX-License-Identifier: Apache-2.0 -use encoding::{de, ser, serde_bytes}; +use encoding::{de, ser}; use num_bigint::BigUint; /// Wrapper around a big int variable to handle token specific functionality @@ -20,9 +20,7 @@ impl ser::Serialize for TokenAmount { where S: ser::Serializer, { - let bz = self.0.to_bytes_be(); - let value = serde_bytes::Bytes::new(&bz); - serde_bytes::Serialize::serialize(value, s) + self.0.serialize(s) } } @@ -31,7 +29,6 @@ impl<'de> de::Deserialize<'de> for TokenAmount { where D: de::Deserializer<'de>, { - let bz: &[u8] = serde_bytes::Deserialize::deserialize(deserializer)?; - Ok(TokenAmount(BigUint::from_bytes_be(bz))) + Ok(Self(BigUint::deserialize(deserializer)?)) } } diff --git a/vm/state_tree/Cargo.toml b/vm/state_tree/Cargo.toml index b39e65e44b7..1b3f636eba9 100644 --- a/vm/state_tree/Cargo.toml +++ b/vm/state_tree/Cargo.toml @@ -11,4 +11,4 @@ vm = { path = "../../vm" } cid = { package = "ferret_cid", path = "../../ipld/cid" } [dev-dependencies] -num-bigint = "0.2.3" +num-bigint = { git = "https://github.com/austinabell/num-bigint", rev = "f7084a9ed5a2b08d9bfb67790cb4ce9212193f31" } diff --git a/vm/tests/params_test.rs b/vm/tests/params_test.rs index ea024c4e2d6..7b26592a6cd 100644 --- a/vm/tests/params_test.rs +++ b/vm/tests/params_test.rs @@ -43,5 +43,5 @@ fn cbor_params() { fn method_num() { // Test constructor available publicly let method = MethodNum::new(1); - assert_eq!(1, method.into()); + assert_eq!(1 as u64, u64::from(method)); }