All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hedera.hapi.streams.schema.StorageChangeSchema Maven / Gradle / Ivy

There is a newer version: 0.54.0
Show newest version
package com.hedera.hapi.streams.schema;

import com.hedera.pbj.runtime.FieldDefinition;
import com.hedera.pbj.runtime.FieldType;
import com.hedera.pbj.runtime.Schema;


/**
 * Schema for StorageChange model object. Generate based on protobuf schema.
 */
public final class StorageChangeSchema implements Schema {

    // -- FIELD DEFINITIONS ---------------------------------------------

    /**
     * (1) The storage slot changed.  Up to 32 bytes, big-endian, zero bytes left trimmed.
     */
    public static final FieldDefinition SLOT = new FieldDefinition("slot", FieldType.BYTES, false, false, false, 1);


    /**
     * (2) The value read from the storage slot.  Up to 32 bytes, big-endian, zero bytes left trimmed.
     *    *
     * Because of the way SSTORE operations are charged the slot is always read before being written to.
     */
    public static final FieldDefinition VALUE_READ = new FieldDefinition("value_read", FieldType.BYTES, false, false, false, 2);


    /**
     * (3) The new value written to the slot.  Up to 32 bytes, big-endian, zero bytes left trimmed.
     *    *
     * If a value of zero is written the valueWritten will be present but the inner value will be absent.
     *    *
     * If a value was read and not written this value will not be present.
     */
    public static final FieldDefinition VALUE_WRITTEN = new FieldDefinition("value_written", FieldType.BYTES, false, true, false, 3);


    // -- OTHER METHODS -------------------------------------------------

    /**
     * Check if a field definition belongs to this schema.
     *
     * @param f field def to check
     * @return true if it belongs to this schema
     */
    public static boolean valid(FieldDefinition f) {
    	return f != null && getField(f.number()) == f;
    }

	/**
	 * Get a field definition given a field number
	 *
	 * @param fieldNumber the fields number to get def for
	 * @return field def or null if field number does not exist
	 */
	public static FieldDefinition getField(final int fieldNumber) {
	    return switch(fieldNumber) {
	        case 1 -> SLOT;
            case 2 -> VALUE_READ;
            case 3 -> VALUE_WRITTEN;
	        default -> null;
	    };
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy