public bool TryGetSideChainBlockInfo(SideChainBlockInfo scb) { if (scb == null) { return(false); } if (!_clientsToSideChains.TryGetValue(scb.ChainId, out var client)) { // TODO: this could be changed. return(true); } var targetHeight = GetSideChainTargetHeight(scb.ChainId).Result; return(client.TryTake(WaitingIntervalInMillisecond, targetHeight, out var blockInfo) && scb.Equals(blockInfo)); }
public async Task VerifyTransactionTest() { Init(); var chainId = Mock.ChainId1; ChainConfig.Instance.ChainId = chainId.DumpHex(); _contract = new SideChainContractShim(Mock, AddressHelpers.GetSystemContractAddress(chainId, SmartContractType.SideChainContract.ToString())); ulong pHeight = 1; ParentChainBlockRootInfo pcbr1 = new ParentChainBlockRootInfo { ChainId = chainId, Height = pHeight, SideChainTransactionsRoot = Hash.Generate(), SideChainBlockHeadersRoot = Hash.Generate() }; ParentChainBlockInfo pcb1 = new ParentChainBlockInfo { Root = pcbr1 }; pcb1.IndexedBlockInfo.Add(0, new MerklePath { Path = { Hash.FromString("Block1"), Hash.FromString("Block2"), Hash.FromString("Block3") } }); await _contract.WriteParentChainBLockInfo(pcb1); var crossChainInfo = new CrossChainInfo(Mock.StateStore); var parentHeight = crossChainInfo.GetParentChainCurrentHeight(); Assert.Equal(pHeight, parentHeight); Transaction t1 = new Transaction { From = Address.FromString("1"), To = Address.FromString("2"), MethodName = "test", Sig = new Signature { P = ByteString.Empty, R = ByteString.Empty, }, Params = ByteString.Empty, RefBlockNumber = 0, RefBlockPrefix = ByteString.Empty }; var hashCount = 10; var list1 = new List <Hash> { t1.GetHash() }; for (int i = 0; i < hashCount; i++) { list1.Add(Hash.Generate()); } var bmt1 = new BinaryMerkleTree(); bmt1.AddNodes(list1); var root1 = bmt1.ComputeRootHash(); var sc1BlockInfo = new SideChainBlockInfo { Height = pHeight, BlockHeaderHash = Hash.Generate(), ChainId = Hash.Generate(), TransactionMKRoot = root1 }; Transaction t2 = new Transaction { From = Address.FromString("3"), To = Address.FromString("4"), MethodName = "test", Sig = new Signature { P = ByteString.Empty, R = ByteString.Empty, }, Params = ByteString.Empty, RefBlockNumber = 1, RefBlockPrefix = ByteString.Empty }; var list2 = new List <Hash> { t2.GetHash(), Hash.FromString("d"), Hash.FromString("e"), Hash.FromString("f"), Hash.FromString("a"), Hash.FromString("b"), Hash.FromString("c") }; var bmt2 = new BinaryMerkleTree(); bmt2.AddNodes(list2); var root2 = bmt2.ComputeRootHash(); var sc2BlockInfo = new SideChainBlockInfo { Height = pHeight, BlockHeaderHash = Hash.Generate(), ChainId = Hash.Generate(), TransactionMKRoot = root2 }; var block = new Block { Header = new BlockHeader(), Body = new BlockBody() }; block.Body.IndexedInfo.Add(new List <SideChainBlockInfo> { sc1BlockInfo, sc2BlockInfo }); block.Body.CalculateMerkleTreeRoots(); pHeight = 2; ParentChainBlockRootInfo parentChainBlockRootInfo = new ParentChainBlockRootInfo { ChainId = chainId, Height = pHeight, SideChainTransactionsRoot = block.Body.SideChainTransactionsRoot, SideChainBlockHeadersRoot = Hash.FromString("SideChainBlockHeadersRoot") }; ParentChainBlockInfo parentChainBlockInfo = new ParentChainBlockInfo { Root = parentChainBlockRootInfo }; var tree = block.Body.BinaryMerkleTreeForSideChainTransactionRoots; var pathForTx1 = bmt1.GenerateMerklePath(0); Assert.Equal(root1, pathForTx1.ComputeRootWith(t1.GetHash())); var pathForSc1Block = tree.GenerateMerklePath(0); pathForTx1.Path.AddRange(pathForSc1Block.Path); var pathForTx2 = bmt2.GenerateMerklePath(0); var pathForSc2Block = tree.GenerateMerklePath(1); pathForTx2.Path.AddRange(pathForSc2Block.Path); //parentChainBlockInfo.IndexedBlockInfo.Add(1, tree.GenerateMerklePath(0)); await _contract.WriteParentChainBLockInfo(parentChainBlockInfo); //crossChainInfo = new CrossChainInfo(Mock.StateStore); parentHeight = crossChainInfo.GetParentChainCurrentHeight(); Assert.Equal(pHeight, parentHeight); var b = await _contract.VerifyTransaction(t1.GetHash(), pathForTx1, parentChainBlockRootInfo.Height); Assert.True(b); b = await _contract.VerifyTransaction(t2.GetHash(), pathForTx2, parentChainBlockRootInfo.Height); Assert.True(b); }