-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add recent history integration test (#138)
- Separate core logic for updating recent history for simpler integration tests. - Add recent history test vectors, and integration test. - Fix bug in MMR. We shouldn't always hash the input.
- Loading branch information
Showing
7 changed files
with
763 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
//go:build integration | ||
|
||
package integration_test | ||
|
||
import ( | ||
"encoding/json" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/eigerco/strawberry/internal/crypto" | ||
"github.com/eigerco/strawberry/internal/state" | ||
"github.com/eigerco/strawberry/internal/statetransition" | ||
"github.com/eigerco/strawberry/internal/testutils" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestRecentHistory(t *testing.T) { | ||
testFiles := []string{ | ||
// Empty hjstory queue. | ||
"vectors/recenthistory/progress_blocks_history-1.json", | ||
|
||
// Not empty nor full history queue. | ||
"vectors/recenthistory/progress_blocks_history-2.json", | ||
|
||
// Fill the history queue. | ||
"vectors/recenthistory/progress_blocks_history-3.json", | ||
|
||
// Shift the history queue. | ||
"vectors/recenthistory/progress_blocks_history-4.json", | ||
} | ||
|
||
for _, tf := range testFiles { | ||
t.Run(filepath.Base(tf), func(t *testing.T) { | ||
file, err := os.ReadFile(tf) | ||
require.NoError(t, err) | ||
|
||
var tv RecentHistoryTestVector | ||
err = json.Unmarshal(file, &tv) | ||
require.NoError(t, err) | ||
|
||
// Inputs. | ||
headerHash := crypto.Hash(testutils.MustFromHex(t, tv.Input.HeaderHash)) | ||
parentStateRoot := crypto.Hash(testutils.MustFromHex(t, tv.Input.ParentStateRoot)) | ||
accumulateRoot := crypto.Hash(testutils.MustFromHex(t, tv.Input.AccumulateRoot)) | ||
|
||
preRecentBlocks := toRecentBlocks(t, tv.PreState) | ||
|
||
workPackages := map[crypto.Hash]crypto.Hash{} | ||
for _, wp := range tv.Input.WorkPackages { | ||
hash := crypto.Hash(testutils.MustFromHex(t, wp.Hash)) | ||
exportsRoot := crypto.Hash(testutils.MustFromHex(t, wp.ExportsRoot)) | ||
|
||
workPackages[hash] = exportsRoot | ||
} | ||
|
||
newRecentBlocks, err := statetransition.UpdateRecentBlocks(headerHash, parentStateRoot, accumulateRoot, preRecentBlocks, workPackages) | ||
require.NoError(t, err) | ||
|
||
postRecentBlocks := toRecentBlocks(t, tv.PostState) | ||
require.Equal(t, postRecentBlocks, newRecentBlocks) | ||
}) | ||
|
||
} | ||
} | ||
|
||
func toRecentBlocks(t *testing.T, s RecentHistoryTestVectorState) []state.BlockState { | ||
intermediateBlocks := make([]state.BlockState, len(s.Beta)) | ||
for i, bs := range s.Beta { | ||
accResultMMR := make([]*crypto.Hash, len(bs.MMR.Peaks)) | ||
for i, p := range bs.MMR.Peaks { | ||
if p != nil { | ||
peak := crypto.Hash(testutils.MustFromHex(t, *p)) | ||
accResultMMR[i] = &peak | ||
} | ||
} | ||
|
||
workReportHashes := map[crypto.Hash]crypto.Hash{} | ||
for _, wr := range bs.Reported { | ||
hash := crypto.Hash(testutils.MustFromHex(t, wr.Hash)) | ||
exportsRoot := crypto.Hash(testutils.MustFromHex(t, wr.ExportsRoot)) | ||
|
||
workReportHashes[hash] = exportsRoot | ||
} | ||
|
||
intermediateBlocks[i] = state.BlockState{ | ||
HeaderHash: crypto.Hash(testutils.MustFromHex(t, bs.HeaderHash)), | ||
StateRoot: crypto.Hash(testutils.MustFromHex(t, bs.StateRoot)), | ||
AccumulationResultMMR: accResultMMR, | ||
WorkReportHashes: workReportHashes, | ||
} | ||
} | ||
|
||
return intermediateBlocks | ||
} | ||
|
||
type RecentHistoryTestVector struct { | ||
Input struct { | ||
HeaderHash string `json:"header_hash"` | ||
ParentStateRoot string `json:"parent_state_root"` | ||
AccumulateRoot string `json:"accumulate_root"` | ||
WorkPackages []struct { | ||
Hash string `json:"hash"` | ||
ExportsRoot string `json:"exports_root"` | ||
} `json:"work_packages"` | ||
} `json:"input"` | ||
PreState RecentHistoryTestVectorState `json:"pre_state"` | ||
Output interface{} `json:"output"` | ||
PostState RecentHistoryTestVectorState `json:"post_state"` | ||
} | ||
|
||
type RecentHistoryTestVectorState struct { | ||
Beta []struct { | ||
HeaderHash string `json:"header_hash"` | ||
MMR struct { | ||
Peaks []*string `json:"peaks"` | ||
} `json:"mmr"` | ||
StateRoot string `json:"state_root"` | ||
Reported []struct { | ||
Hash string `json:"hash"` | ||
ExportsRoot string `json:"exports_root"` | ||
} `json:"reported"` | ||
} `json:"beta"` | ||
} |
44 changes: 44 additions & 0 deletions
44
tests/integration/vectors/recenthistory/progress_blocks_history-1.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
{ | ||
"input": { | ||
"header_hash": "0x530ef4636fedd498e99c7601581271894a53e965e901e8fa49581e525f165dae", | ||
"parent_state_root": "0x0e6c6cbf80b5fb00175001f7b0966bf1af83ff4406ede84f29a666a0fcbac801", | ||
"accumulate_root": "0x8720b97ddd6acc0f6eb66e095524038675a4e4067adc10ec39939eaefc47d842", | ||
"work_packages": [ | ||
{ | ||
"hash": "0x016cb55eb7b84e0d495d40832c7238965baeb468932c415dc2ceffe0afb039e5", | ||
"exports_root": "0x935f6dfef36fa06e10a9ba820f933611c05c06a207b07141fe8d87465870c11c" | ||
}, | ||
{ | ||
"hash": "0x76bcb24901299c331f0ca7342f4874f19b213ee72df613d50699e7e25edb82a6", | ||
"exports_root": "0xc825d16b7325ca90287123bd149d47843c999ce686ed51eaf8592dd2759272e3" | ||
} | ||
] | ||
}, | ||
"pre_state": { | ||
"beta": [] | ||
}, | ||
"output": null, | ||
"post_state": { | ||
"beta": [ | ||
{ | ||
"header_hash": "0x530ef4636fedd498e99c7601581271894a53e965e901e8fa49581e525f165dae", | ||
"mmr": { | ||
"peaks": [ | ||
"0x8720b97ddd6acc0f6eb66e095524038675a4e4067adc10ec39939eaefc47d842" | ||
] | ||
}, | ||
"state_root": "0x0000000000000000000000000000000000000000000000000000000000000000", | ||
"reported": [ | ||
{ | ||
"hash": "0x016cb55eb7b84e0d495d40832c7238965baeb468932c415dc2ceffe0afb039e5", | ||
"exports_root": "0x935f6dfef36fa06e10a9ba820f933611c05c06a207b07141fe8d87465870c11c" | ||
}, | ||
{ | ||
"hash": "0x76bcb24901299c331f0ca7342f4874f19b213ee72df613d50699e7e25edb82a6", | ||
"exports_root": "0xc825d16b7325ca90287123bd149d47843c999ce686ed51eaf8592dd2759272e3" | ||
} | ||
] | ||
} | ||
] | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
tests/integration/vectors/recenthistory/progress_blocks_history-2.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
{ | ||
"input": { | ||
"header_hash": "0x241d129c6edc2114e6dfba7d556f7f7c66399b55ceec3078a53d44c752ba7e9a", | ||
"parent_state_root": "0x1831dde64e40bfd8639c2d122e5ac00fe133c48cd16e1621ca6d5cf0b8e10d3b", | ||
"accumulate_root": "0x7507515a48439dc58bc318c48a120b656136699f42bfd2bd45473becba53462d", | ||
"work_packages": [ | ||
{ | ||
"hash": "0x3cc8d8c94e7b3ee01e678c63fd6b5db894fc807dff7fe10a11ab41e70194894d", | ||
"exports_root": "0xc0edfe377d20b9f4ed7d9df9511ef904c87e24467364f0f7f75f20cfe90dd8fb" | ||
} | ||
] | ||
}, | ||
"pre_state": { | ||
"beta": [ | ||
{ | ||
"header_hash": "0x530ef4636fedd498e99c7601581271894a53e965e901e8fa49581e525f165dae", | ||
"mmr": { | ||
"peaks": [ | ||
"0x8720b97ddd6acc0f6eb66e095524038675a4e4067adc10ec39939eaefc47d842" | ||
] | ||
}, | ||
"state_root": "0x0000000000000000000000000000000000000000000000000000000000000000", | ||
"reported": [ | ||
{ | ||
"hash": "0x016cb55eb7b84e0d495d40832c7238965baeb468932c415dc2ceffe0afb039e5", | ||
"exports_root": "0x935f6dfef36fa06e10a9ba820f933611c05c06a207b07141fe8d87465870c11c" | ||
}, | ||
{ | ||
"hash": "0x76bcb24901299c331f0ca7342f4874f19b213ee72df613d50699e7e25edb82a6", | ||
"exports_root": "0xc825d16b7325ca90287123bd149d47843c999ce686ed51eaf8592dd2759272e3" | ||
} | ||
] | ||
} | ||
] | ||
}, | ||
"output": null, | ||
"post_state": { | ||
"beta": [ | ||
{ | ||
"header_hash": "0x530ef4636fedd498e99c7601581271894a53e965e901e8fa49581e525f165dae", | ||
"mmr": { | ||
"peaks": [ | ||
"0x8720b97ddd6acc0f6eb66e095524038675a4e4067adc10ec39939eaefc47d842" | ||
] | ||
}, | ||
"state_root": "0x1831dde64e40bfd8639c2d122e5ac00fe133c48cd16e1621ca6d5cf0b8e10d3b", | ||
"reported": [ | ||
{ | ||
"hash": "0x016cb55eb7b84e0d495d40832c7238965baeb468932c415dc2ceffe0afb039e5", | ||
"exports_root": "0x935f6dfef36fa06e10a9ba820f933611c05c06a207b07141fe8d87465870c11c" | ||
}, | ||
{ | ||
"hash": "0x76bcb24901299c331f0ca7342f4874f19b213ee72df613d50699e7e25edb82a6", | ||
"exports_root": "0xc825d16b7325ca90287123bd149d47843c999ce686ed51eaf8592dd2759272e3" | ||
} | ||
] | ||
}, | ||
{ | ||
"header_hash": "0x241d129c6edc2114e6dfba7d556f7f7c66399b55ceec3078a53d44c752ba7e9a", | ||
"mmr": { | ||
"peaks": [ | ||
null, | ||
"0x7076c31882a5953e097aef8378969945e72807c4705e53a0c5aacc9176f0d56b" | ||
] | ||
}, | ||
"state_root": "0x0000000000000000000000000000000000000000000000000000000000000000", | ||
"reported": [ | ||
{ | ||
"hash": "0x3cc8d8c94e7b3ee01e678c63fd6b5db894fc807dff7fe10a11ab41e70194894d", | ||
"exports_root": "0xc0edfe377d20b9f4ed7d9df9511ef904c87e24467364f0f7f75f20cfe90dd8fb" | ||
} | ||
] | ||
} | ||
] | ||
} | ||
} |
Oops, something went wrong.