Chaos智能合约测试案例计算机区块链.docx
《Chaos智能合约测试案例计算机区块链.docx》由会员分享,可在线阅读,更多相关《Chaos智能合约测试案例计算机区块链.docx(16页珍藏版)》请在冰豆网上搜索。
![Chaos智能合约测试案例计算机区块链.docx](https://file1.bdocx.com/fileroot1/2023-1/23/7b92d8c5-3ace-4219-8f69-c965d9b6c05f/7b92d8c5-3ace-4219-8f69-c965d9b6c05f1.gif)
Chaos智能合约测试案例计算机区块链
ICO合约案例说明
1、功能说明
通过区块链智能合约项目ico,用户可通过向智能合约里面发送区块链代币,合约会根据参与者的地址与参与金额,合约会自动把参与者发送的金额转到指定的地址,并给其参与者分配相应的权益份额,用于后续权益兑换。
2、合约开发部署流程A、启动一个区块链节点/钱包
B、编写智能合约代码
代码如下:
mylib=require"mylib"
functionUnpack(t,i)i=ior1
ift[i]then
returnt[i],Unpack(t,i+1)
end
end
functionGetValueFromContract(tbl,start,length)assert(start>0,"GetValueFromContractstarterr")assert(length>0,"GetValueFromContractlengtherr")localnewTab={}
locali
fori=0,length-1donewTab[1+i]=tbl[start+i]
end
returnnewTab
end
OPER_TYPE={ENUM_ADD_FREE=1,ENUM_MINUS_FREE=2
}
APP_OPERATOR_TYPE={ENUM_ADD_FREE_OP=1,ENUM_SUB_FREE_OP=2,ENUM_ADD_FREEZED_OP=3,ENUM_SUB_FREEZED_OP=4
}
ADDR_TYPE={ENUM_REGID=1,ENUM_BASE58=2
}
localfuns={}
--众筹
funs[1]=function()
localmoneyTbl={mylib.GetCurTxPayAmount()}
localmoney=mylib.ByteToInteger(Unpack(moneyTbl))assert(money>0,'invalidmoneynum')print('金额:
',money)
localappRecharge={operatorType=APP_OPERATOR_TYPE.ENUM_ADD_FREE_OP,outHeight=0,moneyTbl=moneyTbl,userIdLen=34,userIdTbl={mylib.GetBase58Addr(mylib.GetCurTxAccount())},fundTagLen=0,fundTagTbl={}}
assert(mylib.WriteOutAppOperate(appRecharge),"failedtozc")
localappRecharge={operatorType=APP_OPERATOR_TYPE.ENUM_ADD_FREE_OP,outHeight=0,moneyTbl=moneyTbl,userIdLen=6,userIdTbl={mylib.GetScriptID()},fundTagLen=0,fundTagTbl={}}
assert(mylib.WriteOutAppOperate(appRecharge),"failedtoaddapp")
localdataTbl={
addrType=ADDR_TYPE.ENUM_REGID,
operatorType=OPER_TYPE.ENUM_MINUS_FREE,accountIdTbl={mylib.GetScriptID()},
moneyTbl=moneyTbl,outHeight=0
}
assert(mylib.WriteOutput(dataTbl),'failedtominus')
localdataTbl2={
addrType=ADDR_TYPE.ENUM_BASE58,operatorType=OPER_TYPE.ENUM_ADD_FREE,
accountIdTbl={0x54,0x46,0x67,0x47,0x4d,0x64,0x65,0x74,0x37,0x75,0x43,0x70,0x54,0x36,0x5a,0x46,0x4a,0x67,0x50,0x6d,0x43,0x4b,0x6e,0x73,0x6d,0x39,0x70,0x41,0x48,0x33,0x34,0x5a,0x4b,0x6b},
moneyTbl=moneyTbl,outHeight=0
}
assert(mylib.WriteOutput(dataTbl2),'failedtoadd')
end
--返回结果print('合约执行完成')
returnfuns[contract[2]]andfuns[contract[2]]()ortrue
C、在智能合约界面部署合约
注意:
这一步会消耗一定量的币,返回一个交易ID,等到下一个区块产生后,
可通过这个交易ID查询合约ID,所有跟合约相关的操作都会用到这个合约ID。
部署:
获取合约ID:
参与众筹:
指定调用的智能合约,也就是之前得到的合约ID,参与人的地址,参与金
额,合约内容:
ff01表示参与众筹,点击创建,即会创建一笔合约交易,如果参与者有足够的金额,这笔交易会在下一个区块被确认。
查询份额:
通过合约ID、地址,可查询参与ico所得份额,返回的json字符串,FreeValues
为用户所持份额。
流程图如下:
证券发行合约案例说明
1、功能说明:
证券发行,资产冻结,分期解冻。
3、合约开发部署流程:
A、启动一个区块链节点/钱包
B、编写智能合约代码
代码如下:
mylib=require"mylib"
LOG_TYPE=
{
ENUM_STRING=0,ENUM_NUMBER=1,};
OPER_TYPE=
{
ENUM_ADD_FREE=1,ENUM_MINUS_FREE=2}
APP_OPERATOR_TYPE=
{
ENUM_ADD_FREE_OP=1,ENUM_SUB_FREE_OP=2,
ENUM_ADD_FREEZED_OP=3,ENUM_SUB_FREEZED_OP=4}
ADDR_TYPE=
{
ENUM_REGID=1,ENUM_BASE58=2,}
TX_TYPE=
{
TX_WITHDRAW=1,TX_RECHARGE=2,}
FREEZE_MONTH_NUM=20FREEZE_PERIOD=5
MAX_MONEY=100000000000000000FREE_MONEY=10000000000000000gCheckAccount=true
gSendAccountTbl={
0x00,0x00,0x00,0x00,0x01,0x00
}
functionTableIsEmpty(t)return_G.next(t)==nil
end
functionTableIsNotEmpty(t)returnfalse==TableIsEmpty(t)
end
functionLogPrint(aKey,bLength,cValue)assert(bLength>=1,"LogPrintbLengthinvlaid")if(aKey==LOG_TYPE.ENUM_STRING)then
assert(type(cValue)=="string","LogPrintcValueinvlaid0")elseif(aKey==LOG_TYPE.ENUM_NUMBER)then
assert(TableIsNotEmpty(cValue),"LogPrintcValueinvlaid1")
else
error("LogPrintaKeyinvlaid")
end
locallogTable={
key=LOG_TYPE.ENUM_STRING,length=0,
value=nil
}
logTable.key=aKeylogTable.length=bLengthlogTable.value=cValuemylib.LogPrint(logTable)
end
functionUnpack(t,i)i=ior1
ift[i]then
returnt[i],Unpack(t,i+1)
end
end
functionMemCmp(tDest,tSrc,start1)
assert(TableIsNotEmpty(tDest),"tDestisempty")assert(TableIsNotEmpty(tSrc),"tSrcisempty")locali
fori=#tDest,1,-1do
iftDest[i]>tSrc[i+start1-1]thenreturn1
elseiftDest[i]elseend
endreturn0
end
functionGetValueFromContract(tbl,start,length)assert(start>0,"GetValueFromContractstarterr")assert(length>0,"GetValueFromContractlengtherr")localnewTab={}
locali
fori=0,length-1donewTab[1+i]=tbl[start+i]
end
returnnewTab
end
functionRecharge()
localtoAddrTbl={}toAddrTbl=GetValueFromContract(contract,3,34)localmoneyTbl={}
moneyTbl=GetValueFromContract(contract,37,8)localmoney=mylib.ByteToInteger(Unpack(moneyTbl))assert(money>0,"money<=0")
localfreeMoneyTbl={}
freeMoneyTbl=GetValueFromContract(contract,45,8)
localfreeMoney=mylib.ByteToInteger(Unpack(freeMoneyTbl))assert(freeMoney>0,"freeMoney<=0")
localfreeMonthMoneyTbl={}
freeMonthMoneyTbl=GetValueFromContract(contract,53,8)
localfreeMonthMoney=mylib.ByteToInteger(Unpack(freeMonthMoneyTbl))assert(freeMonthMoney>0,"freeMonthMoney<=0")
localpayMoneyTbl={}
payMoneyTbl={mylib.GetCurTxPayAmount()}assert(TableIsNotEmpty(payMoneyTbl),"GetCurTxPayAmounterror")localpayMoney=mylib.ByteToInteger(Unpack(payMoneyTbl))assert(payMoney>0,"payMoney<=0")
assert(money==payMoney,"充值金额不正确1")ifmoney<1
ormoneyormoneyLogPrint(LOG_TYPE.ENUM_STRING,string.len("充值金额不正确2"),"充值金
额不正确2");
error("充值金额不正确2")
end
ifmoney>=MAX_MONEY
orfreeMoney>=FREE_MONEY
orfreeMonthMoney>=FREE_MONEYthenLogPrint(LOG_TYPE.ENUM_STRING,string.len("充值金额不正确3"),"充值金
额不正确3");
error("充值金额不正确3")
end
localfreezeNum=FREEZE_MONTH_NUM-1assert(freezeNum>0,"月冻结总数不正确")
localfreezeMoney=freeMonthMoney*freezeNumiffreezeMoneyorfreezeMoneyLogPrint(LOG_TYPE.ENUM_STRING,string.len("充值金额不正确4"),"充值金
额不正确4");
error("充值金额不正确4")
end
freezeMoney=freezeMoney+freeMoneyifmoney~=freezeMoneythen
LogPrint(LOG_TYPE.ENUM_STRING,string.len("充值金额不正确5"),"充值金
额不正确5");
error("充值金额不正确5")
end
localwriteOutputTbl=
{
addrType=1,accountIdTbl={},operatorType=0,
outHeight=0,moneyTbl={}}
writeOutputTbl.addrType=ADDR_TYPE.ENUM_BASE58writeOutputTbl.operatorType=OPER_TYPE.ENUM_ADD_FREEwriteOutputTbl.accountIdTbl={Unpack(toAddrTbl)}writeOutputTbl.moneyTbl={Unpack(freeMoneyTbl)}assert(mylib.WriteOutput(writeOutputTbl),"WriteOutputerr1")localcurHeight=0
curHeight=mylib.GetCurRunEnvHeight()localappOperateTbl={
operatorType=0,outHeight=0,moneyTbl={},
userIdLen=0,userIdTbl={},fundTagLen=0,
fundTagTbl={}}
appOperateTbl.operatorType=APP_OPERATOR_TYPE.ENUM_ADD_FREEZED_OPappOperateTbl.userIdLen=34
appOperateTbl.userIdTbl=toAddrTblappOperateTbl.moneyTbl=freeMonthMoneyTblfori=1,freezeNumdo
appOperateTbl.outHeight=curHeight+FREEZE_PERIOD*iassert(mylib.WriteOutAppOperate(appOperateTbl),"WriteOutAppOperate
err1")
end
writeOutputTbl.addrType=ADDR_TYPE.ENUM_REGIDwriteOutputTbl.operatorType=OPER_TYPE.ENUM_MINUS_FREEwriteOutputTbl.accountIdTbl={mylib.GetScriptID()}assert(mylib.WriteOutput(writeOutputTbl),"WriteOutputerr2")returntrue
end
functionWriteWithdrawal(accTbl,moneyTbl)localwriteOutputTbl=
{
addrType=1,accountIdTbl={},operatorType=0,
outHeight=0,moneyTbl={}}
assert(TableIsNotEmpty(accTbl),"WriteWithDrawalaccTblinvlaid1")
assert(TableIsNotEmpty(moneyTbl),"WriteWithDrawalmoneyTblinvlaid1")
writeOutputTbl.addrType=ADDR_TYPE.ENUM_REGID
writeOutputTbl.operatorType=OPER_TYPE.ENUM_ADD_FREE
writeOutputTbl.accountIdTbl={Unpack(accTbl)}
writeOutputTbl.moneyTbl={Unpack(moneyTbl)}
assert(mylib.WriteOutput(writeOutputTbl),"WriteWithDrawalWriteOutputerr0")
writeOutputTbl.operatorType=OPER_TYPE.ENUM_MINUS_FREE
writeOutputTbl.accountIdTbl={mylib.GetScriptID()}
assert(mylib.WriteOutput(writeOutputTbl),"WriteWithDrawalWriteOutputerr1")
returntrue
end
functionWithdraw(addrType)
ifaddrType~=ADDR_TYPE.ENUM_REGIDandaddrType~=
ADDR_TYPE.ENUM_BASE58then
error("InfunctionWithdraw,addrtypeerr")
end
localaccountTbl={0,0,0,0,0,0}accountTbl={mylib.GetCurTxAccount()}
assert(TableIsNotEmpty(accountTbl),"GetCurTxAccounterror")localidTbl=
{
idLen=0,idValueTbl={}
}
ifaddrType==ADDR_TYPE.ENUM_REGIDthenidTbl.idLen=6
idTbl.idValueTbl=accountTbl
else
localbase58Addr={}
base58Addr={mylib.GetBase58Addr(Unpack(accountTbl))}assert(TableIsNotEmpty(base58Addr),"GetBase58Addrerror")idTbl.idLen=34
idTbl.idValueTbl=base58Addr
end
localfreeMoneyTbl={mylib.GetUserAppAccValue(idTbl)}assert(TableIsNotEmpty(freeMoneyTbl),"GetUserAppAccValueerror")localfreeMoney=mylib.ByteToInteger(Unpack(freeMoneyTbl))
assert(freeMoney>0,"Accountbalanceis0.")
localappOperateTbl={
operatorType=0,outHeight=0,moneyTbl={},
userIdLen=0,userIdTbl={},fundTagLen=0,
fundTagTbl={}}
appOperateTbl.operatorType=APP_OPERATOR_TYPE.ENUM_SUB_FREE_OPappOperateTbl.userIdLen=idTbl.idLen
appOperateTbl.userIdTbl=idTbl.idValueTblappOperateTbl.moneyTbl=freeMoneyTbl
assert(mylib.WriteOutAppOperate(appOperateTbl),"WriteOutAppOperateerr1")assert(WriteWithdrawal(accountTbl,freeMoneyTbl),"WriteWithdrawalerr")
returntrue
end
functionMain()
assert(#contract>=2,"contractlengtherr.")assert(contract[1]==0xff,"Contractidentificationerror.")
ifcontract[2]==TX_TYPE.TX_RECHARGEthenassert(#contract==60,"rechargecontractlengtherr.")Recharge()
elseifcontract[2]==TX_TYPE.TX_WITHDRAWthenassert(#contract==11,"withdrawcontractlengtherr.")Withdraw(contract[3])
else
error("RUN_SCRIPT_DATA_ERR