Package convex.core

Class State

All Implemented Interfaces:
IValidated, IWriteable, Map<Keyword,ACell>

public class State extends ARecord
Class representing the immutable state of the CVM State transitions are represented by blocks of transactions, according to the logic: s[n+1] = s[n].applyBlock(b[n]) State contains the following elements - Map of AccountStatus for every Address - Map of PeerStatus for every Peer Address - Global values - Schedule data structure "State. You're doing it wrong" - Rich Hickey
  • Field Details

    • EMPTY

      public static final State EMPTY
      An empty State
  • Method Details

    • get

      public ACell get(ACell k)
      Description copied from class: ARecord
      Gets the record field content for a given key, or null if not found.
      Specified by:
      get in class ARecord
      Parameters:
      k - Key to look up in this record
      Returns:
      Field value for the given key
    • 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 class ARecord
      Returns:
      The number of Refs in this Cell
    • getRef

      public <R extends ACell> Ref<R> getRef(int i)
      Description copied from class: ACell
      Gets a numbered child Ref from within this Cell.
      Overrides:
      getRef in class ARecord
      Type Parameters:
      R - Type of referenced Cell
      Parameters:
      i - Index of ref to get
      Returns:
      The Ref at the specified index
    • updateAll

      protected State updateAll(ACell[] newVals)
      Description copied from class: ARecord
      Updates all values in this record, in declared field order. Returns this if all values are identical.
      Specified by:
      updateAll in class ARecord
      Parameters:
      newVals - New values to replace current
      Returns:
      Updated Record
    • create

      public static State create(AVector<AccountStatus> accounts, BlobMap<AccountKey,PeerStatus> peers, AVector<ACell> globals, BlobMap<ABlob,AVector<ACell>> schedule)
      Create a State
      Parameters:
      accounts - Accounts
      peers - Peers
      globals - Globals
      schedule - Schedule (may be null)
      Returns:
      New State instance
    • 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 interface IWriteable
      Specified by:
      encode in class ACell
      Parameters:
      bs - A byte array to which to write the encoding
      pos - The offset into the byte array
      Returns:
      New position after writing
    • encodeRaw

      public int encodeRaw(byte[] bs, int pos)
      Description copied from class: ARecord
      Writes the raw fields of this record in declared order
      Overrides:
      encodeRaw in class ARecord
      Parameters:
      bs - Array to write to
      pos - The offset into the byte array
      Returns:
      New position after writing
    • getEncodingLength

      public long getEncodingLength()
      Description copied from class: ACell
      Method to calculate the encoding length of a Cell. May be overridden to avoid creating encodings during memory size calculations. This reduces hashing!
      Overrides:
      getEncodingLength in class ACell
      Returns:
      Exact encoding length of this Cell
    • 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 likely to contain the entire object when represented in binary format, including the tag byte.
      Specified by:
      estimatedEncodingSize in interface IWriteable
      Overrides:
      estimatedEncodingSize in class ARecord
      Returns:
      The estimated size for the binary representation of this object.
    • read

      public static State read(ByteBuffer bb) throws BadFormatException
      Reads a State from a ByteBuffer encoding. Assumes tag byte already read.
      Parameters:
      bb - ByteBuffer to decode from
      Returns:
      The State decoded
      Throws:
      BadFormatException - If a State could not be read
    • getAccounts

      public AVector<AccountStatus> getAccounts()
      Get all Accounts in this State
      Returns:
      Vector of Accounts
    • getPeers

      public BlobMap<AccountKey,PeerStatus> getPeers()
      Gets the map of Peers for this State
      Returns:
      A map of addresses to PeerStatus records
    • getBalance

      public Long getBalance(Address address)
      Gets the balance of a specific address, or null if the Address does not exist
      Parameters:
      address - Address to check
      Returns:
      Long balance, or null if Account does not exist
    • withBalance

      public State withBalance(Address address, long newBalance)
    • applyBlock

      public BlockResult applyBlock(Block block)
      Block level state transition function Updates the state by applying a given block of transactions
      Parameters:
      block - Block to Apply
      Returns:
      The BlockResult from applying the given Block to this State
    • applyTransaction

      public <T extends ACell> Context<T> applyTransaction(ATransaction t)
      Applies a transaction to the State. There are three phases in application of a transaction:
      1. Preparation for accounting, with prepareTransaction
      2. Functional application of the transaction with ATransaction.apply(....)
      3. Completion of accounting, with completeTransaction
      SECURITY: Assumes digital signature already checked.
      Type Parameters:
      T - Type of transaction result
      Parameters:
      t - Transaction to apply
      Returns:
      Context containing the updated chain State (may be exceptional)
    • isCanonical

      public boolean isCanonical()
      Description copied from class: ACell
      Returns true if this Cell is in a canonical representation for message writing. Non-canonical objects may be used on a temporary internal basis, they must always be converted to canonical representations for external use (e.g. Encoding).
      Overrides:
      isCanonical in class ARecord
      Returns:
      true if the object is in canonical format, false otherwise
    • computeStakes

      public HashMap<AccountKey,Double> computeStakes()
      Computes the weighted stake for each peer. Adds a single entry for the null key, containing the total stake
      Returns:
      Map of Stakes
    • withAccounts

      public State withAccounts(AVector<AccountStatus> newAccounts)
      Updates the Accounts in this State
      Parameters:
      newAccounts - New Accounts vector
      Returns:
      Updated State
    • putAccount

      public State putAccount(Address address, AccountStatus accountStatus)
      Returns this state after updating the given account
      Parameters:
      address - Address of Account to update
      accountStatus - New Account Status
      Returns:
      Updates State, or this state if Account was unchanged
    • getAccount

      public AccountStatus getAccount(Address target)
      Gets the AccountStatus for a given account, or null if not found.
      Parameters:
      target - Address to look up. Must not be null
      Returns:
      The AccountStatus for the given account, or null.
    • getEnvironment

      public AMap<Symbol,ACell> getEnvironment(Address addr)
      Gets the environment for a given account, or null if not found.
      Parameters:
      addr - Address of account to obtain
      Returns:
      The environment of the given account, or null if not found.
    • withPeers

      public State withPeers(BlobMap<AccountKey,PeerStatus> newPeers)
      Updates the Peers in this State
      Parameters:
      newPeers - New Peer Map
      Returns:
      Updated State
    • getTag

      public byte getTag()
      Description copied from class: ARecord
      Gets the tag byte for this record type. The Tag is the byte used to identify the record in the binary encoding.
      Specified by:
      getTag in class ARecord
      Returns:
      Record tag byte
    • addActor

      public State addActor()
      Adds a new Actor Account. The actor will be the last Account in the resulting State, and will have a default empty Actor environment.
      Returns:
      The updated state with the Actor Account added.
    • computeTotalFunds

      public long computeTotalFunds()
      Compute the total funds existing within this state. Should be constant! 1,000,000,000,000,000,000 in full deployment mode
      Returns:
      The total value of all funds
    • validate

      public void validate() throws InvalidDataException
      Description copied from interface: IValidated
      Validates the complete structure of this object. It is necessary to ensure all child Refs are validated, so the general contract for validate is:
      1. Call super.validate() - which will indirectly call validateCell()
      2. Call validate() on any contained cells in this class
      Specified by:
      validate in interface IValidated
      Overrides:
      validate in class ACell
      Throws:
      InvalidDataException - If the data Value is invalid in any way
    • validateCell

      public void validateCell() throws InvalidDataException
      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 class ACell
      Throws:
      InvalidDataException - If the Cell is invalid
    • getTimeStamp

      public CVMLong getTimeStamp()
      Gets the current global timestamp from this state.
      Returns:
      The timestamp from this state.
    • getJuicePrice

      public CVMLong getJuicePrice()
      Gets the current Juice price
      Returns:
      Juice Price
    • scheduleOp

      public State scheduleOp(long time, Address address, AOp<?> op)
      Schedules an operation with the given timestamp and Op in this state
      Parameters:
      time - Timestamp at which to execute the scheduled op
      address - AccountAddress to schedule op for
      op - Op to execute in schedule
      Returns:
      The updated State
    • getSchedule

      public BlobMap<ABlob,AVector<ACell>> getSchedule()
      Gets the current schedule data structure for this state
      Returns:
      The schedule data structure.
    • getGlobalFees

      public CVMLong getGlobalFees()
      Gets the Global Fees accumulated in the State
      Returns:
      Global Fees
    • withGlobalFees

      public State withGlobalFees(CVMLong newFees)
      Update Global Fees
      Parameters:
      newFees - New Fees
      Returns:
      Updated State
    • getPeer

      public PeerStatus getPeer(AccountKey peerAddress)
      Gets the PeerStatus record for the given Address, or null if it does not exist
      Parameters:
      peerAddress - Address of Peer to check
      Returns:
      PeerStatus
    • withPeer

      public State withPeer(AccountKey peerKey, PeerStatus updatedPeer)
      Updates the specified peer status
      Parameters:
      peerKey - Peer Key
      updatedPeer - New Peer Status
      Returns:
      Updated state
    • nextAddress

      public Address nextAddress()
      Gets the next available address for allocation, i.e. the lowest Address that does not yet exist in this State.
      Returns:
      Next address available
    • lookupCNS

      public Address lookupCNS(String name)
      Look up an Address from CNS
      Parameters:
      name - CNS name String
      Returns:
      Address from CNS, or null if not found
    • getGlobals

      public AVector<ACell> getGlobals()
      Gets globals.
      Returns:
      Vector of global values
    • withTimestamp

      public State withTimestamp(long timestamp)
      Updates the State with a new timestamp
      Parameters:
      timestamp - New timestamp
      Returns:
      Updated State
    • equals

      public boolean equals(ACell a)
      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.
      Overrides:
      equals in class ACell
      Parameters:
      a - Cell to compare with. May be null.
      Returns:
      True if this cell is equal to the other object
    • equals

      public boolean equals(State a)
      Tests if this State is equal to another
      Parameters:
      a - State to compare with
      Returns:
      true if equal, false otherwise