Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sync): removing state from cache #506

Merged
merged 2 commits into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ check:
--enable=misspell \
--enable=revive \
--enable=decorder \
--enable=reassign \
--enable=usestdlibvars \
--enable=nilerr \
--enable=gosec \
--enable=exportloopref \
Expand Down
8 changes: 1 addition & 7 deletions cmd/daemon/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ func Start() func(c *cli.Cmd) {
Name: "p password",
Desc: "The wallet password",
})
// TODO: do we need this?
pprofOpt := c.String(cli.StringOpt{
Name: "pprof",
Desc: "debug pprof server address(not recommended to expose to internet)",
Expand All @@ -39,16 +38,11 @@ func Start() func(c *cli.Cmd) {
err := os.Chdir(workingDir)
cmd.FatalErrorCheck(err)

// separate pprof handlers from DefaultServeMux.
pprofMux := http.DefaultServeMux
http.DefaultServeMux = http.NewServeMux()

if *pprofOpt != "" {
cmd.PrintWarnMsg("Starting Debug pprof server on: %v", *pprofOpt)
cmd.PrintWarnMsg("Starting Debug pprof server on: http://%s/debug/pprof/\n", *pprofOpt)
b00f marked this conversation as resolved.
Show resolved Hide resolved
server := &http.Server{
Addr: *pprofOpt,
ReadHeaderTimeout: 3 * time.Second,
Handler: pprofMux,
}
go func() {
err := server.ListenAndServe()
Expand Down
7 changes: 4 additions & 3 deletions sync/bundle/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ func TestInvalidCBOR(t *testing.T) {
assert.Error(t, err)
}
func TestMessageCompress(t *testing.T) {
var blocks = []*block.Block{}
var blocksData = [][]byte{}
for i := 0; i < 10; i++ {
b := block.GenerateTestBlock(nil, nil)
blocks = append(blocks, b)
d, _ := b.Bytes()
blocksData = append(blocksData, d)
}
msg := message.NewBlocksResponseMessage(message.ResponseCodeBusy, 1234, 888, blocks, nil)
msg := message.NewBlocksResponseMessage(message.ResponseCodeBusy, 1234, 888, blocksData, nil)
bdl := NewBundle(network.TestRandomPeerID(), msg)
bs0, err := bdl.Encode()
assert.NoError(t, err)
Expand Down
19 changes: 7 additions & 12 deletions sync/bundle/message/blocks_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,22 @@ type BlocksResponseMessage struct {
ResponseCode ResponseCode `cbor:"1,keyasint"`
SessionID int `cbor:"2,keyasint"`
From uint32 `cbor:"3,keyasint"`
Blocks []*block.Block `cbor:"4,keyasint"`
BlocksData [][]byte `cbor:"4,keyasint"`
LastCertificate *block.Certificate `cbor:"6,keyasint"`
}

func NewBlocksResponseMessage(code ResponseCode, sid int, from uint32,
blocks []*block.Block, cert *block.Certificate) *BlocksResponseMessage {
blocksData [][]byte, lastCert *block.Certificate) *BlocksResponseMessage {
return &BlocksResponseMessage{
ResponseCode: code,
SessionID: sid,
From: from,
Blocks: blocks,
LastCertificate: cert,
BlocksData: blocksData,
LastCertificate: lastCert,
}
}
func (m *BlocksResponseMessage) SanityCheck() error {
for _, b := range m.Blocks {
if err := b.SanityCheck(); err != nil {
return err
}
}
if m.From == 0 && len(m.Blocks) != 0 {
if m.From == 0 && len(m.BlocksData) != 0 {
return errors.Errorf(errors.ErrInvalidHeight, "unexpected block for height zero")
}
if m.LastCertificate != nil {
Expand All @@ -51,12 +46,12 @@ func (m *BlocksResponseMessage) Type() Type {
}

func (m *BlocksResponseMessage) Count() uint32 {
return uint32(len(m.Blocks))
return uint32(len(m.BlocksData))
}

func (m *BlocksResponseMessage) To() uint32 {
// response message without any block
if len(m.Blocks) == 0 {
if len(m.BlocksData) == 0 {
return 0
}
return m.From + m.Count() - 1
Expand Down
14 changes: 10 additions & 4 deletions sync/bundle/message/blocks_response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,26 @@ func TestBlocksResponseMessage(t *testing.T) {
t.Run("Invalid certificate", func(t *testing.T) {
b := block.GenerateTestBlock(nil, nil)
c := block.NewCertificate(-1, nil, nil, nil)
m := NewBlocksResponseMessage(ResponseCodeMoreBlocks, sid, 100, []*block.Block{b}, c)
d, _ := b.Bytes()
m := NewBlocksResponseMessage(ResponseCodeMoreBlocks, sid, 100, [][]byte{d}, c)

assert.Equal(t, errors.Code(m.SanityCheck()), errors.ErrInvalidRound)
})

t.Run("Unexpected block for height zero", func(t *testing.T) {
b := block.GenerateTestBlock(nil, nil)
m := NewBlocksResponseMessage(ResponseCodeMoreBlocks, sid, 0, []*block.Block{b}, nil)
d, _ := b.Bytes()
m := NewBlocksResponseMessage(ResponseCodeMoreBlocks, sid, 0, [][]byte{d}, nil)

assert.Equal(t, errors.Code(m.SanityCheck()), errors.ErrInvalidHeight)
})

t.Run("OK", func(t *testing.T) {
b1 := block.GenerateTestBlock(nil, nil)
b2 := block.GenerateTestBlock(nil, nil)
m := NewBlocksResponseMessage(ResponseCodeMoreBlocks, sid, 100, []*block.Block{b1, b2}, nil)
d1, _ := b1.Bytes()
d2, _ := b2.Bytes()
m := NewBlocksResponseMessage(ResponseCodeMoreBlocks, sid, 100, [][]byte{d1, d2}, nil)

assert.NoError(t, m.SanityCheck())
assert.Zero(t, m.LastCertificateHeight())
Expand Down Expand Up @@ -66,8 +70,10 @@ func TestLatestBlocksResponseCode(t *testing.T) {
t.Run("OK - MoreBlocks", func(t *testing.T) {
b1 := block.GenerateTestBlock(nil, nil)
b2 := block.GenerateTestBlock(nil, nil)
d1, _ := b1.Bytes()
d2, _ := b2.Bytes()
m := NewBlocksResponseMessage(ResponseCodeMoreBlocks, 1, 100, [][]byte{d1, d2}, nil)

m := NewBlocksResponseMessage(ResponseCodeMoreBlocks, 1, 100, []*block.Block{b1, b2}, nil)
assert.NoError(t, m.SanityCheck())
assert.Equal(t, m.From, uint32(100))
assert.Equal(t, m.To(), uint32(101))
Expand Down
25 changes: 2 additions & 23 deletions sync/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cache

import (
lru "github.com/hashicorp/golang-lru"
"github.com/pactus-project/pactus/state"
"github.com/pactus-project/pactus/types/block"
"github.com/pactus-project/pactus/util"
)
Expand All @@ -14,13 +13,13 @@ const (

type key [32]byte

// TODO, create keys without copy?
func blockKey(height uint32) key {
var k key
k[0] = blockPrefix
copy(k[1:], util.Uint32ToSlice(height))
return k
}

func certificateKey(height uint32) key {
var k key
k[0] = certificatePrefix
Expand All @@ -30,17 +29,15 @@ func certificateKey(height uint32) key {

type Cache struct {
cache *lru.Cache // it's thread safe
state state.Facade
}

func NewCache(size int, state state.Facade) (*Cache, error) {
func NewCache(size int) (*Cache, error) {
c, err := lru.New(size)
if err != nil {
return nil, err
}
return &Cache{
cache: c,
state: state,
}, nil
}

Expand All @@ -55,17 +52,6 @@ func (c *Cache) GetBlock(height uint32) *block.Block {
return i.(*block.Block)
}

sb := c.state.StoredBlock(height)
if sb != nil {
// TODO: This decoding is not necessary.
// To improve the performance, we can send blocks without decoding it.
b := sb.ToBlock()
if b != nil {
c.AddBlock(height, b)
return b
}
}

return nil
}

Expand All @@ -74,13 +60,6 @@ func (c *Cache) AddBlock(height uint32, block *block.Block) {
c.AddCertificate(height-1, block.PrevCertificate())
}

func (c *Cache) AddBlocks(height uint32, blocks []*block.Block) {
for _, block := range blocks {
c.AddBlock(height, block)
height++
}
}

func (c *Cache) GetCertificate(height uint32) *block.Certificate {
i, ok := c.cache.Get(certificateKey(height))
if ok {
Expand Down
37 changes: 15 additions & 22 deletions sync/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,16 @@ package cache
import (
"testing"

"github.com/pactus-project/pactus/crypto/hash"
"github.com/pactus-project/pactus/state"
"github.com/pactus-project/pactus/types/block"
"github.com/pactus-project/pactus/util"
"github.com/stretchr/testify/assert"
)

var tCache *Cache
var tState *state.MockState

func setup(t *testing.T) {
var err error
tState = state.MockingState()
tCache, err = NewCache(10, tState)
tCache, err = NewCache(10)
assert.NoError(t, err)
}

Expand All @@ -29,30 +26,26 @@ func TestKeys(t *testing.T) {
func TestCacheBlocks(t *testing.T) {
setup(t)

b1 := block.GenerateTestBlock(nil, &hash.UndefHash)
b1 := block.GenerateTestBlock(nil, nil)
h1 := b1.Hash()
b2 := block.GenerateTestBlock(nil, &h1)
h2 := b2.Hash()
b3 := block.GenerateTestBlock(nil, &h2)
testHeight := util.RandUint32(0)

tState.TestStore.SaveBlock(1, b1, block.GenerateTestCertificate(b1.Hash()))
tCache.AddBlocks(2, []*block.Block{b2, b3})
tCache.AddBlock(testHeight, b1)
tCache.AddBlock(testHeight+1, b2)

assert.False(t, tCache.HasBlockInCache(1), "Block 1 is not cached")
assert.True(t, tCache.HasBlockInCache(2))
assert.True(t, tCache.HasBlockInCache(3))
assert.False(t, tCache.HasBlockInCache(4))
assert.True(t, tCache.HasBlockInCache(testHeight))
assert.True(t, tCache.HasBlockInCache(testHeight+1))
assert.False(t, tCache.HasBlockInCache(testHeight+3))

assert.NotNil(t, tCache.GetBlock(1))
assert.NotNil(t, tCache.GetBlock(2))
assert.NotNil(t, tCache.GetBlock(3))
assert.Nil(t, tCache.GetBlock(4))
assert.NotNil(t, tCache.GetBlock(testHeight))
assert.NotNil(t, tCache.GetBlock(testHeight+1))
assert.Nil(t, tCache.GetBlock(testHeight+2))

assert.Equal(t, tCache.GetBlock(1).Hash(), b1.Hash())
assert.Equal(t, tCache.GetBlock(2).Hash(), b2.Hash())
assert.Equal(t, tCache.GetBlock(testHeight).Hash(), b1.Hash())
assert.Equal(t, tCache.GetBlock(testHeight+1).Hash(), b2.Hash())
assert.Nil(t, tCache.GetCertificate(0))
assert.Equal(t, tCache.GetCertificate(1).Hash(), b2.PrevCertificate().Hash())
assert.Equal(t, tCache.GetCertificate(2).Hash(), b3.PrevCertificate().Hash())
assert.Equal(t, tCache.GetCertificate(testHeight).Hash(), b2.PrevCertificate().Hash())
assert.Nil(t, tCache.GetCertificate(4))
}

Expand Down
10 changes: 5 additions & 5 deletions sync/handler_blocks_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,17 @@ func (handler *blocksRequestHandler) ParsMessage(m message.Message, initiator pe
// Help this peer to sync up
for {
blockToRead := util.MinU32(handler.config.BlockPerMessage, count)
blocks := handler.prepareBlocks(height, blockToRead)
if len(blocks) == 0 {
blocksData := handler.prepareBlocks(height, blockToRead)
if len(blocksData) == 0 {
break
}

response := message.NewBlocksResponseMessage(message.ResponseCodeMoreBlocks,
msg.SessionID, height, blocks, nil)
msg.SessionID, height, blocksData, nil)
handler.sendTo(response, initiator, msg.SessionID)

height += uint32(len(blocks))
count -= uint32(len(blocks))
height += uint32(len(blocksData))
count -= uint32(len(blocksData))
if count <= 0 {
break
}
Expand Down
14 changes: 13 additions & 1 deletion sync/handler_blocks_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"github.com/libp2p/go-libp2p/core/peer"
"github.com/pactus-project/pactus/sync/bundle"
"github.com/pactus-project/pactus/sync/bundle/message"
"github.com/pactus-project/pactus/types/block"
)

type blocksResponseHandler struct {
Expand All @@ -23,8 +24,19 @@
if msg.IsRequestRejected() {
handler.logger.Warn("blocks request is rejected", "pid", initiator, "response", msg.ResponseCode)
} else {
height := msg.From
for _, d := range msg.BlocksData {
b, err := block.FromBytes(d)
if err != nil {
return err
}
if err := b.SanityCheck(); err != nil {
return err

Check warning on line 34 in sync/handler_blocks_response.go

View check run for this annotation

Codecov / codecov/patch

sync/handler_blocks_response.go#L34

Added line #L34 was not covered by tests
}
handler.cache.AddBlock(height, b)
height++
}
handler.cache.AddCertificate(msg.From, msg.LastCertificate)
handler.cache.AddBlocks(msg.From, msg.Blocks)
handler.tryCommitBlocks()
}
handler.updateSession(msg.SessionID, initiator, msg.ResponseCode)
Expand Down
14 changes: 13 additions & 1 deletion sync/handler_blocks_response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,25 @@ import (
"github.com/stretchr/testify/assert"
)

func TestInvalidBlockData(t *testing.T) {
setup(t)

pid := network.TestRandomPeerID()
sid := tSync.peerSet.OpenSession(pid).SessionID()
msg := message.NewBlocksResponseMessage(message.ResponseCodeMoreBlocks, sid,
0, [][]byte{{1, 2, 3}}, nil)

assert.Error(t, testReceivingNewMessage(tSync, msg, pid))
}

func TestOneBlockShorter(t *testing.T) {
setup(t)

lastBlockHash := tState.LastBlockHash()
lastBlockHeight := tState.LastBlockHeight()
b1 := block.GenerateTestBlock(nil, &lastBlockHash)
c1 := block.GenerateTestCertificate(b1.Hash())
d1, _ := b1.Bytes()
pid := network.TestRandomPeerID()

pub, _ := bls.GenerateTestKeyPair()
Expand All @@ -48,7 +60,7 @@ func TestOneBlockShorter(t *testing.T) {
t.Run("Commit one block", func(t *testing.T) {
sid := tSync.peerSet.OpenSession(pid).SessionID()
msg := message.NewBlocksResponseMessage(message.ResponseCodeSynced, sid,
lastBlockHeight+1, []*block.Block{b1}, c1)
lastBlockHeight+1, [][]byte{d1}, c1)
assert.NoError(t, testReceivingNewMessage(tSync, msg, pid))

assert.Nil(t, tSync.peerSet.FindSession(sid))
Expand Down