From 271cb9b069c063b54433835f532cf86fa424d221 Mon Sep 17 00:00:00 2001 From: kstdl Date: Tue, 24 Jan 2023 12:07:54 +0100 Subject: [PATCH 1/2] feat: add dpos contract logs --- core/vm/evm.go | 4 + taraxa/state/dpos/precompiled/api.go | 5 +- .../state/dpos/precompiled/dpos_contract.go | 88 +++++++----- taraxa/state/dpos/precompiled/dpos_logs.go | 132 ++++++++++++++++++ .../dpos/solidity/dpos_contract_interface.sol | 12 ++ .../dpos_contract_solidity_structs.go | 10 +- taraxa/state/dpos/tests/dpos_test.go | 97 +++++++++++-- taraxa/state/dpos/tests/dpos_test_utils.go | 2 +- taraxa/state/state_dry_runner/dry_runner.go | 2 +- .../state_transition/state_transition.go | 2 +- 10 files changed, 294 insertions(+), 60 deletions(-) create mode 100644 taraxa/state/dpos/precompiled/dpos_logs.go diff --git a/core/vm/evm.go b/core/vm/evm.go index 36d393a0a..af86d9765 100644 --- a/core/vm/evm.go +++ b/core/vm/evm.go @@ -105,6 +105,10 @@ func (self *EVM) Init(get_hash GetHashFunc, state State, opts Opts, config param return self } +func (self *EVM) AddLog(log LogRecord) { + self.state.AddLog(log) +} + // func (self *EVM) GetRules() Rules { // return self.rules // } diff --git a/taraxa/state/dpos/precompiled/api.go b/taraxa/state/dpos/precompiled/api.go index e99b1d3fa..0674f5c2e 100644 --- a/taraxa/state/dpos/precompiled/api.go +++ b/taraxa/state/dpos/precompiled/api.go @@ -9,6 +9,7 @@ import ( "github.com/Taraxa-project/taraxa-evm/common" "github.com/Taraxa-project/taraxa-evm/core/types" + "github.com/Taraxa-project/taraxa-evm/core/vm" ) func ContractAddress() common.Address { @@ -111,8 +112,8 @@ func (self *API) UpdateConfig(blk_n types.BlockNum, cfg Config) { self.cfg = cfg } -func (self *API) NewContract(storage Storage, reader Reader) *Contract { - return new(Contract).Init(self.cfg, storage, reader) +func (self *API) NewContract(storage Storage, reader Reader, evm *vm.EVM) *Contract { + return new(Contract).Init(self.cfg, storage, reader, evm) } func (self *API) NewReader(blk_n types.BlockNum, storage_factory func(types.BlockNum) StorageReader) (ret Reader) { diff --git a/taraxa/state/dpos/precompiled/dpos_contract.go b/taraxa/state/dpos/precompiled/dpos_contract.go index 7b4e4ef4c..9b76d5cfe 100644 --- a/taraxa/state/dpos/precompiled/dpos_contract.go +++ b/taraxa/state/dpos/precompiled/dpos_contract.go @@ -131,6 +131,8 @@ type Contract struct { // ABI of the contract Abi abi.ABI + evm *vm.EVM + // Iterable storages validators Validators delegations Delegations @@ -150,10 +152,11 @@ type Contract struct { } // Initialize contract class -func (self *Contract) Init(cfg Config, storage Storage, readStorage Reader) *Contract { +func (self *Contract) Init(cfg Config, storage Storage, readStorage Reader, evm *vm.EVM) *Contract { self.cfg = cfg self.storage.Init(storage) self.delayedStorage = readStorage + self.evm = evm return self } @@ -177,7 +180,7 @@ func (self *Contract) Register(registry func(*common.Address, vm.PrecompiledCont func (self *Contract) RequiredGas(ctx vm.CallFrame, evm *vm.EVM) uint64 { // Init abi and some of the structures required for calculating gas, e.g. self.validators for getValidators self.lazy_init() - + method, err := self.Abi.MethodById(ctx.Input) if err != nil { return 0 @@ -432,7 +435,6 @@ func (self *Contract) Run(ctx vm.CallFrame, evm *vm.EVM) ([]byte, error) { return nil, self.setCommission(ctx, evm.GetBlock().Number, args) case "registerValidator": - var args sol.RegisterValidatorArgs if err = method.Inputs.Unpack(&args, input); err != nil { fmt.Println("Unable to parse registerValidator input args: ", err) @@ -713,6 +715,8 @@ func (self *Contract) delegate(ctx vm.CallFrame, block types.BlockNum, args sol. state.Count++ self.state_put(&state_k, state) self.validators.ModifyValidator(&args.Validator, validator) + self.evm.AddLog(MakeDelegatedLog(ctx.CallerAccount.Address(), &args.Validator, ctx.Value)) + return nil } @@ -788,11 +792,12 @@ func (self *Contract) undelegate(ctx vm.CallFrame, block types.BlockNum, args so self.state_put(&state_k, state) self.validators.ModifyValidator(&args.Validator, validator) } + self.evm.AddLog(MakeUndelegatedLog(ctx.CallerAccount.Address(), &args.Validator, args.Amount)) return nil } -// Removes undelegation from queue and moves staked toknes back to delegator +// Removes undelegation from queue and moves staked tokens back to delegator // This only works after lock-up period expires func (self *Contract) confirmUndelegate(ctx vm.CallFrame, block types.BlockNum, args sol.ValidatorAddressArgs) error { if !self.undelegations.UndelegationExists(ctx.CallerAccount.Address(), &args.Validator) { @@ -805,6 +810,8 @@ func (self *Contract) confirmUndelegate(ctx vm.CallFrame, block types.BlockNum, self.undelegations.RemoveUndelegation(ctx.CallerAccount.Address(), &args.Validator) // TODO slashing of balance ctx.CallerAccount.AddBalance(undelegation.Amount) + self.evm.AddLog(MakeUndelegateConfirmedLog(ctx.CallerAccount.Address(), &args.Validator, undelegation.Amount)) + return nil } @@ -859,6 +866,8 @@ func (self *Contract) cancelUndelegate(ctx vm.CallFrame, block types.BlockNum, a state.Count++ self.state_put(&state_k, state) self.validators.ModifyValidator(&args.Validator, validator) + self.evm.AddLog(MakeUndelegateCanceledLog(ctx.CallerAccount.Address(), &args.Validator, undelegation.Amount)) + return nil } @@ -937,45 +946,44 @@ func (self *Contract) redelegate(ctx vm.CallFrame, block types.BlockNum, args so } // Now we delegate - { - state, state_k := self.state_get(args.ValidatorTo[:], BlockToBytes(block)) - if state == nil { - old_state := self.state_get_and_decrement(args.ValidatorTo[:], BlockToBytes(validator_to.LastUpdated)) - state = new(State) - state.RewardsPer1Stake = bigutil.Add(old_state.RewardsPer1Stake, self.calculateRewardPer1Stake(validator_to.RewardsPool, validator_to.TotalStake)) - validator_to.RewardsPool = big.NewInt(0) - validator_to.LastUpdated = block - state.Count++ - } - - delegation := self.delegations.GetDelegation(ctx.CallerAccount.Address(), &args.ValidatorTo) + state, state_k := self.state_get(args.ValidatorTo[:], BlockToBytes(block)) + if state == nil { + old_state := self.state_get_and_decrement(args.ValidatorTo[:], BlockToBytes(validator_to.LastUpdated)) + state = new(State) + state.RewardsPer1Stake = bigutil.Add(old_state.RewardsPer1Stake, self.calculateRewardPer1Stake(validator_to.RewardsPool, validator_to.TotalStake)) + validator_to.RewardsPool = big.NewInt(0) + validator_to.LastUpdated = block + state.Count++ + } - if delegation == nil { - self.delegations.CreateDelegation(ctx.CallerAccount.Address(), &args.ValidatorTo, block, args.Amount) - validator_to.TotalStake.Add(validator_to.TotalStake, args.Amount) - } else { - // We need to claim rewards first - old_state := self.state_get_and_decrement(args.ValidatorTo[:], BlockToBytes(delegation.LastUpdated)) - reward_per_stake := bigutil.Sub(state.RewardsPer1Stake, old_state.RewardsPer1Stake) - ctx.CallerAccount.AddBalance(self.calculateDelegatorReward(reward_per_stake, delegation.Stake)) + delegation := self.delegations.GetDelegation(ctx.CallerAccount.Address(), &args.ValidatorTo) - delegation.Stake.Add(delegation.Stake, args.Amount) - delegation.LastUpdated = block - self.delegations.ModifyDelegation(ctx.CallerAccount.Address(), &args.ValidatorTo, delegation) + if delegation == nil { + self.delegations.CreateDelegation(ctx.CallerAccount.Address(), &args.ValidatorTo, block, args.Amount) + validator_to.TotalStake.Add(validator_to.TotalStake, args.Amount) + } else { + // We need to claim rewards first + old_state := self.state_get_and_decrement(args.ValidatorTo[:], BlockToBytes(delegation.LastUpdated)) + reward_per_stake := bigutil.Sub(state.RewardsPer1Stake, old_state.RewardsPer1Stake) + ctx.CallerAccount.AddBalance(self.calculateDelegatorReward(reward_per_stake, delegation.Stake)) - validator_to.TotalStake.Add(validator_to.TotalStake, args.Amount) - } + delegation.Stake.Add(delegation.Stake, args.Amount) + delegation.LastUpdated = block + self.delegations.ModifyDelegation(ctx.CallerAccount.Address(), &args.ValidatorTo, delegation) - new_vote_count := voteCount(validator_to.TotalStake, self.cfg.EligibilityBalanceThreshold, self.cfg.VoteEligibilityBalanceStep) - if prev_vote_count_to != new_vote_count { - self.eligible_vote_count -= prev_vote_count_to - self.eligible_vote_count = add64p(self.eligible_vote_count, new_vote_count) - } + validator_to.TotalStake.Add(validator_to.TotalStake, args.Amount) + } - state.Count++ - self.state_put(&state_k, state) - self.validators.ModifyValidator(&args.ValidatorTo, validator_to) + new_vote_count := voteCount(validator_to.TotalStake, self.cfg.EligibilityBalanceThreshold, self.cfg.VoteEligibilityBalanceStep) + if prev_vote_count_to != new_vote_count { + self.eligible_vote_count -= prev_vote_count_to + self.eligible_vote_count = add64p(self.eligible_vote_count, new_vote_count) } + + state.Count++ + self.state_put(&state_k, state) + self.validators.ModifyValidator(&args.ValidatorTo, validator_to) + self.evm.AddLog(MakeRedelegatedLog(ctx.CallerAccount.Address(), &args.ValidatorFrom, &args.ValidatorTo, args.Amount)) return nil } @@ -1010,6 +1018,7 @@ func (self *Contract) claimRewards(ctx vm.CallFrame, block types.BlockNum, args state.Count++ self.state_put(&state_k, state) + self.evm.AddLog(MakeRewardsClaimedLog(ctx.CallerAccount.Address(), &args.Validator)) return nil } @@ -1034,6 +1043,7 @@ func (self *Contract) claimCommissionRewards(ctx vm.CallFrame, block types.Block } else { self.validators.ModifyValidator(&args.Validator, validator) } + self.evm.AddLog(MakeComissionRewardsClaimedLog(ctx.CallerAccount.Address(), &args.Validator)) return nil } @@ -1101,8 +1111,10 @@ func (self *Contract) registerValidatorWithoutChecks(ctx vm.CallFrame, block typ // Creates validator related objects in storage validator := self.validators.CreateValidator(owner_address, &args.Validator, args.VrfKey, block, args.Commission, args.Description, args.Endpoint) state.Count++ + self.evm.AddLog(MakeValidatorRegisteredLog(&args.Validator)) if ctx.Value.Cmp(big.NewInt(0)) == 1 { + self.evm.AddLog(MakeDelegatedLog(owner_address, &args.Validator, ctx.Value)) self.delegations.CreateDelegation(owner_address, &args.Validator, block, ctx.Value) self.delegate_update_values(ctx, validator, 0) self.validators.ModifyValidator(&args.Validator, validator) @@ -1150,6 +1162,7 @@ func (self *Contract) setValidatorInfo(ctx vm.CallFrame, args sol.SetValidatorIn validator_info.Endpoint = args.Endpoint self.validators.ModifyValidatorInfo(&args.Validator, validator_info) + self.evm.AddLog(MakeValidatorInfoSetLog(&args.Validator)) return nil } @@ -1180,6 +1193,7 @@ func (self *Contract) setCommission(ctx vm.CallFrame, block types.BlockNum, args validator.Commission = args.Commission validator.LastCommissionChange = block self.validators.ModifyValidator(&args.Validator, validator) + self.evm.AddLog(MakeCommissionSetLog(&args.Validator, args.Commission)) return nil } diff --git a/taraxa/state/dpos/precompiled/dpos_logs.go b/taraxa/state/dpos/precompiled/dpos_logs.go new file mode 100644 index 000000000..786839b4a --- /dev/null +++ b/taraxa/state/dpos/precompiled/dpos_logs.go @@ -0,0 +1,132 @@ +package dpos + +import ( + "math/big" + + "github.com/Taraxa-project/taraxa-evm/common" + "github.com/Taraxa-project/taraxa-evm/core/vm" + "github.com/Taraxa-project/taraxa-evm/taraxa/util/keccak256" +) + +func getEventHash(str string) *common.Hash { + return keccak256.Hash([]byte(str)) +} + +// event Delegated(address indexed delegator, address indexed validator, uint256 amount); +var DelegatedEventHash = getEventHash("Delegated(address,address,uint256)") + +func MakeDelegatedLog(delegator, validator *common.Address, amount *big.Int) vm.LogRecord { + topics := make([]common.Hash, 3) + topics[0] = *DelegatedEventHash + topics[1] = delegator.Hash() + topics[2] = validator.Hash() + + return vm.LogRecord{Address: *contract_address, Topics: topics, Data: amount.Bytes()} +} + +// event Undelegated(address indexed delegator, address indexed validator, uint256 amount); +var UndelegatedEventHash = getEventHash("Undelegated(address,address,uint256)") + +func MakeUndelegatedLog(delegator, validator *common.Address, amount *big.Int) vm.LogRecord { + topics := make([]common.Hash, 3) + topics[0] = *UndelegatedEventHash + topics[1] = delegator.Hash() + topics[2] = validator.Hash() + + return vm.LogRecord{Address: *contract_address, Topics: topics, Data: amount.Bytes()} +} + +// event UndelegateConfirmed(address indexed delegator, address indexed validator, uint256 amount); +var UndelegateConfirmedEventHash = getEventHash("UndelegateConfirmed(address,uint256)") + +func MakeUndelegateConfirmedLog(delegator, validator *common.Address, amount *big.Int) vm.LogRecord { + topics := make([]common.Hash, 3) + topics[0] = *UndelegateConfirmedEventHash + topics[1] = delegator.Hash() + topics[2] = validator.Hash() + + return vm.LogRecord{Address: *contract_address, Topics: topics, Data: amount.Bytes()} +} + +// event UndelegateCanceled(address indexed delegator, address indexed validator, uint256 amount); +var UndelegateCanceledEventHash = getEventHash("UndelegateCanceled(address,uint256)") + +func MakeUndelegateCanceledLog(delegator, validator *common.Address, amount *big.Int) vm.LogRecord { + topics := make([]common.Hash, 3) + topics[0] = *UndelegateCanceledEventHash + topics[1] = delegator.Hash() + topics[2] = validator.Hash() + + return vm.LogRecord{Address: *contract_address, Topics: topics, Data: amount.Bytes()} +} + +// event Redelegated(address indexed delegator, address indexed from, address indexed to, uint256 amount); +var RedelegatedEventHash = getEventHash("UndelegateCanceled(address,address,address,uint256)") + +func MakeRedelegatedLog(delegator, from, to *common.Address, amount *big.Int) vm.LogRecord { + topics := make([]common.Hash, 4) + topics[0] = *RedelegatedEventHash + topics[1] = delegator.Hash() + topics[2] = from.Hash() + topics[3] = to.Hash() + + return vm.LogRecord{Address: *contract_address, Topics: topics, Data: amount.Bytes()} +} + +// event RewardsClaimed(address indexed account, address indexed validator); +var RewardsClaimedEventHash = getEventHash("RewardsClaimed(address,address)") + +func MakeRewardsClaimedLog(account, validator *common.Address) vm.LogRecord { + topics := make([]common.Hash, 3) + topics[0] = *RewardsClaimedEventHash + topics[1] = account.Hash() + topics[2] = validator.Hash() + + return vm.LogRecord{Address: *contract_address, Topics: topics, Data: []byte{}} +} + +// event CommissionRewardsClaimed(address indexed account, address indexed validator); +var ComissionRewardsClaimedEventHash = getEventHash("CommissionRewardsClaimed(address,address)") + +func MakeComissionRewardsClaimedLog(account, validator *common.Address) vm.LogRecord { + topics := make([]common.Hash, 3) + topics[0] = *ComissionRewardsClaimedEventHash + topics[1] = account.Hash() + topics[2] = validator.Hash() + + return vm.LogRecord{Address: *contract_address, Topics: topics, Data: []byte{}} +} + +// event CommissionSet(address indexed validator, uint16 comission); +var CommissionSetEventHash = getEventHash("CommissionSet(address,uint16)") + +func MakeCommissionSetLog(account *common.Address, amount uint16) vm.LogRecord { + topics := make([]common.Hash, 2) + topics[0] = *CommissionSetEventHash + topics[1] = account.Hash() + + big_amount := big.NewInt(int64(amount)) + return vm.LogRecord{Address: *contract_address, Topics: topics, Data: big_amount.Bytes()} +} + +// event ValidatorRegistered(address indexed validator); +var ValidatorRegisteredEventHash = getEventHash("ValidatorRegistered(address)") + +func MakeValidatorRegisteredLog(account *common.Address) vm.LogRecord { + topics := make([]common.Hash, 2) + topics[0] = *ValidatorRegisteredEventHash + topics[1] = account.Hash() + + return vm.LogRecord{Address: *contract_address, Topics: topics, Data: []byte{}} +} + +// event ValidatorInfoSet(address indexed validator); +var ValidatorInfoSetEventHash = getEventHash("ValidatorInfoSet(address)") + +func MakeValidatorInfoSetLog(account *common.Address) vm.LogRecord { + topics := make([]common.Hash, 2) + topics[0] = *ValidatorInfoSetEventHash + topics[1] = account.Hash() + + return vm.LogRecord{Address: *contract_address, Topics: topics, Data: []byte{}} +} diff --git a/taraxa/state/dpos/solidity/dpos_contract_interface.sol b/taraxa/state/dpos/solidity/dpos_contract_interface.sol index 58576dce8..7c6d1f7c5 100644 --- a/taraxa/state/dpos/solidity/dpos_contract_interface.sol +++ b/taraxa/state/dpos/solidity/dpos_contract_interface.sol @@ -4,6 +4,18 @@ pragma solidity >=0.8.0; interface DposInterface { + + event Delegated(address indexed delegator, address indexed validator, uint256 amount); + event Undelegated(address indexed delegator, address indexed validator, uint256 amount); + event UndelegateConfirmed(address indexed delegator, address indexed validator, uint256 amount); + event UndelegateCanceled(address indexed delegator, address indexed validator, uint256 amount); + event Redelegated(address indexed delegator, address indexed from, address indexed to, uint256 amount); + event RewardsClaimed(address indexed account, address indexed validator); + event CommissionRewardsClaimed(address indexed account, address indexed validator); + event CommissionSet(address indexed validator, uint16 comission); + event ValidatorRegistered(address indexed validator); + event ValidatorInfoSet(address indexed validator); + struct ValidatorBasicInfo { // Total number of delegated tokens to the validator uint256 total_stake; diff --git a/taraxa/state/dpos/solidity/dpos_contract_solidity_structs.go b/taraxa/state/dpos/solidity/dpos_contract_solidity_structs.go index 90dbd6bc6..52a8247b3 100644 --- a/taraxa/state/dpos/solidity/dpos_contract_solidity_structs.go +++ b/taraxa/state/dpos/solidity/dpos_contract_solidity_structs.go @@ -2,14 +2,14 @@ // // For automatic generation & copy paste struct: // 1. To generate ABI: -// a) run solc --abi --overwrite --optimize dpos_contract_interface.sol --output-dir . +// a) run `solc --abi --overwrite --optimize dpos_contract_interface.sol --output-dir .` // b) replace " by \" and copy&paste the ABI string into the TaraxaDposClientMetaData -// c) remove generated file rm DposInterface.abi +// c) remove generated file `rm DposInterface.abi` // -// 2. To generate dolidity interfacee related structs: -// a) run abigen --abi=abi/DposInterface.abi --pkg=taraxaDposClient --out=dpos_contract_interface.go +// 2. To generate solidity interface related structs: +// a) run `abigen --abi=abi/DposInterface.abi --pkg=taraxaDposClient --out=dpos_contract_interface.go` // b) copy selected structs into this file -// c) remove generated file rm dpos_contract_interface.go +// c) remove generated file `rm dpos_contract_interface.go` package sol diff --git a/taraxa/state/dpos/tests/dpos_test.go b/taraxa/state/dpos/tests/dpos_test.go index 2a6cc2807..e5f5fd45b 100644 --- a/taraxa/state/dpos/tests/dpos_test.go +++ b/taraxa/state/dpos/tests/dpos_test.go @@ -15,6 +15,7 @@ import ( "github.com/Taraxa-project/taraxa-evm/taraxa/util" "github.com/Taraxa-project/taraxa-evm/taraxa/util/bigutil" "github.com/Taraxa-project/taraxa-evm/taraxa/util/keccak256" + "github.com/Taraxa-project/taraxa-evm/taraxa/util/tests" ) func TestProof(t *testing.T) { @@ -79,7 +80,7 @@ func TestDelegateMinMax(t *testing.T) { } func TestRedelegate(t *testing.T) { - _, test := init_test(t, CopyDefaultChainConfig()) + tc, test := init_test(t, CopyDefaultChainConfig()) defer test.end() validator1_owner := addr(1) @@ -88,9 +89,16 @@ func TestRedelegate(t *testing.T) { validator2_owner := addr(2) validator2_addr, validator2_proof := generateAddrAndProof() - test.ExecuteAndCheck(validator1_owner, DefaultMinimumDeposit, test.pack("registerValidator", validator1_addr, validator1_proof, DefaultVrfKey, uint16(10), "test", "test"), util.ErrorString(""), util.ErrorString("")) + reg_res := test.ExecuteAndCheck(validator1_owner, DefaultMinimumDeposit, test.pack("registerValidator", validator1_addr, validator1_proof, DefaultVrfKey, uint16(10), "test", "test"), util.ErrorString(""), util.ErrorString("")) + tc.Assert.Equal(len(reg_res.Logs), 2) + tc.Assert.Equal(reg_res.Logs[0].Topics[0], *dpos.ValidatorRegisteredEventHash) + tc.Assert.Equal(reg_res.Logs[1].Topics[0], *dpos.DelegatedEventHash) + test.ExecuteAndCheck(validator2_owner, DefaultMinimumDeposit, test.pack("registerValidator", validator2_addr, validator2_proof, DefaultVrfKey, uint16(10), "test", "test"), util.ErrorString(""), util.ErrorString("")) - test.ExecuteAndCheck(validator1_owner, big.NewInt(0), test.pack("reDelegate", validator1_addr, validator2_addr, DefaultMinimumDeposit), util.ErrorString(""), util.ErrorString("")) + redelegate_res := test.ExecuteAndCheck(validator1_owner, big.NewInt(0), test.pack("reDelegate", validator1_addr, validator2_addr, DefaultMinimumDeposit), util.ErrorString(""), util.ErrorString("")) + tc.Assert.Equal(len(redelegate_res.Logs), 1) + tc.Assert.Equal(redelegate_res.Logs[0].Topics[0], *dpos.RedelegatedEventHash) + //Validator 1 does not exist as we withdraw all stake test.ExecuteAndCheck(validator1_owner, big.NewInt(0), test.pack("reDelegate", validator1_addr, validator2_addr, DefaultMinimumDeposit), dpos.ErrNonExistentValidator, util.ErrorString("")) @@ -108,7 +116,7 @@ func TestRedelegate(t *testing.T) { test.ExecuteAndCheck(validator1_owner, big.NewInt(0), test.pack("reDelegate", validator1_addr, validator2_addr, DefaultMinimumDeposit), util.ErrorString(""), util.ErrorString("")) // OK test.ExecuteAndCheck(validator1_owner, big.NewInt(0), test.pack("reDelegate", validator1_addr, validator2_addr, DefaultMinimumDeposit), util.ErrorString(""), util.ErrorString("")) - // Validator 1 does not exist as we widthrawl all stake + // Validator 1 does not exist as we withdraw all stake test.ExecuteAndCheck(validator1_owner, big.NewInt(0), test.pack("reDelegate", validator1_addr, validator2_addr, DefaultMinimumDeposit), dpos.ErrNonExistentValidator, util.ErrorString("")) } @@ -133,7 +141,7 @@ func TestRedelegateMinMax(t *testing.T) { } func TestUndelegate(t *testing.T) { - _, test := init_test(t, CopyDefaultChainConfig()) + tc, test := init_test(t, CopyDefaultChainConfig()) defer test.end() val_owner := addr(1) val_addr, proof := generateAddrAndProof() @@ -141,7 +149,9 @@ func TestUndelegate(t *testing.T) { delegator_addr := addr(2) test.ExecuteAndCheck(val_owner, DefaultMinimumDeposit, test.pack("registerValidator", val_addr, proof, DefaultVrfKey, uint16(10), "test", "test"), util.ErrorString(""), util.ErrorString("")) - test.ExecuteAndCheck(val_owner, big.NewInt(0), test.pack("undelegate", val_addr, DefaultMinimumDeposit), util.ErrorString(""), util.ErrorString("")) + undelegate_res := test.ExecuteAndCheck(val_owner, big.NewInt(0), test.pack("undelegate", val_addr, DefaultMinimumDeposit), util.ErrorString(""), util.ErrorString("")) + tc.Assert.Equal(len(undelegate_res.Logs), 1) + tc.Assert.Equal(undelegate_res.Logs[0].Topics[0], *dpos.UndelegatedEventHash) // NonExistentValidator as it was deleted test.ExecuteAndCheck(delegator_addr, big.NewInt(0), test.pack("undelegate", val_addr, DefaultMinimumDeposit), dpos.ErrNonExistentValidator, util.ErrorString("")) test.ExecuteAndCheck(val_owner, DefaultMinimumDeposit, test.pack("registerValidator", val_addr, proof, DefaultVrfKey, uint16(10), "test", "test"), util.ErrorString(""), util.ErrorString("")) @@ -157,7 +167,7 @@ func TestUndelegate(t *testing.T) { } func TestConfirmUndelegate(t *testing.T) { - _, test := init_test(t, CopyDefaultChainConfig()) + tc, test := init_test(t, CopyDefaultChainConfig()) defer test.end() val_owner := addr(1) @@ -178,7 +188,9 @@ func TestConfirmUndelegate(t *testing.T) { test.AdvanceBlock(nil, nil, nil) test.AdvanceBlock(nil, nil, nil) - test.ExecuteAndCheck(delegator_addr, big.NewInt(0), test.pack("confirmUndelegate", val_addr), util.ErrorString(""), util.ErrorString("")) + confirm_res := test.ExecuteAndCheck(delegator_addr, big.NewInt(0), test.pack("confirmUndelegate", val_addr), util.ErrorString(""), util.ErrorString("")) + tc.Assert.Equal(len(confirm_res.Logs), 1) + tc.Assert.Equal(confirm_res.Logs[0].Topics[0], *dpos.UndelegateConfirmedEventHash) // ErrNonExistentDelegation test.ExecuteAndCheck(delegator_addr, big.NewInt(0), test.pack("undelegate", val_addr, DefaultMinimumDeposit), dpos.ErrNonExistentDelegation, util.ErrorString("")) @@ -218,7 +230,9 @@ func TestCancelUndelegate(t *testing.T) { tc.Assert.Equal(DefaultMinimumDeposit, validator.ValidatorInfo.TotalStake) // Cancel undelegate and check if validator's total stake was increased again - test.ExecuteAndCheck(delegator_addr, big.NewInt(0), test.pack("cancelUndelegate", val_addr), util.ErrorString(""), util.ErrorString("")) + cancel_res := test.ExecuteAndCheck(delegator_addr, big.NewInt(0), test.pack("cancelUndelegate", val_addr), util.ErrorString(""), util.ErrorString("")) + tc.Assert.Equal(len(cancel_res.Logs), 1) + tc.Assert.Equal(cancel_res.Logs[0].Topics[0], *dpos.UndelegateCanceledEventHash) test.ExecuteAndCheck(delegator_addr, big.NewInt(0), test.pack("getValidator", val_addr), util.ErrorString(""), util.ErrorString("")) validator_raw = test.ExecuteAndCheck(delegator_addr, big.NewInt(0), test.pack("getValidator", val_addr), util.ErrorString(""), util.ErrorString("")) validator = new(GetValidatorRet) @@ -428,8 +442,12 @@ func TestRewardsAndCommission(t *testing.T) { test.ExecuteAndCheck(delegator1_addr, big.NewInt(0), test.pack("claimRewards", validator1_addr), util.ErrorString(""), util.ErrorString("")) test.ExecuteAndCheck(delegator2_addr, big.NewInt(0), test.pack("claimRewards", validator2_addr), util.ErrorString(""), util.ErrorString("")) - test.ExecuteAndCheck(delegator3_addr, big.NewInt(0), test.pack("claimRewards", validator2_addr), util.ErrorString(""), util.ErrorString("")) - test.ExecuteAndCheck(delegator4_addr, big.NewInt(0), test.pack("claimRewards", validator4_addr), util.ErrorString(""), util.ErrorString("")) + { + test.ExecuteAndCheck(delegator3_addr, big.NewInt(0), test.pack("claimRewards", validator2_addr), util.ErrorString(""), util.ErrorString("")) + clam_res := test.ExecuteAndCheck(delegator4_addr, big.NewInt(0), test.pack("claimRewards", validator4_addr), util.ErrorString(""), util.ErrorString("")) + tc.Assert.Equal(len(clam_res.Logs), 1) + tc.Assert.Equal(clam_res.Logs[0].Topics[0], *dpos.RewardsClaimedEventHash) + } actual_delegator1_reward := bigutil.Sub(test.GetBalance(delegator1_addr), delegator1_old_balance) actual_delegator2_reward := bigutil.Sub(test.GetBalance(delegator2_addr), delegator2_old_balance) @@ -448,7 +466,11 @@ func TestRewardsAndCommission(t *testing.T) { test.ExecuteAndCheck(delegator1_addr, big.NewInt(0), test.pack("claimCommissionRewards", validator1_addr), util.ErrorString(""), util.ErrorString("")) test.ExecuteAndCheck(delegator2_addr, big.NewInt(0), test.pack("claimCommissionRewards", validator2_addr), util.ErrorString(""), util.ErrorString("")) - test.ExecuteAndCheck(delegator4_addr, big.NewInt(0), test.pack("claimCommissionRewards", validator4_addr), util.ErrorString(""), util.ErrorString("")) + { + claim_res := test.ExecuteAndCheck(delegator4_addr, big.NewInt(0), test.pack("claimCommissionRewards", validator4_addr), util.ErrorString(""), util.ErrorString("")) + tc.Assert.Equal(len(claim_res.Logs), 1) + tc.Assert.Equal(claim_res.Logs[0].Topics[0], *dpos.ComissionRewardsClaimedEventHash) + } actual_validator1_commission_reward := bigutil.Sub(test.GetBalance(validator1_owner), validator1_old_balance) actual_validator2_commission_reward := bigutil.Sub(test.GetBalance(validator2_owner), validator2_old_balance) @@ -501,7 +523,11 @@ func TestSetValidatorInfo(t *testing.T) { tc.Assert.Equal("test_endpoint", validator.ValidatorInfo.Endpoint) // Change description & endpoint and see it getValidator returns changed values - test.ExecuteAndCheck(val_owner, big.NewInt(0), test.pack("setValidatorInfo", val_addr, "modified_description", "modified_endpoint"), util.ErrorString(""), util.ErrorString("")) + { + set_res := test.ExecuteAndCheck(val_owner, big.NewInt(0), test.pack("setValidatorInfo", val_addr, "modified_description", "modified_endpoint"), util.ErrorString(""), util.ErrorString("")) + tc.Assert.Equal(len(set_res.Logs), 1) + tc.Assert.Equal(set_res.Logs[0].Topics[0], *dpos.ValidatorInfoSetEventHash) + } validator_raw = test.ExecuteAndCheck(val_addr, big.NewInt(0), test.pack("getValidator", val_addr), util.ErrorString(""), util.ErrorString("")) validator = new(GetValidatorRet) test.unpack(validator, "getValidator", validator_raw.CodeRetval) @@ -1253,3 +1279,48 @@ func TestUndelegationsClass(t *testing.T) { tc.Assert.Equal(uint32(1), undelegations.GetUndelegationsCount(&delegator1_addr)) tc.Assert.Equal(false, undelegations.UndelegationExists(&delegator1_addr, &validator1_addr)) } + +func TestMakeLogsCheckTopics(t *testing.T) { + tc := tests.NewTestCtx(t) + amount := big.NewInt(0) + { + log := dpos.MakeDelegatedLog(&common.ZeroAddress, &common.ZeroAddress, amount) + tc.Assert.Equal(len(log.Topics), 3) + } + { + log := dpos.MakeUndelegatedLog(&common.ZeroAddress, &common.ZeroAddress, amount) + tc.Assert.Equal(len(log.Topics), 3) + } + { + log := dpos.MakeUndelegateConfirmedLog(&common.ZeroAddress, &common.ZeroAddress, amount) + tc.Assert.Equal(len(log.Topics), 3) + } + { + log := dpos.MakeUndelegateCanceledLog(&common.ZeroAddress, &common.ZeroAddress, amount) + tc.Assert.Equal(len(log.Topics), 3) + } + { + log := dpos.MakeRedelegatedLog(&common.ZeroAddress, &common.ZeroAddress, &common.ZeroAddress, amount) + tc.Assert.Equal(len(log.Topics), 4) + } + { + log := dpos.MakeRewardsClaimedLog(&common.ZeroAddress, &common.ZeroAddress) + tc.Assert.Equal(len(log.Topics), 3) + } + { + log := dpos.MakeComissionRewardsClaimedLog(&common.ZeroAddress, &common.ZeroAddress) + tc.Assert.Equal(len(log.Topics), 3) + } + { + log := dpos.MakeCommissionSetLog(&common.ZeroAddress, 0) + tc.Assert.Equal(len(log.Topics), 2) + } + { + log := dpos.MakeValidatorRegisteredLog(&common.ZeroAddress) + tc.Assert.Equal(len(log.Topics), 2) + } + { + log := dpos.MakeValidatorInfoSetLog(&common.ZeroAddress) + tc.Assert.Equal(len(log.Topics), 2) + } +} diff --git a/taraxa/state/dpos/tests/dpos_test_utils.go b/taraxa/state/dpos/tests/dpos_test_utils.go index 1be9c7b80..f3a63999a 100644 --- a/taraxa/state/dpos/tests/dpos_test_utils.go +++ b/taraxa/state/dpos/tests/dpos_test_utils.go @@ -200,7 +200,7 @@ func (self *DposTest) GetDPOSReader() dpos.Reader { return self.SUT.DPOSReader(self.blk_n) } -func (self *DposTest) ExecuteAndCheck(from common.Address, value *big.Int, input []byte, exe_err util.ErrorString, cons_err util.ErrorString) vm.ExecutionResult { +func (self *DposTest) ExecuteAndCheck(from common.Address, value *big.Int, input []byte, exe_err, cons_err util.ErrorString) vm.ExecutionResult { res := self.execute(from, value, input) self.tc.Assert.Equal(cons_err, res.ConsensusErr) self.tc.Assert.Equal(exe_err, res.ExecutionErr) diff --git a/taraxa/state/state_dry_runner/dry_runner.go b/taraxa/state/state_dry_runner/dry_runner.go index f0b65eb40..c6191ec24 100644 --- a/taraxa/state/state_dry_runner/dry_runner.go +++ b/taraxa/state/state_dry_runner/dry_runner.go @@ -51,7 +51,7 @@ func (self *DryRunner) Apply(blk *vm.Block, trx *vm.Transaction) vm.ExecutionRes evm.Init(self.get_block_hash, &evm_state, vm.Opts{}, self.chain_config.EVMChainConfig) evm.SetBlock(blk /*, self.chain_config.EVMChainConfig.Rules(blk.Number)*/) if self.dpos_api != nil { - self.dpos_api.NewContract(dpos.EVMStateStorage{&evm_state}, self.get_reader(blk.Number)).Register(evm.RegisterPrecompiledContract) + self.dpos_api.NewContract(dpos.EVMStateStorage{&evm_state}, self.get_reader(blk.Number), &evm).Register(evm.RegisterPrecompiledContract) } return evm.Main(trx) } diff --git a/taraxa/state/state_transition/state_transition.go b/taraxa/state/state_transition/state_transition.go index dad7d5a39..6bbfaa5d7 100644 --- a/taraxa/state/state_transition/state_transition.go +++ b/taraxa/state/state_transition/state_transition.go @@ -53,7 +53,7 @@ func (self *StateTransition) Init( state_desc := state.GetCommittedDescriptor() self.trie_sink.Init(&state_desc.StateRoot, opts.Trie) if dpos_api != nil { - self.dpos_contract = dpos_api.NewContract(dpos.EVMStateStorage{&self.evm_state}, get_reader(state_desc.BlockNum)) + self.dpos_contract = dpos_api.NewContract(dpos.EVMStateStorage{&self.evm_state}, get_reader(state_desc.BlockNum), &self.evm) } if state_common.IsEmptyStateRoot(&state_desc.StateRoot) { self.begin_block() From 0d93aadac1a2b3cb170b98cfab9e47494cadfd59 Mon Sep 17 00:00:00 2001 From: kstdl Date: Thu, 26 Jan 2023 18:57:20 +0100 Subject: [PATCH 2/2] feat: add events to abi and pack events from it --- accounts/abi/event.go | 28 ++++ .../state/dpos/precompiled/dpos_contract.go | 29 ++-- taraxa/state/dpos/precompiled/dpos_logs.go | 134 +++++++----------- .../dpos_contract_solidity_structs.go | 2 +- taraxa/state/dpos/tests/dpos_test.go | 90 ++++++++---- 5 files changed, 156 insertions(+), 127 deletions(-) diff --git a/accounts/abi/event.go b/accounts/abi/event.go index 7bf28a5a2..42e001de1 100644 --- a/accounts/abi/event.go +++ b/accounts/abi/event.go @@ -18,9 +18,11 @@ package abi import ( "fmt" + "reflect" "strings" "github.com/Taraxa-project/taraxa-evm/common" + "github.com/Taraxa-project/taraxa-evm/core/vm" "github.com/Taraxa-project/taraxa-evm/crypto" ) @@ -55,3 +57,29 @@ func (e Event) Id() common.Hash { } return common.BytesToHash(crypto.Keccak256([]byte(fmt.Sprintf("%v(%v)", e.Name, strings.Join(types, ","))))) } + +func (e Event) MakeLog(args ...interface{}) (*vm.LogRecord, error) { + if len(e.Inputs) != len(args) { + return nil, fmt.Errorf("MakeLog: %v: expected %v arguments, but got %v", e.Name, len(e.Inputs), len(args)) + } + log := new(vm.LogRecord) + log.Topics = append(log.Topics, e.Id()) + data_set := false + for index, input := range e.Inputs { + bytes, err := input.Type.pack(reflect.ValueOf(args[index])) + if err != nil { + return nil, err + } + if input.Indexed { + log.Topics = append(log.Topics, common.BytesToHash(bytes)) + } else { + if data_set { + return nil, fmt.Errorf("Only one not indexed param is supported right now") + } + data_set = true + log.Data = bytes + } + + } + return log, nil +} diff --git a/taraxa/state/dpos/precompiled/dpos_contract.go b/taraxa/state/dpos/precompiled/dpos_contract.go index 9b76d5cfe..11af2d473 100644 --- a/taraxa/state/dpos/precompiled/dpos_contract.go +++ b/taraxa/state/dpos/precompiled/dpos_contract.go @@ -129,9 +129,9 @@ type Contract struct { // delayed storage for PBFT delayedStorage Reader // ABI of the contract - Abi abi.ABI - - evm *vm.EVM + Abi abi.ABI + logs Logs + evm *vm.EVM // Iterable storages validators Validators @@ -282,6 +282,7 @@ func (self *Contract) lazy_init() { } self.Abi, _ = abi.JSON(strings.NewReader(sol.TaraxaDposClientMetaData)) + self.logs = *new(Logs).Init(self.Abi.Events) self.validators.Init(&self.storage, field_validators) self.delegations.Init(&self.storage, field_delegations) @@ -715,7 +716,7 @@ func (self *Contract) delegate(ctx vm.CallFrame, block types.BlockNum, args sol. state.Count++ self.state_put(&state_k, state) self.validators.ModifyValidator(&args.Validator, validator) - self.evm.AddLog(MakeDelegatedLog(ctx.CallerAccount.Address(), &args.Validator, ctx.Value)) + self.evm.AddLog(self.logs.MakeDelegatedLog(ctx.CallerAccount.Address(), &args.Validator, ctx.Value)) return nil } @@ -792,7 +793,7 @@ func (self *Contract) undelegate(ctx vm.CallFrame, block types.BlockNum, args so self.state_put(&state_k, state) self.validators.ModifyValidator(&args.Validator, validator) } - self.evm.AddLog(MakeUndelegatedLog(ctx.CallerAccount.Address(), &args.Validator, args.Amount)) + self.evm.AddLog(self.logs.MakeUndelegatedLog(ctx.CallerAccount.Address(), &args.Validator, args.Amount)) return nil } @@ -810,7 +811,7 @@ func (self *Contract) confirmUndelegate(ctx vm.CallFrame, block types.BlockNum, self.undelegations.RemoveUndelegation(ctx.CallerAccount.Address(), &args.Validator) // TODO slashing of balance ctx.CallerAccount.AddBalance(undelegation.Amount) - self.evm.AddLog(MakeUndelegateConfirmedLog(ctx.CallerAccount.Address(), &args.Validator, undelegation.Amount)) + self.evm.AddLog(self.logs.MakeUndelegateConfirmedLog(ctx.CallerAccount.Address(), &args.Validator, undelegation.Amount)) return nil } @@ -866,7 +867,7 @@ func (self *Contract) cancelUndelegate(ctx vm.CallFrame, block types.BlockNum, a state.Count++ self.state_put(&state_k, state) self.validators.ModifyValidator(&args.Validator, validator) - self.evm.AddLog(MakeUndelegateCanceledLog(ctx.CallerAccount.Address(), &args.Validator, undelegation.Amount)) + self.evm.AddLog(self.logs.MakeUndelegateCanceledLog(ctx.CallerAccount.Address(), &args.Validator, undelegation.Amount)) return nil } @@ -983,7 +984,7 @@ func (self *Contract) redelegate(ctx vm.CallFrame, block types.BlockNum, args so state.Count++ self.state_put(&state_k, state) self.validators.ModifyValidator(&args.ValidatorTo, validator_to) - self.evm.AddLog(MakeRedelegatedLog(ctx.CallerAccount.Address(), &args.ValidatorFrom, &args.ValidatorTo, args.Amount)) + self.evm.AddLog(self.logs.MakeRedelegatedLog(ctx.CallerAccount.Address(), &args.ValidatorFrom, &args.ValidatorTo, args.Amount)) return nil } @@ -1018,7 +1019,7 @@ func (self *Contract) claimRewards(ctx vm.CallFrame, block types.BlockNum, args state.Count++ self.state_put(&state_k, state) - self.evm.AddLog(MakeRewardsClaimedLog(ctx.CallerAccount.Address(), &args.Validator)) + self.evm.AddLog(self.logs.MakeRewardsClaimedLog(ctx.CallerAccount.Address(), &args.Validator)) return nil } @@ -1043,7 +1044,7 @@ func (self *Contract) claimCommissionRewards(ctx vm.CallFrame, block types.Block } else { self.validators.ModifyValidator(&args.Validator, validator) } - self.evm.AddLog(MakeComissionRewardsClaimedLog(ctx.CallerAccount.Address(), &args.Validator)) + self.evm.AddLog(self.logs.MakeCommissionRewardsClaimedLog(ctx.CallerAccount.Address(), &args.Validator)) return nil } @@ -1111,10 +1112,10 @@ func (self *Contract) registerValidatorWithoutChecks(ctx vm.CallFrame, block typ // Creates validator related objects in storage validator := self.validators.CreateValidator(owner_address, &args.Validator, args.VrfKey, block, args.Commission, args.Description, args.Endpoint) state.Count++ - self.evm.AddLog(MakeValidatorRegisteredLog(&args.Validator)) + self.evm.AddLog(self.logs.MakeValidatorRegisteredLog(&args.Validator)) if ctx.Value.Cmp(big.NewInt(0)) == 1 { - self.evm.AddLog(MakeDelegatedLog(owner_address, &args.Validator, ctx.Value)) + self.evm.AddLog(self.logs.MakeDelegatedLog(owner_address, &args.Validator, ctx.Value)) self.delegations.CreateDelegation(owner_address, &args.Validator, block, ctx.Value) self.delegate_update_values(ctx, validator, 0) self.validators.ModifyValidator(&args.Validator, validator) @@ -1162,7 +1163,7 @@ func (self *Contract) setValidatorInfo(ctx vm.CallFrame, args sol.SetValidatorIn validator_info.Endpoint = args.Endpoint self.validators.ModifyValidatorInfo(&args.Validator, validator_info) - self.evm.AddLog(MakeValidatorInfoSetLog(&args.Validator)) + self.evm.AddLog(self.logs.MakeValidatorInfoSetLog(&args.Validator)) return nil } @@ -1193,7 +1194,7 @@ func (self *Contract) setCommission(ctx vm.CallFrame, block types.BlockNum, args validator.Commission = args.Commission validator.LastCommissionChange = block self.validators.ModifyValidator(&args.Validator, validator) - self.evm.AddLog(MakeCommissionSetLog(&args.Validator, args.Commission)) + self.evm.AddLog(self.logs.MakeCommissionSetLog(&args.Validator, args.Commission)) return nil } diff --git a/taraxa/state/dpos/precompiled/dpos_logs.go b/taraxa/state/dpos/precompiled/dpos_logs.go index 786839b4a..0a3039b89 100644 --- a/taraxa/state/dpos/precompiled/dpos_logs.go +++ b/taraxa/state/dpos/precompiled/dpos_logs.go @@ -3,130 +3,98 @@ package dpos import ( "math/big" + "github.com/Taraxa-project/taraxa-evm/accounts/abi" "github.com/Taraxa-project/taraxa-evm/common" "github.com/Taraxa-project/taraxa-evm/core/vm" - "github.com/Taraxa-project/taraxa-evm/taraxa/util/keccak256" ) -func getEventHash(str string) *common.Hash { - return keccak256.Hash([]byte(str)) +func checkError(log *vm.LogRecord, err error) *vm.LogRecord { + if err != nil { + panic("Update logs methods to correspond ABI: " + err.Error()) + } + return log } -// event Delegated(address indexed delegator, address indexed validator, uint256 amount); -var DelegatedEventHash = getEventHash("Delegated(address,address,uint256)") +type Logs struct { + Events map[string]abi.Event +} -func MakeDelegatedLog(delegator, validator *common.Address, amount *big.Int) vm.LogRecord { - topics := make([]common.Hash, 3) - topics[0] = *DelegatedEventHash - topics[1] = delegator.Hash() - topics[2] = validator.Hash() +func (self *Logs) Init(events map[string]abi.Event) *Logs { + self.Events = events - return vm.LogRecord{Address: *contract_address, Topics: topics, Data: amount.Bytes()} + return self } -// event Undelegated(address indexed delegator, address indexed validator, uint256 amount); -var UndelegatedEventHash = getEventHash("Undelegated(address,address,uint256)") +// All Make functions below are making log records for events. +// All hashes and data types should be the same as we have in solidity interface in ../solidity/dpos_contract_interface.sol +// If some event will be added or changed TestMakeLogsCheckTopics test should be modified -func MakeUndelegatedLog(delegator, validator *common.Address, amount *big.Int) vm.LogRecord { - topics := make([]common.Hash, 3) - topics[0] = *UndelegatedEventHash - topics[1] = delegator.Hash() - topics[2] = validator.Hash() +// event Delegated(address indexed delegator, address indexed validator, uint256 amount); +func (self *Logs) MakeDelegatedLog(delegator, validator *common.Address, amount *big.Int) vm.LogRecord { + event := self.Events["Delegated"] - return vm.LogRecord{Address: *contract_address, Topics: topics, Data: amount.Bytes()} + return *checkError(event.MakeLog(delegator, validator, amount)) } -// event UndelegateConfirmed(address indexed delegator, address indexed validator, uint256 amount); -var UndelegateConfirmedEventHash = getEventHash("UndelegateConfirmed(address,uint256)") +// event Undelegated(address indexed delegator, address indexed validator, uint256 amount); +func (self *Logs) MakeUndelegatedLog(delegator, validator *common.Address, amount *big.Int) vm.LogRecord { + event := self.Events["Undelegated"] + + return *checkError(event.MakeLog(delegator, validator, amount)) +} -func MakeUndelegateConfirmedLog(delegator, validator *common.Address, amount *big.Int) vm.LogRecord { - topics := make([]common.Hash, 3) - topics[0] = *UndelegateConfirmedEventHash - topics[1] = delegator.Hash() - topics[2] = validator.Hash() +// event UndelegateConfirmed(address indexed delegator, address indexed validator, uint256 amount); +func (self *Logs) MakeUndelegateConfirmedLog(delegator, validator *common.Address, amount *big.Int) vm.LogRecord { + event := self.Events["UndelegateConfirmed"] - return vm.LogRecord{Address: *contract_address, Topics: topics, Data: amount.Bytes()} + return *checkError(event.MakeLog(delegator, validator, amount)) } // event UndelegateCanceled(address indexed delegator, address indexed validator, uint256 amount); -var UndelegateCanceledEventHash = getEventHash("UndelegateCanceled(address,uint256)") - -func MakeUndelegateCanceledLog(delegator, validator *common.Address, amount *big.Int) vm.LogRecord { - topics := make([]common.Hash, 3) - topics[0] = *UndelegateCanceledEventHash - topics[1] = delegator.Hash() - topics[2] = validator.Hash() +func (self *Logs) MakeUndelegateCanceledLog(delegator, validator *common.Address, amount *big.Int) vm.LogRecord { + event := self.Events["UndelegateCanceled"] - return vm.LogRecord{Address: *contract_address, Topics: topics, Data: amount.Bytes()} + return *checkError(event.MakeLog(delegator, validator, amount)) } // event Redelegated(address indexed delegator, address indexed from, address indexed to, uint256 amount); -var RedelegatedEventHash = getEventHash("UndelegateCanceled(address,address,address,uint256)") +func (self *Logs) MakeRedelegatedLog(delegator, from, to *common.Address, amount *big.Int) vm.LogRecord { + event := self.Events["Redelegated"] -func MakeRedelegatedLog(delegator, from, to *common.Address, amount *big.Int) vm.LogRecord { - topics := make([]common.Hash, 4) - topics[0] = *RedelegatedEventHash - topics[1] = delegator.Hash() - topics[2] = from.Hash() - topics[3] = to.Hash() - - return vm.LogRecord{Address: *contract_address, Topics: topics, Data: amount.Bytes()} + return *checkError(event.MakeLog(delegator, from, to, amount)) } // event RewardsClaimed(address indexed account, address indexed validator); -var RewardsClaimedEventHash = getEventHash("RewardsClaimed(address,address)") - -func MakeRewardsClaimedLog(account, validator *common.Address) vm.LogRecord { - topics := make([]common.Hash, 3) - topics[0] = *RewardsClaimedEventHash - topics[1] = account.Hash() - topics[2] = validator.Hash() +func (self *Logs) MakeRewardsClaimedLog(account, validator *common.Address) vm.LogRecord { + event := self.Events["RewardsClaimed"] - return vm.LogRecord{Address: *contract_address, Topics: topics, Data: []byte{}} + return *checkError(event.MakeLog(account, validator)) } // event CommissionRewardsClaimed(address indexed account, address indexed validator); -var ComissionRewardsClaimedEventHash = getEventHash("CommissionRewardsClaimed(address,address)") +func (self *Logs) MakeCommissionRewardsClaimedLog(account, validator *common.Address) vm.LogRecord { + event := self.Events["CommissionRewardsClaimed"] -func MakeComissionRewardsClaimedLog(account, validator *common.Address) vm.LogRecord { - topics := make([]common.Hash, 3) - topics[0] = *ComissionRewardsClaimedEventHash - topics[1] = account.Hash() - topics[2] = validator.Hash() - - return vm.LogRecord{Address: *contract_address, Topics: topics, Data: []byte{}} + return *checkError(event.MakeLog(account, validator)) } -// event CommissionSet(address indexed validator, uint16 comission); -var CommissionSetEventHash = getEventHash("CommissionSet(address,uint16)") - -func MakeCommissionSetLog(account *common.Address, amount uint16) vm.LogRecord { - topics := make([]common.Hash, 2) - topics[0] = *CommissionSetEventHash - topics[1] = account.Hash() +// event CommissionSet(address indexed validator, uint16 commission); +func (self *Logs) MakeCommissionSetLog(account *common.Address, amount uint16) vm.LogRecord { + event := self.Events["CommissionSet"] - big_amount := big.NewInt(int64(amount)) - return vm.LogRecord{Address: *contract_address, Topics: topics, Data: big_amount.Bytes()} + return *checkError(event.MakeLog(account, amount)) } // event ValidatorRegistered(address indexed validator); -var ValidatorRegisteredEventHash = getEventHash("ValidatorRegistered(address)") +func (self *Logs) MakeValidatorRegisteredLog(account *common.Address) vm.LogRecord { + event := self.Events["ValidatorRegistered"] -func MakeValidatorRegisteredLog(account *common.Address) vm.LogRecord { - topics := make([]common.Hash, 2) - topics[0] = *ValidatorRegisteredEventHash - topics[1] = account.Hash() - - return vm.LogRecord{Address: *contract_address, Topics: topics, Data: []byte{}} + return *checkError(event.MakeLog(account)) } // event ValidatorInfoSet(address indexed validator); -var ValidatorInfoSetEventHash = getEventHash("ValidatorInfoSet(address)") - -func MakeValidatorInfoSetLog(account *common.Address) vm.LogRecord { - topics := make([]common.Hash, 2) - topics[0] = *ValidatorInfoSetEventHash - topics[1] = account.Hash() +func (self *Logs) MakeValidatorInfoSetLog(account *common.Address) vm.LogRecord { + event := self.Events["ValidatorInfoSet"] - return vm.LogRecord{Address: *contract_address, Topics: topics, Data: []byte{}} + return *checkError(event.MakeLog(account)) } diff --git a/taraxa/state/dpos/solidity/dpos_contract_solidity_structs.go b/taraxa/state/dpos/solidity/dpos_contract_solidity_structs.go index 52a8247b3..f5f674ab1 100644 --- a/taraxa/state/dpos/solidity/dpos_contract_solidity_structs.go +++ b/taraxa/state/dpos/solidity/dpos_contract_solidity_structs.go @@ -23,7 +23,7 @@ import ( /**** Automatically generated & Copy pasted structs ****/ /*******************************************************/ -var TaraxaDposClientMetaData = "[{\"inputs\":[{\"internalType\":\"address\",\"name\":\"validator\",\"type\":\"address\"}],\"name\":\"cancelUndelegate\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"validator\",\"type\":\"address\"}],\"name\":\"claimCommissionRewards\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"validator\",\"type\":\"address\"}],\"name\":\"claimRewards\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"validator\",\"type\":\"address\"}],\"name\":\"confirmUndelegate\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"validator\",\"type\":\"address\"}],\"name\":\"delegate\",\"outputs\":[],\"stateMutability\":\"payable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"delegator\",\"type\":\"address\"},{\"internalType\":\"uint32\",\"name\":\"batch\",\"type\":\"uint32\"}],\"name\":\"getDelegations\",\"outputs\":[{\"components\":[{\"internalType\":\"address\",\"name\":\"account\",\"type\":\"address\"},{\"components\":[{\"internalType\":\"uint256\",\"name\":\"stake\",\"type\":\"uint256\"},{\"internalType\":\"uint256\",\"name\":\"rewards\",\"type\":\"uint256\"}],\"internalType\":\"structDposInterface.DelegatorInfo\",\"name\":\"delegation\",\"type\":\"tuple\"}],\"internalType\":\"structDposInterface.DelegationData[]\",\"name\":\"delegations\",\"type\":\"tuple[]\"},{\"internalType\":\"bool\",\"name\":\"end\",\"type\":\"bool\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"getTotalEligibleVotesCount\",\"outputs\":[{\"internalType\":\"uint64\",\"name\":\"\",\"type\":\"uint64\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"delegator\",\"type\":\"address\"},{\"internalType\":\"uint32\",\"name\":\"batch\",\"type\":\"uint32\"}],\"name\":\"getUndelegations\",\"outputs\":[{\"components\":[{\"internalType\":\"uint256\",\"name\":\"stake\",\"type\":\"uint256\"},{\"internalType\":\"uint64\",\"name\":\"block\",\"type\":\"uint64\"},{\"internalType\":\"address\",\"name\":\"validator\",\"type\":\"address\"}],\"internalType\":\"structDposInterface.UndelegationData[]\",\"name\":\"undelegations\",\"type\":\"tuple[]\"},{\"internalType\":\"bool\",\"name\":\"end\",\"type\":\"bool\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"validator\",\"type\":\"address\"}],\"name\":\"getValidator\",\"outputs\":[{\"components\":[{\"internalType\":\"uint256\",\"name\":\"total_stake\",\"type\":\"uint256\"},{\"internalType\":\"uint256\",\"name\":\"commission_reward\",\"type\":\"uint256\"},{\"internalType\":\"uint16\",\"name\":\"commission\",\"type\":\"uint16\"},{\"internalType\":\"uint64\",\"name\":\"last_commission_change\",\"type\":\"uint64\"},{\"internalType\":\"address\",\"name\":\"owner\",\"type\":\"address\"},{\"internalType\":\"string\",\"name\":\"description\",\"type\":\"string\"},{\"internalType\":\"string\",\"name\":\"endpoint\",\"type\":\"string\"}],\"internalType\":\"structDposInterface.ValidatorBasicInfo\",\"name\":\"validator_info\",\"type\":\"tuple\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"validator\",\"type\":\"address\"}],\"name\":\"getValidatorEligibleVotesCount\",\"outputs\":[{\"internalType\":\"uint64\",\"name\":\"\",\"type\":\"uint64\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"uint32\",\"name\":\"batch\",\"type\":\"uint32\"}],\"name\":\"getValidators\",\"outputs\":[{\"components\":[{\"internalType\":\"address\",\"name\":\"account\",\"type\":\"address\"},{\"components\":[{\"internalType\":\"uint256\",\"name\":\"total_stake\",\"type\":\"uint256\"},{\"internalType\":\"uint256\",\"name\":\"commission_reward\",\"type\":\"uint256\"},{\"internalType\":\"uint16\",\"name\":\"commission\",\"type\":\"uint16\"},{\"internalType\":\"uint64\",\"name\":\"last_commission_change\",\"type\":\"uint64\"},{\"internalType\":\"address\",\"name\":\"owner\",\"type\":\"address\"},{\"internalType\":\"string\",\"name\":\"description\",\"type\":\"string\"},{\"internalType\":\"string\",\"name\":\"endpoint\",\"type\":\"string\"}],\"internalType\":\"structDposInterface.ValidatorBasicInfo\",\"name\":\"info\",\"type\":\"tuple\"}],\"internalType\":\"structDposInterface.ValidatorData[]\",\"name\":\"validators\",\"type\":\"tuple[]\"},{\"internalType\":\"bool\",\"name\":\"end\",\"type\":\"bool\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"validator\",\"type\":\"address\"}],\"name\":\"isValidatorEligible\",\"outputs\":[{\"internalType\":\"bool\",\"name\":\"\",\"type\":\"bool\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"validator_from\",\"type\":\"address\"},{\"internalType\":\"address\",\"name\":\"validator_to\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"amount\",\"type\":\"uint256\"}],\"name\":\"reDelegate\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"validator\",\"type\":\"address\"},{\"internalType\":\"bytes\",\"name\":\"proof\",\"type\":\"bytes\"},{\"internalType\":\"bytes\",\"name\":\"vrf_key\",\"type\":\"bytes\"},{\"internalType\":\"uint16\",\"name\":\"commission\",\"type\":\"uint16\"},{\"internalType\":\"string\",\"name\":\"description\",\"type\":\"string\"},{\"internalType\":\"string\",\"name\":\"endpoint\",\"type\":\"string\"}],\"name\":\"registerValidator\",\"outputs\":[],\"stateMutability\":\"payable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"validator\",\"type\":\"address\"},{\"internalType\":\"uint16\",\"name\":\"commission\",\"type\":\"uint16\"}],\"name\":\"setCommission\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"validator\",\"type\":\"address\"},{\"internalType\":\"string\",\"name\":\"description\",\"type\":\"string\"},{\"internalType\":\"string\",\"name\":\"endpoint\",\"type\":\"string\"}],\"name\":\"setValidatorInfo\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"validator\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"amount\",\"type\":\"uint256\"}],\"name\":\"undelegate\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"}]" +var TaraxaDposClientMetaData = "[{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"address\",\"name\":\"account\",\"type\":\"address\"},{\"indexed\":true,\"internalType\":\"address\",\"name\":\"validator\",\"type\":\"address\"}],\"name\":\"CommissionRewardsClaimed\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"address\",\"name\":\"validator\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"uint16\",\"name\":\"commission\",\"type\":\"uint16\"}],\"name\":\"CommissionSet\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"address\",\"name\":\"delegator\",\"type\":\"address\"},{\"indexed\":true,\"internalType\":\"address\",\"name\":\"validator\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"amount\",\"type\":\"uint256\"}],\"name\":\"Delegated\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"address\",\"name\":\"delegator\",\"type\":\"address\"},{\"indexed\":true,\"internalType\":\"address\",\"name\":\"from\",\"type\":\"address\"},{\"indexed\":true,\"internalType\":\"address\",\"name\":\"to\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"amount\",\"type\":\"uint256\"}],\"name\":\"Redelegated\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"address\",\"name\":\"account\",\"type\":\"address\"},{\"indexed\":true,\"internalType\":\"address\",\"name\":\"validator\",\"type\":\"address\"}],\"name\":\"RewardsClaimed\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"address\",\"name\":\"delegator\",\"type\":\"address\"},{\"indexed\":true,\"internalType\":\"address\",\"name\":\"validator\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"amount\",\"type\":\"uint256\"}],\"name\":\"UndelegateCanceled\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"address\",\"name\":\"delegator\",\"type\":\"address\"},{\"indexed\":true,\"internalType\":\"address\",\"name\":\"validator\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"amount\",\"type\":\"uint256\"}],\"name\":\"UndelegateConfirmed\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"address\",\"name\":\"delegator\",\"type\":\"address\"},{\"indexed\":true,\"internalType\":\"address\",\"name\":\"validator\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"amount\",\"type\":\"uint256\"}],\"name\":\"Undelegated\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"address\",\"name\":\"validator\",\"type\":\"address\"}],\"name\":\"ValidatorInfoSet\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"address\",\"name\":\"validator\",\"type\":\"address\"}],\"name\":\"ValidatorRegistered\",\"type\":\"event\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"validator\",\"type\":\"address\"}],\"name\":\"cancelUndelegate\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"validator\",\"type\":\"address\"}],\"name\":\"claimCommissionRewards\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"validator\",\"type\":\"address\"}],\"name\":\"claimRewards\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"validator\",\"type\":\"address\"}],\"name\":\"confirmUndelegate\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"validator\",\"type\":\"address\"}],\"name\":\"delegate\",\"outputs\":[],\"stateMutability\":\"payable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"delegator\",\"type\":\"address\"},{\"internalType\":\"uint32\",\"name\":\"batch\",\"type\":\"uint32\"}],\"name\":\"getDelegations\",\"outputs\":[{\"components\":[{\"internalType\":\"address\",\"name\":\"account\",\"type\":\"address\"},{\"components\":[{\"internalType\":\"uint256\",\"name\":\"stake\",\"type\":\"uint256\"},{\"internalType\":\"uint256\",\"name\":\"rewards\",\"type\":\"uint256\"}],\"internalType\":\"struct DposInterface.DelegatorInfo\",\"name\":\"delegation\",\"type\":\"tuple\"}],\"internalType\":\"struct DposInterface.DelegationData[]\",\"name\":\"delegations\",\"type\":\"tuple[]\"},{\"internalType\":\"bool\",\"name\":\"end\",\"type\":\"bool\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"getTotalEligibleVotesCount\",\"outputs\":[{\"internalType\":\"uint64\",\"name\":\"\",\"type\":\"uint64\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"delegator\",\"type\":\"address\"},{\"internalType\":\"uint32\",\"name\":\"batch\",\"type\":\"uint32\"}],\"name\":\"getUndelegations\",\"outputs\":[{\"components\":[{\"internalType\":\"uint256\",\"name\":\"stake\",\"type\":\"uint256\"},{\"internalType\":\"uint64\",\"name\":\"block\",\"type\":\"uint64\"},{\"internalType\":\"address\",\"name\":\"validator\",\"type\":\"address\"}],\"internalType\":\"struct DposInterface.UndelegationData[]\",\"name\":\"undelegations\",\"type\":\"tuple[]\"},{\"internalType\":\"bool\",\"name\":\"end\",\"type\":\"bool\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"validator\",\"type\":\"address\"}],\"name\":\"getValidator\",\"outputs\":[{\"components\":[{\"internalType\":\"uint256\",\"name\":\"total_stake\",\"type\":\"uint256\"},{\"internalType\":\"uint256\",\"name\":\"commission_reward\",\"type\":\"uint256\"},{\"internalType\":\"uint16\",\"name\":\"commission\",\"type\":\"uint16\"},{\"internalType\":\"uint64\",\"name\":\"last_commission_change\",\"type\":\"uint64\"},{\"internalType\":\"address\",\"name\":\"owner\",\"type\":\"address\"},{\"internalType\":\"string\",\"name\":\"description\",\"type\":\"string\"},{\"internalType\":\"string\",\"name\":\"endpoint\",\"type\":\"string\"}],\"internalType\":\"struct DposInterface.ValidatorBasicInfo\",\"name\":\"validator_info\",\"type\":\"tuple\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"validator\",\"type\":\"address\"}],\"name\":\"getValidatorEligibleVotesCount\",\"outputs\":[{\"internalType\":\"uint64\",\"name\":\"\",\"type\":\"uint64\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"uint32\",\"name\":\"batch\",\"type\":\"uint32\"}],\"name\":\"getValidators\",\"outputs\":[{\"components\":[{\"internalType\":\"address\",\"name\":\"account\",\"type\":\"address\"},{\"components\":[{\"internalType\":\"uint256\",\"name\":\"total_stake\",\"type\":\"uint256\"},{\"internalType\":\"uint256\",\"name\":\"commission_reward\",\"type\":\"uint256\"},{\"internalType\":\"uint16\",\"name\":\"commission\",\"type\":\"uint16\"},{\"internalType\":\"uint64\",\"name\":\"last_commission_change\",\"type\":\"uint64\"},{\"internalType\":\"address\",\"name\":\"owner\",\"type\":\"address\"},{\"internalType\":\"string\",\"name\":\"description\",\"type\":\"string\"},{\"internalType\":\"string\",\"name\":\"endpoint\",\"type\":\"string\"}],\"internalType\":\"struct DposInterface.ValidatorBasicInfo\",\"name\":\"info\",\"type\":\"tuple\"}],\"internalType\":\"struct DposInterface.ValidatorData[]\",\"name\":\"validators\",\"type\":\"tuple[]\"},{\"internalType\":\"bool\",\"name\":\"end\",\"type\":\"bool\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"validator\",\"type\":\"address\"}],\"name\":\"isValidatorEligible\",\"outputs\":[{\"internalType\":\"bool\",\"name\":\"\",\"type\":\"bool\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"validator_from\",\"type\":\"address\"},{\"internalType\":\"address\",\"name\":\"validator_to\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"amount\",\"type\":\"uint256\"}],\"name\":\"reDelegate\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"validator\",\"type\":\"address\"},{\"internalType\":\"bytes\",\"name\":\"proof\",\"type\":\"bytes\"},{\"internalType\":\"bytes\",\"name\":\"vrf_key\",\"type\":\"bytes\"},{\"internalType\":\"uint16\",\"name\":\"commission\",\"type\":\"uint16\"},{\"internalType\":\"string\",\"name\":\"description\",\"type\":\"string\"},{\"internalType\":\"string\",\"name\":\"endpoint\",\"type\":\"string\"}],\"name\":\"registerValidator\",\"outputs\":[],\"stateMutability\":\"payable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"validator\",\"type\":\"address\"},{\"internalType\":\"uint16\",\"name\":\"commission\",\"type\":\"uint16\"}],\"name\":\"setCommission\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"validator\",\"type\":\"address\"},{\"internalType\":\"string\",\"name\":\"description\",\"type\":\"string\"},{\"internalType\":\"string\",\"name\":\"endpoint\",\"type\":\"string\"}],\"name\":\"setValidatorInfo\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"validator\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"amount\",\"type\":\"uint256\"}],\"name\":\"undelegate\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"}]" // DposInterfaceDelegationData is an auto generated low-level Go binding around an user-defined struct. type DposInterfaceDelegationData struct { diff --git a/taraxa/state/dpos/tests/dpos_test.go b/taraxa/state/dpos/tests/dpos_test.go index e5f5fd45b..e4412c0a8 100644 --- a/taraxa/state/dpos/tests/dpos_test.go +++ b/taraxa/state/dpos/tests/dpos_test.go @@ -4,13 +4,16 @@ import ( "bytes" "fmt" "math/big" + "strings" "testing" + "github.com/Taraxa-project/taraxa-evm/accounts/abi" "github.com/Taraxa-project/taraxa-evm/common" "github.com/Taraxa-project/taraxa-evm/core" "github.com/Taraxa-project/taraxa-evm/core/vm" "github.com/Taraxa-project/taraxa-evm/crypto" dpos "github.com/Taraxa-project/taraxa-evm/taraxa/state/dpos/precompiled" + sol "github.com/Taraxa-project/taraxa-evm/taraxa/state/dpos/solidity" "github.com/Taraxa-project/taraxa-evm/taraxa/state/rewards_stats" "github.com/Taraxa-project/taraxa-evm/taraxa/util" "github.com/Taraxa-project/taraxa-evm/taraxa/util/bigutil" @@ -18,6 +21,18 @@ import ( "github.com/Taraxa-project/taraxa-evm/taraxa/util/tests" ) +// This strings should correspond to event signatures in ../solidity/dpos_contract_interface.sol file +var DelegatedEventHash = *keccak256.Hash([]byte("Delegated(address,address,uint256)")) +var UndelegatedEventHash = *keccak256.Hash([]byte("Undelegated(address,address,uint256)")) +var UndelegateConfirmedEventHash = *keccak256.Hash([]byte("UndelegateConfirmed(address,address,uint256)")) +var UndelegateCanceledEventHash = *keccak256.Hash([]byte("UndelegateCanceled(address,address,uint256)")) +var RedelegatedEventHash = *keccak256.Hash([]byte("Redelegated(address,address,address,uint256)")) +var RewardsClaimedEventHash = *keccak256.Hash([]byte("RewardsClaimed(address,address)")) +var CommissionRewardsClaimedEventHash = *keccak256.Hash([]byte("CommissionRewardsClaimed(address,address)")) +var CommissionSetEventHash = *keccak256.Hash([]byte("CommissionSet(address,uint16)")) +var ValidatorRegisteredEventHash = *keccak256.Hash([]byte("ValidatorRegistered(address)")) +var ValidatorInfoSetEventHash = *keccak256.Hash([]byte("ValidatorInfoSet(address)")) + func TestProof(t *testing.T) { pubkey, seckey := generateKeyPair() addr := common.BytesToAddress(keccak256.Hash(pubkey[1:])[12:]) @@ -91,13 +106,13 @@ func TestRedelegate(t *testing.T) { reg_res := test.ExecuteAndCheck(validator1_owner, DefaultMinimumDeposit, test.pack("registerValidator", validator1_addr, validator1_proof, DefaultVrfKey, uint16(10), "test", "test"), util.ErrorString(""), util.ErrorString("")) tc.Assert.Equal(len(reg_res.Logs), 2) - tc.Assert.Equal(reg_res.Logs[0].Topics[0], *dpos.ValidatorRegisteredEventHash) - tc.Assert.Equal(reg_res.Logs[1].Topics[0], *dpos.DelegatedEventHash) + tc.Assert.Equal(reg_res.Logs[0].Topics[0], ValidatorRegisteredEventHash) + tc.Assert.Equal(reg_res.Logs[1].Topics[0], DelegatedEventHash) test.ExecuteAndCheck(validator2_owner, DefaultMinimumDeposit, test.pack("registerValidator", validator2_addr, validator2_proof, DefaultVrfKey, uint16(10), "test", "test"), util.ErrorString(""), util.ErrorString("")) redelegate_res := test.ExecuteAndCheck(validator1_owner, big.NewInt(0), test.pack("reDelegate", validator1_addr, validator2_addr, DefaultMinimumDeposit), util.ErrorString(""), util.ErrorString("")) tc.Assert.Equal(len(redelegate_res.Logs), 1) - tc.Assert.Equal(redelegate_res.Logs[0].Topics[0], *dpos.RedelegatedEventHash) + tc.Assert.Equal(redelegate_res.Logs[0].Topics[0], RedelegatedEventHash) //Validator 1 does not exist as we withdraw all stake test.ExecuteAndCheck(validator1_owner, big.NewInt(0), test.pack("reDelegate", validator1_addr, validator2_addr, DefaultMinimumDeposit), dpos.ErrNonExistentValidator, util.ErrorString("")) @@ -151,7 +166,7 @@ func TestUndelegate(t *testing.T) { test.ExecuteAndCheck(val_owner, DefaultMinimumDeposit, test.pack("registerValidator", val_addr, proof, DefaultVrfKey, uint16(10), "test", "test"), util.ErrorString(""), util.ErrorString("")) undelegate_res := test.ExecuteAndCheck(val_owner, big.NewInt(0), test.pack("undelegate", val_addr, DefaultMinimumDeposit), util.ErrorString(""), util.ErrorString("")) tc.Assert.Equal(len(undelegate_res.Logs), 1) - tc.Assert.Equal(undelegate_res.Logs[0].Topics[0], *dpos.UndelegatedEventHash) + tc.Assert.Equal(undelegate_res.Logs[0].Topics[0], UndelegatedEventHash) // NonExistentValidator as it was deleted test.ExecuteAndCheck(delegator_addr, big.NewInt(0), test.pack("undelegate", val_addr, DefaultMinimumDeposit), dpos.ErrNonExistentValidator, util.ErrorString("")) test.ExecuteAndCheck(val_owner, DefaultMinimumDeposit, test.pack("registerValidator", val_addr, proof, DefaultVrfKey, uint16(10), "test", "test"), util.ErrorString(""), util.ErrorString("")) @@ -190,7 +205,7 @@ func TestConfirmUndelegate(t *testing.T) { confirm_res := test.ExecuteAndCheck(delegator_addr, big.NewInt(0), test.pack("confirmUndelegate", val_addr), util.ErrorString(""), util.ErrorString("")) tc.Assert.Equal(len(confirm_res.Logs), 1) - tc.Assert.Equal(confirm_res.Logs[0].Topics[0], *dpos.UndelegateConfirmedEventHash) + tc.Assert.Equal(confirm_res.Logs[0].Topics[0], UndelegateConfirmedEventHash) // ErrNonExistentDelegation test.ExecuteAndCheck(delegator_addr, big.NewInt(0), test.pack("undelegate", val_addr, DefaultMinimumDeposit), dpos.ErrNonExistentDelegation, util.ErrorString("")) @@ -232,7 +247,7 @@ func TestCancelUndelegate(t *testing.T) { // Cancel undelegate and check if validator's total stake was increased again cancel_res := test.ExecuteAndCheck(delegator_addr, big.NewInt(0), test.pack("cancelUndelegate", val_addr), util.ErrorString(""), util.ErrorString("")) tc.Assert.Equal(len(cancel_res.Logs), 1) - tc.Assert.Equal(cancel_res.Logs[0].Topics[0], *dpos.UndelegateCanceledEventHash) + tc.Assert.Equal(cancel_res.Logs[0].Topics[0], UndelegateCanceledEventHash) test.ExecuteAndCheck(delegator_addr, big.NewInt(0), test.pack("getValidator", val_addr), util.ErrorString(""), util.ErrorString("")) validator_raw = test.ExecuteAndCheck(delegator_addr, big.NewInt(0), test.pack("getValidator", val_addr), util.ErrorString(""), util.ErrorString("")) validator = new(GetValidatorRet) @@ -446,7 +461,7 @@ func TestRewardsAndCommission(t *testing.T) { test.ExecuteAndCheck(delegator3_addr, big.NewInt(0), test.pack("claimRewards", validator2_addr), util.ErrorString(""), util.ErrorString("")) clam_res := test.ExecuteAndCheck(delegator4_addr, big.NewInt(0), test.pack("claimRewards", validator4_addr), util.ErrorString(""), util.ErrorString("")) tc.Assert.Equal(len(clam_res.Logs), 1) - tc.Assert.Equal(clam_res.Logs[0].Topics[0], *dpos.RewardsClaimedEventHash) + tc.Assert.Equal(clam_res.Logs[0].Topics[0], RewardsClaimedEventHash) } actual_delegator1_reward := bigutil.Sub(test.GetBalance(delegator1_addr), delegator1_old_balance) @@ -469,7 +484,7 @@ func TestRewardsAndCommission(t *testing.T) { { claim_res := test.ExecuteAndCheck(delegator4_addr, big.NewInt(0), test.pack("claimCommissionRewards", validator4_addr), util.ErrorString(""), util.ErrorString("")) tc.Assert.Equal(len(claim_res.Logs), 1) - tc.Assert.Equal(claim_res.Logs[0].Topics[0], *dpos.ComissionRewardsClaimedEventHash) + tc.Assert.Equal(claim_res.Logs[0].Topics[0], CommissionRewardsClaimedEventHash) } actual_validator1_commission_reward := bigutil.Sub(test.GetBalance(validator1_owner), validator1_old_balance) @@ -526,7 +541,7 @@ func TestSetValidatorInfo(t *testing.T) { { set_res := test.ExecuteAndCheck(val_owner, big.NewInt(0), test.pack("setValidatorInfo", val_addr, "modified_description", "modified_endpoint"), util.ErrorString(""), util.ErrorString("")) tc.Assert.Equal(len(set_res.Logs), 1) - tc.Assert.Equal(set_res.Logs[0].Topics[0], *dpos.ValidatorInfoSetEventHash) + tc.Assert.Equal(set_res.Logs[0].Topics[0], ValidatorInfoSetEventHash) } validator_raw = test.ExecuteAndCheck(val_addr, big.NewInt(0), test.pack("getValidator", val_addr), util.ErrorString(""), util.ErrorString("")) validator = new(GetValidatorRet) @@ -1283,44 +1298,61 @@ func TestUndelegationsClass(t *testing.T) { func TestMakeLogsCheckTopics(t *testing.T) { tc := tests.NewTestCtx(t) amount := big.NewInt(0) + + Abi, _ := abi.JSON(strings.NewReader(sol.TaraxaDposClientMetaData)) + logs := *new(dpos.Logs).Init(Abi.Events) + + count := 0 { - log := dpos.MakeDelegatedLog(&common.ZeroAddress, &common.ZeroAddress, amount) - tc.Assert.Equal(len(log.Topics), 3) + log := logs.MakeDelegatedLog(&common.ZeroAddress, &common.ZeroAddress, amount) + tc.Assert.Equal(log.Topics[0], DelegatedEventHash) + count++ } { - log := dpos.MakeUndelegatedLog(&common.ZeroAddress, &common.ZeroAddress, amount) - tc.Assert.Equal(len(log.Topics), 3) + log := logs.MakeUndelegatedLog(&common.ZeroAddress, &common.ZeroAddress, amount) + tc.Assert.Equal(log.Topics[0], UndelegatedEventHash) + count++ } { - log := dpos.MakeUndelegateConfirmedLog(&common.ZeroAddress, &common.ZeroAddress, amount) - tc.Assert.Equal(len(log.Topics), 3) + log := logs.MakeUndelegateConfirmedLog(&common.ZeroAddress, &common.ZeroAddress, amount) + tc.Assert.Equal(log.Topics[0], UndelegateConfirmedEventHash) + count++ } { - log := dpos.MakeUndelegateCanceledLog(&common.ZeroAddress, &common.ZeroAddress, amount) - tc.Assert.Equal(len(log.Topics), 3) + log := logs.MakeUndelegateCanceledLog(&common.ZeroAddress, &common.ZeroAddress, amount) + tc.Assert.Equal(log.Topics[0], UndelegateCanceledEventHash) + count++ } { - log := dpos.MakeRedelegatedLog(&common.ZeroAddress, &common.ZeroAddress, &common.ZeroAddress, amount) - tc.Assert.Equal(len(log.Topics), 4) + log := logs.MakeRedelegatedLog(&common.ZeroAddress, &common.ZeroAddress, &common.ZeroAddress, amount) + tc.Assert.Equal(log.Topics[0], RedelegatedEventHash) + count++ } { - log := dpos.MakeRewardsClaimedLog(&common.ZeroAddress, &common.ZeroAddress) - tc.Assert.Equal(len(log.Topics), 3) + log := logs.MakeRewardsClaimedLog(&common.ZeroAddress, &common.ZeroAddress) + tc.Assert.Equal(log.Topics[0], RewardsClaimedEventHash) + count++ } { - log := dpos.MakeComissionRewardsClaimedLog(&common.ZeroAddress, &common.ZeroAddress) - tc.Assert.Equal(len(log.Topics), 3) + log := logs.MakeCommissionRewardsClaimedLog(&common.ZeroAddress, &common.ZeroAddress) + tc.Assert.Equal(log.Topics[0], CommissionRewardsClaimedEventHash) + count++ } { - log := dpos.MakeCommissionSetLog(&common.ZeroAddress, 0) - tc.Assert.Equal(len(log.Topics), 2) + log := logs.MakeCommissionSetLog(&common.ZeroAddress, 0) + tc.Assert.Equal(log.Topics[0], CommissionSetEventHash) + count++ } { - log := dpos.MakeValidatorRegisteredLog(&common.ZeroAddress) - tc.Assert.Equal(len(log.Topics), 2) + log := logs.MakeValidatorRegisteredLog(&common.ZeroAddress) + tc.Assert.Equal(log.Topics[0], ValidatorRegisteredEventHash) + count++ } { - log := dpos.MakeValidatorInfoSetLog(&common.ZeroAddress) - tc.Assert.Equal(len(log.Topics), 2) + log := logs.MakeValidatorInfoSetLog(&common.ZeroAddress) + tc.Assert.Equal(log.Topics[0], ValidatorInfoSetEventHash) + count++ } + // Check that we tested all events from the ABI + tc.Assert.Equal(count, len(Abi.Events)) }