-
Notifications
You must be signed in to change notification settings - Fork 25
Hash Code
Jeff Disher edited this page Aug 16, 2019
·
1 revision
As the identity hash code of an object instance is a point of nondeterminism within the standard JVM, this is something which needs to be redefined for the AVM.
In the AVM, the identity hash code of an object is an incrementing counter. It is a standard 4-byte signed Java int
, meaning that it is does overflow and wrap back into negative after 2^31-1
.
Life Cycle
- When a new contract is deployed, its next hash code is initialized to
1
: https://github.com/aionnetwork/AVM/blob/764e6195d6131d539eb46333d830d3a1f81edd2f/org.aion.avm.core/src/org/aion/avm/core/DAppCreator.java#L223 - When an object instance is created, it increments this counter for the currently running contract: https://github.com/aionnetwork/AVM/blob/764e6195d6131d539eb46333d830d3a1f81edd2f/org.aion.avm.rt/src/s/java/lang/Object.java#L24
- This hash code is loaded/saved within the object graph, as its first 4 bytes: https://github.com/aionnetwork/AVM/blob/764e6195d6131d539eb46333d830d3a1f81edd2f/org.aion.avm.core/src/org/aion/avm/core/persistence/Serializer.java#L27
- If an internal call was re-entrant and a success, we update the caller's next hash code to be the same as the callee's next hash code: https://github.com/aionnetwork/AVM/blob/764e6195d6131d539eb46333d830d3a1f81edd2f/org.aion.avm.core/src/org/aion/avm/core/BlockchainRuntimeImpl.java#L475
Special Cases
- Constants allocated by the core AVM runtime, and shared by all contracts, do not rely on this counter, but have fixed values: https://github.com/aionnetwork/AVM/blob/764e6195d6131d539eb46333d830d3a1f81edd2f/org.aion.avm.rt/src/s/java/lang/Object.java#L39
-
Class
instances do not use or increment this counter, but are based on the hash of theString
representations of their names: https://github.com/aionnetwork/AVM/blob/764e6195d6131d539eb46333d830d3a1f81edd2f/org.aion.avm.rt/src/s/java/lang/Class.java#L121 (note thatClass
instances which are also constants use the constant scheme: https://github.com/aionnetwork/AVM/blob/764e6195d6131d539eb46333d830d3a1f81edd2f/org.aion.avm.rt/src/s/java/lang/Class.java#L126 )
Consequences
- The first objects allocated by a contract are its constants, including
Enum
constants defined within their code. These are given identity hash codes which cannot overflow (as deployment energy limits cannot allocate 4 billionObject
s, definitely not 4 billionEnum
constants). As a consequence, these initial constants will have identity hash codes which cannot collide with each other.