Ejemplo n.º 1
0
        public virtual BigInteger BalanceOf(DataCache snapshot, UInt160 account)
        {
            StorageItem storage = snapshot.TryGet(CreateStorageKey(Prefix_Account).Add(account));

            if (storage is null)
            {
                return(BigInteger.Zero);
            }
            return(storage.GetInteroperable <TState>().Balance);
        }
Ejemplo n.º 2
0
        public virtual BigInteger BalanceOf(StoreView snapshot, UInt160 account)
        {
            StorageItem storage = snapshot.Storages.TryGet(CreateAccountKey(account));

            if (storage is null)
            {
                return(BigInteger.Zero);
            }
            return(storage.GetInteroperable <TState>().Balance);
        }
Ejemplo n.º 3
0
        private async ContractTask <bool> Vote(ApplicationEngine engine, UInt160 account, ECPoint voteTo)
        {
            if (!engine.CheckWitnessInternal(account))
            {
                return(false);
            }
            NeoAccountState state_account = engine.Snapshot.GetAndChange(CreateStorageKey(Prefix_Account).Add(account))?.GetInteroperable <NeoAccountState>();

            if (state_account is null)
            {
                return(false);
            }
            CandidateState validator_new = null;

            if (voteTo != null)
            {
                validator_new = engine.Snapshot.GetAndChange(CreateStorageKey(Prefix_Candidate).Add(voteTo))?.GetInteroperable <CandidateState>();
                if (validator_new is null)
                {
                    return(false);
                }
                if (!validator_new.Registered)
                {
                    return(false);
                }
            }
            if (state_account.VoteTo is null ^ voteTo is null)
            {
                StorageItem item = engine.Snapshot.GetAndChange(CreateStorageKey(Prefix_VotersCount));
                if (state_account.VoteTo is null)
                {
                    item.Add(state_account.Balance);
                }
                else
                {
                    item.Add(-state_account.Balance);
                }
            }
            await DistributeGas(engine, account, state_account);

            if (state_account.VoteTo != null)
            {
                StorageKey     key = CreateStorageKey(Prefix_Candidate).Add(state_account.VoteTo);
                StorageItem    storage_validator = engine.Snapshot.GetAndChange(key);
                CandidateState state_validator   = storage_validator.GetInteroperable <CandidateState>();
                state_validator.Votes -= state_account.Balance;
                CheckCandidate(engine.Snapshot, state_account.VoteTo, state_validator);
            }
            state_account.VoteTo = voteTo;
            if (validator_new != null)
            {
                validator_new.Votes += state_account.Balance;
            }
            return(true);
        }
Ejemplo n.º 4
0
 internal override void OnPersist(ApplicationEngine engine)
 {
     // Set next committee
     if (ShouldRefreshCommittee(engine.PersistingBlock.Index, engine.ProtocolSettings.CommitteeMembersCount))
     {
         StorageItem storageItem     = engine.Snapshot.GetAndChange(CreateStorageKey(Prefix_Committee));
         var         cachedCommittee = storageItem.GetInteroperable <CachedCommittee>();
         cachedCommittee.Clear();
         cachedCommittee.AddRange(ComputeCommitteeMembers(engine.Snapshot, engine.ProtocolSettings));
     }
 }
