State
state
is the state storage on the chain. For example, after A initiates an on-chain transfer to B, the account balance information of A and B will be stored. Below we refer to all operations on the state store in each transaction as a transaction
.
Transactions in state
are atomic. For example, there are 3 operations that modify the state in a transaction, then these 3 operations can either be modified successfully, or none of them are successful.
eg:
func (a *A) DoTest(ctx *context.WriteContext) error {
a.State.Set(a, []byte("yu"), []byte("yu")) // 1. Mute state
err := DoOther()
if err != nil {
return err
}
a.State.Set(a, []byte("qi"), []byte("qi")) // 2. Mute state
}
In the above code, there are two Mute state
operations. If the code executes successfully in the first place, but returns an error in DoOther()
, then the modify state
in the second place cannot be completed. at this time
The framework will also cancel the first state modification to ensure atomicity.
Currently, only the storage form of kvdb is supported internally. The state is stored in blocks, and the transactions in each block are executed in sequence and stored in the database.
Code is here
func StartBlock(blockHash)
StartBlock()
is called during the start block
phase of the blockchain operation to tell state
the current block hash.
func SetCanRead(blockHash)
SetCanRead()
sets the state in the block that can currently be read. After a block transaction is executed and stored in the chain, it may not be allowed to be read immediately, especially the finalize type chain
, which requires
The result state of the Writing in the block can not be read by the outside world until the block is finalized
. Of course non-finalized chains
can be read as soon as the block is stored in the chain.
func Commit() (Hash, error)
Commit()
commits all transactions in the block and returns the stateroot
of the state tree under the block.
func Discard()
Discard()
discards current transaction.
func DiscardAll()
DiscardAll()
discards all transactions in the current block.
func NextTxn()
NextTxn()
indicates that the current transaction is completed and the next transaction is started.