/// <summary> /// Serializes the log and topics to bytes. /// </summary> /// <returns></returns> private (List <byte[]>, List <byte[]>) Serialize(IContractPrimitiveSerializer serializer) { var logType = this.LogStruct.GetType(); // first topic is the log type name var logTypeName = serializer.Serialize(logType.Name); var topics = new List <byte[]> { logTypeName }; var fields = new List <byte[]>(); // rest of the topics are the indexed fields. foreach (FieldInfo field in logType.GetFields()) { object value = field.GetValue(this.LogStruct); byte[] serialized = value != null ? serializer.Serialize(value) : new byte[0]; if (field.CustomAttributes.Any(y => y.AttributeType == typeof(IndexAttribute))) { // It's an index, add to the topics topics.Add(serialized); } fields.Add(serialized); } return(topics, fields); }
/// <summary> /// Transforms this log into the log type used by consensus. /// /// TODO: Cache this value. /// </summary> public Log ToLog(IContractPrimitiveSerializer serializer) { var topics = new List <byte[]>(); // first topic is the log type name topics.Add(Encoding.UTF8.GetBytes(this.LogStruct.GetType().Name)); // rest of the topics are the indexed fields. foreach (FieldInfo field in this.LogStruct.GetType().GetFields().Where(x => x.CustomAttributes.Any(y => y.AttributeType == typeof(IndexAttribute)))) { object value = field.GetValue(this.LogStruct); byte[] serialized = serializer.Serialize(value); topics.Add(serialized); } return(new Log(this.ContractAddress, topics, serializer.Serialize(this.LogStruct))); }
/// <summary> /// Transforms this log into the log type used by consensus. /// </summary> public Log ToLog(IContractPrimitiveSerializer serializer) { var topics = new List <byte[]>(); // first topic is the log type name topics.Add(Encoding.UTF8.GetBytes(this.LogStruct.GetType().Name)); // rest of the topics are the indexed fields. TODO: This currently gets all fields. foreach (FieldInfo field in this.LogStruct.GetType().GetFields()) { object value = field.GetValue(this.LogStruct); byte[] serialized = serializer.Serialize(value); topics.Add(serialized); } return(new Log(this.ContractAddress, topics, serializer.Serialize(this.LogStruct))); }