Exemple #1
0
        public int GetChainStatus(Hash chainId)
        {
            Api.Assert(_sideChainInfos.GetValue(chainId) != null, "Not existed side chain.");
            var info = _sideChainInfos[chainId];

            return((int)info.Status);
        }
Exemple #2
0
        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}");
        }
Exemple #3
0
        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());
        }
Exemple #4
0
        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);
        }
Exemple #5
0
        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);
        }
Exemple #6
0
 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);
 }
Exemple #7
0
        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());
        }
Exemple #8
0
        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();
        }
Exemple #9
0
        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();
        }
Exemple #10
0
        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));
        }
Exemple #11
0
        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();
        }
Exemple #12
0
        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();
        }
Exemple #13
0
        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();
        }