From e30d909643db94f7bb84cfed477285a486c52f0c Mon Sep 17 00:00:00 2001 From: Banana-J Date: Tue, 19 Dec 2023 19:02:32 +1100 Subject: [PATCH] Add config into network information API (#381) --- consensus/XDPoS/api.go | 4 ++++ consensus/tests/api_test.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 consensus/tests/api_test.go diff --git a/consensus/XDPoS/api.go b/consensus/XDPoS/api.go index 0228c5ce14..768e8b9a5c 100644 --- a/consensus/XDPoS/api.go +++ b/consensus/XDPoS/api.go @@ -23,6 +23,7 @@ import ( "github.com/XinFinOrg/XDPoSChain/consensus" "github.com/XinFinOrg/XDPoSChain/consensus/XDPoS/utils" "github.com/XinFinOrg/XDPoSChain/core/types" + "github.com/XinFinOrg/XDPoSChain/params" "github.com/XinFinOrg/XDPoSChain/rlp" "github.com/XinFinOrg/XDPoSChain/rpc" ) @@ -53,6 +54,7 @@ type NetworkInformation struct { XDCXListingAddress common.Address XDCZAddress common.Address LendingAddress common.Address + ConsensusConfigs params.XDPoSConfig } type SignerTypes struct { @@ -278,6 +280,8 @@ func (api *API) NetworkInformation() NetworkInformation { info.XDCXListingAddress = common.XDCXListingSMC info.XDCZAddress = common.TRC21IssuerSMC } + info.ConsensusConfigs = *api.XDPoS.config + return info } diff --git a/consensus/tests/api_test.go b/consensus/tests/api_test.go new file mode 100644 index 0000000000..3d6318151c --- /dev/null +++ b/consensus/tests/api_test.go @@ -0,0 +1,35 @@ +package tests + +import ( + "math/big" + "testing" + + "github.com/XinFinOrg/XDPoSChain/accounts/abi/bind/backends" + "github.com/XinFinOrg/XDPoSChain/consensus/XDPoS" + "github.com/XinFinOrg/XDPoSChain/core" + "github.com/XinFinOrg/XDPoSChain/crypto" + "github.com/XinFinOrg/XDPoSChain/params" + "github.com/stretchr/testify/assert" +) + +var ( + voterKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee04aefe388d1e14474d32c45c72ce7b7a") + voterAddr = crypto.PubkeyToAddress(voterKey.PublicKey) //xdc5F74529C0338546f82389402a01c31fB52c6f434 +) + +func TestConfigApi(t *testing.T) { + + bc := backends.NewXDCSimulatedBackend(core.GenesisAlloc{ + voterAddr: {Balance: new(big.Int).SetUint64(10000000000)}, + }, 10000000, params.TestXDPoSMockChainConfig) + + engine := bc.GetBlockChain().Engine().(*XDPoS.XDPoS) + + info := engine.APIs(bc.GetBlockChain())[0].Service.(*XDPoS.API).NetworkInformation() + + assert.Equal(t, info.NetworkId, big.NewInt(1337)) + assert.Equal(t, info.ConsensusConfigs.V2.CurrentConfig.MaxMasternodes, 18) + assert.Equal(t, info.ConsensusConfigs.V2.CurrentConfig.CertThreshold, 0.667) + assert.Equal(t, info.ConsensusConfigs.V2.CurrentConfig.MinePeriod, 2) + assert.Equal(t, info.ConsensusConfigs.V2.CurrentConfig.TimeoutSyncThreshold, 2) +}