Skip to content

Commit

Permalink
get last checkpoint fix in publisher handler
Browse files Browse the repository at this point in the history
  • Loading branch information
ssd04 committed Aug 6, 2024
1 parent 865512d commit 7fbca4c
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 4 deletions.
7 changes: 7 additions & 0 deletions process/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"math/big"

"github.com/multiversx/mx-chain-storage-go/types"
"github.com/multiversx/mx-chain-ws-connector-firehose-go/data"
"github.com/multiversx/mx-chain-ws-connector-firehose-go/data/hyperOutportBlocks"
)

const (
Expand Down Expand Up @@ -42,3 +44,8 @@ func (ps *pruningStorer) GetPersisterPaths() ([]string, error) {
func (o *outportBlockConverter) CastBigInt(i *big.Int) ([]byte, error) {
return o.castBigInt(i)
}

// GetLastBlockCheckpoint -
func (ph *publisherHandler) GetLastBlockCheckpoint(hyperOutportBlock *hyperOutportBlocks.HyperOutportBlock) (*data.BlockCheckpoint, error) {
return ph.getLastBlockCheckpoint(hyperOutportBlock)
}
38 changes: 36 additions & 2 deletions process/firehosePublisher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func createHyperOutportBlock() *data.HyperOutportBlock {
ShardID: 1,
BlockData: &data.MetaBlockData{
Header: &data.MetaHeader{
Nonce: 1,
Nonce: 16,
PrevHash: []byte("prev hash"),
TimeStamp: 100,
},
Expand All @@ -43,7 +43,41 @@ func createHyperOutportBlock() *data.HyperOutportBlock {
HighestFinalBlockNonce: 0,
HighestFinalBlockHash: []byte{},
},
NotarizedHeadersOutportData: []*data.NotarizedHeaderOutportData{},
NotarizedHeadersOutportData: []*data.NotarizedHeaderOutportData{
&data.NotarizedHeaderOutportData{
OutportBlock: &data.ShardOutportBlock{
ShardID: 0,
BlockData: &data.BlockData{
ShardID: 0,
Header: &data.Header{
Nonce: 10,
},
},
},
},
&data.NotarizedHeaderOutportData{
OutportBlock: &data.ShardOutportBlock{
ShardID: 2,
BlockData: &data.BlockData{
ShardID: 2,
Header: &data.Header{
Nonce: 12,
},
},
},
},
&data.NotarizedHeaderOutportData{
OutportBlock: &data.ShardOutportBlock{
ShardID: 1,
BlockData: &data.BlockData{
ShardID: 1,
Header: &data.Header{
Nonce: 11,
},
},
},
},
},
}

return hyperOutportBlock
Expand Down
7 changes: 5 additions & 2 deletions process/publisherHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,11 @@ func (ph *publisherHandler) getLastBlockCheckpoint(hyperOutportBlock *hyperOutpo
return nil, err
}

checkpoint := &data.BlockCheckpoint{
LastNonces: make(map[uint32]uint64),
checkpoint, err := ph.outportBlocksPool.GetLastCheckpoint()
if err != nil {
checkpoint = &data.BlockCheckpoint{
LastNonces: make(map[uint32]uint64),
}
}

metaBlock := hyperOutportBlock.MetaOutportBlock.BlockData.Header
Expand Down
82 changes: 82 additions & 0 deletions process/publisherHandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,88 @@ func TestPublisherHandler_PublishBlock(t *testing.T) {
})
}

func TestPublisherHandler_GetLastBlockCheckpoint(t *testing.T) {
t.Parallel()

args := createDefaultPublisherHandlerArgs()

hyperOutportBlock := &hyperOutportBlocks.HyperOutportBlock{
MetaOutportBlock: &hyperOutportBlocks.MetaOutportBlock{
ShardID: 1,
BlockData: &hyperOutportBlocks.MetaBlockData{
Header: &hyperOutportBlocks.MetaHeader{
Nonce: 16,
PrevHash: []byte("prev hash"),
TimeStamp: 100,
},
HeaderType: string(core.ShardHeaderV1),
HeaderHash: []byte("hash"),
},
NotarizedHeadersHashes: []string{},
NumberOfShards: 0,
SignersIndexes: []uint64{},
HighestFinalBlockNonce: 0,
HighestFinalBlockHash: []byte{},
},
NotarizedHeadersOutportData: []*hyperOutportBlocks.NotarizedHeaderOutportData{
&hyperOutportBlocks.NotarizedHeaderOutportData{
OutportBlock: &hyperOutportBlocks.ShardOutportBlock{
ShardID: 0,
BlockData: &hyperOutportBlocks.BlockData{
ShardID: 0,
Header: &hyperOutportBlocks.Header{
Nonce: 11,
},
},
},
},
&hyperOutportBlocks.NotarizedHeaderOutportData{
OutportBlock: &hyperOutportBlocks.ShardOutportBlock{
ShardID: 2,
BlockData: &hyperOutportBlocks.BlockData{
ShardID: 2,
Header: &hyperOutportBlocks.Header{
Nonce: 13,
},
},
},
},
},
}

args.OutportBlocksPool = &testscommon.HyperBlocksPoolMock{
GetLastCheckpointCalled: func() (*data.BlockCheckpoint, error) {
return &data.BlockCheckpoint{
LastNonces: map[uint32]uint64{
core.MetachainShardId: 15,
0: 10,
1: 11,
2: 12,
},
}, nil
},
}

ph, err := process.NewPublisherHandler(args)
require.Nil(t, err)
require.NotNil(t, ph)

// shard 1 nonce is taken from last block checkpoint since it was missing from hyper outport block
expectedCheckpoint := &data.BlockCheckpoint{
LastNonces: map[uint32]uint64{
core.MetachainShardId: 16,
0: 11,
1: 11,
2: 13,
},
}

lastCheckpoint, err := ph.GetLastBlockCheckpoint(hyperOutportBlock)
require.Nil(t, err)

require.Equal(t, expectedCheckpoint, lastCheckpoint)
}

func TestPublisherHandler_Close(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit 7fbca4c

Please sign in to comment.