《區(qū)塊鏈技術(shù)應(yīng)用》課件-8-Truffle合約測(cè)試-Truffle Contract Testing_第1頁(yè)
《區(qū)塊鏈技術(shù)應(yīng)用》課件-8-Truffle合約測(cè)試-Truffle Contract Testing_第2頁(yè)
《區(qū)塊鏈技術(shù)應(yīng)用》課件-8-Truffle合約測(cè)試-Truffle Contract Testing_第3頁(yè)
《區(qū)塊鏈技術(shù)應(yīng)用》課件-8-Truffle合約測(cè)試-Truffle Contract Testing_第4頁(yè)
《區(qū)塊鏈技術(shù)應(yīng)用》課件-8-Truffle合約測(cè)試-Truffle Contract Testing_第5頁(yè)
已閱讀5頁(yè),還剩34頁(yè)未讀 繼續(xù)免費(fèi)閱讀

付費(fèi)下載

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

ApplicationofBlockchainTechnologyMicrocoursename:SmartContractFunctionVerificationSchemeBasedonTruffleTestingMainlecturer:GuoLina區(qū)塊鏈技術(shù)的應(yīng)用微課程名稱:基于Truffle測(cè)試的智能合約功能驗(yàn)證方案主講人:郭莉娜21PARTIntroductiontoTruffleFramework第一部分:Truffle框架簡(jiǎn)介41.IntroductiontoTruffleFrameworkTruffleisaframeworkspecificallydesignedforthedevelopmentofEthereumDAppsandholdsasignificantpositioninthefieldofsmartcontractdevelopment.Ithascomprehensivefunctionsandcanprovidekeyfeaturessuchascompilation,deployment,andtesting,greatlysimplifyingthedevelopmentprocessandenablingdeveloperstobuildDAppsmoreefficiently.Atthesametime,itperformswellindevelopmentenvironmentintegration,providingidealintegrationofdevelopmenttoolsandenvironmentsettings,whichhelpsimprovecodemanagementandversioncontrolcapabilities.TruffleframeworkTruffle是一款專為以太坊去中心化應(yīng)用(DApps)開(kāi)發(fā)設(shè)計(jì)的框架,在智能合約開(kāi)發(fā)領(lǐng)域占據(jù)重要地位。該框架功能全面,可提供編譯、部署、測(cè)試等關(guān)鍵功能,極大地簡(jiǎn)化了開(kāi)發(fā)流程,使開(kāi)發(fā)者能夠更高效地構(gòu)建去中心化應(yīng)用。同時(shí),它在開(kāi)發(fā)環(huán)境集成方面表現(xiàn)出色,能實(shí)現(xiàn)開(kāi)發(fā)工具與環(huán)境設(shè)置的理想集成,有助于提升代碼管理和版本控制能力。62PARTHelloWorldSmartContractAnalysis第二部分:“HelloWorld”智能合約分析82.AnalysisofHelloWorldSmartContractpragmasolidity^0.4.25;contractHelloWorld{stringname;functionHelloWorld(){name="Hello,World!";}functionget()constantreturns(string){returnname;}functionset(stringn){name=n;}}ThiscontractiswritteninSoliditylanguage,version0.4.25.ThecontractisnamedHelloWorld.Itcontainsastatevariablenameoftypestring,whichisusedtostorestringdata.ThecontracthasaconstructorcalledHelloWorld,whichisautomaticallycalledwhenthecontractiscreated.Itsfunctionistoinitializethenameto"Hello,World!".Inaddition,therearetwofunctionsinthecontract.Thegetfunctionismarkedasconstant,whichmeansitdoesnotmodifythestateofthecontract.Itsfunctionistoreturnastringstoredinthenamevariable;Thesetfunctionacceptsaparameternoftypestring,whichisusedtoupdatethevalueofthenamevariableandsetittothepassedstring.本合約采用Solidity語(yǔ)言編寫(xiě),版本為0.4.25,合約名稱為“HelloWorld”。合約包含一個(gè)字符串(string)類型的狀態(tài)變量name,用于存儲(chǔ)字符串?dāng)?shù)據(jù)。合約有一個(gè)名為HelloWorld的構(gòu)造函數(shù),在合約創(chuàng)建時(shí)會(huì)自動(dòng)調(diào)用,其功能是將name初始化為“Hello,World!”。此外,合約中還包含兩個(gè)函數(shù):get函數(shù)被標(biāo)記為constant,表示該函數(shù)不會(huì)修改合約狀態(tài),功能是返回存儲(chǔ)在name變量中的字符串;set函數(shù)接收一個(gè)字符串類型的參數(shù)n,用于更新name變量的值,將其設(shè)為傳入的字符串。103PARTActualoperationprocess第三部分:實(shí)際操作流程123.1.CreatingaProjectRunningTrufflerequiresnodejsasthefoundation(thisoperationusesnode-v20.18.1version)Commandtoinstalltruffle:npminstall-gtruffleAfterinstallation,createadirectoryusingthetruffleinitcommand,asshowninthefollowingfigure3.1創(chuàng)建項(xiàng)目運(yùn)行Truffle需以Node.js為基礎(chǔ)(本次操作使用的Node.js版本為v20.18.1)。安裝Truffle的命令:npminstall-gtruffle。安裝完成后,使用truffleinit命令創(chuàng)建目錄,如下所示:143.1.CreatingaProjectAftersuccessfullycreatingthedirectory,itwillgeneratedirectorystructuresandconfigurationfilesrelatedtocontractediting,contractdeployment,contractfunctionverification,etc.WecanuseVSCodetoopenthetruffleproject(asshowninthefigurebelow)forsubsequentoperations.ContractEditingContractdeploymentContractfunctionverificationconfigurationfile目錄創(chuàng)建成功后,會(huì)生成與合約編輯、合約部署、合約功能驗(yàn)證等相關(guān)的目錄結(jié)構(gòu)和配置文件。我們可以使用VSCode打開(kāi)Truffle項(xiàng)目(如下圖所示),以便進(jìn)行后續(xù)操作。163.2.InstallandstartGanacheGanacheisapersonalblockchainsimulatorusedforEthereumblockchaindevelopment,providingalocalsimulationenvironmentforourdevelopmenttesting.3.2安裝并啟動(dòng)GanacheGanache是一款用于以太坊區(qū)塊鏈開(kāi)發(fā)的個(gè)人區(qū)塊鏈模擬器,可為我們的開(kāi)發(fā)測(cè)試提供本地模擬環(huán)境。183.3.ConfigureTruffletoconnecttoGanacheWhenconfiguringTruffletoconnecttoGanache,itisimportanttoensurethatthetwoportsareconsistenttoensurenormalcommunication(asshowninthefollowingfigure).3.3配置Truffle以連接Ganache配置Truffle連接Ganache時(shí),務(wù)必確保兩者的端口一致,以保證通信正常(如下圖所示)。203.4.ModifyTestContractVersionModifyittobethesameasoursmartcontractversion,whichis0.4.25(asshowninthefollowingfigure).3.4修改測(cè)試合約版本將測(cè)試合約版本修改為與我們的智能合約版本一致,即0.4.25(如下圖所示)。223.5.AddcontractstobetestedAddthecontractthatneedstobetested,whichisourHelloWorldcontract.3.5添加待測(cè)試合約添加需要測(cè)試的合約,即我們的“HelloWorld”合約。243.6.EditingDeploymentFilesCreatethe1_deploy_comtract.exefileinthemigrationsdirectory(asshowninthefollowingfigure).IntheTruffleproject,artifactsareglobalobjectsprovidedbyTruffle.Theartifacts.requiremethodisusedtoloadanabstractrepresentationofasmartcontract.HelloWorldhereisthecontractwewanttoload.ThepurposeofthecodeistoobtainanabstractrepresentationoftheHelloWorldsmartcontractforfuturedeploymentorinteractionwiththecontract.Definedafunctionthattakesadeployerobjectasaparameter.DeployerisadeploymenttoolobjectprovidedbyTruffle.ThemaincontentofthefunctionistousethedeployerobjecttodeploythepreviouslyloadedHelloWorldsmartcontract.WhenTruffleexecutesthedeploymentscript,itcallsthisfunctionandpassesintheappropriatedeployerobjecttodeploytheHelloWorldsmartcontracttoGanach.3.6編輯部署文件在“migrations”目錄下創(chuàng)建“1_deploy_contract.js”文件(如下圖所示)。在Truffle項(xiàng)目中,artifacts是Truffle提供的全局對(duì)象,artifacts.require方法用于加載智能合約的抽象表示。此處的“HelloWorld”即我們要加載的合約。該代碼的作用是獲取“HelloWorld”智能合約的抽象表示,以便后續(xù)進(jìn)行合約部署或與合約交互。定義了一個(gè)以deployer對(duì)象為參數(shù)的函數(shù),deployer是Truffle提供的部署工具對(duì)象。該函數(shù)的主要內(nèi)容是使用deployer對(duì)象部署之前加載的“HelloWorld”智能合約。當(dāng)Truffle執(zhí)行部署腳本時(shí),會(huì)調(diào)用該函數(shù)并傳入相應(yīng)的deployer對(duì)象,從而將“HelloWorld”智能合約部署到Ganache中。263.7.EditTestScriptCreateHelloWorld.jsinthetestdirectory.HereisatestsuitedefinedfortheHelloWorldsmartcontract.TheaccountsparameterisanarrayprovidedbyTruffle,containingtheaddressesofEthereumaccountsinthetestingenvironment,whichcanbeusedtosimulateoperationssuchastransactionsenders.3.7編輯測(cè)試腳本在“test”目錄下創(chuàng)建“helloworld.js”文件。此處為“HelloWorld”智能合約定義了一個(gè)測(cè)試套件。accounts參數(shù)是Truffle提供的一個(gè)數(shù)組,包含測(cè)試環(huán)境中以太坊賬戶的地址,可用于模擬交易發(fā)送者等操作。284PARTtestcase第四部分:測(cè)試用例304.1.TestCase1Instancebasedcontract:First,createanewinstanceoftheHelloWorldcontract.Theawaitkeywordindicatesthatthisisanasynchronousoperation,ascreatingacontractinstanceinvolvesinteractingwiththeEthereumblockchainandtakestime.StepOneSetnewvalueandvalidateresult:CallthegetmethodintheHelloWorldcontractinstance.ThismethodshouldbedefinedintheHelloWorldcontracttoobtainthenamevalue,whichisinferredtobeastringstoredinthecontractbasedonthefollowingassertion.assert.equal(result,"Hello,World!");Thisisanassertionstatementusedtoverifywhetherthevaluereturnedbythegetmethodisequaltotheexpected"Hello,World!".Ifitisnotequal,thetestfails.StepOne4.1測(cè)試用例1第一步:基于合約創(chuàng)建實(shí)例首先,創(chuàng)建一個(gè)“HelloWorld”合約的新實(shí)例。await關(guān)鍵字表示這是一個(gè)異步操作,因?yàn)閯?chuàng)建合約實(shí)例需要與以太坊區(qū)塊鏈進(jìn)行交互,此過(guò)程需要一定時(shí)間。32第二步:設(shè)置新值并驗(yàn)證結(jié)果調(diào)用“HelloWorld”合約實(shí)例中的get方法。該方法應(yīng)在“HelloWorld”合約中定義,用于獲取name的值,根據(jù)后續(xù)的斷言可推斷其為存儲(chǔ)在合約中的字符串。assert.equal(result,"Hello,World!");這是一條斷言語(yǔ)句,用于驗(yàn)證get方法返回的值是否與預(yù)期的“Hello,World!”相等。若不相等,則測(cè)試失敗。4.2.TestCase2Similarly,firstperformcontractinstantiation.StepOneSetnewvalueandvalidateresult:CallthesetmethodintheHelloWorldcontractinstance,passing'NewName'asaparameter.Thismethodisusedtosetanewvalueinthecontract,whichmaybetoupdatethevalueofastoragevariable.Callthegetmethodagaintoobtaintheupdatedvalue.assert.equal(newResult,'NewName');Finally,verifythroughassertionswhetherthenewvaluereturnedbythegetmethodisequaltothepreviouslyset'NewName'.Ifitisequal,itindicatesthatthesetmethodhascorrectlyupdat

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論