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); }
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); }
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); }
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)); }
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); }
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); }
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); }
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 }); }
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); }
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); }
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; } }