/
interface.go
196 lines (143 loc) · 6.74 KB
/
interface.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright (C) 2018 ZVChain
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package types
import (
"math/big"
"github.com/zvchain/zvchain/common"
)
// BlockChain is a interface, encapsulates some methods for manipulating the blockchain
type BlockChain interface {
ChainReader
AccountRepository
// CastBlock cast a block, current casters synchronization operation in the group
CastBlock(height uint64, proveValue []byte, qn uint64, castor []byte, groupSeed common.Hash) *Block
// AddBlockOnChain add a block on blockchain, there are five cases of return value:
// 0, successfully add block on blockchain
// -1, verification failed
// 1, the block already exist on the blockchain, then we should discard it
// 2, the same height block with a larger QN value on the chain, then we should discard it
// 3, need adjust the blockchain, there will be a fork
AddBlockOnChain(source string, b *Block) AddBlockResult
// TotalQN of chain
TotalQN() uint64
// LatestAccountDB returns chain's last account database
LatestAccountDB() (AccountDB, error)
// QueryBlockByHash query the block by hash
QueryBlockByHash(hash common.Hash) *Block
// QueryBlockByHeight query the block by height
QueryBlockByHeight(height uint64) *Block
// QueryBlockCeil query first block whose height >= height
QueryBlockCeil(height uint64) *Block
// QueryBlockHeaderCeil query first block header whose height >= height
QueryBlockHeaderCeil(height uint64) *BlockHeader
// QueryBlockFloor query first block whose height <= height
QueryBlockFloor(height uint64) *Block
// QueryBlockHeaderFloor query first block header whose height <= height
QueryBlockHeaderFloor(height uint64) *BlockHeader
// BatchGetBlocksAfterHeight query blocks after the specified height
BatchGetBlocksAfterHeight(height uint64, limit int) []*Block
// GetTransactionByHash get a transaction by hash
GetTransactionByHash(onlyReward bool, h common.Hash) *Transaction
// GetTransactionPool return the transaction pool waiting for the block
GetTransactionPool() TransactionPool
// IsAdjusting means whether need to adjust blockchain, which means there may be a fork
IsAdjusting() bool
// Remove removes the block and blocks after it from the chain. Only used in a debug file, should be removed later
Remove(block *Block) bool
// Clear clear blockchain all data
Clear() error
// Close the open levelDb files
Close()
// GetRewardManager returns the reward manager
GetRewardManager() RewardManager
// GetAccountDBByHash returns account database with specified block hash
GetAccountDBByHash(hash common.Hash) (AccountDB, error)
// AccountDBAt returns account database with specified block height
AccountDBAt(height uint64) (AccountDB, error)
// GetConsensusHelper returns consensus helper reference
GetConsensusHelper() ConsensusHelper
// Version of chain Id
Version() int
// ResetTop reset the current top block with parameter bh
ResetTop(bh *BlockHeader) error
// countBlocksInRange returns the count of block in a range of block height. the block with startHeight and endHeight
// will be included
CountBlocksInRange(startHeight uint64, endHeight uint64) uint64
// returns latest checkpoint of the chain
LatestCheckPoint() *BlockHeader
IsSyncing() bool
}
type RewardManager interface {
GetRewardTransactionByBlockHash(blockHash common.Hash) *Transaction
GenerateReward(targetIds []int32, blockHash common.Hash, gSeed common.Hash, totalValue uint64, packFee uint64) (*Reward, *Transaction, error)
ParseRewardTransaction(msg TxMessage) (gSeed common.Hash, targets [][]byte, blockHash common.Hash, packFee *big.Int, err error)
CalculateCastRewardShare(height uint64, gasFee uint64) *CastRewardShare
HasRewardedOfBlock(blockHash common.Hash, accountdb AccountDB) bool
MarkBlockRewarded(blockHash common.Hash, transactionHash common.Hash, accountdb AccountDB)
}
// ExecutedTransaction contains the transaction and its receipt
type ExecutedTransaction struct {
Receipt *Receipt
Transaction *Transaction
}
type TransactionPool interface {
// PackForCast returns a list of transactions for casting a block
PackForCast() []*Transaction
// AddTransaction adds new transaction to the transaction pool which will be broadcast
AddTransaction(tx *Transaction) (bool, error)
// AsyncAddTransaction adds transaction to the transaction pool which won't be broadcast
AsyncAddTransaction(tx *Transaction) error
// GetTransaction trys to find a transaction from pool by hash and return it
GetTransaction(reward bool, hash common.Hash) *Transaction
// GetReceipt returns the transaction's recipe by hash
GetReceipt(hash common.Hash) *Receipt
// GetReceived returns the received transactions in the pool with a limited size
GetReceived() []*Transaction
// GetAllTxs returns the all received transactions(including pending and queue) in the pool with a limited size
GetAllTxs() []*Transaction
// GetRewardTxs returns all the reward transactions in the pool
GetRewardTxs() []*Transaction
// TxNum returns the number of transactions in the pool
TxNum() uint64
// TxNum returns the number of transactions in the queue
TxQueueNum() uint64
// RemoveFromPool removes the transactions from pool by hash
RemoveFromPool(txs []common.Hash)
// BackToPool will put the transactions back to pool
BackToPool(txs []*Transaction)
// RecoverAndValidateTx recovers the sender of the transaction and also validates the transaction
RecoverAndValidateTx(tx *Transaction) error
SaveReceipts(blockHash common.Hash, receipts Receipts) error
DeleteReceipts(txs []common.Hash) error
//check transaction hash exist in local
IsTransactionExisted(hash common.Hash) (exists bool, where int)
}
// GroupInfoI is a group management interface
type GroupInfoI interface {
}
// VMExecutor is a VM executor
type VMExecutor interface {
Execute(statedb AccountDB, block *Block) (Receipts, *common.Hash, uint64, error)
}
// AccountRepository contains account query interface
type AccountRepository interface {
// GetBalance return the balance of specified address
GetBalance(address common.Address) *big.Int
// GetBalance returns the nonce of specified address
GetNonce(address common.Address) uint64
}
type Account interface {
MinerSk() string
}