good morning!!!!

Skip to content
Snippets Groups Projects
Commit 8f7174fb authored by Shivam Sharma's avatar Shivam Sharma
Browse files

Refactor Code

parent 949822c0
No related branches found
No related tags found
No related merge requests found
...@@ -53,96 +53,71 @@ func TestChain2HeadEvent(t *testing.T) { ...@@ -53,96 +53,71 @@ func TestChain2HeadEvent(t *testing.T) {
t.Fatalf("failed to insert chain: %v", err) t.Fatalf("failed to insert chain: %v", err)
} }
// first two block of the secondary chain are for a brief moment considered
// side chains because up to that point the first one is considered the
// heavier chain.
expectedReorgHashes := map[common.Hash]bool{
replacementBlocks[0].Hash(): true,
replacementBlocks[1].Hash(): true,
replacementBlocks[2].Hash(): true,
}
expectedReplacedHashes := map[common.Hash]bool{
chain[0].Hash(): true,
chain[1].Hash(): true,
chain[2].Hash(): true,
}
expectedForkHashes := map[common.Hash]bool{
replacementBlocks[0].Hash(): true,
replacementBlocks[1].Hash(): true,
}
expectedHeadHashes := map[common.Hash]bool{
chain[2].Hash(): true,
replacementBlocks[3].Hash(): true,
}
i := 0 i := 0
tot := 0
readEvent := func() *Chain2HeadEvent { readEvent := func() *Chain2HeadEvent {
select { select {
case ev := <-chain2HeadCh: case ev := <-chain2HeadCh:
i++ i++
if ev.Type == Chain2HeadReorgEvent { return &ev
//Reorg Event Sends Chain of Added Blocks in NewChain. So need to check all of them in reorgHashes case <-time.After(2 * time.Second):
for j := 0; j < len(ev.OldChain); j++ { t.Fatal("timeout")
tot++ }
block := ev.OldChain[j] return nil
if _, ok := expectedReplacedHashes[block.Hash()]; !ok {
t.Errorf("%d: didn't expect %x to be in side chain", i, block.Hash())
} }
// head event
event1 := readEvent()
if event1.Type != Chain2HeadCanonicalEvent {
t.Fatal("it should be type head")
} }
//Reorg Event also Sends Chain of Removed Blocks in NewChain. So need to check all of them in replacedHashes if event1.NewChain[0].Hash() != chain[2].Hash() {
for j := 0; j < len(ev.OldChain); j++ { t.Fatalf("%d : Hash Does Not Match", i)
tot++
block := ev.NewChain[j]
if _, ok := expectedReorgHashes[block.Hash()]; !ok {
t.Errorf("%d: didn't expect %x to be in side chain", i, block.Hash())
} }
// fork event
event2 := readEvent()
if event2.Type != Chain2HeadForkEvent {
t.Fatal("it should be type fork")
} }
if event2.NewChain[0].Hash() != replacementBlocks[0].Hash() {
t.Fatalf("%d : Hash Does Not Match", i)
} }
if ev.Type == Chain2HeadForkEvent { // fork event
tot++ event3 := readEvent()
block := ev.NewChain[0] if event3.Type != Chain2HeadForkEvent {
if _, ok := expectedForkHashes[block.Hash()]; !ok { t.Fatal("it should be type fork")
t.Errorf("%d: didn't expect %x to be in fork chain", i, block.Hash())
} }
if event3.NewChain[0].Hash() != replacementBlocks[1].Hash() {
t.Fatalf("%d : Hash Does Not Match", i)
} }
if ev.Type == Chain2HeadCanonicalEvent { // reorg event
tot++ //In this event the channel recieves an array of Blocks in NewChain and OldChain
block := ev.NewChain[0] expectedOldChainHashes := [3]common.Hash{0: chain[2].Hash(), 1: chain[1].Hash(), 2: chain[0].Hash()}
if _, ok := expectedHeadHashes[block.Hash()]; !ok { expectedNewChainHashes := [3]common.Hash{0: replacementBlocks[2].Hash(), 1: replacementBlocks[1].Hash(), 2: replacementBlocks[0].Hash()}
t.Errorf("%d: didn't expect %x to be in head chain", i, block.Hash())
event4 := readEvent()
if event4.Type != Chain2HeadReorgEvent {
t.Fatal("it should be type reorg")
} }
for j := 0; j < len(event4.OldChain); j++ {
if event4.OldChain[j].Hash() != expectedOldChainHashes[j] {
t.Fatalf("%d : Oldchain hashes Does Not Match", i)
} }
return nil
case <-time.After(2 * time.Second):
t.Fatal("timeout")
} }
return nil for j := 0; j < len(event4.NewChain); j++ {
if event4.NewChain[j].Hash() != expectedNewChainHashes[j] {
t.Fatalf("%d : Newchain hashes Does Not Match", i)
}
} }
// head event // head event
readEvent() event5 := readEvent()
if event5.Type != Chain2HeadCanonicalEvent {
// fork event t.Fatal("it should be type head")
readEvent() }
if event5.NewChain[0].Hash() != replacementBlocks[3].Hash() {
// fork event t.Fatalf("%d : Hash Does Not Match", i)
readEvent() }
// reorg event
readEvent()
// head event
readEvent()
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment