From 69035c1e03781829eae583754bcbd3b5d91ab69b Mon Sep 17 00:00:00 2001 From: nidhi-singh02 Date: Thu, 26 Sep 2024 13:22:39 +0530 Subject: [PATCH] split echo and beacon response Signed-off-by: nidhi-singh02 --- .../handlers/beacon/types/data/fork.go | 46 ++++++++++ .../handlers/beacon/types/data/genesis.go | 29 ++++++ .../handlers/beacon/types/data/header.go | 37 ++++++++ .../handlers/beacon/types/data/randao.go | 27 ++++++ .../handlers/beacon/types/data/rewards.go | 30 ++++++ .../handlers/beacon/types/data/root.go | 27 ++++++ .../handlers/beacon/types/data/validator.go | 91 +++++++++++++++++++ .../beacon/types/data/validator_balance.go | 41 +++++++++ 8 files changed, 328 insertions(+) create mode 100644 mod/node-api/handlers/beacon/types/data/fork.go create mode 100644 mod/node-api/handlers/beacon/types/data/genesis.go create mode 100644 mod/node-api/handlers/beacon/types/data/header.go create mode 100644 mod/node-api/handlers/beacon/types/data/randao.go create mode 100644 mod/node-api/handlers/beacon/types/data/rewards.go create mode 100644 mod/node-api/handlers/beacon/types/data/root.go create mode 100644 mod/node-api/handlers/beacon/types/data/validator.go create mode 100644 mod/node-api/handlers/beacon/types/data/validator_balance.go diff --git a/mod/node-api/handlers/beacon/types/data/fork.go b/mod/node-api/handlers/beacon/types/data/fork.go new file mode 100644 index 0000000000..25e61c835f --- /dev/null +++ b/mod/node-api/handlers/beacon/types/data/fork.go @@ -0,0 +1,46 @@ +// SPDX-License-Identifier: BUSL-1.1 +// +// Copyright (C) 2024, Berachain Foundation. All rights reserved. +// Use of this software is governed by the Business Source License included +// in the LICENSE file of this repository and at www.mariadb.com/bsl11. +// +// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY +// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER +// VERSIONS OF THE LICENSED WORK. +// +// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF +// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF +// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). +// +// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON +// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, +// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND +// TITLE. + +package data + +import ( + "encoding/json" + "strconv" + + "github.com/berachain/beacon-kit/mod/node-api/handlers/beacon/types" +) + +type ForkData struct { + types.Fork +} + +type forkJSON struct { + PreviousVersion string `json:"previous_version"` + CurrentVersion string `json:"current_version"` + Epoch string `json:"epoch"` +} + +func (fr ForkData) MarshalJSON() ([]byte, error) { + return json.Marshal(forkJSON{ + PreviousVersion: fr.GetPreviousVersion().String(), + CurrentVersion: fr.GetCurrentVersion().String(), + Epoch: strconv.FormatUint(fr.GetEpoch().Unwrap(), 10), + }) +} diff --git a/mod/node-api/handlers/beacon/types/data/genesis.go b/mod/node-api/handlers/beacon/types/data/genesis.go new file mode 100644 index 0000000000..dd7bc6e018 --- /dev/null +++ b/mod/node-api/handlers/beacon/types/data/genesis.go @@ -0,0 +1,29 @@ +// SPDX-License-Identifier: BUSL-1.1 +// +// Copyright (C) 2024, Berachain Foundation. All rights reserved. +// Use of this software is governed by the Business Source License included +// in the LICENSE file of this repository and at www.mariadb.com/bsl11. +// +// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY +// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER +// VERSIONS OF THE LICENSED WORK. +// +// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF +// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF +// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). +// +// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON +// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, +// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND +// TITLE. + +package data + +import "github.com/berachain/beacon-kit/mod/primitives/pkg/common" + +type GenesisData struct { + GenesisTime string `json:"genesis_time"` + GenesisValidatorsRoot common.Root `json:"genesis_validators_root"` + GenesisForkVersion string `json:"genesis_fork_version"` +} diff --git a/mod/node-api/handlers/beacon/types/data/header.go b/mod/node-api/handlers/beacon/types/data/header.go new file mode 100644 index 0000000000..d1d4e073a5 --- /dev/null +++ b/mod/node-api/handlers/beacon/types/data/header.go @@ -0,0 +1,37 @@ +// SPDX-License-Identifier: BUSL-1.1 +// +// Copyright (C) 2024, Berachain Foundation. All rights reserved. +// Use of this software is governed by the Business Source License included +// in the LICENSE file of this repository and at www.mariadb.com/bsl11. +// +// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY +// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER +// VERSIONS OF THE LICENSED WORK. +// +// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF +// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF +// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). +// +// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON +// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, +// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND +// TITLE. + +package data + +import ( + "github.com/berachain/beacon-kit/mod/primitives/pkg/bytes" + "github.com/berachain/beacon-kit/mod/primitives/pkg/common" +) + +type BlockHeaderResponse[BlockHeaderT any] struct { + Root common.Root `json:"root"` + Canonical bool `json:"canonical"` + Header *BlockHeader[BlockHeaderT] `json:"header"` +} + +type BlockHeader[BlockHeaderT any] struct { + Message BlockHeaderT `json:"message"` + Signature bytes.B48 `json:"signature"` +} diff --git a/mod/node-api/handlers/beacon/types/data/randao.go b/mod/node-api/handlers/beacon/types/data/randao.go new file mode 100644 index 0000000000..e27d882ea8 --- /dev/null +++ b/mod/node-api/handlers/beacon/types/data/randao.go @@ -0,0 +1,27 @@ +// SPDX-License-Identifier: BUSL-1.1 +// +// Copyright (C) 2024, Berachain Foundation. All rights reserved. +// Use of this software is governed by the Business Source License included +// in the LICENSE file of this repository and at www.mariadb.com/bsl11. +// +// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY +// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER +// VERSIONS OF THE LICENSED WORK. +// +// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF +// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF +// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). +// +// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON +// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, +// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND +// TITLE. + +package data + +import "github.com/berachain/beacon-kit/mod/primitives/pkg/common" + +type RandaoData struct { + Randao common.Bytes32 `json:"randao"` +} diff --git a/mod/node-api/handlers/beacon/types/data/rewards.go b/mod/node-api/handlers/beacon/types/data/rewards.go new file mode 100644 index 0000000000..abd2096add --- /dev/null +++ b/mod/node-api/handlers/beacon/types/data/rewards.go @@ -0,0 +1,30 @@ +// SPDX-License-Identifier: BUSL-1.1 +// +// Copyright (C) 2024, Berachain Foundation. All rights reserved. +// Use of this software is governed by the Business Source License included +// in the LICENSE file of this repository and at www.mariadb.com/bsl11. +// +// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY +// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER +// VERSIONS OF THE LICENSED WORK. +// +// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF +// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF +// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). +// +// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON +// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, +// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND +// TITLE. + +package data + +type BlockRewardsData struct { + ProposerIndex uint64 `json:"proposer_index,string"` + Total uint64 `json:"total,string"` + Attestations uint64 `json:"attestations,string"` + SyncAggregate uint64 `json:"sync_aggregate,string"` + ProposerSlashings uint64 `json:"proposer_slashings,string"` + AttesterSlashings uint64 `json:"attester_slashings,string"` +} diff --git a/mod/node-api/handlers/beacon/types/data/root.go b/mod/node-api/handlers/beacon/types/data/root.go new file mode 100644 index 0000000000..95d528667d --- /dev/null +++ b/mod/node-api/handlers/beacon/types/data/root.go @@ -0,0 +1,27 @@ +// SPDX-License-Identifier: BUSL-1.1 +// +// Copyright (C) 2024, Berachain Foundation. All rights reserved. +// Use of this software is governed by the Business Source License included +// in the LICENSE file of this repository and at www.mariadb.com/bsl11. +// +// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY +// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER +// VERSIONS OF THE LICENSED WORK. +// +// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF +// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF +// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). +// +// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON +// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, +// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND +// TITLE. + +package data + +import "github.com/berachain/beacon-kit/mod/primitives/pkg/common" + +type RootData struct { + Root common.Root `json:"root"` +} diff --git a/mod/node-api/handlers/beacon/types/data/validator.go b/mod/node-api/handlers/beacon/types/data/validator.go new file mode 100644 index 0000000000..c4f75cafd9 --- /dev/null +++ b/mod/node-api/handlers/beacon/types/data/validator.go @@ -0,0 +1,91 @@ +// SPDX-License-Identifier: BUSL-1.1 +// +// Copyright (C) 2024, Berachain Foundation. All rights reserved. +// Use of this software is governed by the Business Source License included +// in the LICENSE file of this repository and at www.mariadb.com/bsl11. +// +// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY +// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER +// VERSIONS OF THE LICENSED WORK. +// +// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF +// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF +// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). +// +// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON +// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, +// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND +// TITLE. + +package data + +import ( + "encoding/hex" + "encoding/json" + "strconv" + + "github.com/berachain/beacon-kit/mod/node-api/handlers/beacon/types" +) + +type ValidatorData[ + ValidatorT types.Validator[WithdrawalCredentialsT], + WithdrawalCredentialsT types.WithdrawalCredentials, +] struct { + ValidatorBalanceData + Status string `json:"status"` + Validator ValidatorT `json:"validator"` +} + +type validatorJSON struct { + PublicKey string `json:"pubkey"` + WithdrawalCredentials string `json:"withdrawal_credentials"` + EffectiveBalance string `json:"effective_balance"` + Slashed bool `json:"slashed"` + ActivationEligibilityEpoch string `json:"activation_eligibility_epoch"` + ActivationEpoch string `json:"activation_epoch"` + ExitEpoch string `json:"exit_epoch"` + WithdrawableEpoch string `json:"withdrawable_epoch"` +} + +type responseJSON struct { + Index string `json:"index"` + Balance string `json:"balance"` + Status string `json:"status"` + Validator validatorJSON `json:"validator"` +} + +func (vd ValidatorData[ + ValidatorT, WithdrawalCredentialsT, +]) MarshalJSON() ([]byte, error) { + withdrawalCredentials := vd.Validator.GetWithdrawalCredentials() + withdrawalCredentialsBytes := withdrawalCredentials.Bytes() + + return json.Marshal(responseJSON{ + Index: strconv.FormatUint(vd.Index, 10), + Balance: strconv.FormatUint(vd.Balance, 10), + Status: vd.Status, + Validator: validatorJSON{ + PublicKey: vd.Validator.GetPubkey().String(), + WithdrawalCredentials: "0x" + hex.EncodeToString( + withdrawalCredentialsBytes, + ), + EffectiveBalance: strconv.FormatUint( + vd.Validator.GetEffectiveBalance().Unwrap(), 10, + ), + Slashed: vd.Validator.IsSlashed(), + ActivationEligibilityEpoch: strconv.FormatUint( + vd.Validator.GetActivationEligibilityEpoch().Unwrap(), 10, + ), + ActivationEpoch: strconv.FormatUint( + vd.Validator.GetActivationEpoch().Unwrap(), 10, + ), + ExitEpoch: strconv.FormatUint( + vd.Validator.GetExitEpoch().Unwrap(), 10, + ), + WithdrawableEpoch: strconv.FormatUint( + vd.Validator.GetWithdrawableEpoch().Unwrap(), 10, + ), + }, + }) +} diff --git a/mod/node-api/handlers/beacon/types/data/validator_balance.go b/mod/node-api/handlers/beacon/types/data/validator_balance.go new file mode 100644 index 0000000000..585f458d05 --- /dev/null +++ b/mod/node-api/handlers/beacon/types/data/validator_balance.go @@ -0,0 +1,41 @@ +// SPDX-License-Identifier: BUSL-1.1 +// +// Copyright (C) 2024, Berachain Foundation. All rights reserved. +// Use of this software is governed by the Business Source License included +// in the LICENSE file of this repository and at www.mariadb.com/bsl11. +// +// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY +// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER +// VERSIONS OF THE LICENSED WORK. +// +// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF +// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF +// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). +// +// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON +// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, +// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND +// TITLE. + +package data + +import ( + "encoding/json" + "strconv" +) + +type ValidatorBalanceData struct { + Index uint64 `json:"index"` + Balance uint64 `json:"balance"` +} + +func (vbd ValidatorBalanceData) MarshalJSON() ([]byte, error) { + return json.Marshal(struct { + Index string `json:"index"` + Balance string `json:"balance"` + }{ + Index: strconv.FormatUint(vbd.Index, 10), + Balance: strconv.FormatUint(vbd.Balance, 10), + }) +}