protocols

package
v0.0.0-...-f8b7a73 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 16, 2019 License: GPL-3.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Sender   = Payer(true)
	Receiver = Payer(false)
)
View Source
const (
	ErrMsgTooLong = iota
	ErrDecode
	ErrWrite
	ErrInvalidMsgCode
	ErrInvalidMsgType
	ErrHandshake
	ErrNoHandler
	ErrHandler
)

此协议方案使用的错误代码

View Source
const AccountingVersion = "1.0"

会计API文本版本号

Variables

View Source
var (
	MetricsRegistry metrics.Registry
)

定义一些指标

Functions

This section is empty.

Types

type Accounting

type Accounting struct {
	Balance //会计逻辑接口
	Prices  //价格逻辑接口
}

会计实现钩子接口 它通过余额接口与余额接口连接, 通过价格接口与协议及其价格进行接口时

func NewAccounting

func NewAccounting(balance Balance, po Prices) *Accounting

func (*Accounting) Receive

func (ah *Accounting) Receive(peer *Peer, size uint32, msg interface{}) error

接收需要一个对等点、一个大小和一个消息以及 -使用价格接口计算从对等端接收大小消息的本地节点的成本 -使用余额界面的贷记/借记本地节点

func (*Accounting) Send

func (ah *Accounting) Send(peer *Peer, size uint32, msg interface{}) error

发送需要一个对等点、一个大小和一个消息以及 -使用价格接口计算本地节点向对等端发送大小消息的成本 -使用余额界面的贷记/借记本地节点

type AccountingApi

type AccountingApi struct {
	// contains filtered or unexported fields
}

AccountingAPI提供了一个API来访问与帐户相关的信息

func NewAccountingApi

func NewAccountingApi(m *AccountingMetrics) *AccountingApi

new accountingapi创建新的accountingapi m将用于检查会计指标是否启用

func (*AccountingApi) Balance

func (self *AccountingApi) Balance() (int64, error)

余额返回本地节点余额(贷记单位-借记单位)

func (*AccountingApi) BalanceCredit

func (self *AccountingApi) BalanceCredit() (int64, error)

BalanceCredit返回本地节点贷记的单位总数

func (*AccountingApi) BalanceDebit

func (self *AccountingApi) BalanceDebit() (int64, error)

BalanceCredit返回本地节点借记的单位总数

func (*AccountingApi) BytesCredit

func (self *AccountingApi) BytesCredit() (int64, error)

BytesCredit返回本地节点贷记的字节总数

func (*AccountingApi) BytesDebit

func (self *AccountingApi) BytesDebit() (int64, error)

BalanceCredit返回本地节点借记的字节总数

func (*AccountingApi) MsgCredit

func (self *AccountingApi) MsgCredit() (int64, error)

msgcredit返回本地节点贷记的消息总数

func (*AccountingApi) MsgDebit

func (self *AccountingApi) MsgDebit() (int64, error)

func (*AccountingApi) PeerDrops

func (self *AccountingApi) PeerDrops() (int64, error)

func (*AccountingApi) SelfDrops

func (self *AccountingApi) SelfDrops() (int64, error)

type AccountingMetrics

type AccountingMetrics struct {
	// contains filtered or unexported fields
}

AccountMetrics提取了度量数据库,并 报告者坚持标准

func NewAccountingMetrics

func NewAccountingMetrics(r metrics.Registry, d time.Duration, path string) *AccountingMetrics

NewMetricsDB创建一个新的LevelDB实例,用于持久化定义的度量 在p2p/protocols/accounting.go中

func SetupAccountingMetrics

func SetupAccountingMetrics(reportInterval time.Duration, path string) *AccountingMetrics

SetupAccountingMetrics为P2P会计指标创建单独的注册表; 这个注册表应该独立于任何其他指标,因为它在不同的端点上持续存在。 它还实例化给定的度量并启动持续执行例程,该例程 在经过的时间间隔内,将度量值写入级别数据库

func (*AccountingMetrics) Close

func (am *AccountingMetrics) Close()

关闭节点时将调用Close。 为了优雅的清理

type Balance

type Balance interface {
	//使用远程节点“peer”将金额添加到本地余额中;
	//正数=贷方本地节点
	//负金额=借方本地节点
	Add(amount int64, peer *Peer) error
}

余额是实际的会计实例 余额定义了会计所需的操作 实现在内部维护每个对等点的平衡

type Error

