Ejemplo n.º 1
0
        private void DistributeGas(ApplicationEngine engine, UInt160 account, AccountState 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);
            engine.Snapshot.Storages.GetAndChange(CreateAccountKey(account)).Value = state.ToByteArray();
        }
Ejemplo n.º 2
0
        private StackItem Vote(ApplicationEngine engine, VMArray args)
        {
            UInt160 account = new UInt160(args[0].GetByteArray());

            ECPoint[] pubkeys = ((VMArray)args[1]).Select(p => p.GetByteArray().AsSerializable <ECPoint>()).ToArray();
            if (!InteropService.CheckWitness(engine, account))
            {
                return(false);
            }
            StorageKey key_account = CreateAccountKey(account);

            if (engine.Snapshot.Storages.TryGet(key_account) is null)
            {
                return(false);
            }
            StorageItem  storage_account = engine.Snapshot.Storages.GetAndChange(key_account);
            AccountState state_account   = new AccountState(storage_account.Value);

            foreach (ECPoint pubkey in state_account.Votes)
            {
                StorageItem    storage_validator = engine.Snapshot.Storages.GetAndChange(CreateStorageKey(Prefix_Validator, pubkey.ToArray()));
                ValidatorState state_validator   = ValidatorState.FromByteArray(storage_validator.Value);
                state_validator.Votes  -= state_account.Balance;
                storage_validator.Value = state_validator.ToByteArray();
            }
            pubkeys = pubkeys.Distinct().Where(p => engine.Snapshot.Storages.TryGet(CreateStorageKey(Prefix_Validator, p.ToArray())) != null).ToArray();
            if (pubkeys.Length != state_account.Votes.Length)
            {
                StorageItem storage_count = engine.Snapshot.Storages.GetAndChange(CreateStorageKey(Prefix_ValidatorsCount), () => new StorageItem
                {
                    Value = new ValidatorsCountState().ToByteArray()
                });
                ValidatorsCountState state_count = ValidatorsCountState.FromByteArray(storage_count.Value);
                if (state_account.Votes.Length > 0)
                {
                    state_count.Votes[state_account.Votes.Length - 1] -= state_account.Balance;
                }
                if (pubkeys.Length > 0)
                {
                    state_count.Votes[pubkeys.Length - 1] += state_account.Balance;
                }
                storage_count.Value = state_count.ToByteArray();
            }
            state_account.Votes   = pubkeys;
            storage_account.Value = state_account.ToByteArray();
            foreach (ECPoint pubkey in state_account.Votes)
            {
                StorageItem    storage_validator = engine.Snapshot.Storages.GetAndChange(CreateStorageKey(Prefix_Validator, pubkey.ToArray()));
                ValidatorState state_validator   = ValidatorState.FromByteArray(storage_validator.Value);
                state_validator.Votes  += state_account.Balance;
                storage_validator.Value = state_validator.ToByteArray();
            }
            return(true);
        }