-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathblock.go
136 lines (117 loc) · 4.69 KB
/
block.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package models
import (
"encoding/hex"
"time"
tmversion "github.com/tendermint/tendermint/proto/tendermint/version"
tmctypes "github.com/tendermint/tendermint/rpc/core/types"
tmtypes "github.com/tendermint/tendermint/types"
"github.com/tendermint/tendermint/version"
"github.com/forbole/juno/v4/common"
)
type PartSetHeader struct {
Total uint32 `gorm:"-"`
Hash common.Hash `gorm:"-"`
}
type BlockID struct {
Hash common.Hash `gorm:"column:hash;type:BINARY(32);not null;uniqueIndex:idx_hash"`
PartSetHeader PartSetHeader `gorm:"-"`
}
type Header struct {
Height uint64 `gorm:"column:height;not null;uniqueIndex:idx_height"`
LastCommitHash common.Hash `gorm:"last_commit_hash;type:BINARY(32)"` // commit from validators from the last block
DataHash common.Hash `gorm:"data_hash;type:BINARY(32)"` // transactions
ValidatorsHash common.Hash `gorm:"validators_hash;type:BINARY(32)"` // validators for the current block
NextValidatorsHash common.Hash `gorm:"next_validators_hash;type:BINARY(32)"` // validators for the next block
ConsensusHash common.Hash `gorm:"consensus_hash;type:BINARY(32)"` // consensus params for current block
AppHash common.Hash `gorm:"app_hash;type:BINARY(32)"` // state after txs from the previous block
// root hash of all results from the txs from the previous block
// see `deterministicResponseDeliverTx` to understand which parts of a tx is hashed into here
LastResultsHash common.Hash `gorm:"last_results_hash;type:BINARY(32)"`
// consensus info
EvidenceHash common.Hash `gorm:"evidence_hash;type:BINARY(32)"` // evidence included in the block
ProposerAddress common.Address `gorm:"column:proposer_address;type:BINARY(20);index:idx_proposer_address"`
Timestamp uint64 `gorm:"column:timestamp"`
}
type Block struct {
ID uint64 `gorm:"column:id;primaryKey" json:"-"`
BlockID
Header
NumTxs uint64 `gorm:"column:num_txs"`
TotalGas uint64 `gorm:"column:total_gas"`
}
func (*Block) TableName() string {
return "blocks"
}
func (b *Block) ToTmBlock() *tmctypes.ResultBlock {
blockID := tmtypes.BlockID{}
blockID.Hash, _ = hex.DecodeString(b.Hash.Hex()[2:])
header := tmtypes.Header{
Version: tmversion.Consensus{Block: version.BlockProtocol},
//ChainID: ,
Height: int64(b.Height),
Time: time.Unix(int64(b.Timestamp), 0).UTC(),
//LastBlockID: ,
}
header.LastCommitHash, _ = hex.DecodeString(b.LastResultsHash.Hex()[2:])
header.DataHash, _ = hex.DecodeString(b.DataHash.Hex()[2:])
header.ValidatorsHash, _ = hex.DecodeString(b.DataHash.Hex()[2:])
header.NextValidatorsHash, _ = hex.DecodeString(b.NextValidatorsHash.Hex()[2:])
header.ConsensusHash, _ = hex.DecodeString(b.ConsensusHash.Hex()[2:])
header.AppHash, _ = hex.DecodeString(b.AppHash.Hex()[2:])
header.LastResultsHash, _ = hex.DecodeString(b.LastResultsHash.Hex()[2:])
header.EvidenceHash, _ = hex.DecodeString(b.EvidenceHash.Hex()[2:])
header.ProposerAddress, _ = hex.DecodeString(b.ProposerAddress.Hex()[2:])
block := &tmtypes.Block{
Header: header,
}
return &tmctypes.ResultBlock{
BlockID: blockID,
Block: block,
}
}
func (b *Block) ToResultBlock() *ResultBlock {
tmBlock := b.ToTmBlock()
return &ResultBlock{
BlockID: tmBlock.BlockID,
Block: tmBlock.Block,
NumTxs: b.NumTxs,
}
}
// NewBlockFromTmBlock builds a new Block instance from a given ResultBlock object
func NewBlockFromTmBlock(blk *tmctypes.ResultBlock, totalGas uint64) *Block {
return &Block{
BlockID: BlockID{
Hash: common.HexToHash(blk.Block.Hash().String()),
},
Header: Header{
uint64(blk.Block.Height),
common.HexToHash(blk.Block.Header.LastCommitHash.String()),
common.HexToHash(blk.Block.Header.DataHash.String()),
common.HexToHash(blk.Block.Header.ValidatorsHash.String()),
common.HexToHash(blk.Block.Header.NextValidatorsHash.String()),
common.HexToHash(blk.Block.Header.ConsensusHash.String()),
common.HexToHash(blk.Block.Header.AppHash.String()),
common.HexToHash(blk.Block.Header.LastResultsHash.String()),
common.HexToHash(blk.Block.Header.EvidenceHash.String()),
common.HexToAddress(blk.Block.Header.ProposerAddress.String()),
uint64(blk.Block.Time.UTC().Unix()),
},
NumTxs: uint64(len(blk.Block.Txs)),
TotalGas: totalGas,
}
}
type Genesis struct {
OneRowId bool `gorm:"one_row_id;primaryKey;default:true"`
ChainID string `gorm:"chain_id"`
Timestamp uint64 `gorm:"timestamp"`
InitialHeight uint64 `gorm:"initial_height"`
}
func (*Genesis) TableName() string {
return "geneses"
}
// Single block (with meta)
type ResultBlock struct {
BlockID tmtypes.BlockID `json:"block_id"`
Block *tmtypes.Block `json:"block"`
NumTxs uint64 `json:"num_txs"`
}