diff options
author | holisticode <holistic.computing@gmail.com> | 2018-10-26 06:26:31 +0800 |
---|---|---|
committer | Viktor TrĂ³n <viktor.tron@gmail.com> | 2018-10-26 06:26:31 +0800 |
commit | 8ed4739176f435d09dfa36d8b2e2a3c8c6f407dd (patch) | |
tree | b25dff2a750a91a71e7c98c0803bcc6b56b06d5a /p2p/protocols/protocol.go | |
parent | 80d390776742a2a3cfc2f3041fd01ffe82f43d23 (diff) | |
download | dexon-8ed4739176f435d09dfa36d8b2e2a3c8c6f407dd.tar dexon-8ed4739176f435d09dfa36d8b2e2a3c8c6f407dd.tar.gz dexon-8ed4739176f435d09dfa36d8b2e2a3c8c6f407dd.tar.bz2 dexon-8ed4739176f435d09dfa36d8b2e2a3c8c6f407dd.tar.lz dexon-8ed4739176f435d09dfa36d8b2e2a3c8c6f407dd.tar.xz dexon-8ed4739176f435d09dfa36d8b2e2a3c8c6f407dd.tar.zst dexon-8ed4739176f435d09dfa36d8b2e2a3c8c6f407dd.zip |
p2p accounting (#17951)
* p2p/protocols: introduced protocol accounting
* p2p/protocols: added TestExchange simulation
* p2p/protocols: add accounting simulation
* p2p/protocols: remove unnecessary tests
* p2p/protocols: comments for accounting simulation
* p2p/protocols: addressed PR comments
* p2p/protocols: finalized accounting implementation
* p2p/protocols: removed unused code
* p2p/protocols: addressed @nonsense PR comments
Diffstat (limited to 'p2p/protocols/protocol.go')
-rw-r--r-- | p2p/protocols/protocol.go | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/p2p/protocols/protocol.go b/p2p/protocols/protocol.go index 615f74b56..7dddd852f 100644 --- a/p2p/protocols/protocol.go +++ b/p2p/protocols/protocol.go @@ -122,6 +122,16 @@ type WrappedMsg struct { Payload []byte } +//For accounting, the design is to allow the Spec to describe which and how its messages are priced +//To access this functionality, we provide a Hook interface which will call accounting methods +//NOTE: there could be more such (horizontal) hooks in the future +type Hook interface { + //A hook for sending messages + Send(peer *Peer, size uint32, msg interface{}) error + //A hook for receiving messages + Receive(peer *Peer, size uint32, msg interface{}) error +} + // Spec is a protocol specification including its name and version as well as // the types of messages which are exchanged type Spec struct { @@ -141,6 +151,9 @@ type Spec struct { // each message must have a single unique data type Messages []interface{} + //hook for accounting (could be extended to multiple hooks in the future) + Hook Hook + initOnce sync.Once codes map[reflect.Type]uint64 types map[uint64]reflect.Type @@ -274,6 +287,15 @@ func (p *Peer) Send(ctx context.Context, msg interface{}) error { Payload: r, } + //if the accounting hook is set, call it + if p.spec.Hook != nil { + err := p.spec.Hook.Send(p, wmsg.Size, msg) + if err != nil { + p.Drop(err) + return err + } + } + code, found := p.spec.GetCode(msg) if !found { return errorf(ErrInvalidMsgType, "%v", code) @@ -336,6 +358,14 @@ func (p *Peer) handleIncoming(handle func(ctx context.Context, msg interface{}) return errorf(ErrDecode, "<= %v: %v", msg, err) } + //if the accounting hook is set, call it + if p.spec.Hook != nil { + err := p.spec.Hook.Receive(p, wmsg.Size, val) + if err != nil { + return err + } + } + // call the registered handler callbacks // a registered callback take the decoded message as argument as an interface // which the handler is supposed to cast to the appropriate type |