From 62e6fcfcf97b2b5d77c51052170dd900d2e566e1 Mon Sep 17 00:00:00 2001 From: Edgar Aroutiounian Date: Mon, 9 Mar 2020 11:06:23 -0700 Subject: [PATCH] [webhooks] Additional support for downstream monitoring (#2434) * [webhook] Move webhook with call for when cannot commit block * [webhook] Move as own root package * [webhook] Report bad block to webhook --- cmd/harmony/main.go | 2 +- internal/configs/node/config.go | 2 +- node/node.go | 2 +- node/node_handler.go | 15 +++++++++++++-- staking/slash/webhook.example.yaml | 4 ---- webhooks/webhook.example.yaml | 8 ++++++++ {staking/webhooks => webhooks}/yaml.go | 16 +++++++++++----- 7 files changed, 35 insertions(+), 14 deletions(-) delete mode 100644 staking/slash/webhook.example.yaml create mode 100644 webhooks/webhook.example.yaml rename {staking/webhooks => webhooks}/yaml.go (74%) diff --git a/cmd/harmony/main.go b/cmd/harmony/main.go index df6421264..e2b73c2a3 100644 --- a/cmd/harmony/main.go +++ b/cmd/harmony/main.go @@ -40,7 +40,7 @@ import ( "github.com/harmony-one/harmony/p2p/p2pimpl" p2putils "github.com/harmony-one/harmony/p2p/utils" "github.com/harmony-one/harmony/shard" - "github.com/harmony-one/harmony/staking/webhooks" + "github.com/harmony-one/harmony/webhooks" golog "github.com/ipfs/go-log" "github.com/pkg/errors" gologging "github.com/whyrusleeping/go-logging" diff --git a/internal/configs/node/config.go b/internal/configs/node/config.go index 75f4b8d6c..110e53cb0 100644 --- a/internal/configs/node/config.go +++ b/internal/configs/node/config.go @@ -13,7 +13,7 @@ import ( "github.com/harmony-one/harmony/internal/params" "github.com/harmony-one/harmony/multibls" "github.com/harmony-one/harmony/shard" - "github.com/harmony-one/harmony/staking/webhooks" + "github.com/harmony-one/harmony/webhooks" p2p_crypto "github.com/libp2p/go-libp2p-crypto" "github.com/pkg/errors" ) diff --git a/node/node.go b/node/node.go index 2b5f77afb..1b336ce5e 100644 --- a/node/node.go +++ b/node/node.go @@ -38,7 +38,7 @@ import ( "github.com/harmony-one/harmony/shard/committee" "github.com/harmony-one/harmony/staking/slash" staking "github.com/harmony-one/harmony/staking/types" - "github.com/harmony-one/harmony/staking/webhooks" + "github.com/harmony-one/harmony/webhooks" ) // State is a state of a node. diff --git a/node/node_handler.go b/node/node_handler.go index 74d6c7c24..5a646a5d2 100644 --- a/node/node_handler.go +++ b/node/node_handler.go @@ -25,7 +25,7 @@ import ( "github.com/harmony-one/harmony/staking/availability" "github.com/harmony-one/harmony/staking/slash" staking "github.com/harmony-one/harmony/staking/types" - "github.com/harmony-one/harmony/staking/webhooks" + "github.com/harmony-one/harmony/webhooks" libp2p_peer "github.com/libp2p/go-libp2p-core/peer" ) @@ -363,6 +363,17 @@ func (node *Node) VerifyNewBlock(newBlock *types.Block) error { } if err := node.Blockchain().ValidateNewBlock(newBlock); err != nil { + if hooks := node.NodeConfig.WebHooks.Hooks; hooks != nil { + if p := hooks.ProtocolIssues; p != nil { + url := p.OnCannotCommit + go func() { + webhooks.DoPost(url, map[string]interface{}{ + "bad-header": newBlock.Header().String(), + "reason": err.Error(), + }) + }() + } + } utils.Logger().Error(). Str("blockHash", newBlock.Hash().Hex()). Int("numTx", len(newBlock.Transactions())). @@ -479,7 +490,7 @@ func (node *Node) PostConsensusProcessing( signed, toSign, quotient, err := availability.ComputeCurrentSigning(snapshot, wrapper) if err != nil && availability.IsBelowSigningThreshold(quotient) { - url := h.Availability.DroppedBelowThreshold + url := h.Availability.OnDroppedBelowThreshold go func() { webhooks.DoPost(url, staking.Computed{signed, toSign, quotient}) }() diff --git a/staking/slash/webhook.example.yaml b/staking/slash/webhook.example.yaml deleted file mode 100644 index 7f5764c04..000000000 --- a/staking/slash/webhook.example.yaml +++ /dev/null @@ -1,4 +0,0 @@ -web-hooks: - # node will call these webhooks - notice-double-sign: http://localhost:5430/on-notice-double-sign - this-node-double-signed: http://localhost:5430/on-this-node-double-signed diff --git a/webhooks/webhook.example.yaml b/webhooks/webhook.example.yaml new file mode 100644 index 000000000..5d67fadde --- /dev/null +++ b/webhooks/webhook.example.yaml @@ -0,0 +1,8 @@ +slashing-hooks: + on-notice-double-sign: http://localhost:5430/on-notice-double-sign + +availability-hooks: + on-dropped-below-threshold: http://localhost:5430/on-dropped-below-threshold + +protocol-hooks: + on-cannot-commit-block: http://localhost:5430/on-cannot-commit-block diff --git a/staking/webhooks/yaml.go b/webhooks/yaml.go similarity index 74% rename from staking/webhooks/yaml.go rename to webhooks/yaml.go index 38f4c14ea..d459188a1 100644 --- a/staking/webhooks/yaml.go +++ b/webhooks/yaml.go @@ -11,23 +11,29 @@ import ( const ( // DefaultWebHookPath .. - DefaultWebHookPath = "staking/webhooks/webhook.example.yaml" + DefaultWebHookPath = "webhooks/webhook.example.yaml" ) // AvailabilityHooks .. type AvailabilityHooks struct { - DroppedBelowThreshold string `yaml:"dropped-below-threshold"` + OnDroppedBelowThreshold string `yaml:"on-dropped-below-threshold"` } // DoubleSignWebHooks .. type DoubleSignWebHooks struct { - OnNoticeDoubleSign string `yaml:"notice-double-sign"` + OnNoticeDoubleSign string `yaml:"on-notice-double-sign"` +} + +// BadBlockHooks .. +type BadBlockHooks struct { + OnCannotCommit string `yaml:"on-cannot-commit-block"` } // Hooks .. type Hooks struct { - Slashing *DoubleSignWebHooks `yaml:"slashing-hooks"` - Availability *AvailabilityHooks `yaml:"availability-hooks"` + Slashing *DoubleSignWebHooks `yaml:"slashing-hooks"` + Availability *AvailabilityHooks `yaml:"availability-hooks"` + ProtocolIssues *BadBlockHooks `yaml:"protocol-hooks"` } // ReportResult ..