public int GetChainStatus(Hash chainId) { Api.Assert(_sideChainInfos.GetValue(chainId) != null, "Not existed side chain."); var info = _sideChainInfos[chainId]; return((int)info.Status); }
public void WriteParentChainBlockInfo(ParentChainBlockInfo parentChainBlockInfo) { ulong parentChainHeight = parentChainBlockInfo.Height; var currentHeight = _currentParentChainHeight.GetValue(); var target = currentHeight != 0 ? currentHeight + 1: GlobalConfig.GenesisBlockHeight; Api.Assert(target == parentChainHeight, $"Parent chain block info at height {target} is needed, not {parentChainHeight}"); Console.WriteLine("ParentChainBlockInfo.Height is correct."); var key = new UInt64Value { Value = parentChainHeight }; Api.Assert(_parentChainBlockInfo.GetValue(key) == null, $"Already written parent chain block info at height {parentChainHeight}"); Console.WriteLine("Writing ParentChainBlockInfo.."); foreach (var _ in parentChainBlockInfo.IndexedBlockInfo) { BindParentChainHeight(_.Key, parentChainHeight); AddIndexedTxRootMerklePathInParentChain(_.Key, _.Value); } _parentChainBlockInfo.SetValueAsync(key, parentChainBlockInfo).Wait(); _currentParentChainHeight.SetValue(parentChainHeight); // only for debug Console.WriteLine($"WriteParentChainBlockInfo success at {parentChainHeight}"); }
public byte[] LockedAddress(Hash chainId) { Api.Assert(_sideChainInfos.GetValue(chainId) != null, "Not existed side chain."); var info = _sideChainInfos[chainId]; Api.Assert(info.Status != (SideChainStatus)3, "Disposed side chain."); return(info.LockedAddress.DumpByteArray()); }
public ulong LockedToken(Hash chainId) { Api.Assert(_sideChainInfos.GetValue(chainId) != null, "Not existed side chain."); var info = _sideChainInfos[chainId]; Api.Assert(info.Status != (SideChainStatus)3, "Disposed side chain."); return(info.LockedToken); }
public void TransferFrom(Address from, Address to, ulong amount) { var allowance = Allowances.GetAllowance(from, Api.GetTransaction().From); Api.Assert(allowance > amount, "Insufficient allowance."); DoTransfer(from, to, amount); Allowances.Reduce(from, amount); }
public void Initialize(string symbol, string tokenName, ulong totalSupply, uint decimals) { Api.Assert(!_initialized.GetValue(), "Already initialized."); // Api.Assert(Api.GetContractOwner().Equals(Api.GetTransaction().From), "Only owner can initialize the contract state."); _symbol.SetValue(symbol); _tokenName.SetValue(tokenName); _totalSupply.SetValue(totalSupply); _decimals.SetValue(decimals); _balances[Api.GetTransaction().From] = totalSupply; _initialized.SetValue(true); }
private void AddIndexedTxRootMerklePathInParentChain(ulong height, MerklePath path) { var key = new UInt64Value { Value = height }; Api.Assert(_txRootMerklePathInParentChain.GetValue(key) == null, $"Merkle path already bound at height {height}."); // _txRootMerklePathInParentChain[key] = path; _txRootMerklePathInParentChain.SetValueAsync(key, path).Wait(); Console.WriteLine("Path: {0}", path.Path[0].DumpHex()); }
private void BindParentChainHeight(ulong childHeight, ulong parentHeight) { var key = new UInt64Value { Value = childHeight }; Api.Assert(_childHeightToParentChainHeight.GetValue(key) == null, $"Already bound at height {childHeight} with parent chain"); // _childHeightToParentChainHeight[key] = new UInt64Value {Value = parentHeight}; _childHeightToParentChainHeight.SetValueAsync(key, new UInt64Value { Value = parentHeight }).Wait(); }
public void DisposeSideChain(Hash chainId) { Api.Assert(_sideChainInfos.GetValue(chainId) != null, "Not existed side chain"); // TODO: Only privileged account can trigger this method var info = _sideChainInfos[chainId]; info.Status = SideChainStatus.Terminated; _sideChainInfos[chainId] = info; new SideChainDisposal { chainId = chainId }.Fire(); }
public bool VerifyTransaction(Hash tx, MerklePath path, ulong parentChainHeight) { var key = new UInt64Value { Value = parentChainHeight }; Api.Assert(_parentChainBlockInfo.GetValue(key) != null, $"Parent chain block at height {parentChainHeight} is not recorded."); var rootCalculated = path.ComputeRootWith(tx); var parentRoot = _parentChainBlockInfo.GetValue(key)?.Root?.SideChainTransactionsRoot; //Api.Assert((parentRoot??Hash.Zero).Equals(rootCalculated), "Transaction verification Failed"); return((parentRoot ?? Hash.Zero).Equals(rootCalculated)); }
public void ApproveSideChain(Hash chainId) { // TODO: Only privileged account can trigger this method var info = _sideChainInfos[chainId]; Api.Assert(info != null, "Invalid chain id."); Api.Assert(info?.Status == SideChainStatus.Pending, "Invalid chain status."); info.Status = SideChainStatus.Active; _sideChainInfos[chainId] = info; new SideChainCreationRequestApproved() { Info = info.Clone() }.Fire(); }
private void DoTransfer(Address from, Address to, ulong amount) { var balSender = _balances[from]; Api.Assert(balSender >= amount, "Insufficient balance."); var balReceiver = _balances[to]; balSender = balSender.Sub(amount); balReceiver = balReceiver.Add(amount); _balances[from] = balSender; _balances[to] = balReceiver; new Transfered() { From = from, To = to, Amount = amount }.Fire(); }
public void Transfer(Hash to, ulong amount) { var from = Api.GetTransaction().From; var balSender = _balances[from]; Api.Assert(balSender > amount, "Insufficient balance."); var balReceiver = _balances[to]; balSender = balSender.Sub(amount); balReceiver = balReceiver.Add(amount); _balances[from] = balSender; _balances[to] = balReceiver; new Transfered() { From = from, To = to, Amount = amount }.Fire(); }