Ejemplo n.º 5
0
 protected virtual bool Transfer(ApplicationEngine engine, UInt160 from, UInt160 to, BigInteger amount, StackItem data)
 {
     if (amount.Sign < 0) throw new ArgumentOutOfRangeException(nameof(amount));
     if (!from.Equals(engine.CallingScriptHash) && !engine.CheckWitnessInternal(from))
         return false;
     StorageKey key_from = CreateStorageKey(Prefix_Account).Add(from);
     StorageItem storage_from = engine.Snapshot.GetAndChange(key_from);
     if (amount.IsZero)
     {
         if (storage_from != null)
         {
             TState state_from = storage_from.GetInteroperable<TState>();
             OnBalanceChanging(engine, from, state_from, amount);
         }
     }
     else
     {
         if (storage_from is null) return false;
         TState state_from = storage_from.GetInteroperable<TState>();
         if (state_from.Balance < amount) return false;
         if (from.Equals(to))
         {
             OnBalanceChanging(engine, from, state_from, BigInteger.Zero);
         }
         else
         {
             OnBalanceChanging(engine, from, state_from, -amount);
             if (state_from.Balance == amount)
                 engine.Snapshot.Delete(key_from);
             else
                 state_from.Balance -= amount;
             StorageKey key_to = CreateStorageKey(Prefix_Account).Add(to);
             StorageItem storage_to = engine.Snapshot.GetAndChange(key_to, () => new StorageItem(new TState()));
             TState state_to = storage_to.GetInteroperable<TState>();
             OnBalanceChanging(engine, to, state_to, amount);
             state_to.Balance += amount;
         }
     }
     PostTransfer(engine, from, to, amount, data, true);
     return true;
 }
Ejemplo n.º 6
0
 internal protected virtual void Mint(ApplicationEngine engine, UInt160 account, BigInteger amount, bool callOnPayment)
 {
     if (amount.Sign < 0) throw new ArgumentOutOfRangeException(nameof(amount));
     if (amount.IsZero) return;
     StorageItem storage = engine.Snapshot.GetAndChange(CreateStorageKey(Prefix_Account).Add(account), () => new StorageItem(new TState()));
     TState state = storage.GetInteroperable<TState>();
     OnBalanceChanging(engine, account, state, amount);
     state.Balance += amount;
     storage = engine.Snapshot.GetAndChange(CreateStorageKey(Prefix_TotalSupply), () => new StorageItem(BigInteger.Zero));
     storage.Add(amount);
     PostTransfer(engine, null, account, amount, StackItem.Null, callOnPayment);
 }
Ejemplo n.º 7
0
        public BigInteger UnclaimedGas(StoreView snapshot, UInt160 account, uint end)
        {
            StorageItem storage = snapshot.Storages.TryGet(CreateStorageKey(Prefix_Account).Add(account));

            if (storage is null)
            {
                return(BigInteger.Zero);
            }
            NeoAccountState state = storage.GetInteroperable <NeoAccountState>();

            return(CalculateBonus(snapshot, state.Balance, state.BalanceHeight, end));
        }
Ejemplo n.º 8
0
        public BigInteger UnclaimedValt(DataCache snapshot, UInt160 account, uint end)
        {
            StorageItem storage = snapshot.TryGet(CreateStorageKey(Prefix_Account).Add(account));

            if (storage is null)
            {
                return(BigInteger.Zero);
            }
            VauthAccountState state = storage.GetInteroperable <VauthAccountState>();

            return(CalculateBonus(snapshot, state.VoteTo, state.Balance, state.BalanceHeight, end));
        }
Ejemplo n.º 9
0
        protected override void OnPersist(ApplicationEngine engine)
        {
            base.OnPersist(engine);

            // Set next committee
            if (ShouldRefreshCommittee(engine.Snapshot.PersistingBlock.Index))
            {
                StorageItem storageItem     = engine.Snapshot.Storages.GetAndChange(CreateStorageKey(Prefix_Committee));
                var         cachedCommittee = storageItem.GetInteroperable <CachedCommittee>();
                cachedCommittee.Clear();
                cachedCommittee.AddRange(ComputeCommitteeMembers(engine.Snapshot));
            }
        }
Ejemplo n.º 10
0
        private bool RegisterCandidate(ApplicationEngine engine, ECPoint pubkey)
        {
            if (!engine.CheckWitnessInternal(Contract.CreateSignatureRedeemScript(pubkey).ToScriptHash()))
            {
                return(false);
            }
            StorageKey     key   = CreateStorageKey(Prefix_Candidate).Add(pubkey);
            StorageItem    item  = engine.Snapshot.Storages.GetAndChange(key, () => new StorageItem(new CandidateState()));
            CandidateState state = item.GetInteroperable <CandidateState>();

            state.Registered = true;
            return(true);
        }
