forked from aergoio/aergo
/
blockstate.go
69 lines (61 loc) · 1.58 KB
/
blockstate.go
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
66
67
68
69
package state
import (
"github.com/aergoio/aergo/types"
"github.com/willf/bloom"
)
// BlockInfo contains BlockHash and StateRoot
type BlockInfo struct {
BlockHash types.BlockID
StateRoot types.HashID
}
// BlockState contains BlockInfo and statedb for block
type BlockState struct {
StateDB
BpReward []byte //final bp reward, increment when tx executes
receipts types.Receipts
CodeMap map[types.AccountID][]byte
}
// NewBlockInfo create new blockInfo contains blockNo, blockHash and blockHash of previous block
func NewBlockInfo(blockHash types.BlockID, stateRoot types.HashID) *BlockInfo {
return &BlockInfo{
BlockHash: blockHash,
StateRoot: stateRoot,
}
}
// GetStateRoot return bytes of bi.StateRoot
func (bi *BlockInfo) GetStateRoot() []byte {
if bi == nil {
return nil
}
return bi.StateRoot.Bytes()
}
// NewBlockState create new blockState contains blockInfo, account states and undo states
func NewBlockState(states *StateDB) *BlockState {
return &BlockState{
StateDB: *states,
CodeMap: make(map[types.AccountID][]byte),
}
}
func (bs *BlockState) AddReceipt(r *types.Receipt) error {
if len(r.Events) > 0 {
rBloom := bloom.New(types.BloomBitBits, types.BloomHashKNum)
for _, e := range r.Events {
rBloom.Add(e.ContractAddress)
rBloom.Add([]byte(e.EventName))
}
binary, _ := rBloom.GobEncode()
r.Bloom = binary[24:]
err := bs.receipts.MergeBloom(rBloom)
if err != nil {
return err
}
}
bs.receipts.Set(append(bs.receipts.Get(), r))
return nil
}
func (bs *BlockState) Receipts() *types.Receipts {
if bs == nil {
return nil
}
return &bs.receipts
}