-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblock.go
More file actions
52 lines (45 loc) · 1.56 KB
/
block.go
File metadata and controls
52 lines (45 loc) · 1.56 KB
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
package models
import (
"github.com/jackc/pgtype"
"gorm.io/gorm"
)
type Block struct {
Hash string `json:"hash" gorm:"primaryKey"`
Size uint64 `json:"size"`
// Header fields
ParentHash string `json:"parent_hash"`
UncleHash string `json:"uncle_hash"`
Coinbase string `json:"coinbase"`
Root string `json:"root"`
TxHash string `json:"tx_hash"`
ReceiptHash string `json:"receipt_hash"`
Bloom []byte `json:"bloom"`
Difficulty pgtype.Numeric `json:"difficulty" gorm:"type:numeric"`
Number pgtype.Numeric `json:"number" gorm:"index:,sort:desc;type:numeric"`
GasLimit uint64 `json:"gas_limit"`
GasUsed uint64 `json:"gas_used"`
Time uint64 `json:"time"`
Extra []byte `json:"extra"`
MixDigest string `json:"mix_digest"`
Nonce pgtype.Numeric `json:"nonce" gorm:"type:numeric"`
BaseFee pgtype.Numeric `json:"base_fee" gorm:"type:numeric"`
}
func (db *DB) GetHead() (*Block, error) {
var head Block
result := db.Order("number desc").Limit(1).Find(&head)
if result.Error != nil {
return nil, result.Error
}
if result.RowsAffected == 0 {
return nil, gorm.ErrRecordNotFound
}
return &head, nil
}
func (db *DB) GetBlockByHash(blockHash string) (*Block, error) {
var block Block
return &block, db.Where("hash = ?", blockHash).First(&block).Error
}
func (db *DB) GetBlockByNumber(blockNumber pgtype.Numeric) (*Block, error) {
var block Block
return &block, db.Where("number = ?", blockNumber).First(&block).Error
}