Ejemplo n.º 11
0
 protected override void OnBalanceChanging(ApplicationEngine engine, UInt160 account, NeoAccountState state, BigInteger amount)
 {
     DistributeGas(engine, account, state);
     if (amount.IsZero)
     {
         return;
     }
     if (state.VoteTo != null)
     {
         StorageItem    storage_validator = engine.Snapshot.Storages.GetAndChange(CreateStorageKey(Prefix_Candidate, state.VoteTo.ToArray()));
         CandidateState state_validator   = storage_validator.GetInteroperable <CandidateState>();
         state_validator.Votes += amount;
     }
 }
Ejemplo n.º 12
0
 internal protected virtual void Burn(ApplicationEngine engine, UInt160 account, BigInteger amount)
 {
     if (amount.Sign < 0) throw new ArgumentOutOfRangeException(nameof(amount));
     if (amount.IsZero) return;
     StorageKey key = CreateStorageKey(Prefix_Account).Add(account);
     StorageItem storage = engine.Snapshot.GetAndChange(key);
     TState state = storage.GetInteroperable<TState>();
     if (state.Balance < amount) throw new InvalidOperationException();
     OnBalanceChanging(engine, account, state, -amount);
     if (state.Balance == amount)
         engine.Snapshot.Delete(key);
     else
         state.Balance -= amount;
     storage = engine.Snapshot.GetAndChange(CreateStorageKey(Prefix_TotalSupply));
     storage.Add(-amount);
     PostTransfer(engine, account, null, amount, StackItem.Null, false);
 }
Ejemplo n.º 13
0
        private bool UnregisterCandidate(ApplicationEngine engine, ECPoint pubkey)
        {
            if (!engine.CheckWitnessInternal(Contract.CreateSignatureRedeemScript(pubkey).ToScriptHash()))
            {
                return(false);
            }
            StorageKey key = CreateStorageKey(Prefix_Candidate).Add(pubkey);

            if (engine.Snapshot.TryGet(key) is null)
            {
                return(true);
            }
            StorageItem    item  = engine.Snapshot.GetAndChange(key);
            CandidateState state = item.GetInteroperable <CandidateState>();

            state.Registered = false;
            CheckCandidate(engine.Snapshot, pubkey, state);
            return(true);
        }
Ejemplo n.º 14
0
        private bool UnregisterCandidate(StoreView snapshot, ECPoint pubkey)
        {
            StorageKey key = CreateStorageKey(Prefix_Candidate, pubkey);

            if (snapshot.Storages.TryGet(key) is null)
            {
                return(true);
            }
            StorageItem    item  = snapshot.Storages.GetAndChange(key);
            CandidateState state = item.GetInteroperable <CandidateState>();

            if (state.Votes.IsZero)
            {
                snapshot.Storages.Delete(key);
            }
            else
            {
                state.Registered = false;
            }
            return(true);
        }
Ejemplo n.º 15
0
        internal protected virtual void Mint(ApplicationEngine engine, UInt160 account, BigInteger amount)
        {
            if (amount.Sign < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(amount));
            }
            if (amount.IsZero)
            {
                return;
            }
            StorageItem storage = engine.Snapshot.Storages.GetAndChange(CreateStorageKey(Prefix_Account).Add(account), () => new StorageItem(new TState()));
            TState      state   = storage.GetInteroperable <TState>();

            OnBalanceChanging(engine, account, state, amount);
            state.Balance += amount;
            storage        = engine.Snapshot.Storages.GetAndChange(CreateStorageKey(Prefix_TotalSupply), () => new StorageItem(BigInteger.Zero));
            storage.Add(amount);
            engine.SendNotification(Hash, "Transfer", new Array {
                StackItem.Null, account.ToArray(), amount
            });
        }