type Error struct {
	Code int
	// contains filtered or unexported fields
}

func (Error) Error

func (e Error) Error() (message string)

type Hook

type Hook interface {
	//发送信息的钩子
	Send(peer *Peer, size uint32, msg interface{}) error
	//接收信息的钩子
	Receive(peer *Peer, size uint32, msg interface{}) error
}

对于会计,设计是允许规范描述消息的定价方式和内容。 为了访问这个功能,我们提供了一个钩子接口,它将调用会计方法 注意:将来可能会有更多这样的(水平)钩子

type Payer

type Payer bool

type Peer

type Peer struct {
	*p2p.Peer //代表远程的p2p.peer对象
	// contains filtered or unexported fields
}

对等机表示在与的对等连接上运行的远程对等机或协议实例 远程对等体

func NewPeer

func NewPeer(p *p2p.Peer, rw p2p.MsgReadWriter, spec *Spec) *Peer

new peer构造新的peer 此构造函数由p2p.protocol run函数调用 前两个参数是传递给p2p.protocol.run函数的参数 第三个参数是描述协议的规范

func (*Peer) Drop

func (p *Peer) Drop(err error)

DROP断开对等机的连接。 TODO:可能只需要实现协议删除?不想把同伴踢开 如果它们对其他协议有用

func (*Peer) Handshake

func (p *Peer) Handshake(ctx context.Context, hs interface{}, verify func(interface{}) error) (rhs interface{}, err error)

握手在对等连接上协商握手 *参数 *上下文 *要发送到远程对等机的本地握手 *在远程握手时要调用的函数(可以为零) *需要相同类型的远程握手 *拨号对等端需要先发送握手,然后等待远程 *侦听对等机等待远程握手,然后发送它 返回远程握手和错误

func (*Peer) Run

func (p *Peer) Run(handler func(ctx context.Context, msg interface{}) error) error

运行启动处理传入消息的Forever循环 在p2p.protocol run函数中调用 handler参数是为接收到的每个消息调用的函数。 从远程对等机返回的错误导致循环退出 导致断开

func (*Peer) Send

func (p *Peer) Send(ctx context.Context, msg interface{}) error

send接收一条消息,将其编码为rlp,找到正确的消息代码并发送 向对等端发送消息 这个低级调用将由提供路由或广播发送的库包装。 但通常只用于转发和将消息推送到直接连接的对等端

type Price

type Price struct {
	Value   uint64
	PerByte bool //如果价格为每字节或单位,则为真
	Payer   Payer
}

价格表示消息的成本

func (*Price) For

func (p *Price) For(payer Payer, size uint32) int64

因为给了信息的价格 协议以绝对值提供消息价格 然后,此方法返回正确的有符号金额, 根据“付款人”论点确定的付款人: “send”将传递“sender”付款人,“receive”将传递“receiver”参数。 因此:如果发送人和发送人支付,金额为正,否则为负。 如果收货方付款,金额为正,否则为负。

type Prices

type Prices interface {
	//返回消息的价格
	Price(interface{}) *Price
}

价格定义如何将价格传递给会计实例

type Spec

type Spec struct {
	//名称是协议的名称,通常是三个字母的单词
	Name string

	//version是协议的版本号
	Version uint

	//maxmsgsize是消息有效负载的最大可接受长度
	MaxMsgSize uint32

	//messages是此协议使用的消息数据类型的列表,
	//发送的每个消息类型及其数组索引作为代码(因此
	//[&foo,&bar,&baz]将发送带有代码的foo、bar和baz
	//分别为0、1和2)
	//每条消息必须有一个唯一的数据类型
	Messages []interface{}

	//会计挂钩(将来可扩展到多个挂钩)
	Hook Hook
	// contains filtered or unexported fields
}

规范是一种协议规范,包括其名称和版本以及 交换的消息类型

func (*Spec) GetCode

func (s *Spec) GetCode(msg interface{}) (uint64, bool)

getcode返回一个类型的消息代码,Boolean第二个参数是 如果未找到消息类型,则为false

func (*Spec) Length

func (s *Spec) Length() uint64

length返回协议中的消息类型数

func (*Spec) NewMsg

func (s *Spec) NewMsg(code uint64) (interface{}, bool)

newmsg构造给定代码的新消息类型

type WrappedMsg

type WrappedMsg struct {
	Context []byte
	Size    uint32
	Payload []byte
}

wrappedmsg用于在消息有效负载旁边传播已封送的上下文

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL