예제 #1
0
        public void TestValidatorState_ToByteArray()
        {
            ValidatorState input = new ValidatorState {
                Votes = new BigInteger(1000)
            };

            input.ToByteArray().ToHexString().Should().Be("e803");
        }
예제 #2
0
        public void TestValidatorState_FromByteArray()
        {
            ValidatorState input = new ValidatorState {
                Votes = new BigInteger(1000)
            };
            ValidatorState output = ValidatorState.FromByteArray(input.ToByteArray());

            output.Should().BeEquivalentTo(input);
        }
예제 #3
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);
        }
예제 #4
0
        protected override void OnBalanceChanging(ApplicationEngine engine, UInt160 account, AccountState state, BigInteger amount)
        {
            DistributeGas(engine, account, state);
            if (amount.IsZero)
            {
                return;
            }
            if (state.Votes.Length == 0)
            {
                return;
            }
            foreach (ECPoint pubkey in state.Votes)
            {
                StorageItem    storage_validator = engine.Snapshot.Storages.GetAndChange(CreateStorageKey(Prefix_Validator, pubkey.ToArray()));
                ValidatorState state_validator   = ValidatorState.FromByteArray(storage_validator.Value);
                state_validator.Votes  += amount;
                storage_validator.Value = state_validator.ToByteArray();
            }
            StorageItem          storage_count = engine.Snapshot.Storages.GetAndChange(CreateStorageKey(Prefix_ValidatorsCount));
            ValidatorsCountState state_count   = ValidatorsCountState.FromByteArray(storage_count.Value);

            state_count.Votes[state.Votes.Length - 1] += amount;
            storage_count.Value = state_count.ToByteArray();
        }