示例#1
0
        private void DistributeGas(ApplicationEngine engine, UInt160 account, NeoAccountState state)
        {
            BigInteger gas = CalculateBonus(engine.Snapshot, state.Balance, state.BalanceHeight, engine.Snapshot.PersistingBlock.Index);

            state.BalanceHeight = engine.Snapshot.PersistingBlock.Index;
            GAS.Mint(engine, account, gas);
        }
示例#2
0
        public void TestEconomicParameter()
        {
            var snapshot = Blockchain.Singleton.GetSnapshot().Clone();

            snapshot.PersistingBlock = new Block {
                Index = 0
            };

            (BigInteger, bool)result = Check_GetGasPerBlock(snapshot);
            result.Item2.Should().BeTrue();
            result.Item1.Should().Be(5 * NativeContract.GAS.Factor);

            snapshot.PersistingBlock = new Block {
                Index = 10
            };
            (VM.Types.Boolean, bool)result1 = Check_SetGasPerBlock(snapshot, 10 * NativeContract.GAS.Factor);
            result1.Item2.Should().BeTrue();
            result1.Item1.GetBoolean().Should().BeTrue();

            snapshot.PersistingBlock.Index++;
            result = Check_GetGasPerBlock(snapshot);
            result.Item2.Should().BeTrue();
            result.Item1.Should().Be(10 * NativeContract.GAS.Factor);

            // Check calculate bonus
            StorageItem     storage = snapshot.Storages.GetOrAdd(CreateStorageKey(20, UInt160.Zero.ToArray()), () => new StorageItem(new NeoAccountState()));
            NeoAccountState state   = storage.GetInteroperable <NeoAccountState>();

            state.Balance       = 1000;
            state.BalanceHeight = 0;
            NativeContract.NEO.UnclaimedGas(snapshot, UInt160.Zero, snapshot.PersistingBlock.Index + 1).Should().Be(6500);
        }
示例#3
0
        private bool Vote(ApplicationEngine engine, UInt160 account, ECPoint voteTo)
        {
            if (!engine.CheckWitnessInternal(account))
            {
                return(false);
            }
            NeoAccountState state_account = engine.Snapshot.Storages.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.Storages.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.Storages.GetAndChange(CreateStorageKey(Prefix_VotersCount));
                if (state_account.VoteTo is null)
                {
                    item.Add(state_account.Balance);
                }
                else
                {
                    item.Add(-state_account.Balance);
                }
            }
            if (state_account.VoteTo != null)
            {
                StorageKey     key = CreateStorageKey(Prefix_Candidate).Add(state_account.VoteTo);
                StorageItem    storage_validator = engine.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)
                {
                    engine.Snapshot.Storages.Delete(key);
                }
            }
            state_account.VoteTo = voteTo;
            if (validator_new != null)
            {
                validator_new.Votes += state_account.Balance;
            }
            return(true);
        }
示例#4
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));
        }
示例#5
0
        private void DistributeGas(ApplicationEngine engine, UInt160 account, NeoAccountState state)
        {
            // PersistingBlock is null when running under the debugger
            if (engine.Snapshot.PersistingBlock == null)
            {
                return;
            }

            BigInteger gas = CalculateBonus(engine.Snapshot, state.Balance, state.BalanceHeight, engine.Snapshot.PersistingBlock.Index);

            state.BalanceHeight = engine.Snapshot.PersistingBlock.Index;
            GAS.Mint(engine, account, gas);
        }
示例#6
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);
        }
示例#7
0
        protected override void OnBalanceChanging(ApplicationEngine engine, UInt160 account, NeoAccountState state, BigInteger amount)
        {
            DistributeGas(engine, account, state);
            if (amount.IsZero)
            {
                return;
            }
            if (state.VoteTo is null)
            {
                return;
            }
            engine.Snapshot.Storages.GetAndChange(CreateStorageKey(Prefix_VotersCount)).Add(amount);
            StorageKey     key       = CreateStorageKey(Prefix_Candidate).Add(state.VoteTo);
            CandidateState candidate = engine.Snapshot.Storages.GetAndChange(key).GetInteroperable <CandidateState>();

            candidate.Votes += amount;
            CheckCandidate(engine.Snapshot, key, candidate);
        }
示例#8
0
        private GasDistribution DistributeGas(ApplicationEngine engine, UInt160 account, NeoAccountState state)
        {
            // PersistingBlock is null when running under the debugger
            if (engine.PersistingBlock is null)
            {
                return(null);
            }

            BigInteger gas = CalculateBonus(engine.Snapshot, state.VoteTo, state.Balance, state.BalanceHeight, engine.PersistingBlock.Index);

            state.BalanceHeight = engine.PersistingBlock.Index;

            if (gas == 0)
            {
                return(null);
            }
            return(new GasDistribution
            {
                Account = account,
                Amount = gas
            });
        }
示例#9
0
        internal override void OnBalanceChanging(ApplicationEngine engine, UInt160 account, NeoAccountState state, BigInteger amount)
        {
            GasDistribution distribution = DistributeGas(engine, account, state);

            if (distribution is not null)
            {
                var list = engine.CurrentContext.GetState <List <GasDistribution> >();
                list.Add(distribution);
            }
            if (amount.IsZero)
            {
                return;
            }
            if (state.VoteTo is null)
            {
                return;
            }
            engine.Snapshot.GetAndChange(CreateStorageKey(Prefix_VotersCount)).Add(amount);
            StorageKey     key       = CreateStorageKey(Prefix_Candidate).Add(state.VoteTo);
            CandidateState candidate = engine.Snapshot.GetAndChange(key).GetInteroperable <CandidateState>();

            candidate.Votes += amount;
            CheckCandidate(engine.Snapshot, state.VoteTo, candidate);
        }
示例#10
0
        private async ContractTask DistributeGas(ApplicationEngine engine, UInt160 account, NeoAccountState state)
        {
            // PersistingBlock is null when running under the debugger
            if (engine.PersistingBlock is null)
            {
                return;
            }

            BigInteger gas = CalculateBonus(engine.Snapshot, state.VoteTo, state.Balance, state.BalanceHeight, engine.PersistingBlock.Index);

            state.BalanceHeight = engine.PersistingBlock.Index;
            await GAS.Mint(engine, account, gas, true);
        }
示例#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;
     }
 }