Ejemplo n.º 16
0
        private bool Vote(StoreView snapshot, UInt160 account, ECPoint voteTo)
        {
            StorageKey key_account = CreateAccountKey(account);

            if (snapshot.Storages.TryGet(key_account) is null)
            {
                return(false);
            }
            StorageItem     storage_account = snapshot.Storages.GetAndChange(key_account);
            NeoAccountState state_account   = storage_account.GetInteroperable <NeoAccountState>();

            if (state_account.VoteTo != null)
            {
                StorageKey     key = CreateStorageKey(Prefix_Candidate, state_account.VoteTo.ToArray());
                StorageItem    storage_validator = snapshot.Storages.GetAndChange(key);
                CandidateState state_validator   = storage_validator.GetInteroperable <CandidateState>();
                state_validator.Votes -= state_account.Balance;
                if (!state_validator.Registered && state_validator.Votes.IsZero)
                {
                    snapshot.Storages.Delete(key);
                }
            }
            state_account.VoteTo = voteTo;
            if (voteTo != null)
            {
                StorageKey key = CreateStorageKey(Prefix_Candidate, voteTo.ToArray());
                if (snapshot.Storages.TryGet(key) is null)
                {
                    return(false);
                }
                StorageItem    storage_validator = snapshot.Storages.GetAndChange(key);
                CandidateState state_validator   = storage_validator.GetInteroperable <CandidateState>();
                if (!state_validator.Registered)
                {
                    return(false);
                }
                state_validator.Votes += state_account.Balance;
            }
            return(true);
        }
Ejemplo n.º 17
0
        internal protected virtual void Burn(ApplicationEngine engine, UInt160 account, BigInteger amount)
        {
            if (amount.Sign < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(amount));
            }
            if (amount.IsZero)
            {
                return;
            }
            StorageKey  key     = CreateStorageKey(Prefix_Account).Add(account);
            StorageItem storage = engine.Snapshot.Storages.GetAndChange(key);
            TState      state   = storage.GetInteroperable <TState>();

            if (state.Balance < amount)
            {
                throw new InvalidOperationException();
            }
            OnBalanceChanging(engine, account, state, -amount);
            if (state.Balance == amount)
            {
                engine.Snapshot.Storages.Delete(key);
            }
            else
            {
                state.Balance -= amount;
            }
            storage = engine.Snapshot.Storages.GetAndChange(CreateStorageKey(Prefix_TotalSupply));
            BigInteger totalSupply = new BigInteger(storage.Value);

            totalSupply  -= amount;
            storage.Value = totalSupply.ToByteArrayStandard();
            engine.SendNotification(Hash, "Transfer", new Array {
                account.ToArray(), StackItem.Null, amount
            });
        }
        public void Check_UnregisterCandidate()
        {
            var snapshot = Blockchain.Singleton.GetSnapshot();

            var keyCount = snapshot.Storages.GetChangeSet().Count();
            var point    = Blockchain.StandbyValidators[0].EncodePoint(true);

            //without register
            var ret = Check_UnregisterCandidate(snapshot, point);

            ret.State.Should().BeTrue();
            ret.Result.Should().BeTrue();
            snapshot.Storages.GetChangeSet().Count().Should().Be(keyCount);

            //register and then unregister
            ret = Check_RegisterValidator(snapshot, point);
            StorageItem item = snapshot.Storages.GetAndChange(CreateStorageKey(33, point));

            ret.State.Should().BeTrue();
            ret.Result.Should().BeTrue();

            var members = NativeContract.NEO.GetCandidates(snapshot);

            Assert.AreEqual(1, members.Length);
            snapshot.Storages.GetChangeSet().Count().Should().Be(keyCount + 1);
            StorageKey key = CreateStorageKey(33, point);

            snapshot.Storages.TryGet(key).Should().NotBeNull();

            ret = Check_UnregisterCandidate(snapshot, point);
            ret.State.Should().BeTrue();
            ret.Result.Should().BeTrue();
            snapshot.Storages.GetChangeSet().Count().Should().Be(keyCount);

            members = NativeContract.NEO.GetCandidates(snapshot);
            Assert.AreEqual(0, members.Length);
            snapshot.Storages.TryGet(key).Should().BeNull();

            //register with votes, then unregister
            ret = Check_RegisterValidator(snapshot, point);
            var G_Account = Contract.CreateSignatureContract(ECCurve.Secp256r1.G).ScriptHash.ToArray();

            snapshot.Storages.Add(CreateStorageKey(20, G_Account), new StorageItem(new NeoAccountState()));
            var accountState = snapshot.Storages.TryGet(CreateStorageKey(20, G_Account)).GetInteroperable <NeoAccountState>();

            accountState.Balance = 100;
            Check_Vote(snapshot, G_Account, Blockchain.StandbyValidators[0].ToArray(), true);
            ret = Check_UnregisterCandidate(snapshot, point);
            ret.State.Should().BeTrue();
            ret.Result.Should().BeTrue();
            snapshot.Storages.TryGet(key).Should().NotBeNull();
            StorageItem    pointItem  = snapshot.Storages.TryGet(key);
            CandidateState pointState = pointItem.GetInteroperable <CandidateState>();

            pointState.Registered.Should().BeFalse();
            pointState.Votes.Should().Be(100);

            //vote fail
            ret = Check_Vote(snapshot, G_Account, Blockchain.StandbyValidators[0].ToArray(), true);
            ret.State.Should().BeTrue();
            ret.Result.Should().BeFalse();
            accountState.VoteTo.Should().Be(Blockchain.StandbyValidators[0]);
        }
