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

Integrate fee handler in node's system contracts #5

Merged
merged 2 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
shallow = true
[submodule "core-contracts"]
path = core-contracts
url = https://github.com/R-Santev/core-contracts.git
url = https://github.com/Hydra-Chain/hydragon-core-contracts.git
shallow = true
branch = h-main
15 changes: 7 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
.PHONY: download-submodules
download-submodules: check-git
git submodule init
git submodule update
git submodule update --init --remote ./core-contracts

.PHONY: check-git
check-git:
Expand Down Expand Up @@ -39,14 +38,14 @@ protoc: check-protoc
.PHONY: build
build: check-go check-git
$(eval COMMIT_HASH = $(shell git rev-parse HEAD))
$(eval VERSION = $(shell git tag --points-at $COMMIT_HASH))
$(eval VERSION = $(shell git tag --points-at $(COMMIT_HASH)))
$(eval BRANCH = $(shell git rev-parse --abbrev-ref HEAD | tr -d '\040\011\012\015\n'))
$(eval TIME = $(shell date))
go build -o polygon-edge -ldflags="\
-X 'github.com/0xPolygon/polygon-edge/versioning.Version=$(VERSION)' \
-X 'github.com/0xPolygon/polygon-edge/versioning.Commit=$(COMMIT_HASH)'\
-X 'github.com/0xPolygon/polygon-edge/versioning.Branch=$(BRANCH)'\
-X 'github.com/0xPolygon/polygon-edge/versioning.BuildTime=$(TIME)'" \
go build -o hydra -ldflags="\
-X 'github.com/Hydra-Chain/hydragon-node/versioning.Version=$(VERSION)' \
-X 'github.com/Hydra-Chain/hydragon-node/versioning.Commit=$(COMMIT_HASH)'\
-X 'github.com/Hydra-Chain/hydragon-node/versioning.Branch=$(BRANCH)'\
-X 'github.com/Hydra-Chain/hydragon-node/versioning.BuildTime=$(TIME)'" \
main.go

.PHONY: lint
Expand Down
4 changes: 4 additions & 0 deletions command/genesis/polybft_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,10 @@ func (p *genesisParams) deployContracts(
artifact: contractsapi.LiquidityToken,
address: contracts.LiquidityTokenContract,
},
{
artifact: contractsapi.FeeHandler,
address: contracts.FeeHandlerContractV1,
},
}

// if !params.nativeTokenConfig.IsMintable {
Expand Down
15 changes: 14 additions & 1 deletion consensus/polybft/contracts_initializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func initRewardPool(polybftConfig PolyBFTConfig, transition *state.Transition) e
NewRewardWallet: types.Address{0x22}, // TODO: Remove reward wallet or fully implement it in the contracts
NewMinDelegation: big.NewInt(minDelegation),
// TODO: AprManager is temporary solution to enable update in the parameters. Remove it when "Dynamic APR variables" epic is finished
AprManager: polybftConfig.Governance,
Manager: polybftConfig.Governance,
}

input, err := initFn.EncodeAbi()
Expand All @@ -76,6 +76,19 @@ func initRewardPool(polybftConfig PolyBFTConfig, transition *state.Transition) e
contracts.RewardPoolContract, input, "RewardPool.initialize", transition)
}

func initFeeHandler(polybftConfig PolyBFTConfig, transition *state.Transition) error {
initFn := &contractsapi.InitializeFeeHandlerFn{
Owner: polybftConfig.Governance,
}

input, err := initFn.EncodeAbi()
if err != nil {
return fmt.Errorf("FeeHandler.initialize params encoding failed: %w", err)
}

return callContract(contracts.SystemCaller, contracts.FeeHandlerContract, input, "FeeHandler.initialize", transition)
}

