-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
afa3e68
commit 404f5fe
Showing
4 changed files
with
348 additions
and
6 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
251 changes: 251 additions & 0 deletions
251
dot/parachain/prospective-parachains/fragment-chain/fragment_chain_test.go
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,251 @@ | ||
package fragmentchain | ||
|
||
import ( | ||
"testing" | ||
|
||
parachaintypes "github.com/ChainSafe/gossamer/dot/parachain/types" | ||
inclusionemulator "github.com/ChainSafe/gossamer/dot/parachain/util/inclusion-emulator" | ||
"github.com/ChainSafe/gossamer/lib/common" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCandidateStorage_RemoveCandidate(t *testing.T) { | ||
storage := &CandidateStorage{ | ||
byParentHead: make(map[common.Hash]map[parachaintypes.CandidateHash]any), | ||
byOutputHead: make(map[common.Hash]map[parachaintypes.CandidateHash]any), | ||
byCandidateHash: make(map[parachaintypes.CandidateHash]*CandidateEntry), | ||
} | ||
|
||
candidateHash := parachaintypes.CandidateHash{Value: common.Hash{1, 2, 3}} | ||
parentHeadHash := common.Hash{4, 5, 6} | ||
outputHeadHash := common.Hash{7, 8, 9} | ||
|
||
entry := &CandidateEntry{ | ||
candidateHash: candidateHash, | ||
parentHeadDataHash: parentHeadHash, | ||
outputHeadDataHash: outputHeadHash, | ||
state: Backed, | ||
} | ||
|
||
storage.byCandidateHash[candidateHash] = entry | ||
storage.byParentHead[parentHeadHash] = map[parachaintypes.CandidateHash]any{candidateHash: struct{}{}} | ||
storage.byOutputHead[outputHeadHash] = map[parachaintypes.CandidateHash]any{candidateHash: struct{}{}} | ||
|
||
storage.removeCandidate(candidateHash) | ||
|
||
_, exists := storage.byCandidateHash[candidateHash] | ||
assert.False(t, exists, "candidate should be removed from byCandidateHash") | ||
|
||
_, exists = storage.byParentHead[parentHeadHash] | ||
assert.False(t, exists, "candidate should be removed from byParentHead") | ||
|
||
_, exists = storage.byOutputHead[outputHeadHash] | ||
assert.False(t, exists, "candidate should be removed from byOutputHead") | ||
} | ||
|
||
func TestCandidateStorage_MarkBacked(t *testing.T) { | ||
storage := &CandidateStorage{ | ||
byParentHead: make(map[common.Hash]map[parachaintypes.CandidateHash]any), | ||
byOutputHead: make(map[common.Hash]map[parachaintypes.CandidateHash]any), | ||
byCandidateHash: make(map[parachaintypes.CandidateHash]*CandidateEntry), | ||
} | ||
|
||
candidateHash := parachaintypes.CandidateHash{Value: common.Hash{1, 2, 3}} | ||
parentHeadHash := common.Hash{4, 5, 6} | ||
outputHeadHash := common.Hash{7, 8, 9} | ||
|
||
entry := &CandidateEntry{ | ||
candidateHash: candidateHash, | ||
parentHeadDataHash: parentHeadHash, | ||
outputHeadDataHash: outputHeadHash, | ||
state: Seconded, | ||
} | ||
|
||
storage.byCandidateHash[candidateHash] = entry | ||
storage.byParentHead[parentHeadHash] = map[parachaintypes.CandidateHash]any{candidateHash: struct{}{}} | ||
storage.byOutputHead[outputHeadHash] = map[parachaintypes.CandidateHash]any{candidateHash: struct{}{}} | ||
|
||
storage.MarkBacked(candidateHash) | ||
|
||
assert.Equal(t, Backed, entry.state, "candidate state should be marked as backed") | ||
} | ||
|
||
func TestCandidateStorage_HeadDataByHash(t *testing.T) { | ||
tests := map[string]struct { | ||
setup func() *CandidateStorage | ||
hash common.Hash | ||
expected *parachaintypes.HeadData | ||
}{ | ||
"find_head_data_of_first_candidate_using_output_head_data_hash": { | ||
setup: func() *CandidateStorage { | ||
storage := &CandidateStorage{ | ||
byParentHead: make(map[common.Hash]map[parachaintypes.CandidateHash]any), | ||
byOutputHead: make(map[common.Hash]map[parachaintypes.CandidateHash]any), | ||
byCandidateHash: make(map[parachaintypes.CandidateHash]*CandidateEntry), | ||
} | ||
|
||
candidateHash := parachaintypes.CandidateHash{Value: common.Hash{1, 2, 3}} | ||
parentHeadHash := common.Hash{4, 5, 6} | ||
outputHeadHash := common.Hash{7, 8, 9} | ||
headData := parachaintypes.HeadData{Data: []byte{10, 11, 12}} | ||
|
||
entry := &CandidateEntry{ | ||
candidateHash: candidateHash, | ||
parentHeadDataHash: parentHeadHash, | ||
outputHeadDataHash: outputHeadHash, | ||
candidate: inclusionemulator.ProspectiveCandidate{ | ||
Commitments: parachaintypes.CandidateCommitments{ | ||
HeadData: headData, | ||
}, | ||
}, | ||
} | ||
|
||
storage.byCandidateHash[candidateHash] = entry | ||
storage.byParentHead[parentHeadHash] = map[parachaintypes.CandidateHash]any{candidateHash: struct{}{}} | ||
storage.byOutputHead[outputHeadHash] = map[parachaintypes.CandidateHash]any{candidateHash: struct{}{}} | ||
|
||
return storage | ||
}, | ||
hash: common.Hash{7, 8, 9}, | ||
expected: ¶chaintypes.HeadData{Data: []byte{10, 11, 12}}, | ||
}, | ||
"find_head_data_using_parent_head_data_hash_from_second_candidate": { | ||
setup: func() *CandidateStorage { | ||
storage := &CandidateStorage{ | ||
byParentHead: make(map[common.Hash]map[parachaintypes.CandidateHash]any), | ||
byOutputHead: make(map[common.Hash]map[parachaintypes.CandidateHash]any), | ||
byCandidateHash: make(map[parachaintypes.CandidateHash]*CandidateEntry), | ||
} | ||
|
||
candidateHash := parachaintypes.CandidateHash{Value: common.Hash{13, 14, 15}} | ||
parentHeadHash := common.Hash{16, 17, 18} | ||
outputHeadHash := common.Hash{19, 20, 21} | ||
headData := parachaintypes.HeadData{Data: []byte{22, 23, 24}} | ||
|
||
entry := &CandidateEntry{ | ||
candidateHash: candidateHash, | ||
parentHeadDataHash: parentHeadHash, | ||
outputHeadDataHash: outputHeadHash, | ||
candidate: inclusionemulator.ProspectiveCandidate{ | ||
PersistedValidationData: parachaintypes.PersistedValidationData{ | ||
ParentHead: headData, | ||
}, | ||
}, | ||
} | ||
|
||
storage.byCandidateHash[candidateHash] = entry | ||
storage.byParentHead[parentHeadHash] = map[parachaintypes.CandidateHash]any{candidateHash: struct{}{}} | ||
storage.byOutputHead[outputHeadHash] = map[parachaintypes.CandidateHash]any{candidateHash: struct{}{}} | ||
|
||
return storage | ||
}, | ||
hash: common.Hash{16, 17, 18}, | ||
expected: ¶chaintypes.HeadData{Data: []byte{22, 23, 24}}, | ||
}, | ||
"use_nonexistent_hash_and_should_get_nil": { | ||
setup: func() *CandidateStorage { | ||
storage := &CandidateStorage{ | ||
byParentHead: make(map[common.Hash]map[parachaintypes.CandidateHash]any), | ||
byOutputHead: make(map[common.Hash]map[parachaintypes.CandidateHash]any), | ||
byCandidateHash: make(map[parachaintypes.CandidateHash]*CandidateEntry), | ||
} | ||
return storage | ||
}, | ||
hash: common.Hash{99, 99, 99}, | ||
expected: nil, | ||
}, | ||
"insert_0_candidates_and_try_to_find_but_should_get_nil": { | ||
setup: func() *CandidateStorage { | ||
return &CandidateStorage{ | ||
byParentHead: make(map[common.Hash]map[parachaintypes.CandidateHash]any), | ||
byOutputHead: make(map[common.Hash]map[parachaintypes.CandidateHash]any), | ||
byCandidateHash: make(map[parachaintypes.CandidateHash]*CandidateEntry), | ||
} | ||
}, | ||
hash: common.Hash{7, 8, 9}, | ||
expected: nil, | ||
}, | ||
} | ||
|
||
for name, tt := range tests { | ||
tt := tt | ||
t.Run(name, func(t *testing.T) { | ||
storage := tt.setup() | ||
result := storage.HeadDataByHash(tt.hash) | ||
assert.Equal(t, tt.expected, result) | ||
}) | ||
} | ||
} | ||
|
||
func TestCandidateStorage_PossibleBackedParaChildren(t *testing.T) { | ||
tests := map[string]struct { | ||
setup func() *CandidateStorage | ||
hash common.Hash | ||
expected []*CandidateEntry | ||
}{ | ||
"insert_2_candidates_for_same_parent_one_seconded_one_backed": { | ||
setup: func() *CandidateStorage { | ||
storage := &CandidateStorage{ | ||
byParentHead: make(map[common.Hash]map[parachaintypes.CandidateHash]any), | ||
byOutputHead: make(map[common.Hash]map[parachaintypes.CandidateHash]any), | ||
byCandidateHash: make(map[parachaintypes.CandidateHash]*CandidateEntry), | ||
} | ||
|
||
candidateHash1 := parachaintypes.CandidateHash{Value: common.Hash{1, 2, 3}} | ||
parentHeadHash := common.Hash{4, 5, 6} | ||
outputHeadHash1 := common.Hash{7, 8, 9} | ||
|
||
candidateHash2 := parachaintypes.CandidateHash{Value: common.Hash{10, 11, 12}} | ||
outputHeadHash2 := common.Hash{13, 14, 15} | ||
|
||
entry1 := &CandidateEntry{ | ||
candidateHash: candidateHash1, | ||
parentHeadDataHash: parentHeadHash, | ||
outputHeadDataHash: outputHeadHash1, | ||
state: Seconded, | ||
} | ||
|
||
entry2 := &CandidateEntry{ | ||
candidateHash: candidateHash2, | ||
parentHeadDataHash: parentHeadHash, | ||
outputHeadDataHash: outputHeadHash2, | ||
state: Backed, | ||
} | ||
|
||
storage.byCandidateHash[candidateHash1] = entry1 | ||
storage.byCandidateHash[candidateHash2] = entry2 | ||
storage.byParentHead[parentHeadHash] = map[parachaintypes.CandidateHash]any{ | ||
candidateHash1: struct{}{}, | ||
candidateHash2: struct{}{}, | ||
} | ||
|
||
return storage | ||
}, | ||
hash: common.Hash{4, 5, 6}, | ||
expected: []*CandidateEntry{{candidateHash: parachaintypes.CandidateHash{Value: common.Hash{10, 11, 12}}, parentHeadDataHash: common.Hash{4, 5, 6}, outputHeadDataHash: common.Hash{13, 14, 15}, state: Backed}}, | ||
}, | ||
"insert_nothing_and_call_function_should_return_nothing": { | ||
setup: func() *CandidateStorage { | ||
return &CandidateStorage{ | ||
byParentHead: make(map[common.Hash]map[parachaintypes.CandidateHash]any), | ||
byOutputHead: make(map[common.Hash]map[parachaintypes.CandidateHash]any), | ||
byCandidateHash: make(map[parachaintypes.CandidateHash]*CandidateEntry), | ||
} | ||
}, | ||
hash: common.Hash{4, 5, 6}, | ||
expected: nil, | ||
}, | ||
} | ||
|
||
for name, tt := range tests { | ||
tt := tt | ||
t.Run(name, func(t *testing.T) { | ||
storage := tt.setup() | ||
var result []*CandidateEntry | ||
for entry := range storage.PossibleBackedParaChildren(tt.hash) { | ||
result = append(result, entry) | ||
} | ||
assert.Equal(t, tt.expected, result) | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.