Skip to content

Commit

Permalink
chore: fix TestVerifyAuthorshipRight_Equivocation test
Browse files Browse the repository at this point in the history
  • Loading branch information
EclesioMeloJunior committed Jul 3, 2023
1 parent 0ea1537 commit 9e0ce89
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 15 deletions.
47 changes: 39 additions & 8 deletions dot/state/slot.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ func NewSlotState(db *chaindb.BadgerDB) *SlotState {
}

type headerAndSigner struct {
Header *types.Header
Signer types.AuthorityID
Header *types.Header `scale:"1"`
Signer types.AuthorityID `scale:"2"`
}

func (s *SlotState) CheckEquivocation(slotNow, slot uint64, header *types.Header,
Expand All @@ -56,16 +56,34 @@ func (s *SlotState) CheckEquivocation(slotNow, slot uint64, header *types.Header
binary.LittleEndian.PutUint64(slotEncoded, slot)

currentSlotKey := bytes.Join([][]byte{slotHeaderMapKey, slotEncoded[:]}, nil)
encodedheadersWithSigners, err := s.db.Get(currentSlotKey)
encodedHeadersWithSigners, err := s.db.Get(currentSlotKey)
if err != nil && !errors.Is(err, chaindb.ErrKeyNotFound) {
return nil, fmt.Errorf("getting key slot header map key %d: %w", slot, err)
}

headersWithSigners := make([]headerAndSigner, 0)
if len(encodedheadersWithSigners) > 0 {
err = scale.Unmarshal(encodedheadersWithSigners, &headersWithSigners)
if len(encodedHeadersWithSigners) > 0 {
encodedSliceHeadersWithSigners := make([][]byte, 0)

err = scale.Unmarshal(encodedHeadersWithSigners, &encodedSliceHeadersWithSigners)
if err != nil {
return nil, fmt.Errorf("unmarshaling headers with signers: %w", err)
return nil, fmt.Errorf("unmarshaling encoded headers with signers: %w", err)
}

for _, encodedHeaderAndSigner := range encodedSliceHeadersWithSigners {
// each header and signer instance should have an empty header
// so we will be able to scale decode the whole byte stream with
// the digests correctly in place
decodedHeaderAndSigner := headerAndSigner{
Header: types.NewEmptyHeader(),
}

err := scale.Unmarshal(encodedHeaderAndSigner, &decodedHeaderAndSigner)
if err != nil {
return nil, fmt.Errorf("unmarshaling header with signer: %w", err)
}

headersWithSigners = append(headersWithSigners, decodedHeaderAndSigner)
}
}

Expand Down Expand Up @@ -121,13 +139,26 @@ func (s *SlotState) CheckEquivocation(slotNow, slot uint64, header *types.Header
}

headersWithSigners = append(headersWithSigners, headerAndSigner{Header: header, Signer: signer})
encodedheadersWithSigners, err = scale.Marshal(headersWithSigners)
encodedHeaderAndSigner := make([][]byte, len(headersWithSigners))

// encode each header and signer and push to a slice of bytes
// that will be scale encoded and stored in the database
for idx, headerAndSigner := range headersWithSigners {
encoded, err := scale.Marshal(headerAndSigner)
if err != nil {
return nil, fmt.Errorf("marshalling header and signer: %w", err)
}

encodedHeaderAndSigner[idx] = encoded
}

encodedHeadersWithSigners, err = scale.Marshal(encodedHeaderAndSigner)
if err != nil {
return nil, fmt.Errorf("marshalling: %w", err)
}

batch := s.db.NewBatch()
err = batch.Put(currentSlotKey, encodedheadersWithSigners)
err = batch.Put(currentSlotKey, encodedHeadersWithSigners)
if err != nil {
return nil, fmt.Errorf("while batch putting encoded headers with signers: %w", err)
}
Expand Down
22 changes: 15 additions & 7 deletions lib/babe/verify_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,21 +497,29 @@ func TestVerifyAuthorshipRight_Equivocation(t *testing.T) {
epochData, err := babeService.initiateEpoch(testEpochIndex)
require.NoError(t, err)

bestBlockHash := babeService.blockState.BestBlockHash()
runtime, err := babeService.blockState.GetRuntime(bestBlockHash)
require.NoError(t, err)
// bestBlockHash := babeService.blockState.BestBlockHash()
// runtime, err := babeService.blockState.GetRuntime(bestBlockHash)
// require.NoError(t, err)

// slots are 6 seconds on westend and using time.Now() allows us to create a block at any point in the slot.
// So we need to manually set time to produce consistent results. See here:
// https://github.com/paritytech/substrate/blob/09de7b41599add51cf27eca8f1bc4c50ed8e9453/frame/timestamp/src/lib.rs#L229
// https://github.com/paritytech/substrate/blob/09de7b41599add51cf27eca8f1bc4c50ed8e9453/frame/timestamp/src/lib.rs#L206
timestamp := time.Unix(6, 0)
slot := getSlot(t, runtime, timestamp)
block := createTestBlockWithSlot(t, babeService, &genesisHeader, [][]byte{}, testEpochIndex, epochData, slot)

const slotDuration = 6 * time.Second
slotNumber := getCurrentSlot(slotDuration)
startTime := getSlotStartTime(slotNumber, slotDuration)
slot := NewSlot(startTime, slotDuration, slotNumber)

if time.Now().After(startTime) {
slot = NewSlot(startTime.Add(6*time.Second), slotDuration, slotNumber+1)
}

block := createTestBlockWithSlot(t, babeService, &genesisHeader, [][]byte{}, testEpochIndex, epochData, *slot)
block.Header.Hash()

// create new block for same slot
block2 := createTestBlockWithSlot(t, babeService, &genesisHeader, [][]byte{}, testEpochIndex, epochData, slot)
block2 := createTestBlockWithSlot(t, babeService, &genesisHeader, [][]byte{}, testEpochIndex, epochData, *slot)
block2.Header.Hash()

err = babeService.blockState.AddBlock(block)
Expand Down

0 comments on commit 9e0ce89

Please sign in to comment.