Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documents to the function (irishandler) #3

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion chains/irisnet/conn/secret_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ func (sc *SecretConnection) SetWriteDeadline(t time.Time) error {
return sc.conn.(net.Conn).SetWriteDeadline(t)
}

// genEphKeys generates Ephemeral key-pair
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a library imported from tendermint itself. No need to put commnts support here.

func genEphKeys() (ephPub, ephPriv *[32]byte) {
var err error
ephPub, ephPriv, err = box.GenerateKey(crand.Reader)
Expand All @@ -264,7 +265,7 @@ func genEphKeys() (ephPub, ephPriv *[32]byte) {
}
return
}

// shareEphPubKey shares the generated Ephemeral key-pair
func shareEphPubKey(conn io.ReadWriteCloser, locEphPub *[32]byte) (remEphPub *[32]byte, err error) {

// Send our pubkey and receive theirs in tandem.
Expand Down Expand Up @@ -335,6 +336,8 @@ var blacklist = [][32]byte{
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f},
}

// hasSmallOrder used in shareEphPubKey, the function is used to
// check the if the Empherial key generated has small order or not
func hasSmallOrder(pubKey [32]byte) bool {
isSmallOrderPoint := false
for _, bl := range blacklist {
Expand All @@ -346,6 +349,9 @@ func hasSmallOrder(pubKey [32]byte) bool {
return isSmallOrderPoint
}

// Creates hash function according to challenge, generally the
// hash function created is encrypted with 256 bytes and here the
// last 32 bytes has been encrypted for the challenge
func deriveSecretAndChallenge(dhSecret *[32]byte, locIsLeast bool) (recvSecret, sendSecret *[aeadKeySize]byte, challenge *[32]byte) {
hash := sha256.New
hkdf := hkdf.New(hash, dhSecret[:], nil, []byte("TENDERMINT_SECRET_CONNECTION_KEY_AND_CHALLENGE_GEN"))
Expand Down Expand Up @@ -418,6 +424,8 @@ type authSigMessage struct {
Sig []byte
}

// Used in MakeSecretConnection and used to share the
// Authentic Share
func shareAuthSignature(sc *SecretConnection, pubKey crypto.PubKey, signature []byte) (recvMsg authSigMessage, err error) {

// Send our info and receive theirs in tandem.
Expand Down
66 changes: 64 additions & 2 deletions chains/irisnet/handlerIrisnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ var ServicedTMCore chains.NodeType = chains.NodeType{Version: "0.32.2", Network:

// ---------------------- DATA CONNECT INTERFACE --------------------------------


// RunDataConnect checks for errors while making connection with the base.
// This Error may include base connection establishment, creating TM Handler,
// handhsaking, upgrading connection of handshaking. It will also reattempt
// the connection.
func RunDataConnect(peerAddr string,
marlinTo chan marlinTypes.MarlinMessage,
marlinFrom chan marlinTypes.MarlinMessage,
Expand Down Expand Up @@ -93,6 +98,10 @@ func RunDataConnect(peerAddr string,
}
}

// dialPeer will check if the Peer has dialed succesfully or not,
// This function is used RunDataConnect, if there are errors
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't mention caller

// found while dialling the connection then it will return
// an error to RunDataConnect, otherwise return "nil"
func (h *TendermintHandler) dialPeer() error {
var err error
h.baseConnection, err = net.DialTimeout("tcp", h.peerAddr, 2000*time.Millisecond)
Expand All @@ -103,6 +112,10 @@ func (h *TendermintHandler) dialPeer() error {
return nil
}

// acceptPeer will check the if the Peer has connected succesfully
// or not, this function is used in RunDataConnect, if there are
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dont mention caller

// errors found while making an successful connection then it will return
// an error to RunDataConnect, otherwise return "nil"
func (h *TendermintHandler) acceptPeer() error {
log.Info("TMCore side listening for dials to ",
string(hex.EncodeToString(h.privateKey.PubKey().Address())), "@<SYSTEM-IP-ADDR>:", h.listenPort)
Expand All @@ -120,6 +133,10 @@ func (h *TendermintHandler) acceptPeer() error {
return nil
}

// upgradeConnectionAndHandshake checks if there has been a secret
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rephrase. It ESTABLISHES, not CHECKS

// connecton established or if there is a problem with handshaking.
// if no error has been captured, it will give a successful connection
// with Address and node info
func (h *TendermintHandler) upgradeConnectionAndHandshake() error {
var err error
h.secretConnection, err = conn.MakeSecretConnection(h.baseConnection, h.privateKey)
Expand All @@ -138,6 +155,12 @@ func (h *TendermintHandler) upgradeConnectionAndHandshake() error {
return nil
}

// handshake function follows the protocol set on amino spec,
// MarshalBinaryLengthPrefixed encodes the object according to the Amino spec
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't mention exact amino codec setup. We just need to know that how handshake happens

// same goes for UnmarshalBinaryLengthPrefixedReader
// Error encounterd while seniding handhshaking message or reciving
// using Amino spec will be checked here and
// returned to upgradeConnectionAndHandshake
func (h *TendermintHandler) handshake() error {
var (
errc = make(chan error, 2)
Expand Down Expand Up @@ -183,6 +206,8 @@ func (h *TendermintHandler) handshake() error {
return nil
}

// forms a P2P connection with the registered node
// sends and recives routines accordingly
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrong... it "services" the TM core and not forms the connection...

func (h *TendermintHandler) beginServicing() error {
// Register Messages
RegisterPacket(h.codec)
Expand Down Expand Up @@ -215,6 +240,13 @@ func (h *TendermintHandler) beginServicing() error {
return nil
}

// Datas are recived by TM Core
// sendRoutine sends PING and PONG message to TM Core
// case h.p2pConnection.pingTimer.C: Sends PING messages to TM Core
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cases are bad in comments, not needed. Mention agenda, not code.

// case h.p2pConnection.pong: Sends PONG messages to TM Core
// case timeout: Check if PONG messages are received in time
// case h.signalShutSend: Block to Shut down sendRoutine
// case marlinmsg: messages are recived from the marlin relay
func (h *TendermintHandler) sendRoutine() {
log.Info("TMCore <- Connector Routine Started")

Expand Down Expand Up @@ -394,6 +426,10 @@ func (h *TendermintHandler) sendRoutine() {
}
}

// Data processed and sent back
// case PacketPing: Received PING messages from TM Core
// case PacketPong: Received PONG messages from TM Core
// case PacketMsg: Actual message packets from TM Core (encoded form)
func (h *TendermintHandler) recvRoutine() {
log.Info("TMCore -> Connector Routine Started")

Expand Down Expand Up @@ -619,6 +655,7 @@ FOR_LOOP:
}
}

//decodes the Consensus Messages From the Channel Buffer
func (h *TendermintHandler) decodeConsensusMsgFromChannelBuffer(chanbuf []marlinTypes.PacketMsg) (ConsensusMessage, error) {
var databuf []byte
var msg ConsensusMessage
Expand All @@ -633,6 +670,8 @@ func (h *TendermintHandler) decodeConsensusMsgFromChannelBuffer(chanbuf []marlin
return msg, err
}

// Stop the PONG time when the PING is called upon in
// recvRoutine
func (c *P2PConnection) stopPongTimer() {
if c.pongTimer != nil {
_ = c.pongTimer.Stop()
Expand All @@ -643,6 +682,8 @@ func (c *P2PConnection) stopPongTimer() {
// ---------------------- SPAM FILTER INTERFACE --------------------------------

// RunSpamFilter serves as the entry point for a TM Core handler when serving as a spamfilter
// This function also acts as filter at the very begining of the TM Core, spam filter depends
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrong!. spamfilter is completely different mode. Number of spam filter is wrong. it is number of worker goroutines spawned

// on the Core count. Number of spam filter will be "2* core count".
func RunSpamFilter(rpcAddr string,
marlinTo chan marlinTypes.MarlinMessage,
marlinFrom chan marlinTypes.MarlinMessage) {
Expand Down Expand Up @@ -670,6 +711,10 @@ func RunSpamFilter(rpcAddr string,
handler.throughput.presentThroughput(5, handler.signalShutThroughput)
}

// Spam Filter executions begins from beginServicingSpamFilter
// this function will check for all possible spam from TM Core.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not from TMCore. Spam is always checked at ingress point of marlin relay

// Spam can also be produced from Marlin Relay. So beginServicingSpamFilter
// will also check for that.
func (h *TendermintHandler) beginServicingSpamFilter(id int) {
log.Info("Running TM side spam filter handler ", id)
// Register Messages
Expand Down Expand Up @@ -764,6 +809,8 @@ func (h *TendermintHandler) beginServicingSpamFilter(id int) {
}
}

// thoroughMessageCheck is used in beginServicingSpamFilter.
// thoroughMessageCheck verify the Messages from the Marlin Relay
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrong

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you tell, What would be the correct Statement here!

func (h *TendermintHandler) thoroughMessageCheck(msg ConsensusMessage) bool {
switch msg.(type) {
case *VoteMessage:
Expand Down Expand Up @@ -791,6 +838,7 @@ func (h *TendermintHandler) thoroughMessageCheck(msg ConsensusMessage) bool {
}
}

//
func (vote *Vote) SignBytes(chainID string, cdc *amino.Codec) []byte {
bz, err := cdc.MarshalBinaryLengthPrefixed(CanonicalizeVote(chainID, vote))
if err != nil {
Expand All @@ -799,6 +847,8 @@ func (vote *Vote) SignBytes(chainID string, cdc *amino.Codec) []byte {
return bz
}

// Get the height of block chain
//
func (h *TendermintHandler) getValidators(height int64) ([]Validator, bool) {
if height+10 < h.maxValidHeight {
// Don't service messages too old
Expand Down Expand Up @@ -853,6 +903,12 @@ func (h *TendermintHandler) getValidators(height int64) ([]Validator, bool) {
}
}

// spamVerdictMessage used in beginServicingSpamFilter. This function
// is used to store Messages of Marlin and even return the Boolean value
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

store messages? it just gives out signal to either allow or deny messages

// in beginServicingSpamFilter, according to boolean value. flow of this
// function gets executed. If the messages are recived in the form of 0x01
// channel, it will allow the request to be proccessed, otherwise it will
// deny it
func (h *TendermintHandler) spamVerdictMessage(msg marlinTypes.MarlinMessage, allow bool) marlinTypes.MarlinMessage {
if allow {
return marlinTypes.MarlinMessage{
Expand All @@ -876,6 +932,7 @@ var isKeyFileUsed, memoized bool
var keyFileLocation string
var privateKey ed25519.PrivKeyEd25519

//Generates privatekey and publickey
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ED25519 Keypair

func GenerateKeyFile(fileLocation string) {
log.Info("Generating KeyPair for irisnet-0.16.3-mainnet")

Expand Down Expand Up @@ -905,6 +962,7 @@ func GenerateKeyFile(fileLocation string) {
log.Info("Successfully written keyfile ", fileLocation)
}

// VerifyKeyFile verify's the 'key' file-location
func VerifyKeyFile(fileLocation string) (bool, error) {
log.Info("Accessing disk to extract info from KeyFile: ", fileLocation)
jsonFile, err := os.Open(fileLocation)
Expand Down Expand Up @@ -933,6 +991,8 @@ func VerifyKeyFile(fileLocation string) (bool, error) {
}
}

// This functions gets the private key from the keyfile!
// Also verifies the Keyfile integrity
func getPrivateKey() ed25519.PrivKeyEd25519 {
if !isKeyFileUsed {
return ed25519.GenPrivKey()
Expand Down Expand Up @@ -971,7 +1031,7 @@ func getPrivateKey() ed25519.PrivKeyEd25519 {

// ---------------------- COMMON UTILITIES ---------------------------------


//Creates Tendermint Handler between Marlin Relay and TM Core
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

creates between relay and TM core? no really... it just creates a handler object

func createTMHandler(peerAddr string,
rpcAddr string,
marlinTo chan marlinTypes.MarlinMessage,
Expand Down Expand Up @@ -1016,6 +1076,7 @@ func createTMHandler(peerAddr string,
}, nil
}

//putInfo function into "to", "from", "spam"
func (t *throughPutData) putInfo(direction string, key string, count uint32) {
t.mu.Lock()
switch direction {
Expand All @@ -1028,7 +1089,8 @@ func (t *throughPutData) putInfo(direction string, key string, count uint32) {
}
t.mu.Unlock()
}

// This function display the logs/stats of marlin to
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not related to marlinTo and marlinFrom actually

// and marlin from or SpamFilter
func (t *throughPutData) presentThroughput(sec time.Duration, shutdownCh chan struct{}) {
for {
time.Sleep(sec * time.Second)
Expand Down
13 changes: 12 additions & 1 deletion chains/irisnet/structsTendermint.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@ import (
"github.com/tendermint/tendermint/crypto/merkle"
)

// ProtocolVersion defines the structure consisting of P2P, block and App
type ProtocolVersion struct {
P2P uint64 `json:"p2p"`
Block uint64 `json:"block"`
App uint64 `json:"app"`
}

// DefaultNodeInfo defines the structure consisting of ID, Listener Address,
// Chain/Network ID, Version(Major/Minor), Channels, Moniker
type DefaultNodeInfo struct {
ProtocolVersion ProtocolVersion `json:"protocol_version"`

Expand All @@ -43,6 +46,7 @@ type DefaultNodeInfoOther struct {
RPCAddress string `json:"rpc_address"`
}

// P2PConnection defines the structure of P2P Layer
type P2PConnection struct {
conn net.Conn
bufConnReader *bufio.Reader
Expand Down Expand Up @@ -87,7 +91,8 @@ type P2PConnection struct {
type Packet interface {
AssertIsPacket()
}

// RegisterPacket registers the PING and PONG in the
// form of packet
func RegisterPacket(cdc *amino.Codec) {
cdc.RegisterInterface((*Packet)(nil), nil)
cdc.RegisterConcrete(PacketPing{}, "tendermint/p2p/PacketPing", nil)
Expand All @@ -99,18 +104,22 @@ func (_ PacketPing) AssertIsPacket() {}
func (_ PacketPong) AssertIsPacket() {}
func (_ PacketMsg) AssertIsPacket() {}

// Structure for Ping Packet
type PacketPing struct {
}

// Strucutre for Pong Packet
type PacketPong struct {
}

// Structure for Messages packet
type PacketMsg struct {
ChannelID byte
EOF byte // 1 means message ends here.
Bytes []byte
}

// String fucntion for Channel ID, Bytes, EOF
func (mp PacketMsg) String() string {
return fmt.Sprintf("PacketMsg{%X:%X T:%X}", mp.ChannelID, mp.Bytes, mp.EOF)
}
Expand All @@ -120,6 +129,8 @@ type ConsensusMessage interface {
ValidateBasic() error
}

// RegisterConsensusMessages registers the Consensus Messages
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

between tendermint and consensus layer? consensus layer is in tendermint

// between tendermint and consensus layer
func RegisterConsensusMessages(cdc *amino.Codec) {
cdc.RegisterInterface((*ConsensusMessage)(nil), nil)
cdc.RegisterConcrete(&NewRoundStepMessage{}, "tendermint/NewRoundStepMessage", nil)
Expand Down