aboutsummaryrefslogtreecommitdiffstats
path: root/eth/protocol.go
blob: fcc5f21e2e91673627ac69329cbe4cecd04938ba (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 2014 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 eth

import (
    "math/big"

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

// Supported versions of the eth protocol (first is primary).
var ProtocolVersions = []uint{62, 61, 60}

// Number of implemented message corresponding to different protocol versions.
var ProtocolLengths = []uint64{13, 9, 8}

const (
    NetworkId          = 1
    ProtocolMaxMsgSize = 10 * 1024 * 1024 // Maximum cap on the size of a protocol message
)

// eth protocol message codes
const (
    // Protocol messages belonging to eth/60
    StatusMsg = iota
    NewBlockHashesMsg
    TxMsg
    GetBlockHashesMsg
    BlockHashesMsg
    GetBlocksMsg
    BlocksMsg
    NewBlockMsg

    // Protocol messages belonging to eth/61
    GetBlockHashesFromNumberMsg

    // Protocol messages belonging to eth/62
    GetBlockHeadersMsg
    BlockHeadersMsg
    GetNodeDataMsg
    NodeDataMsg
)

type errCode int

const (
    ErrMsgTooLarge = iota
    ErrDecode
    ErrInvalidMsgCode
    ErrProtocolVersionMismatch
    ErrNetworkIdMismatch
    ErrGenesisBlockMismatch
    ErrNoStatusMsg
    ErrExtraStatusMsg
    ErrSuspendedPeer
)

func (e errCode) String() string {
    return errorToString[int(e)]
}

// XXX change once legacy code is out
var errorToString = map[int]string{
    ErrMsgTooLarge:             "Message too long",
    ErrDecode:                  "Invalid message",
    ErrInvalidMsgCode:          "Invalid message code",
    ErrProtocolVersionMismatch: "Protocol version mismatch",
    ErrNetworkIdMismatch:       "NetworkId mismatch",
    ErrGenesisBlockMismatch:    "Genesis block mismatch",
    ErrNoStatusMsg:             "No status message",
    ErrExtraStatusMsg:          "Extra status message",
    ErrSuspendedPeer:           "Suspended peer",
}

type txPool interface {
    // AddTransactions should add the given transactions to the pool.
    AddTransactions([]*types.Transaction)

    // GetTransactions should return pending transactions.
    // The slice should be modifiable by the caller.
    GetTransactions() types.Transactions
}

type chainManager interface {
    GetBlockHashesFromHash(hash common.Hash, amount uint64) (hashes []common.Hash)
    GetBlock(hash common.Hash) (block *types.Block)
    Status() (td *big.Int, currentBlock common.Hash, genesisBlock common.Hash)
}

// statusData is the network packet for the status message.
type statusData struct {
    ProtocolVersion uint32
    NetworkId       uint32
    TD              *big.Int
    CurrentBlock    common.Hash
    GenesisBlock    common.Hash
}

// getBlockHashesData is the network packet for the hash based hash retrieval.
type getBlockHashesData struct {
    Hash   common.Hash
    Amount uint64
}

// getBlockHashesFromNumberData is the network packet for the number based hash
// retrieval.
type getBlockHashesFromNumberData struct {
    Number uint64
    Amount uint64
}

// newBlockData is the network packet for the block propagation message.
type newBlockData struct {
    Block *types.Block
    TD    *big.Int
}

// nodeDataData is the network response packet for a node data retrieval.
type nodeDataData []struct {
    Value []byte
}