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

[filedao] impl sized file dao #4382

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions blockchain/filedao/blockstore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package filedao

import (
"fmt"

"github.com/iotexproject/iotex-proto/golang/iotextypes"

"github.com/iotexproject/iotex-core/blockchain/block"
"github.com/iotexproject/iotex-core/pkg/util/byteutil"
)

const (
blockstoreDefaultVersion = byte(0)
blockstoreHeaderSize = 8
)

type (
// blockstore is a byte slice that contains a block, its receipts and transaction logs
// the first 8 bytes is the size of the block
// the next n bytes is the serialized block and receipts, n is the size of the block
// the rest bytes is the serialized transaction logs
blockstore []byte
)

var (
errInvalidBlockstore = fmt.Errorf("invalid blockstore")
)

func convertToBlockStore(blk *block.Block) (blockstore, error) {
data := make(blockstore, 0)
s := &block.Store{
Block: blk,
Receipts: blk.Receipts,
}
tmp, err := s.Serialize()
if err != nil {
return nil, err
}
data = append(data, byteutil.Uint64ToBytesBigEndian(uint64(len(tmp)))...)
data = append(data, tmp...)
txLog := blk.TransactionLog()
if txLog != nil {
tmp = txLog.Serialize()
data = append(data, tmp...)
}
return data, nil
}

func (s blockstore) Block(deser *block.Deserializer) (*block.Block, error) {
size := s.blockSize()
bs, err := deser.DeserializeBlockStore(s[blockstoreHeaderSize : size+blockstoreHeaderSize])
if err != nil {
return nil, err
}
bs.Block.Receipts = bs.Receipts
return bs.Block, nil
}

func (s blockstore) TransactionLogs() (*iotextypes.TransactionLogs, error) {
size := s.blockSize()
if uint64(len(s)) == size+blockstoreHeaderSize {
return nil, nil
}
return block.DeserializeSystemLogPb(s[size+blockstoreHeaderSize:])
}

func (s blockstore) Serialize() []byte {
return append([]byte{blockstoreDefaultVersion}, s...)
}

func (s *blockstore) Deserialize(data []byte) error {
if len(data) == 0 {
return errInvalidBlockstore
}
switch data[0] {
case blockstoreDefaultVersion:
bs := blockstore(data[1:])
if err := bs.Validate(); err != nil {
return err
}
*s = bs
return nil
default:
return errInvalidBlockstore
}
}

func (s blockstore) blockSize() uint64 {
return byteutil.BytesToUint64BigEndian(s[:blockstoreHeaderSize])
}

func (s blockstore) Validate() error {
if len(s) < blockstoreHeaderSize {
return errInvalidBlockstore
}
blkSize := s.blockSize()
if uint64(len(s)) < blkSize+blockstoreHeaderSize {
return errInvalidBlockstore
}
return nil
}
40 changes: 40 additions & 0 deletions blockchain/filedao/blockstore_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package filedao

import (
"testing"

"github.com/iotexproject/go-pkgs/hash"
"github.com/stretchr/testify/require"

"github.com/iotexproject/iotex-core/blockchain/block"
)

func TestBlockStore(t *testing.T) {
r := require.New(t)
builder := block.NewTestingBuilder()
blk := createTestingBlock(builder, 1, hash.ZeroHash256)
bs, err := convertToBlockStore(blk)
r.NoError(err)
data := bs.Serialize()
dbs := new(blockstore)
r.NoError(dbs.Deserialize(data))
r.Equal(bs[:], (*dbs)[:], "serialized block store should be equal to deserialized block store")
// check deserialized block
deser := block.NewDeserializer(0)
dBlk, err := dbs.Block(deser)
r.NoError(err)
dTxLogs, err := dbs.TransactionLogs()
r.NoError(err)
r.NoError(fillTransactionLog(dBlk.Receipts, dTxLogs.Logs))
r.Equal(blk.Header, dBlk.Header)
r.Equal(blk.Body, dBlk.Body)
r.Equal(blk.Footer, dBlk.Footer)
r.Equal(len(blk.Receipts), len(dBlk.Receipts))
for i := range blk.Receipts {
r.Equal(blk.Receipts[i].Hash(), dBlk.Receipts[i].Hash())
r.Equal(len(blk.Receipts[i].TransactionLogs()), len(dBlk.Receipts[i].TransactionLogs()))
for j := range blk.Receipts[i].TransactionLogs() {
r.Equal(blk.Receipts[i].TransactionLogs()[j], dBlk.Receipts[i].TransactionLogs()[j])
}
}
}
Loading
Loading