aboutsummaryrefslogtreecommitdiffstats
path: root/core/chain_util.go
blob: 0e3fa31f9e5d469d9ee5aba850641d89434ab8a7 (plain) (blame)
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// Copyright 2015 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package core

import (
    "bytes"
    "math/big"

    "github.com/ethereum/go-ethereum/common"
    "github.com/ethereum/go-ethereum/core/types"
    "github.com/ethereum/go-ethereum/logger"
    "github.com/ethereum/go-ethereum/logger/glog"
    "github.com/ethereum/go-ethereum/params"
    "github.com/ethereum/go-ethereum/rlp"
)

var (
    headHeaderKey = []byte("LastHeader")
    headBlockKey  = []byte("LastBlock")

    blockPrefix    = []byte("block-")
    blockNumPrefix = []byte("block-num-")

    headerSuffix = []byte("-header")
    bodySuffix   = []byte("-body")
    tdSuffix     = []byte("-td")

    ExpDiffPeriod = big.NewInt(100000)
    blockHashPre  = []byte("block-hash-") // [deprecated by eth/63]
)

// CalcDifficulty is the difficulty adjustment algorithm. It returns
// the difficulty that a new block b should have when created at time
// given the parent block's time and difficulty.
func CalcDifficulty(time, parentTime uint64, parentNumber, parentDiff *big.Int) *big.Int {
    diff := new(big.Int)
    adjust := new(big.Int).Div(parentDiff, params.DifficultyBoundDivisor)
    bigTime := new(big.Int)
    bigParentTime := new(big.Int)

    bigTime.SetUint64(time)
    bigParentTime.SetUint64(parentTime)

    if bigTime.Sub(bigTime, bigParentTime).Cmp(params.DurationLimit) < 0 {
        diff.Add(parentDiff, adjust)
    } else {
        diff.Sub(parentDiff, adjust)
    }
    if diff.Cmp(params.MinimumDifficulty) < 0 {
        diff = params.MinimumDifficulty
    }

    periodCount := new(big.Int).Add(parentNumber, common.Big1)
    periodCount.Div(periodCount, ExpDiffPeriod)
    if periodCount.Cmp(common.Big1) > 0 {
        // diff = diff + 2^(periodCount - 2)
        expDiff := periodCount.Sub(periodCount, common.Big2)
        expDiff.Exp(common.Big2, expDiff, nil)
        diff.Add(diff, expDiff)
        diff = common.BigMax(diff, params.MinimumDifficulty)
    }

    return diff
}

// CalcGasLimit computes the gas limit of the next block after parent.
// The result may be modified by the caller.
// This is miner strategy, not consensus protocol.
func CalcGasLimit(parent *types.Block) *big.Int {
    // contrib = (parentGasUsed * 3 / 2) / 1024
    contrib := new(big.Int).Mul(parent.GasUsed(), big.NewInt(3))
    contrib = contrib.Div(contrib, big.NewInt(2))
    contrib = contrib.Div(contrib, params.GasLimitBoundDivisor)

    // decay = parentGasLimit / 1024 -1
    decay := new(big.Int).Div(parent.GasLimit(), params.GasLimitBoundDivisor)
    decay.Sub(decay, big.NewInt(1))

    /*
        strategy: gasLimit of block-to-mine is set based on parent's
        gasUsed value.  if parentGasUsed > parentGasLimit * (2/3) then we
        increase it, otherwise lower it (or leave it unchanged if it's right
        at that usage) the amount increased/decreased depends on how far away
        from parentGasLimit * (2/3) parentGasUsed is.
    */
    gl := new(big.Int).Sub(parent.GasLimit(), decay)
    gl = gl.Add(gl, contrib)
    gl.Set(common.BigMax(gl, params.MinGasLimit))

    // however, if we're now below the target (GenesisGasLimit) we increase the
    // limit as much as we can (parentGasLimit / 1024 -1)
    if gl.Cmp(params.GenesisGasLimit) < 0 {
        gl.Add(parent.GasLimit(), decay)
        gl.Set(common.BigMin(gl, params.GenesisGasLimit))
    }
    return gl
}

// GetCanonicalHash retrieves a hash assigned to a canonical block number.
func GetCanonicalHash(db common.Database, number uint64) common.Hash {
    data, _ := db.Get(append(blockNumPrefix, big.NewInt(int64(number)).Bytes()...))
    if len(data) == 0 {
        return common.Hash{}
    }
    return common.BytesToHash(data)
}

