Package convex.core.data
Class Address
- All Implemented Interfaces:
IValidated
,IWriteable
,Comparable<ABlobLike<?>>
Immutable class representing an Address, generally used to uniquely identify an Account.
An Address is a specialised 8-byte long blob instance that wraps a non-negative long Account number. This number
serves as an index into the vector of accounts for the current state.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
static final int
static final Address
The maximum possible Addressstatic final Address
The Zero AddressFields inherited from class convex.core.data.ACell
cachedRef, memorySize
-
Method Summary
Modifier and TypeMethodDescriptionfinal byte
byteAt
(long i) Gets the byte at the specified position.final byte
byteAtUnchecked
(long i) Gets the byte at the specified position in this data object, possibly skipping bounds checking.protected long
Calculates the Memory Size for this Cell.protected int
compareTo
(long bvalue) int
long
count()
Returns the number of elements in this valuestatic Address
create
(long number) Obtains an Address.static Address
Obtains an Address from a blob.empty()
Returns a canonical, singleton empty instance of the same type as this Countable value.int
encode
(byte[] bs, int pos) Writes this Cell's encoding to a byte array, including a tag byte which will be written first.int
encodeRaw
(byte[] bs, int pos) Writes this Cell's encoding to a byte array, excluding the tag byte.boolean
Checks for equality with another Cell.final boolean
boolean
boolean
equalsBytes
(ABlob b) Compare the byte content of this BlobLike to a Blob valueint
Estimate the encoded data size for this Cell.static Address
Obtains an Address from a hex stringget
(long i) Gets the element at the specified element index in this valuefinal int
getBytes
(byte[] bs, int pos) Copies the bytes from this instance to a given destination arraygetRef
(int i) Gets a numbered child Ref from within this Cell.int
Gets the number of Refs contained within this Cell.byte
getTag()
Gets the tag byte for this cell.getType()
Gets the most specific known runtime Type for this Cell.int
hashCode()
Gets the Java hashCode for this cell.long
Returns the number of matching hex digits in the given hex range of another Blob.boolean
Returns true if this Cell is in a canonical representation.boolean
Returns true if this cell is a first class CVM Value allowable in the CVM state Sub-structural cells that are not themselves first class values should return false Records and types that are not permissible on the CVM should return false.long
Converts this BlobLike to the corresponding long value.offset
(long offset) Creates a new Address at an offset to this Addressstatic Address
Attempts to parse an address from an arbitrary object.static Address
Obtains an Address from an arbitrary String, attempting to parse possible formats '123' '0xc3' or '#123'boolean
print
(BlobBuilder sb, long limit) Prints this Object to a readable String Representation.static Address
slice
(long start, long end) Gets a slice of this data structuretoBlob()
Convert this BlobLike object to an ABlob instance, in the most efficient way.Converts this Cell to a canonical version.toCVMString
(long limit) Returns the CVM String representation of this Cell.Converts the contents of this value to a flat array-backed Blob instance.toString()
Returns the Java String representation of this Cell.static Address
unsafeCreate
(long number) Creates an Address without checking.updateRefs
(IRefFunction func) Updates all Refs in this object using the given function.void
Validates the local structure and invariants of this cell.Methods inherited from class convex.core.data.ABlobLike
appendHex, getBytes, getElementRef, getHexDigit, hexEquals, hexLength, hexMatch, isDataValue, toHexString, toHexString
Methods inherited from class convex.core.data.ACountable
isEmpty, size, slice
Methods inherited from class convex.core.data.ACell
attachMemorySize, attachRef, cachedEncoding, cachedHash, createEncoding, createRef, genericEquals, getCanonical, getChildRefs, getEncoding, getEncodingLength, getHash, getMemorySize, getMemorySize, getRef, isEmbedded, validate
Methods inherited from class convex.core.data.AObject
attachEncoding, print, print
-
Field Details
-
LENGTH
public static final int LENGTH- See Also:
-
ZERO
The Zero Address -
MAX_VALUE
The maximum possible Address -
MAX_ENCODING_LENGTH
public static final int MAX_ENCODING_LENGTH- See Also:
-
-
Method Details
-
create
Obtains an Address. Number must be a valid non-negative long value.- Parameters:
number
- Account number- Returns:
- Address instance, or null if not valid
-
unsafeCreate
Creates an Address without checking.- Parameters:
number
- Account number- Returns:
- Address instance, may be invalid
-
create
-
getType
-
hashCode
public int hashCode()Description copied from class:ACell
Gets the Java hashCode for this cell. Must be consistent with equals. Default is the hashCode of the Encoding blob, since this is consistent with encoding-based equality. However, different Types may provide more efficient hashcodes provided that the usual invariants are preserved -
equals
-
equals
Description copied from class:ACell
Checks for equality with another Cell. In general, Cells are considered equal if they have the same canonical representation, i.e. an identical encoding with the same hash value. Subclasses SHOULD override this if they have a more efficient equals implementation. MUST NOT require reads from Store. -
equals
-
fromHex
-
parse
-
parse
-
read
- Throws:
BadFormatException
-
encode
public int encode(byte[] bs, int pos) Description copied from class:ACell
Writes this Cell's encoding to a byte array, including a tag byte which will be written first. Cell must be canonical, or else an error may occur.- Specified by:
encode
in interfaceIWriteable
- Specified by:
encode
in classACell
- Parameters:
bs
- A byte array to which to write the encodingpos
- The offset into the byte array- Returns:
- New position after writing
-
encodeRaw
public int encodeRaw(byte[] bs, int pos) Description copied from class:ACell
Writes this Cell's encoding to a byte array, excluding the tag byte. -
print
Description copied from class:AObject
Prints this Object to a readable String Representation. SECURITY: Must halt and return false in O(1) time when limit of printing is exceeded otherwise DoS attacks may be possible. -
toCVMString
Description copied from class:ACell
Returns the CVM String representation of this Cell. Normally, this is as printed, but may be different for some types. SHOULD return null in O(1) time if the length of the CVM String can be proved to exceed the limit. MUST complete in O(limit) time and space otherwise The String representation is intended to be a easy-to-read textual representation of the Cell's data content.- Overrides:
toCVMString
in classACell
- Parameters:
limit
- Limit of CVM String length in UTF-8 bytes- Returns:
- CVM String, or null if limit exceeded
-
toString
-
estimatedEncodingSize
public int estimatedEncodingSize()Description copied from interface:IWriteable
Estimate the encoded data size for this Cell. Used for quickly sizing buffers. Implementations should try to return a size that is highly likely to contain the entire object when encoded, including the tag byte. Should not traverse soft Refs, i.e. must be usable on arbitrary partial data structures- Returns:
- The estimated size for the binary representation of this object.
-
validateCell
Description copied from class:ACell
Validates the local structure and invariants of this cell. Called by validate() super implementation. Should validate directly contained data, but should not validate all other structure of this cell. In particular, should not traverse potentially missing child Refs.- Specified by:
validateCell
in classACell
- Throws:
InvalidDataException
- If the Cell is invalid
-
slice
Description copied from class:ACountable
Gets a slice of this data structure- Specified by:
slice
in classACountable<CVMLong>
- Parameters:
start
- Start index (inclusive)end
- end index (exclusive)- Returns:
- Slice of data structure, or null if invalid slice
-
toFlatBlob
Description copied from class:ABlobLike
Converts the contents of this value to a flat array-backed Blob instance. Warning: might be O(n) in size of Blob, may not be canonical etc.- Overrides:
toFlatBlob
in classABlobLike<CVMLong>
- Returns:
- A Blob instance containing the same data as this value. Might be `this` if already a flat Blob.
-
getTag
public byte getTag()Description copied from class:ACell
Gets the tag byte for this cell. The tag byte is always equal to the first byte of the Cell's canonical Encoding, and is sufficient to distinguish how to read the rest of the encoding. -
toCanonical
Description copied from class:ACell
Converts this Cell to a canonical version. Must return this Cell if already canonical, may be O(n) in size of value otherwise. Callers should usually use getCanonical(), which caches canonical instances once created- Specified by:
toCanonical
in classACell
- Returns:
- Canonical version of Cell
-
offset
Creates a new Address at an offset to this Address- Parameters:
offset
- Offset to add to this Address (may be negative)- Returns:
- New Address, or null if would be invalid
-
byteAt
-
byteAtUnchecked
public final byte byteAtUnchecked(long i) Description copied from class:ABlobLike
Gets the byte at the specified position in this data object, possibly skipping bounds checking. Only safe if index is known to be in bounds, otherwise result is undefined.- Overrides:
byteAtUnchecked
in classABlobLike<CVMLong>
- Parameters:
i
- Index of the byte to get- Returns:
- The byte at the specified position
-
hexMatch
Description copied from class:ABlobLike
Returns the number of matching hex digits in the given hex range of another Blob. Assumes range is valid for both blobs. Returns length if this Blob is exactly equal to the specified hex range. -
empty
Description copied from class:ACountable
Returns a canonical, singleton empty instance of the same type as this Countable value. -
getBytes
public final int getBytes(byte[] bs, int pos) Description copied from class:ABlobLike
Copies the bytes from this instance to a given destination array -
longValue
public long longValue()Description copied from class:ABlobLike
Converts this BlobLike to the corresponding long value. Assumes big-endian format, as if the entire blob is interpreted as an unsigned big integer. Higher bytes outside the Long range will be ignored, i.e. the lowest 64 bits are taken -
toBlob
-
equalsBytes
Description copied from class:ABlobLike
Compare the byte content of this BlobLike to a Blob value- Specified by:
equalsBytes
in classABlobLike<CVMLong>
- Parameters:
b
- Blob value to compare with- Returns:
- `true` if byte contents are exactly equal, `false` otherwise
-
compareTo
-
compareTo
protected int compareTo(long bvalue) -
count
public long count()Description copied from class:ACountable
Returns the number of elements in this value- Specified by:
count
in classACountable<CVMLong>
- Returns:
- Number of elements
-
get
Description copied from class:ACountable
Gets the element at the specified element index in this value- Specified by:
get
in classACountable<CVMLong>
- Parameters:
i
- Index of element to get- Returns:
- Element at the specified index
-
isCanonical
public boolean isCanonical()Description copied from class:ACell
Returns true if this Cell is in a canonical representation. Non-canonical objects may be used on a temporary internal basis, they should be converted to canonical representations for general purpose use.- Specified by:
isCanonical
in classACell
- Returns:
- true if the object is in canonical format, false otherwise
-
calcMemorySize
protected long calcMemorySize()Description copied from class:ACell
Calculates the Memory Size for this Cell. Assumes not already calculated Requires any child Refs to be either direct or of persisted in store at minimum, or you might get a MissingDataException- Overrides:
calcMemorySize
in classACell
- Returns:
- Memory Size of this Cell
-
isCVMValue
public boolean isCVMValue()Description copied from class:ACell
Returns true if this cell is a first class CVM Value allowable in the CVM state Sub-structural cells that are not themselves first class values should return false Records and types that are not permissible on the CVM should return false. Pretty much everything else should return true. Note: CVM values might not be in a canonical format, e.g. temporary data structures- Specified by:
isCVMValue
in classACell
- Returns:
- true if the object is a CVM Value, false otherwise
-
getRef
Description copied from class:ACell
Gets a numbered child Ref from within this Cell. WARNING: May need to convert to a canonical instance -
updateRefs
Description copied from class:ACell
Updates all Refs in this object using the given function. The function *must not* change the hash value of Refs, in order to ensure structural integrity of modified data structures. The implementation *should* re-attach any original encoding in order to prevent re-encoding or surplus hashing This is a building block for a very sneaky trick that enables use to do a lot of efficient operations on large trees of smart references. Must return the same object if no Refs are altered.- Overrides:
updateRefs
in classACell
- Parameters:
func
- Ref update function- Returns:
- Cell with updated Refs
-
getRefCount
public int getRefCount()Description copied from class:ACell
Gets the number of Refs contained within this Cell. This number is final / immutable for any given instance and is defined by the Cell encoding rules. Contained Refs may be either external or embedded.- Overrides:
getRefCount
in classACell
- Returns:
- The number of Refs in this Cell
-