aboutsummaryrefslogtreecommitdiffstats
path: root/miner/unconfirmed.go
blob: 3a176e8bd6f68b1cbd400f7a74aa1c259cbc2a06 (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
// Copyright 2016 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 miner

import (
    "container/ring"
    "sync"

    "github.com/ethereum/go-ethereum/common"
    "github.com/ethereum/go-ethereum/core/types"
    "github.com/ethereum/go-ethereum/log"
)

// chainRetriever is used by the unconfirmed block set to verify whether a previously
// mined block is part of the canonical chain or not.
type chainRetriever interface {
    // GetHeaderByNumber retrieves the canonical header associated with a block number.
    GetHeaderByNumber(number uint64) *types.Header

    // GetBlockByNumber retrieves the canonical block associated with a block number.
    GetBlockByNumber(number uint64) *types.Block
}

// unconfirmedBlock is a small collection of metadata about a locally mined block
// that is placed into a unconfirmed set for canonical chain inclusion tracking.
type unconfirmedBlock struct {
    index uint64
    hash  common.Hash
}

// unconfirmedBlocks implements a data structure to maintain locally mined blocks
// have not yet reached enough maturity to guarantee chain inclusion. It is
// used by the miner to provide logs to the user when a previously mined block
// has a high enough guarantee to not be reorged out of the canonical chain.
type unconfirmedBlocks struct {
    chain  chainRetriever // Blockchain to verify canonical status through
    depth  uint           // Depth after which to discard previous blocks
    blocks *ring.Ring     // Block infos to allow canonical chain cross checks
    lock   sync.RWMutex   // Protects the fields from concurrent access
}

// newUnconfirmedBlocks returns new data structure to track currently unconfirmed blocks.
func newUnconfirmedBlocks(chain chainRetriever, depth uint) *unconfirmedBlocks {
    return &unconfirmedBlocks{
        chain: chain,
        depth: depth,
    }
}

// Insert adds a new block to the set of unconfirmed ones.
func (set *unconfirmedBlocks) Insert(index uint64, hash common.Hash) {
    // If a new block was mined locally, shift out any old enough blocks
    set.Shift(index)

    // Create the new item as its own ring
    item := ring.New(1)
    item.Value = &unconfirmedBlock{
        index: index,
        hash:  hash,
    }
    // Set as the initial ring or append to the end
    set.lock.Lock()
    defer set.lock.Unlock()

    if set.blocks == nil {
        set.blocks = item
    } else {
        set.blocks.Move(-1).Link(item)
    }
    // Display a log for the user to notify of a new mined block unconfirmed
    log.Info("🔨 mined potential block", "number", index, "hash", hash)
}

// Shift drops all unconfirmed blocks from the set which exceed the unconfirmed sets depth
// allowance, checking them against the canonical chain for inclusion or staleness
// report.
func (set *unconfirmedBlocks) Shift(height uint64) {
    set.lock.Lock()
    defer set.lock.Unlock()

    for set.blocks != nil {
        // Retrieve the next unconfirmed block and abort if too fresh
        next := set.blocks.Value.(*unconfirmedBlock)
        if next.index+uint64(set.depth) > height {
            break
        }
        // Block seems to exceed depth allowance, check for canonical status
        header := set.chain.GetHeaderByNumber(next.index)
        switch {
        case header == nil:
            log.Warn("Failed to retrieve header of mined block", "number", next.index, "hash", next.hash)
        case header.Hash() == next.hash:
            log.Info("🔗 block reached canonical chain", "number", next.index, "hash", next.hash)
        default:
            // Block is not canonical, check whether we have an uncle or a lost block
            included := false
            for number := next.index; !included && number < next.index+uint64(set.depth) && number <= height; number++ {
                if block := set.chain.GetBlockByNumber(number); block != nil {
                    for _, uncle := range block.Uncles() {
                        if uncle.Hash() == next.hash {
                            included = true
                            break
                        }
                    }
                }
            }
            if included {
                log.Info("⑂ block became an uncle", "number", next.index, "hash", next.hash)
            } else {
                log.Info("😱 block lost", "number", next.index, "hash", next.hash)
            }
        }
        // Drop the block out of the ring
        if set.blocks.Value == set.blocks.Next().Value {
            set.blocks = nil
        } else {
            set.blocks = set.blocks.Move(-1)
            set.blocks.Unlink(1)
            set.blocks = set.blocks.Move(1)
        }
    }
}