// GetHeadHeaderHash retrieves the hash of the current canonical head block's
// header. The difference between this and GetHeadBlockHash is that whereas the
// last block hash is only updated upon a full block import, the last header
// hash is updated already at header import, allowing head tracking for the
// fast synchronization mechanism.
func GetHeadHeaderHash(db common.Database) common.Hash {
    data, _ := db.Get(headHeaderKey)
    if len(data) == 0 {
        return common.Hash{}
    }
    return common.BytesToHash(data)
}

// GetHeadBlockHash retrieves the hash of the current canonical head block.
func GetHeadBlockHash(db common.Database) common.Hash {
    data, _ := db.Get(headBlockKey)
    if len(data) == 0 {
        return common.Hash{}
    }
    return common.BytesToHash(data)
}

// GetHeaderRLP retrieves a block header in its raw RLP database encoding, or nil
// if the header's not found.
func GetHeaderRLP(db common.Database, hash common.Hash) rlp.RawValue {
    data, _ := db.Get(append(append(blockPrefix, hash[:]...), headerSuffix...))
    return data
}

// GetHeader retrieves the block header corresponding to the hash, nil if none
// found.
func GetHeader(db common.Database, hash common.Hash) *types.Header {
    data := GetHeaderRLP(db, hash)
    if len(data) == 0 {
        return nil
    }
    header := new(types.Header)
    if err := rlp.Decode(bytes.NewReader(data), header); err != nil {
        glog.V(logger.Error).Infof("invalid block header RLP for hash %x: %v", hash, err)
        return nil
    }
    return header
}

// GetBodyRLP retrieves the block body (transactions and uncles) in RLP encoding.
func GetBodyRLP(db common.Database, hash common.Hash) rlp.RawValue {
    data, _ := db.Get(append(append(blockPrefix, hash[:]...), bodySuffix...))
    return data
}

// GetBody retrieves the block body (transactons, uncles) corresponding to the
// hash, nil if none found.
func GetBody(db common.Database, hash common.Hash) *types.Body {
    data := GetBodyRLP(db, hash)
    if len(data) == 0 {
        return nil
    }
    body := new(types.Body)
    if err := rlp.Decode(bytes.NewReader(data), body); err != nil {
        glog.V(logger.Error).Infof("invalid block body RLP for hash %x: %v", hash, err)
        return nil
    }
    return body
}

// GetTd retrieves a block's total difficulty corresponding to the hash, nil if
// none found.
func GetTd(db common.Database, hash common.Hash) *big.Int {
    data, _ := db.Get(append(append(blockPrefix, hash.Bytes()...), tdSuffix...))
    if len(data) == 0 {
        return nil
    }
    td := new(big.Int)
    if err := rlp.Decode(bytes.NewReader(data), td); err != nil {
        glog.V(logger.Error).Infof("invalid block total difficulty RLP for hash %x: %v", hash, err)
        return nil
    }
    return td
}

// GetBlock retrieves an entire block corresponding to the hash, assembling it
// back from the stored header and body.
func GetBlock(db common.Database, hash common.Hash) *types.Block {
    // Retrieve the block header and body contents
    header := GetHeader(db, hash)
    if header == nil {
        return nil
    }
    body := GetBody(db, hash)
    if body == nil {
        return nil
    }
    // Reassemble the block and return
    return types.NewBlockWithHeader(header).WithBody(body.Transactions, body.Uncles)
}

// WriteCanonicalHash stores the canonical hash for the given block number.
func WriteCanonicalHash(db common.Database, hash common.Hash, number uint64) error {
    key := append(blockNumPrefix, big.NewInt(int64(number)).Bytes()...)
    if err := db.Put(key, hash.Bytes()); err != nil {
        glog.Fatalf("failed to store number to hash mapping into database: %v", err)
        return err
    }
    return nil
}

// WriteHeadHeaderHash stores the head header's hash.
func WriteHeadHeaderHash(db common.Database, hash common.Hash) error {
    if err := db.Put(headHeaderKey, hash.Bytes()); err != nil {
        glog.Fatalf("failed to store last header's hash into database: %v", err)
        return err
    }
    return nil
}