Ejemplo n.º 19
0
        protected virtual bool Transfer(ApplicationEngine engine, UInt160 from, UInt160 to, BigInteger amount)
        {
            if (amount.Sign < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(amount));
            }
            if (!from.Equals(engine.CallingScriptHash) && !engine.CheckWitnessInternal(from))
            {
                return(false);
            }
            ContractState contract_to = engine.Snapshot.Contracts.TryGet(to);

            if (contract_to?.Payable == false)
            {
                return(false);
            }
            StorageKey  key_from     = CreateStorageKey(Prefix_Account).Add(from);
            StorageItem storage_from = engine.Snapshot.Storages.GetAndChange(key_from);

            if (amount.IsZero)
            {
                if (storage_from != null)
                {
                    TState state_from = storage_from.GetInteroperable <TState>();
                    OnBalanceChanging(engine, from, state_from, amount);
                }
            }
            else
            {
                if (storage_from is null)
                {
                    return(false);
                }
                TState state_from = storage_from.GetInteroperable <TState>();
                if (state_from.Balance < amount)
                {
                    return(false);
                }
                if (from.Equals(to))
                {
                    OnBalanceChanging(engine, from, state_from, BigInteger.Zero);
                }
                else
                {
                    OnBalanceChanging(engine, from, state_from, -amount);
                    if (state_from.Balance == amount)
                    {
                        engine.Snapshot.Storages.Delete(key_from);
                    }
                    else
                    {
                        state_from.Balance -= amount;
                    }
                    StorageKey  key_to     = CreateStorageKey(Prefix_Account).Add(to);
                    StorageItem storage_to = engine.Snapshot.Storages.GetAndChange(key_to, () => new StorageItem(new TState()));
                    TState      state_to   = storage_to.GetInteroperable <TState>();
                    OnBalanceChanging(engine, to, state_to, amount);
                    state_to.Balance += amount;
                }
            }
            engine.SendNotification(Hash, "Transfer", new Array {
                from.ToArray(), to.ToArray(), amount
            });
            return(true);
        }