public bool DirectoryExists(string path) { if (mCache.IsValid("DE", path)) { return((bool)mCache.Get("DE", path)); } var exists = mClient.DirectoryExists(path); mCache.Add("DE", path, exists); return(exists); }
public XmlMapper() { if (MetaDataCache.Contains <T>()) { ClassMetaData metaData = MetaDataCache.Get <T>(); if (metaData.ClassAttributeContext.ContainsAttribute <XmlMapperAttribute>()) { xmlMapperAttribute = metaData.ClassAttributeContext.GetAttribute <XmlMapperAttribute>(); } } }
public virtual IEnumerable <ECPoint> GetValidators(IEnumerable <Transaction> others) { TR.Enter(); DataCache <UInt160, AccountState> accounts = GetStates <UInt160, AccountState>(); DataCache <ECPoint, ValidatorState> validators = GetStates <ECPoint, ValidatorState>(); MetaDataCache <ValidatorsCountState> validators_count = GetMetaData <ValidatorsCountState>(); foreach (Transaction tx in others) { foreach (TransactionOutput output in tx.Outputs) { AccountState account = accounts.GetAndChange(output.ScriptHash, () => new AccountState(output.ScriptHash)); if (account.Balances.ContainsKey(output.AssetId)) { account.Balances[output.AssetId] += output.Value; } else { account.Balances[output.AssetId] = output.Value; } if (output.AssetId.Equals(GoverningToken.Hash) && account.Votes.Length > 0) { foreach (ECPoint pubkey in account.Votes) { validators.GetAndChange(pubkey, () => new ValidatorState(pubkey)).Votes += output.Value; } validators_count.GetAndChange().Votes[account.Votes.Length - 1] += output.Value; } } foreach (var group in tx.Inputs.GroupBy(p => p.PrevHash)) { Transaction tx_prev = GetTransaction(group.Key, out int height); foreach (CoinReference input in group) { TransactionOutput out_prev = tx_prev.Outputs[input.PrevIndex]; AccountState account = accounts.GetAndChange(out_prev.ScriptHash); if (out_prev.AssetId.Equals(GoverningToken.Hash)) { if (account.Votes.Length > 0) { foreach (ECPoint pubkey in account.Votes) { ValidatorState validator = validators.GetAndChange(pubkey); validator.Votes -= out_prev.Value; if (!validator.Registered && validator.Votes.Equals(Fixed8.Zero)) { validators.Delete(pubkey); } } validators_count.GetAndChange().Votes[account.Votes.Length - 1] -= out_prev.Value; } } account.Balances[out_prev.AssetId] -= out_prev.Value; } } switch (tx) { #pragma warning disable CS0612 case EnrollmentTransaction tx_enrollment: validators.GetAndChange(tx_enrollment.PublicKey, () => new ValidatorState(tx_enrollment.PublicKey)).Registered = true; break; #pragma warning restore CS0612 case StateTransaction tx_state: foreach (StateDescriptor descriptor in tx_state.Descriptors) { switch (descriptor.Type) { case StateType.Account: ProcessAccountStateDescriptor(descriptor, accounts, validators, validators_count); break; case StateType.Validator: ProcessValidatorStateDescriptor(descriptor, validators); break; } } break; } } int count = (int)validators_count.Get().Votes.Select((p, i) => new { Count = i, Votes = p }).Where(p => p.Votes > Fixed8.Zero).ToArray().WeightedFilter(0.25, 0.75, p => p.Votes.GetData(), (p, w) => new { p.Count, Weight = w }).WeightedAverage(p => p.Count, p => p.Weight); count = Math.Max(count, StandbyValidators.Length); HashSet <ECPoint> sv = new HashSet <ECPoint>(StandbyValidators); ECPoint[] pubkeys = validators.Find().Select(p => p.Value).Where(p => (p.Registered && p.Votes > Fixed8.Zero) || sv.Contains(p.PublicKey)).OrderByDescending(p => p.Votes).ThenBy(p => p.PublicKey).Select(p => p.PublicKey).Take(count).ToArray(); IEnumerable <ECPoint> result; if (pubkeys.Length == count) { result = pubkeys; } else { HashSet <ECPoint> hashSet = new HashSet <ECPoint>(pubkeys); for (int i = 0; i < StandbyValidators.Length && hashSet.Count < count; i++) { hashSet.Add(StandbyValidators[i]); } result = hashSet; } return(TR.Exit(result.OrderBy(p => p))); }
protected override T TryGetInternal() { return(innerCache.Get().Clone()); }
private object ToObject(object instance, XmlNode xmlNode, string nodeName = null) { Type type = instance.GetType(); ClassMetaData classMetaData = MetaDataCache.Get(type); XmlMappingOperation xmlMappingOperation = xmlMapperAttribute.MappingOperation; if (classMetaData.ClassAttributeContext.ContainsAttribute <XmlMapperAttribute>()) { XmlMapperAttribute xmlMapper = classMetaData.ClassAttributeContext.GetAttribute <XmlMapperAttribute>(); xmlMappingOperation = xmlMapper.MappingOperation; if (nodeName == null) { nodeName = xmlMapper.ParentNodeName; xmlNode = xmlNode.SelectSingleNode($"//{nodeName}"); } } foreach (IFieldPropertyInfo property in classMetaData.Properties) { string propertyName = property.Name; System.Type propertyType = property.Type; if (classMetaData.HasPropertyAttributeContext <PropertyAttributeContext>(property)) { PropertyAttributeContext propertyAttributeContext = classMetaData.GetAttributeContextForProperty <PropertyAttributeContext>(property); if (propertyAttributeContext.HasIgnoreAttribute) { continue; } propertyName = propertyAttributeContext.HasNameAttribute ? propertyAttributeContext.NameAttribute.Name : propertyName; } object propertyValue = GetNodeValue(xmlMappingOperation, xmlNode, nodeName, propertyName); if (classMetaData.HasPropertyAttributeContext <XmlPropertyAttributeContext>(property)) { XmlPropertyAttributeContext xmlPropertyAttributeContext = classMetaData.GetAttributeContextForProperty <XmlPropertyAttributeContext>(property); if (xmlPropertyAttributeContext.HasXmlListAttribute) { XmlListAttribute xmlList = xmlPropertyAttributeContext.XmlListAttribute; XmlNodeList results = xmlNode.SelectNodes($"//{nodeName}/{propertyName}/{xmlList.NodeName}"); if (results.Count > 0) { object childInstance = CollectionXmlNodeListToObject(results, propertyType); property.SetValue(instance, childInstance); } continue; } else if (xmlPropertyAttributeContext.HasXmlDictionaryAttribute) { XmlDictionaryAttribute xmlList = xmlPropertyAttributeContext.XmlDictionaryAttribute; XmlNodeList results = xmlNode.SelectNodes($"//{nodeName}/{propertyName}/*"); if (results.Count > 0) { object childInstance = DictionaryXmlNodeListToObject(results, propertyType); property.SetValue(instance, childInstance); } continue; } else if (xmlPropertyAttributeContext.HasXmlFlattenHierarchyAttribute) { object childInstance = Activator.CreateInstance(propertyType); childInstance = ToObject(childInstance, xmlNode, nodeName); property.SetValue(instance, childInstance); continue; } else if (xmlPropertyAttributeContext.HasXmlPropertyConverterAttribute && propertyValue != null) { XmlPropertyConverterAttribute converter = xmlPropertyAttributeContext.XmlPropertyConverterAttribute; try { propertyValue = converter.ConvertToSourceType(propertyValue); } catch (Exception ex) { throw new XmlPropertyConverterException("XmlPropertyConverter threw an exception", ex); } } } else { if (propertyType.IsDictionary()) { XmlNodeList results = xmlNode.SelectNodes($"//{nodeName}/{propertyName}/*"); if (results.Count > 0) { object childInstance = DictionaryXmlNodeListToObject(results, propertyType); property.SetValue(instance, childInstance); } continue; } else if (propertyType.IsCollection()) { string listItemNodeName = propertyType.GenericTypeArguments[0].Name; XmlNodeList results = xmlNode.SelectNodes($"//{nodeName}/{propertyName}/{listItemNodeName}"); if (results.Count > 0) { object childInstance = CollectionXmlNodeListToObject(results, propertyType); property.SetValue(instance, childInstance); } continue; } if (propertyType.IsClass && MetaDataCache.Contains(propertyType)) { // TODO: Dont think this will work object childInstance = Activator.CreateInstance(propertyType); XmlNode results = xmlNode.SelectSingleNode($"//{nodeName}/{propertyName}"); if (results != null) { childInstance = ToObject(childInstance, results, propertyName); property.SetValue(instance, childInstance); } continue; } } if (propertyValue == null) { continue; } property.SetValue(instance, UniversalTypeConverter.Convert(propertyValue, propertyType)); } return(instance); }
private XElement ToXml(object instance, Type type, XElement element = null, string nodeName = null) { ClassMetaData classMetaData = MetaDataCache.Get(type); XmlMappingOperation xmlMappingOperation = xmlMapperAttribute.MappingOperation; bool ignoreNulls = xmlMapperAttribute.IgnoreNulls; if (classMetaData.ClassAttributeContext.ContainsAttribute <XmlMapperAttribute>()) { XmlMapperAttribute xmlMapper = classMetaData.ClassAttributeContext.GetAttribute <XmlMapperAttribute>(); xmlMappingOperation = xmlMapper.MappingOperation; ignoreNulls = xmlMapper.IgnoreNulls; if (element == null) { element = new XElement(xmlMapper.ParentNodeName); nodeName = xmlMapper.ParentNodeName; } } //element.Name = nodeName; foreach (IFieldPropertyInfo property in classMetaData.Properties) { string propertyName = property.Name; object propertyValue = property.GetValue(instance); Type propertyType = property.Type; if (propertyValue == null && ignoreNulls) { continue; } if (classMetaData.HasPropertyAttributeContext <PropertyAttributeContext>(property)) { PropertyAttributeContext propertyAttributeContext = classMetaData.GetAttributeContextForProperty <PropertyAttributeContext>(property); if (propertyAttributeContext.HasIgnoreAttribute) { continue; } propertyName = propertyAttributeContext.HasNameAttribute ? propertyAttributeContext.NameAttribute.Name : propertyName; } if (classMetaData.HasPropertyAttributeContext <XmlPropertyAttributeContext>(property)) { XmlPropertyAttributeContext xmlPropertyAttributeContext = classMetaData.GetAttributeContextForProperty <XmlPropertyAttributeContext>(property); if (xmlPropertyAttributeContext.HasXmlPropertyConverterAttribute && propertyValue != null) { XmlPropertyConverterAttribute converter = xmlPropertyAttributeContext.XmlPropertyConverterAttribute; try { propertyValue = converter.ConvertToDestinationType(propertyValue); AddToXElement(element, xmlMappingOperation, propertyName, propertyValue); continue; } catch (Exception ex) { throw new XmlPropertyConverterException("XmlPropertyConverter threw an exception", ex); } } else if (xmlPropertyAttributeContext.HasXmlDictionaryAttribute) { XmlDictionaryAttribute xmlDictionary = xmlPropertyAttributeContext.XmlDictionaryAttribute; element.Add(DictionaryToXElement(propertyValue, propertyName)); continue; } else if (xmlPropertyAttributeContext.HasXmlListAttribute) { XmlListAttribute xmlList = xmlPropertyAttributeContext.XmlListAttribute; element.Add(CollectionToXElement(propertyValue, propertyName, xmlList.NodeName)); continue; } else if (xmlPropertyAttributeContext.HasXmlFlattenHierarchyAttribute) { element = ToXml(propertyValue, propertyType, element, propertyName); continue; } } else { if (propertyType.IsDictionary()) { element.Add(DictionaryToXElement(propertyValue, propertyName)); continue; } else if (propertyType.IsCollection()) { element.Add(CollectionToXElement(propertyValue, propertyName, propertyType.GenericTypeArguments[0].Name)); continue; } else if (propertyType.IsClass && MetaDataCache.Contains(propertyType)) { XElement propertyElement = new XElement(propertyName); propertyElement = ToXml(propertyValue, propertyType, propertyElement, propertyName); element.Add(propertyElement); continue; } } AddToXElement(element, xmlMappingOperation, propertyName, propertyValue); } return(element); }