// WriteHeadBlockHash stores the head block's hash.
func WriteHeadBlockHash(db common.Database, hash common.Hash) error {
    if err := db.Put(headBlockKey, hash.Bytes()); err != nil {
        glog.Fatalf("failed to store last block's hash into database: %v", err)
        return err
    }
    return nil
}

// WriteHeader serializes a block header into the database.
func WriteHeader(db common.Database, header *types.Header) error {
    data, err := rlp.EncodeToBytes(header)
    if err != nil {
        return err
    }
    key := append(append(blockPrefix, header.Hash().Bytes()...), headerSuffix...)
    if err := db.Put(key, data); err != nil {
        glog.Fatalf("failed to store header into database: %v", err)
        return err
    }
    glog.V(logger.Debug).Infof("stored header #%v [%x…]", header.Number, header.Hash().Bytes()[:4])
    return nil
}

// WriteBody serializes the body of a block into the database.
func WriteBody(db common.Database, hash common.Hash, body *types.Body) error {
    data, err := rlp.EncodeToBytes(body)
    if err != nil {
        return err
    }
    key := append(append(blockPrefix, hash.Bytes()...), bodySuffix...)
    if err := db.Put(key, data); err != nil {
        glog.Fatalf("failed to store block body into database: %v", err)
        return err
    }
    glog.V(logger.Debug).Infof("stored block body [%x…]", hash.Bytes()[:4])
    return nil
}

// WriteTd serializes the total difficulty of a block into the database.
func WriteTd(db common.Database, hash common.Hash, td *big.Int) error {
    data, err := rlp.EncodeToBytes(td)
    if err != nil {
        return err
    }
    key := append(append(blockPrefix, hash.Bytes()...), tdSuffix...)
    if err := db.Put(key, data); err != nil {
        glog.Fatalf("failed to store block total difficulty into database: %v", err)
        return err
    }
    glog.V(logger.Debug).Infof("stored block total difficulty [%x…]: %v", hash.Bytes()[:4], td)
    return nil
}

// WriteBlock serializes a block into the database, header and body separately.
func WriteBlock(db common.Database, block *types.Block) error {
    // Store the body first to retain database consistency
    if err := WriteBody(db, block.Hash(), &types.Body{block.Transactions(), block.Uncles()}); err != nil {
        return err
    }
    // Store the header too, signaling full block ownership
    if err := WriteHeader(db, block.Header()); err != nil {
        return err
    }
    return nil
}

// DeleteCanonicalHash removes the number to hash canonical mapping.
func DeleteCanonicalHash(db common.Database, number uint64) {
    db.Delete(append(blockNumPrefix, big.NewInt(int64(number)).Bytes()...))
}

// DeleteHeader removes all block header data associated with a hash.
func DeleteHeader(db common.Database, hash common.Hash) {
    db.Delete(append(append(blockPrefix, hash.Bytes()...), headerSuffix...))
}

// DeleteBody removes all block body data associated with a hash.
func DeleteBody(db common.Database, hash common.Hash) {
    db.Delete(append(append(blockPrefix, hash.Bytes()...), bodySuffix...))
}

// DeleteTd removes all block total difficulty data associated with a hash.
func DeleteTd(db common.Database, hash common.Hash) {
    db.Delete(append(append(blockPrefix, hash.Bytes()...), tdSuffix...))
}

// DeleteBlock removes all block data associated with a hash.
func DeleteBlock(db common.Database, hash common.Hash) {
    DeleteHeader(db, hash)
    DeleteBody(db, hash)
    DeleteTd(db, hash)
}

// [deprecated by eth/63]
// GetBlockByHashOld returns the old combined block corresponding to the hash
// or nil if not found. This method is only used by the upgrade mechanism to
// access the old combined block representation. It will be dropped after the
// network transitions to eth/63.
func GetBlockByHashOld(db common.Database, hash common.Hash) *types.Block {
    data, _ := db.Get(append(blockHashPre, hash[:]...))
    if len(data) == 0 {
        return nil
    }
    var block types.StorageBlock
    if err := rlp.Decode(bytes.NewReader(data), &block); err != nil {
        glog.V(logger.Error).Infof("invalid block RLP for hash %x: %v", hash, err)
        return nil
    }
    return (*types.Block)(&block)
}