diff --git a/src/blockchain.rs b/src/blockchain.rs index 9614e8ba5..cb188fa61 100644 --- a/src/blockchain.rs +++ b/src/blockchain.rs @@ -259,7 +259,7 @@ impl BlockChain { TreeRoute { blocks: from_branch, - ancestor: from_details.parent, + ancestor: current_from, index: index } } @@ -565,4 +565,94 @@ mod tests { assert_eq!(bc.block_details(&genesis_hash).unwrap().children, vec![first_hash.clone()]); assert_eq!(bc.block_hash(&U256::from(2u8)), None); } + + #[test] + fn test_small_fork() { + let genesis = "f901fcf901f7a00000000000000000000000000000000000000000000000000000000000000000a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a07dba07d6b448a186e9612e5f737d1c909dce473e53199901a302c00646d523c1a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302000080832fefd8808454c98c8142a059262c330941f3fe2a34d16d6e3c7b30d2ceb37c6a0e9a994c494ee1a61d2410885aa4c8bf8e56e264c0c0".from_hex().unwrap(); + let b1 = "f90261f901f9a05716670833ec874362d65fea27a7cd35af5897d275b31a44944113111e4e96d2a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a0cb52de543653d86ccd13ba3ddf8b052525b04231c6884a4db3188a184681d878a0e78628dd45a1f8dc495594d83b76c588a3ee67463260f8b7d4a42f574aeab29aa0e9244cf7503b79c03d3a099e07a80d2dbc77bb0b502d8a89d51ac0d68dd31313b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302000001832fefd882520884562791e580a051b3ecba4e3f2b49c11d42dd0851ec514b1be3138080f72a2b6e83868275d98f8877671f479c414b47f862f86080018304cb2f94095e7baea6a6c7c4c2dfeb977efac326af552d870a801ca09e2709d7ec9bbe6b1bbbf0b2088828d14cd5e8642a1fee22dc74bfa89761a7f9a04bd8813dee4be989accdb708b1c2e325a7e9c695a8024e30e89d6c644e424747c0".from_hex().unwrap(); + let b2 = "f902ccf901f9a0437e51676ff10756fcfee5edd9159fa41dbcb1b2c592850450371cbecd54ee4fa01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a0c70a5dc56146e5ef025e4e5726a6373c6f12fd2f6784093a19ead0a7d17fb292a040645cbce4fd399e7bb9160b4c30c40d7ee616a030d4e18ef0ed3b02bdb65911a086e608555f63628417032a011d107b36427af37d153f0da02ce3f90fdd5e8c08b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302004002832fefd882c0e384562791e880a0e3cc39ff775cc0a32f175995b92e84b729e5c9a3563ff899e3555b908bc21d75887c3cde283f4846a6f8cdf8cb01018304cb2f8080b87e6060604052606e8060106000396000f360606040526000357c010000000000000000000000000000000000000000000000000000000090048063c0406226146037576035565b005b60406004506056565b6040518082815260200191505060405180910390f35b6000600560006000508190555060059050606b565b90561ba05258615c63503c0a600d6994b12ea5750d45b3c69668e2a371b4fbfb9eeff6b8a0a11be762bc90491231274a2945be35a43f23c27775b1ff24dd521702fe15f73ec0".from_hex().unwrap(); + let b3a = "f90261f901f9a036fde1253128666fcb95a5956da14a73489e988bb72738717ec1d31e1cee781aa01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a05fb2b4bfdef7b314451cb138a534d225c922fc0e5fbe25e451142732c3e25c25a09dc4b1357c0b7b8108f8a098f4f9a1a274957bc9ebc22a9ae67ae81739e5b19ca007c6fdfa8eea7e86b81f5b0fc0f78f90cc19f4aa60d323151e0cac660199e9a1b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302008003832fefd882524d84562791eb80a074861666bd346c025889745c793b91ab9cd1e2ca19b5cf3c50d04d135b0a4d2b8809fe9587ea4cdc04f862f86002018304cb2f94ec0e71ad0a90ffe1909d27dac207f7680abba42d01801ba06fd84874d36d5de9e8e48978c03619b53a96b7ae0a4cd1ac118f103098b44801a00572596974dd7df4f9f69bd7456585618c568d8434ef6453391b89281ce12ae1c0".from_hex().unwrap(); + let b3b = "f90265f901f9a036fde1253128666fcb95a5956da14a73489e988bb72738717ec1d31e1cee781aa01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a0ab87dc338bfd6f662b1cd90bc0c9e40a1b2146a095312393c9e13ce3a5008b09a0e609b7a7d4b8a2403ec1268627ecd98783627246e8f1b26addb3ff504f76a054a0592fabf92476512952db3a69a2481a42912e668a1ee28c4c322e703bb665f8beb90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302008003832fefd882a1f084562791ee80a0fe7098fa7e4ac5d637eea81fb23f8f78346826dbab430068dd9a249d0afa99818853e1a6b201ae3545f866f86402018304cb2f94ec0e71ad0a90ffe1909d27dac207f7680abba42d0284c04062261ca06edc9ce8e7da4cc34067beb325dcad59e5655a164a5100a50bc3eb681b12c716a0abf9053d5de65b1be81fe50d327b84de685efbeecea34e7b747180a6c6023e44c0".from_hex().unwrap(); + + let genesis_hash = H256::from_str("5716670833ec874362d65fea27a7cd35af5897d275b31a44944113111e4e96d2").unwrap(); + let b1_hash = H256::from_str("437e51676ff10756fcfee5edd9159fa41dbcb1b2c592850450371cbecd54ee4f").unwrap(); + let b2_hash = H256::from_str("36fde1253128666fcb95a5956da14a73489e988bb72738717ec1d31e1cee781a").unwrap(); + let b3a_hash = H256::from_str("c208f88c9f5bf7e00840439742c12e5226d9752981f3ec0521bdcb6dd08af277").unwrap(); + let b3b_hash = H256::from_str("bf72270ae0d95c9ea39a6adab994793fddb8c10fba7391e26279474124605d54").unwrap(); + + // b3a is a part of canon chain, whereas b3b is part of sidechain + let best_block_hash = H256::from_str("c208f88c9f5bf7e00840439742c12e5226d9752981f3ec0521bdcb6dd08af277").unwrap(); + + let mut dir = env::temp_dir(); + dir.push(H32::random().hex()); + + let bc = BlockChain::new(&genesis, &dir); + bc.insert_block(&b1); + bc.insert_block(&b2); + bc.insert_block(&b3a); + bc.insert_block(&b3b); + + assert_eq!(bc.best_block_hash(), best_block_hash); + assert_eq!(bc.block_number(&genesis_hash).unwrap(), U256::from(0)); + assert_eq!(bc.block_number(&b1_hash).unwrap(), U256::from(1)); + assert_eq!(bc.block_number(&b2_hash).unwrap(), U256::from(2)); + assert_eq!(bc.block_number(&b3a_hash).unwrap(), U256::from(3)); + assert_eq!(bc.block_number(&b3b_hash).unwrap(), U256::from(3)); + + assert_eq!(bc.block_hash(&U256::from(0)).unwrap(), genesis_hash); + assert_eq!(bc.block_hash(&U256::from(1)).unwrap(), b1_hash); + assert_eq!(bc.block_hash(&U256::from(2)).unwrap(), b2_hash); + assert_eq!(bc.block_hash(&U256::from(3)).unwrap(), b3a_hash); + + // test trie route + let r0_1 = bc.tree_route(genesis_hash.clone(), b1_hash.clone()); + assert_eq!(r0_1.ancestor, genesis_hash); + assert_eq!(r0_1.blocks, [b1_hash.clone()]); + assert_eq!(r0_1.index, 0); + + let r0_2 = bc.tree_route(genesis_hash.clone(), b2_hash.clone()); + assert_eq!(r0_2.ancestor, genesis_hash); + assert_eq!(r0_2.blocks, [b1_hash.clone(), b2_hash.clone()]); + assert_eq!(r0_2.index, 0); + + let r1_3a = bc.tree_route(b1_hash.clone(), b3a_hash.clone()); + assert_eq!(r1_3a.ancestor, b1_hash); + assert_eq!(r1_3a.blocks, [b2_hash.clone(), b3a_hash.clone()]); + assert_eq!(r1_3a.index, 0); + + let r1_3b = bc.tree_route(b1_hash.clone(), b3b_hash.clone()); + assert_eq!(r1_3b.ancestor, b1_hash); + assert_eq!(r1_3b.blocks, [b2_hash.clone(), b3b_hash.clone()]); + assert_eq!(r1_3b.index, 0); + + let r3a_3b = bc.tree_route(b3a_hash.clone(), b3b_hash.clone()); + assert_eq!(r3a_3b.ancestor, b2_hash); + assert_eq!(r3a_3b.blocks, [b3a_hash.clone(), b3b_hash.clone()]); + assert_eq!(r3a_3b.index, 1); + + let r1_0 = bc.tree_route(b1_hash.clone(), genesis_hash.clone()); + assert_eq!(r1_0.ancestor, genesis_hash); + assert_eq!(r1_0.blocks, [b1_hash.clone()]); + assert_eq!(r1_0.index, 1); + + let r2_0 = bc.tree_route(b2_hash.clone(), genesis_hash.clone()); + assert_eq!(r2_0.ancestor, genesis_hash); + assert_eq!(r2_0.blocks, [b2_hash.clone(), b1_hash.clone()]); + assert_eq!(r2_0.index, 2); + + let r3a_1 = bc.tree_route(b3a_hash.clone(), b1_hash.clone()); + assert_eq!(r3a_1.ancestor, b1_hash); + assert_eq!(r3a_1.blocks, [b3a_hash.clone(), b2_hash.clone()]); + assert_eq!(r3a_1.index, 2); + + let r3b_1 = bc.tree_route(b3b_hash.clone(), b1_hash.clone()); + assert_eq!(r3b_1.ancestor, b1_hash); + assert_eq!(r3b_1.blocks, [b3b_hash.clone(), b2_hash.clone()]); + assert_eq!(r3b_1.index, 2); + + let r3b_3a = bc.tree_route(b3b_hash.clone(), b3a_hash.clone()); + assert_eq!(r3b_3a.ancestor, b2_hash); + assert_eq!(r3b_3a.blocks, [b3b_hash.clone(), b3a_hash.clone()]); + assert_eq!(r3b_3a.index, 1); + } }