加密交易概念(二)支付处理器的实现
一、状态(state)和状态转换 (state transition)
状态(state): 二进制的的0101010 可以用来表示某些事物的状态;
状态转换函数(state transition function):反映一个时刻移动到另外一个时刻;
通过获取一个输入和一个状态,来返回一个新的状态,即下一个状态;
f(state, input){ return new state‘ }
创世区块 genesis state
Object { }
第一个状态, 类型 铸币
第二个状态, 类型 发送
交易函数
apply_transcation(state, transction){ // Check the tx signature // Check the nonce // Apply 'mint' or 'send' tx return new state }
有两种交易类型, 铸币 和 发送
铸币 只能由中央支付处理器创建;
检查签名 check the tx signature :
const signer = EthCrypto.recover(tx.sig, getTxHash(tx.contents)) if(signer !== tx.contents.from){ throw new Error('Invalid signature!') }
检查随机数, check the nonce
if(tx.contents.nonce != state[[tx.contents.from]].nonce){ throw new Error('Invalid nonce!') }
铸币 和 发送
if(tx.contents.type == 'mint' && tx.contents.from == accounts.paymentProcessor.address){ state[[tx.contents.to]].balance += tx.contents.amount }else if(tx.contents.type == 'send'){
if(state[[tx.cocntents.form]].balance - tx.contents.acount<0){
throw new Error('Not enough money!')
}
state[[tx.contents.from]].balance -= tx.content.amount
.....
}
铸币的第一个状态


发送的第二状态

v

浙公网安备 33010602011771号