Esempio n. 1
0
        private StackItem UnclaimedGas(ApplicationEngine engine, VMArray args)
        {
            UInt160 account = new UInt160(args[0].GetByteArray());
            uint    end     = (uint)args[1].GetBigInteger();

            return(UnclaimedGas(engine.Snapshot, account, end));
        }
Esempio 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);
        }
Esempio n. 3
0
 private StackItem GetNextBlockValidators(ApplicationEngine engine, VMArray args)
 {
     return(GetNextBlockValidators(engine.Snapshot).Select(p => (StackItem)p.ToArray()).ToList());
 }
Esempio n. 4
0
 private StackItem GetRegisteredValidators(ApplicationEngine engine, VMArray args)
 {
     return(GetRegisteredValidators(engine.Snapshot).Select(p => new Struct(new StackItem[] { p.PublicKey.ToArray(), p.Votes })).ToArray());
 }
Esempio n. 5
0
        private StackItem RegisterValidator(ApplicationEngine engine, VMArray args)
        {
            ECPoint pubkey = args[0].GetByteArray().AsSerializable <ECPoint>();

            return(RegisterValidator(engine.Snapshot, pubkey));
        }
Esempio n. 6
0
        private static StackItem DeserializeStackItem(BinaryReader reader, uint maxArraySize, uint maxItemSize)
        {
            Stack <StackItem> deserialized = new Stack <StackItem>();
            int undeserialized             = 1;

            while (undeserialized-- > 0)
            {
                StackItemType type = (StackItemType)reader.ReadByte();
                switch (type)
                {
                case StackItemType.ByteArray:
                    deserialized.Push(new ByteArray(reader.ReadVarBytes((int)maxItemSize)));
                    break;

                case StackItemType.Boolean:
                    deserialized.Push(new VMBoolean(reader.ReadBoolean()));
                    break;

                case StackItemType.Integer:
                    deserialized.Push(new Integer(new BigInteger(reader.ReadVarBytes(ExecutionEngine.MaxSizeForBigInteger))));
                    break;

                case StackItemType.Array:
                case StackItemType.Struct:
                {
                    int count = (int)reader.ReadVarInt(maxArraySize);
                    deserialized.Push(new ContainerPlaceholder
                        {
                            Type         = type,
                            ElementCount = count
                        });
                    undeserialized += count;
                }
                break;

                case StackItemType.Map:
                {
                    int count = (int)reader.ReadVarInt(maxArraySize);
                    deserialized.Push(new ContainerPlaceholder
                        {
                            Type         = type,
                            ElementCount = count
                        });
                    undeserialized += count * 2;
                }
                break;

                default:
                    throw new FormatException();
                }
            }
            Stack <StackItem> stack_temp = new Stack <StackItem>();

            while (deserialized.Count > 0)
            {
                StackItem item = deserialized.Pop();
                if (item is ContainerPlaceholder placeholder)
                {
                    switch (placeholder.Type)
                    {
                    case StackItemType.Array:
                        VMArray array = new VMArray();
                        for (int i = 0; i < placeholder.ElementCount; i++)
                        {
                            array.Add(stack_temp.Pop());
                        }
                        item = array;
                        break;

                    case StackItemType.Struct:
                        Struct @struct = new Struct();
                        for (int i = 0; i < placeholder.ElementCount; i++)
                        {
                            @struct.Add(stack_temp.Pop());
                        }
                        item = @struct;
                        break;

                    case StackItemType.Map:
                        Map map = new Map();
                        for (int i = 0; i < placeholder.ElementCount; i++)
                        {
                            StackItem key   = stack_temp.Pop();
                            StackItem value = stack_temp.Pop();
                            map.Add(key, value);
                        }
                        item = map;
                        break;
                    }
                }
                stack_temp.Push(item);
            }
            return(stack_temp.Peek());
        }