This repository has been archived by the owner on Jul 30, 2019. It is now read-only.
/
types.go
169 lines (152 loc) · 8.19 KB
/
types.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
// Copyright (C) 2018 MediBloc
//
// 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 core
import (
"errors"
"math/big"
"github.com/medibloc/go-medibloc/common"
dState "github.com/medibloc/go-medibloc/consensus/dpos/state"
"github.com/medibloc/go-medibloc/storage"
"github.com/medibloc/go-medibloc/util"
)
// Transaction's message types.
const (
MessageTypeNewTx = "newtx"
)
// Block's message types.
const (
MessageTypeNewBlock = "newblock"
MessageTypeRequestBlock = "rqstblock"
MessageTypeResponseBlock = "respblock"
)
// InflationRate is rate for reward
var InflationRate = big.NewRat(464, 100000000000) // 4.64e-09
// InflationRoundDown is constant for round down reward
const InflationRoundDown = 10000000000 // 1e10
// Points limit per block
const (
NumberOfBlocksInSingleTimeWindow = 201600 // 7 * 86400 / 3 (time window: 7days, block interval: 3 sec)
CPULimit = 3000000 // 1000 TPS (block interval: 3 sec, transfer tx: 1000 )
NetLimit = 3000000 // 3MB
)
// Points Price related defaults
var (
PriceMinimumRate = big.NewRat(1, 100)
PriceIncreaseRate = big.NewRat(105, 100)
PriceDecreaseRate = big.NewRat(95, 100)
)
// Points Price related defaults
const (
ThresholdRatioNum = 5
ThresholdRatioDenom = 10
)
// Error types of core package.
var (
ErrNotFound = storage.ErrKeyNotFound
ErrFailedValidateHeightAndHeight = errors.New("failed to verify height and timestamp by lib")
ErrCannotRemoveBlockOnCanonical = errors.New("cannot remove block on canonical chain")
ErrDuplicatedBlock = errors.New("duplicated block")
ErrDuplicatedTransaction = errors.New("duplicated transaction")
ErrGenesisNotMatch = errors.New("genesis block does not match")
ErrInvalidProtoToBlock = errors.New("protobuf message cannot be converted into Block")
ErrInvalidProtoToBlockHeader = errors.New("protobuf message cannot be converted into BlockHeader")
ErrInvalidBlockChainID = errors.New("invalid block chainID")
ErrInvalidBlockToProto = errors.New("block cannot be converted into proto")
ErrInvalidBlockHash = errors.New("invalid block hash")
ErrInvalidTimestamp = errors.New("child block's timestamp is smaller than parent block's")
ErrBlockAlreadySealed = errors.New("cannot seal an already sealed block")
ErrNilArgument = errors.New("argument(s) is nil")
ErrMissingParentBlock = errors.New("cannot find the block's parent block in storage")
ErrBlockNotExist = errors.New("block not exist")
ErrBlockNotSealed = errors.New("block should be sealed first to be signed")
ErrInvalidBlockHeight = errors.New("block height should be one block higher than the parent")
ErrInvalidBlockReward = errors.New("invalid reward")
ErrInvalidBlockSupply = errors.New("invalid supply")
ErrInvalidBlockAccountsRoot = errors.New("invalid account state root hash")
ErrInvalidBlockTxsRoot = errors.New("invalid transactions state root hash")
ErrInvalidBlockDposRoot = errors.New("invalid block dpos root hash")
ErrTooOldTransaction = errors.New("transaction timestamp is too old")
ErrBlockSignatureNotExist = errors.New("block signature does not exist in the blockheader")
ErrFailedToDirectPush = errors.New("cannot direct push to chain")
ErrExceedBlockMaxCPUUsage = errors.New("transaction exceeds block's max cpu usage")
ErrExceedBlockMaxNetUsage = errors.New("transaction exceeds block's max net usage")
ErrNoTransactionReceipt = errors.New("failed to load transaction receipt")
ErrWrongReceipt = errors.New("transaction receipt is wrong in block data")
ErrInvalidCPUPrice = errors.New("invalid cpu price")
ErrInvalidNetPrice = errors.New("invalid Net price")
ErrInvalidCPUUsage = errors.New("block uses too much cpu bandwidth")
ErrInvalidNetUsage = errors.New("block ueses too much net bandwidth")
ErrWrongCPUUsage = errors.New("block cpu usage is not matched with sum of tx cpu usage")
ErrWrongNetUsage = errors.New("block net usage is not matched with sum of tx net usage")
ErrInvalidAlias = errors.New("invalid alias")
ErrInvalidHash = errors.New("invalid hash")
ErrFailedToReplacePendingTx = errors.New("cannot replace pending transaction in 10 minute")
ErrBlockExecutionTimeout = errors.New("block is not executed on time")
ErrAlreadyOnTheChain = errors.New("block is already on the chain")
ErrCannotFindParentBlockOnTheChain = errors.New("cannot find parent block on the chain")
ErrForkedBeforeLIB = errors.New("block is forked before LIB")
ErrInvalidBlock = errors.New("invalid block")
ErrSameDynasty = errors.New("new block is in same dynasty with parent block")
ErrStakingNotEnough = errors.New("staking is not enough")
ErrPointNotEnough = errors.New("points are not enough")
ErrCannotConvertReceipt = errors.New("proto message cannot be converted into Receipt")
ErrInvalidReceiptToProto = errors.New("receipt cannot be converted into proto")
ErrCannotConvertTransaction = errors.New("proto message cannot be converted into Transaction")
ErrTransactionSignatureNotExist = errors.New("signature does not exist in the tx")
ErrPayerSignatureNotExist = errors.New("payer signature does not exist in the tx")
ErrInvalidTransactionHash = errors.New("invalid transaction hash")
ErrInvalidTransactionType = errors.New("invalid transaction type")
ErrCannotRecoverPayer = errors.New("failed to recover payer from payer sign")
ErrInvalidTxChainID = errors.New("invalid transaction chainID")
ErrGenesisSignShouldNotExist = errors.New("genesis sign should not exist")
ErrNotGenesisBlock = errors.New("block is not genesis")
ErrGenesisDistributionAllowedOnce = errors.New("genesis Distribution is allowed once per account")
ErrAtomicError = errors.New("failed to process atomic operation")
ErrNonceNotExecutable = errors.New("transaction nonce not executable")
ErrCoinbaseNotSet = errors.New("coinbase is not set")
)
// HashableBlock is an interface that can get its own or parent's hash.
type HashableBlock interface {
Hash() []byte
ParentHash() []byte
}
// Consensus is an interface of consensus model
type Consensus interface {
NewConsensusState(dposRootBytes []byte, stor storage.Storage) (*dState.State, error)
DynastySize() int
MakeMintDynasty(ts int64, parentState *BlockState) ([]common.Address, error)
VerifyHeightAndTimestamp(lib, bd *BlockData) error
MissingBlocks(lib, bd *BlockData) uint64
VerifyInterval(bd *BlockData, parent *Block) error
VerifyProposer(b *Block) error
FindLIB(bc *BlockChain) (newLIB *Block)
FindMintProposer(ts int64, parent *Block) (common.Address, error)
}
// SyncService interface for sync
type SyncService interface {
Download(bd *BlockData) error
IsDownloadActivated() bool
}
// Canonical is an interface of tail block in canonical chain.
type Canonical interface {
TailBlock() *Block
}
// ExecutableTx interface for execute transaction on state
type ExecutableTx interface {
Execute(b *Block) error
Bandwidth() *common.Bandwidth
PointChange() (neg bool, abs *util.Uint128)
RecoverFrom() (common.Address, error)
}