-
Notifications
You must be signed in to change notification settings - Fork 100
/
lightBackend.go
126 lines (96 loc) · 3.97 KB
/
lightBackend.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
package light
import (
"math/big"
"github.com/seeleteam/go-seele/api"
"github.com/seeleteam/go-seele/common"
"github.com/seeleteam/go-seele/common/errors"
"github.com/seeleteam/go-seele/core/store"
"github.com/seeleteam/go-seele/core/types"
"github.com/seeleteam/go-seele/log"
"github.com/seeleteam/go-seele/p2p"
)
// LightBackend represents a channel (client) that communicate with backend node service.
type LightBackend struct {
s *ServiceClient
}
// NewLightBackend creates a LightBackend
func NewLightBackend(s *ServiceClient) *LightBackend {
return &LightBackend{s}
}
// TxPoolBackend gets the instance of tx pool
func (l *LightBackend) TxPoolBackend() api.Pool { return l.s.txPool }
// GetNetVersion gets the network version
func (l *LightBackend) GetNetVersion() string { return l.s.netVersion }
// GetNetWorkID gets the network id
func (l *LightBackend) GetNetWorkID() string { return l.s.networkID }
// GetP2pServer gets instance of p2pServer
func (l *LightBackend) GetP2pServer() *p2p.Server { return l.s.p2pServer }
// ChainBackend gets instance of blockchain
func (l *LightBackend) ChainBackend() api.Chain { return l.s.chain }
// Log gets instance of log
func (l *LightBackend) Log() *log.SeeleLog { return l.s.log }
func (l *LightBackend) IsSyncing() bool {
return l.s.seeleProtocol.downloader.syncStatus == statusDownloading
}
// ProtocolBackend gets instance of seeleProtocol
func (l *LightBackend) ProtocolBackend() api.Protocol { return l.s.seeleProtocol }
// GetBlock gets a specific block through block's hash and height
func (l *LightBackend) GetBlock(hash common.Hash, height int64) (*types.Block, error) {
request := &odrBlock{Hash: hash}
var err error
if hash.IsEmpty() {
if height < 0 {
request.Hash = l.ChainBackend().CurrentHeader().Hash()
} else if request.Hash, err = l.ChainBackend().GetStore().GetBlockHash(uint64(height)); err != nil {
return nil, errors.NewStackedErrorf(err, "failed to get block hash by height %v", height)
}
}
filter := peerFilter{blockHash: hash}
response, err := l.s.odrBackend.retrieveWithFilter(request, filter)
if err != nil {
return nil, errors.NewStackedError(err, "failed to retrieve ODR block")
}
return response.(*odrBlock).Block, nil
}
// GetBlockTotalDifficulty gets total difficulty by block hash
func (l *LightBackend) GetBlockTotalDifficulty(hash common.Hash) (*big.Int, error) {
return l.ChainBackend().GetStore().GetBlockTotalDifficulty(hash)
}
// GetReceiptByTxHash gets block's receipt by tx hash
func (l *LightBackend) GetReceiptByTxHash(hash common.Hash) (*types.Receipt, error) {
blockHash := l.s.txPool.GetBlockHash(hash)
filter := peerFilter{blockHash: blockHash}
response, err := l.s.odrBackend.retrieveWithFilter(&odrReceiptRequest{TxHash: hash}, filter)
if err != nil {
return nil, err
}
result := response.(*odrReceiptResponse)
return result.Receipt, nil
}
// GetTransaction gets tx, block index and its debt by tx hash
func (l *LightBackend) GetTransaction(pool api.PoolCore, bcStore store.BlockchainStore, txHash common.Hash) (*types.Transaction, *api.BlockIndex, error) {
if tx := l.s.txPool.GetTransaction(txHash); tx != nil {
return tx, nil, nil
}
blockHash := l.s.txPool.GetBlockHash(txHash)
filter := peerFilter{blockHash: blockHash}
response, err := l.s.odrBackend.retrieveWithFilter(&odrTxByHashRequest{TxHash: txHash}, filter)
if err != nil {
return nil, nil, err
}
result := response.(*odrTxByHashResponse)
return result.Tx, result.BlockIndex, nil
}
// RemoveTransaction removes tx of the specified tx hash from tx pool.
func (l *LightBackend) RemoveTransaction(txHash common.Hash) {
l.s.txPool.Remove(txHash)
}
// GetDebt returns the debt and its index for the specified debt hash.
func (l *LightBackend) GetDebt(debtHash common.Hash) (*types.Debt, *api.BlockIndex, error) {
response, err := l.s.odrBackend.retrieve(&odrDebtRequest{DebtHash: debtHash})
if err != nil {
return nil, nil, err
}
result := response.(*odrDebtResponse)
return result.Debt, result.BlockIndex, nil
}