func initLiquidityToken(polyBFTConfig PolyBFTConfig, transition *state.Transition) error {
initFn := contractsapi.InitializeLiquidityTokenFn{
Name_: "Liquid Hydra",
Expand Down
4 changes: 4 additions & 0 deletions consensus/polybft/contractsapi/artifacts-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ func main() {
"LiquidityToken/LiquidityToken.sol",
"LiquidityToken",
},
{
"FeeHandler/FeeHandler.sol",
"FeeHandler",
},
{
"GenesisProxy/GenesisProxy.sol",
"GenesisProxy",
Expand Down
9 changes: 9 additions & 0 deletions consensus/polybft/contractsapi/bindings-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ func main() {
},
[]string{},
},
{
"FeeHandler",
gensc.FeeHandler,
false,
[]string{
"initialize",
},
[]string{},
},
{
"GenesisProxy",
gensc.GenesisProxy,
Expand Down
18 changes: 17 additions & 1 deletion consensus/polybft/contractsapi/contractsapi.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion consensus/polybft/contractsapi/gen_sc_data.go

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions consensus/polybft/contractsapi/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ var (
TestSimple *artifact.Artifact
TestRewardToken *artifact.Artifact
LiquidityToken *artifact.Artifact
FeeHandler *artifact.Artifact
)

func init() {
Expand All @@ -89,6 +90,11 @@ func init() {
log.Fatal(err)
}

FeeHandler, err = artifact.DecodeArtifact([]byte(FeeHandlerArtifact))
if err != nil {
log.Fatal(err)
}

GenesisProxy, err = artifact.DecodeArtifact([]byte(GenesisProxyArtifact))
if err != nil {
log.Fatal(err)
Expand Down
5 changes: 5 additions & 0 deletions consensus/polybft/polybft.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,11 @@ func GenesisPostHookFactory(config *chain.Chain, engineName string) func(txn *st
return err
}

// initialize FeeHandler SC
if err = initFeeHandler(polyBFTConfig, transition); err != nil {
return err
}

// // approve reward pool
// if err = approveRewardPoolAsSpender(polyBFTConfig, transition); err != nil {
// return err
Expand Down
60 changes: 54 additions & 6 deletions consensus/polybft/sc_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,6 @@ func TestIntegration_CommitEpoch(t *testing.T) {
input, err := commitEpochInput.EncodeAbi()
require.NoError(t, err)

// Normally injecting balance to the system caller is handled by a higher order method in the executor.go
// but here we use call2 directly so we need to do it manually
transition.Txn().AddBalance(contracts.SystemCaller, maxRewardToDistribute)

// call commit epoch
result := transition.Call2(contracts.SystemCaller, contracts.ValidatorSetContract, input, big.NewInt(0), 10000000000)
require.NoError(t, result.Err)
Expand All @@ -164,6 +160,11 @@ func TestIntegration_CommitEpoch(t *testing.T) {
distributeRewards := createTestDistributeRewardsInput(t, 1, accSet, polyBFTConfig.EpochSize)
distributeRewardsInput, err := distributeRewards.EncodeAbi()
require.NoError(t, err)

// Normally injecting balance to the system caller is handled by a higher order method in the executor.go
// but here we use call2 directly so we need to do it manually
transition.Txn().AddBalance(contracts.SystemCaller, maxRewardToDistribute)

// call reward distributor
result = transition.Call2(contracts.SystemCaller, contracts.RewardPoolContract, distributeRewardsInput, maxRewardToDistribute, 10000000000)
require.NoError(t, result.Err)
Expand All @@ -173,8 +174,6 @@ func TestIntegration_CommitEpoch(t *testing.T) {
input, err = commitEpochInput.EncodeAbi()
require.NoError(t, err)

transition.Txn().AddBalance(contracts.SystemCaller, maxRewardToDistribute)

// call commit epoch
result = transition.Call2(contracts.SystemCaller, contracts.ValidatorSetContract, input, big.NewInt(0), 10000000000)
require.NoError(t, result.Err)
Expand All @@ -183,13 +182,62 @@ func TestIntegration_CommitEpoch(t *testing.T) {
distributeRewards = createTestDistributeRewardsInput(t, 2, accSet, polyBFTConfig.EpochSize)
distributeRewardsInput, err = distributeRewards.EncodeAbi()
require.NoError(t, err)

transition.Txn().AddBalance(contracts.SystemCaller, maxRewardToDistribute)

// call reward distributor
result = transition.Call2(contracts.SystemCaller, contracts.RewardPoolContract, distributeRewardsInput, maxRewardToDistribute, 10000000000)
require.NoError(t, result.Err)
t.Logf("Number of validators %d on reward distribution when we add %d of delegators, Gas used %+v\n", accSet.Len(), accSet.Len()*delegatorsPerValidator, result.GasUsed)
}
}

// Test Transaction fees distribution to FeeHandler
func TestIntegration_DistributeFee(t *testing.T) {
fromAddr := types.Address{0x1}
toAddr := &types.Address{0x2}
value := big.NewInt(1)
gasPrice := big.NewInt(10)
txFees := new(big.Int).Mul(gasPrice, big.NewInt(21000))
fromBalance := new(big.Int).Add(value, txFees)

alloc := map[types.Address]*chain.GenesisAccount{
contracts.FeeHandlerContract: {
Code: contractsapi.FeeHandler.DeployedBytecode,
},
fromAddr: {
Balance: fromBalance,
},
}

transition := newTestTransition(t, alloc)

polyBFTConfig := PolyBFTConfig{
// just an address for governance
Governance: *toAddr,
}

// init ValidatorSet
err := initFeeHandler(polyBFTConfig, transition)
require.NoError(t, err)

tx := &types.Transaction{
Nonce: 0,
From: fromAddr,
To: toAddr,
Value: value,
Type: types.LegacyTx,
GasPrice: gasPrice,
Gas: 21000,
}

err = transition.Write(tx)
require.NoError(t, err)

// Balance of FeeHandler must increase with 50% of the reward
require.Equal(t, transition.GetBalance(contracts.FeeHandlerContract), new(big.Int).Div(txFees, big.NewInt(2)))
}

func deployAndInitContract(t *testing.T, transition *state.Transition, bytecode []byte, sender types.Address,
initCallback func() ([]byte, error)) types.Address {
t.Helper()
Expand Down
2 changes: 1 addition & 1 deletion consensus/polybft/system_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func newTestTransition(t *testing.T, alloc map[types.Address]*chain.GenesisAccou

transition, err := ex.BeginTxn(
rootHash,
&types.Header{},
&types.Header{GasLimit: 10000000},
types.ZeroAddress,
)
assert.NoError(t, err)
Expand Down
6 changes: 5 additions & 1 deletion contracts/system_addresses.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ var (
L2StateSenderContract = types.StringToAddress("0x1002")
// L2StateSenderContractV1 is an address of bridge contract to the rootchain
L2StateSenderContractV1 = types.StringToAddress("0x10021")
// FeeHandlerContract is an address of the fee handler proxy contract
FeeHandlerContract = types.StringToAddress("0x107")
// FeeHandlerContract is an address of fee handler implementation contract
FeeHandlerContractV1 = types.StringToAddress("0x1071")

// ChildERC20Contract is an address of bridgable ERC20 token contract on the child chain
ChildERC20Contract = types.StringToAddress("0x1003")
Expand Down Expand Up @@ -94,7 +98,6 @@ var (
BlockListBridgeAddr = types.StringToAddress("0x0300000000000000000000000000000000000004")

// Hydra: Additional system contracts and special addresses
FeeHandlerContract = types.StringToAddress("0x1012")
HydraBurnAddress = types.StringToAddress("0x0000000000000000000000000000000000000000")
LiquidityTokenContract = types.StringToAddress("0x1013")
)
Expand All @@ -108,6 +111,7 @@ func GetProxyImplementationMapping() map[types.Address]types.Address {
// L2StateSenderContract: L2StateSenderContractV1,
ValidatorSetContract: ValidatorSetContractV1,
RewardPoolContract: RewardPoolContractV1,
FeeHandlerContract: FeeHandlerContractV1,
// NativeERC20TokenContract: NativeERC20TokenContractV1,
// ChildERC20PredicateContract: ChildERC20PredicateContractV1,
// ChildERC721PredicateContract: ChildERC721PredicateContractV1,
Expand Down
2 changes: 1 addition & 1 deletion h_docs/submodules.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ We are working on a fork of the core contracts, so the submodule is update the w
Commands used for the module update:

```
git submodule set-url core-contracts https://github.com/R-Santev/core-contracts.git
git submodule set-url core-contracts https://github.com/Hydra-Chain/hydragon-core-contracts.git
```

```
Expand Down
Loading