aboutsummaryrefslogtreecommitdiff
path: root/pkg/blockchain/blockinfodatabase/blockrecord.go
blob: 8f8846a196d2c0d18242891b933f52863f96e6d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package blockinfodatabase

import (
	"Chain/pkg/block"
	"Chain/pkg/pro"
)

// BlockRecord contains information about where a Block
// and its UndoBlock are stored on Disk.
// Header is the Block's Header.
// Height is the height of the Block.
// NumberOfTransactions is the number of Transactions in the Block.
// BlockFile is the name of the file where the Block is stored.
// BlockStartOffset is the starting offset of the Block within the
// BlockFile.
// BlockEndOffset is the ending offset of the Block within
// the BlockFile.
// UndoFile is the name of the file where the UndoBlock is stored.
// UndoStartOffset is the starting offset of the UndoBlock within
// the UndoFile.
// UndoEndOffset is the ending offset of the UndoBlock within the
// UndoFile.
type BlockRecord struct {
	Header               *block.Header
	Height               uint32
	NumberOfTransactions uint32

	BlockFile        string
	BlockStartOffset uint32
	BlockEndOffset   uint32

	UndoFile        string
	UndoStartOffset uint32
	UndoEndOffset   uint32
}

// EncodeBlockRecord returns a pro.BlockRecord given a BlockRecord.
func EncodeBlockRecord(br *BlockRecord) *pro.BlockRecord {
	return &pro.BlockRecord{
		Header:               block.EncodeHeader(br.Header),
		Height:               br.Height,
		NumberOfTransactions: br.NumberOfTransactions,
		BlockFile:            br.BlockFile,
		BlockStartOffset:     br.BlockStartOffset,
		BlockEndOffset:       br.BlockEndOffset,
		UndoFile:             br.UndoFile,
		UndoStartOffset:      br.UndoStartOffset,
		UndoEndOffset:        br.UndoEndOffset,
	}
}

// DecodeBlockRecord returns a BlockRecord given a pro.BlockRecord.
func DecodeBlockRecord(pbr *pro.BlockRecord) *BlockRecord {
	return &BlockRecord{
		Header:               block.DecodeHeader(pbr.GetHeader()),
		Height:               pbr.GetHeight(),
		NumberOfTransactions: pbr.GetNumberOfTransactions(),
		BlockFile:            pbr.GetBlockFile(),
		BlockStartOffset:     pbr.GetBlockStartOffset(),
		BlockEndOffset:       pbr.GetBlockEndOffset(),
		UndoFile:             pbr.GetUndoFile(),
		UndoStartOffset:      pbr.GetUndoStartOffset(),
		UndoEndOffset:        pbr.GetUndoEndOffset(),
	}
}