智能合约
智能合约是一种特殊交易
当前智能合约的载体是交易,也就是说智能合约是一种特殊的交易。
它只有输入,没有输出。
它可被调用。
合约代码存储在交易,确定后是不可变的,但其状态和关联数据会单独存放。
智能合约是合同,区块链是账本
“合同上”可以写张三转账 100 个币给李四,但“账本上”查不到对应交易记录。
因为发送方使用了环签名,接收方使用了一次性地址,交易金额使用了机密交易。不但本次交易不可查,张三的币从哪来,李四的币又到哪去,也不可查。
Process_SC
两步走:
将原来的文件、字符串,解析成可执行代码
DVM 执行合约代码
// this will process the SC transaction
// the tx should only be processed , if it has been processed
tx.Verify_SC_Signature *
dvm.Initialize_TX_store
chain.LoadSCValue
dvm.ParseSmartContract *
tx_store.Store
dvm.GetBalanceKey
// 开始 execute
tx_store.Balance
tx_store.ReceiveInternal
dvm.RunSmartContract *
chain.store_changes
由谁 & 什么时候运行?
由客户端协议运行。
另外,有防范措施,如果交易为“冲突交易”或“重复交易”,本合约代码不会运行。
Revert_SC
// this will revert the SC transaction changes to the DB
chain.Load_SCChangelog
chain.StoreSCValue
Load_SCChangelog
store_changes
// this will store the changes
LoadSCValue
// this will load the value from the chain
ReadSC
// reads a value from SC, always read balance
ReadSCValue
// reads a value from SC, always read balance
StoreSCValue
// store the value in the chain
TX_SC_storage
数据结构
// all these are stored in a tx container
type TX_SC_storage struct {
SCID crypto.Key `msgpack:"S,omitempty"`
Key crypto.Key `msgpack:"K,omitempty"`
Previous []byte `msgpack:"P,omitempty"` // previous value
Current []byte `msgpack:"-"` // current value // this need not be stored
TransferE []dvm.TransferExternal `msgpack:"T,omitempty"`
}
GetEphermalKey