Skip to content

Commit

Permalink
Restructure and add empty block test case
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan-Ethernal committed Nov 6, 2024
1 parent af47f24 commit f3bdf67
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 15 deletions.
48 changes: 33 additions & 15 deletions smt/pkg/smt/smt_state_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,48 @@ import (
)

func TestSMTApplyTraces(t *testing.T) {
type testCase struct {
type testData struct {
Witness string `json:"witness"`
StateRoot libcommon.Hash `json:"stateRoot"`
Traces map[libcommon.Address]*types.TxnTrace `json:"traces,omitempty"`
}

data, err := os.ReadFile("./testdata/smt-traces.json")
require.NoError(t, err)
cases := []struct {
name string
file string
}{
{
name: "Empty block traces",
file: "./testdata/zerotraces/empty-block.json",
},
{
name: "Non-empty block traces",
file: "./testdata/zerotraces/non-empty-block.json",
},
}

for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
rawTestData, err := os.ReadFile(c.file)
require.NoError(t, err)

var tc testCase
err = json.Unmarshal(data, &tc)
require.NoError(t, err)
var td testData
err = json.Unmarshal(rawTestData, &td)
require.NoError(t, err)

decodedWitnessRaw, err := hex.DecodeString(strings.TrimPrefix(tc.Witness, "0x"))
require.NoError(t, err)
decodedWitnessRaw, err := hex.DecodeString(strings.TrimPrefix(td.Witness, "0x"))
require.NoError(t, err)

witness, err := trie.NewWitnessFromReader(bytes.NewReader(decodedWitnessRaw), false)
require.NoError(t, err)
witness, err := trie.NewWitnessFromReader(bytes.NewReader(decodedWitnessRaw), false)
require.NoError(t, err)

smt, err := BuildSMTfromWitness(witness)
require.NoError(t, err)
smt, err := BuildSMTfromWitness(witness)
require.NoError(t, err)

newSMT, err := smt.ApplyTraces(tc.Traces)
require.NoError(t, err)
newSMT, err := smt.ApplyTraces(td.Traces)
require.NoError(t, err)

require.Equal(t, tc.StateRoot, libcommon.BigToHash(newSMT.LastRoot()))
require.Equal(t, td.StateRoot, libcommon.BigToHash(newSMT.LastRoot()))
})
}
}
5 changes: 5 additions & 0 deletions smt/pkg/smt/testdata/zerotraces/empty-block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"witness": "0x0102030203020302030203020303824f9d5de25a884a5136a93eeffddfef66e372035013d62f7f3eb0b0cc4d02d703e6df3fc31ce4cee4d5251aa31d6dd238e8a7f2424a1eb0f9234e965b814868b10202020303f54d12eb6a118f4beda7736ead8a62e002515333fabfa47200cb4f0d588f870e03f97e71f1558e66521d1571ddca275ce7b23060df4659fc08b7ac190f2cfa7c580203070454b93b2fd69ce28f0db91842abfa40720d7e2b8fd74208330203031e8d607be437f2e43259967c675f94af27aac18757f191d4264eca17da3a4ae303c8657378baa9478c5c231d5723a36ec5802f15b7f7cc8cc3a9cf2402db50cbb6020302020203036c0f9ead22d02e840c2397bedf4956b25b0aaaad312a415c2654f0eee711c736035b0c4607f591ce6a43e7cc5e4f7c7b4c38d71527fadb25cfdcf6a2f5e456132a0202036c1063a46e5a2255b46fa4ca24b7f0a4fae3565e2ba83646c0a2d8d38be4d6d0020302030203020103e4a6c1a5183d2460928b7f8e38d4e5a8cf67e13bbe09e10b36309802b252dec202030203070354d71f8f956ad979cc2988381b8a743a2fe280537d5820360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc549afdfc705db4cbfff9f28f40e37ada1edbfaf2b80700545b06837a43bdc3dd9f114558daf4b26ed49842ed4927ca64f2f6470be7f0070154e5cf69183cfcf0571e733d59a1a53d4e6ced6e85410402020203020202030201020203ff672d2448bb0afdfee7d4d8adb19b0b6e776c1ef9383d742cb8e85193dad2850700548943545177806ed17b9f23f0a21ee5948ecaa77649056bc75e2d6310000007035421d5360f869bf65acfe784070db4698e843b35a358200000000000000000000000000000000000000000000000000000000000000002420e100203070054c3913d4d8bab4914328651c2eae817c8b78e1f4c49056bc75e2d63100000020303c327cf3feffd2364d8b8b2879d8b9fbeb417084baa43524412c48c50cc53c56d039f39a504c02052705841b8c491cfce0b3b1bd517a9b8e05e616d490e43c10ca7020302030203020303a346362a3dc30703197894fd0f7252d962c1d3e0cafe3631abaa79dfedaa14b6035cf64faa193d391e0f97094650c27b44b3fb5204dc4a879f9a906a634746db6102030203032b326cc79f4e464e439b4e0bf4f9737ae53072a71eb5320c1bec69599231b4ed03db888a3c3460ff5977abd4ec9c388300710361042b87086ff7f03476bcd112240701544e59b44847b379578588920ca78fbf26c0b4956c4101020302020316df8cdac7226c92797dda9e3f35ba051c109d2dd2af8925bbb4bfff178768cf0203020303c063aca0beccd28963285de9bd04fb597f73ba3aadaaf94705c4ec31499af38e03df3d7c41c829e8d24c4c358ab1e4eb82e03b0b8ab10efb84caef4c80a8d67a270203020307035421d5360f869bf65acfe784070db4698e843b35a358203412d5605ac6cd444957cedb533e5dacad6378b4bc819ebe3652188a665066d658205f58e3a2316349923ce3780f8d587db2d72378aed66a8261c916544fa6846ca5070054f93ee4cf8c6c40b329b0c0626f28333c132cf24149056bc75e2d63100000070454eca9c346cb0b76340a645fc03a077c2b8ddc5b104201ab0203020102030203035338380753bc9b5c17091af8265d39993f0a0997ed7fffc2e3ea6ea9ce7051ea032a9c17d1f4190e199ddf1b6243c8df291e9d346e54e267d6c93817fdcfd9ab1a020307035421d5360f869bf65acfe784070db4698e843b35a3582064494413541ff93b31aa309254e3fed72a7456e9845988b915b4c7a7ceba881458205f58e3a2316349923ce3780f8d587db2d72378aed66a8261c916544fa6846ca5070054741bfe4802ce1c4b5b00f9df2f5f179a1c89171a49056bc75e2d631000000203020303f81057e4847dc18df8d6c7f1b602f6edb30529227c0aa34d8edf4ffe2f8a6ea5035f0091c8a327d0c1829947c815bc9f9dbe83edd5b90132a6f017937c1085369b0203020303ad2f9d1a28cac851ee66064c55c8ce74f264a87232d307a3bb0dd0924c51981803fcf29668a8ef83e5113c29d62d7fd09b9712da1a77bcd6e493303baae3812da80203070354e5cf69183cfcf0571e733d59a1a53d4e6ced6e855820000000000000000000000000000000000000000000000000000000000000000054e34aaf64b29273b7d567fcfc40544c014eee99700458457fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe03601600081602082378035828234f58015156039578182fd5b8082525050506014600cf30702544e59b44847b379578588920ca78fbf26c0b4956c582065601281d292aaee412dcd3c946ff1e7e339dd8d9e4fe890008e62cd88240ac7",
"stateRoot": "0x76f387e8076dbde5907718ab49b3dd2d0745e79e7e09d222c4b004ebdb9788b9",
"traces": {}
}
File renamed without changes.

0 comments on commit f3bdf67

Please sign in to comment.