Example #1
0
        public static async Task <IReadOnlyList <AccountStatus> > GetAccount(this ILedgerQueries queries, string account)
        {
            ByteString             prefix  = new ByteString(Encoding.UTF8.GetBytes(account + ":ACC:"));
            IReadOnlyList <Record> records = await queries.GetKeyStartingFrom(prefix);

            return(records
                   .Where(record => !record.Value.Equals(ByteString.Empty))
                   .Select(record => AccountStatus.FromRecord(RecordKey.Parse(record.Key), record))
                   .ToList()
                   .AsReadOnly());
        }
Example #2
0
        public static ParsedMutation Parse(Mutation mutation)
        {
            List <AccountStatus> accountMutations = new List <AccountStatus>();
            List <KeyValuePair <RecordKey, ByteString> > dataRecords = new List <KeyValuePair <RecordKey, ByteString> >();

            foreach (Record record in mutation.Records)
            {
                // This is used for optimistic concurrency and does not participate in the validation
                if (record.Value == null)
                {
                    continue;
                }

                try
                {
                    RecordKey key = RecordKey.Parse(record.Key);
                    switch (key.RecordType)
                    {
                    case RecordType.Account:
                        accountMutations.Add(AccountStatus.FromRecord(key, record));
                        break;

                    case RecordType.Data:
                        dataRecords.Add(new KeyValuePair <RecordKey, ByteString>(key, record.Value));
                        break;
                    }
                }
                catch (ArgumentOutOfRangeException ex) when(ex.ParamName == "keyData")
                {
                    // Deserializing and re-serializing the record gives a different result
                    throw new TransactionInvalidException("NonCanonicalSerialization");
                }
                catch (ArgumentOutOfRangeException ex) when(ex.ParamName == "path")
                {
                    // The path is invalid
                    throw new TransactionInvalidException("InvalidPath");
                }
                catch (ArgumentOutOfRangeException ex) when(ex.ParamName == "recordType")
                {
                    // The specified record type is unknown
                    throw new TransactionInvalidException("InvalidRecord");
                }
                catch (ArgumentOutOfRangeException ex) when(ex.ParamName == "record")
                {
                    // The value of an ACC record could not be deserialized
                    throw new TransactionInvalidException("InvalidRecord");
                }
            }

            return(new ParsedMutation(accountMutations, dataRecords));
        }
Example #3
0
        public static async Task <IReadOnlyDictionary <AccountKey, AccountStatus> > GetAccounts(this IStorageEngine store, IEnumerable <AccountKey> accounts)
        {
            IReadOnlyList <Record> records = await store.GetRecords(accounts.Select(account => account.Key.ToBinary()));

            return(records.Select(record => AccountStatus.FromRecord(RecordKey.Parse(record.Key), record)).ToDictionary(account => account.AccountKey, account => account));
        }