Skip to content

Commit

Permalink
Merge branch 'develop' into bls-precompiled
Browse files Browse the repository at this point in the history
  • Loading branch information
MatusKysel committed Jan 23, 2024
2 parents 4b649d2 + c03717c commit 71baa31
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 7 deletions.
6 changes: 5 additions & 1 deletion accounts/abi/abi.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ func (abi *ABI) UnmarshalJSON(data []byte) error {
return nil
}

func MethodNotFoundError(id []byte) error {
return fmt.Errorf("no method with id: %#x", id)
}

// MethodById looks up a method by the 4-byte id
// returns nil if none found
func (abi *ABI) MethodById(sigdata []byte) (*Method, error) {
Expand All @@ -148,7 +152,7 @@ func (abi *ABI) MethodById(sigdata []byte) (*Method, error) {
return &method, nil
}
}
return nil, fmt.Errorf("no method with id: %#x", sigdata[:4])
return nil, MethodNotFoundError(sigdata[:4])
}

var revertSelector = crypto.Keccak256([]byte("Error(string)"))[:4]
Expand Down
5 changes: 5 additions & 0 deletions taraxa/state/chain_config/chain_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,17 @@ type AspenHfConfig struct {

type HardforksConfig struct {
FixRedelegateBlockNum uint64
PhalaenopsisHfBlockNum uint64
Redelegations []Redelegation
RewardsDistributionFrequency map[uint64]uint32
MagnoliaHf MagnoliaHfConfig
AspenHf AspenHfConfig
}

func (c *HardforksConfig) IsPhalaenopsisHardfork(block types.BlockNum) bool {
return block >= c.PhalaenopsisHfBlockNum
}

func (c *HardforksConfig) IsMagnoliaHardfork(block types.BlockNum) bool {
return block >= c.MagnoliaHf.BlockNum
}
Expand Down
19 changes: 19 additions & 0 deletions taraxa/state/contracts/dpos/precompiled/dpos_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ const (
DposGetMethodsGas uint64 = 5000
DposBatchGetMethodsGas uint64 = 5000
DefaultDposMethodGas uint64 = 20000
BurnGas uint64 = 1000
)

// Contract methods error return values
Expand Down Expand Up @@ -315,6 +316,11 @@ func (self *Contract) RequiredGas(ctx vm.CallFrame, evm *vm.EVM) uint64 {

undelegations_count := self.batch_items_count(uint64(self.undelegations.GetUndelegationsCount(&args.Delegator)), uint64(args.Batch), GetUndelegationsMaxCount)
return undelegations_count * DposBatchGetMethodsGas
case "burn":
if !self.isPhalaenopsisHardfork(evm.GetBlock().Number) {
return 0
}
return BurnGas
default:
}

Expand Down Expand Up @@ -628,6 +634,11 @@ func (self *Contract) Run(ctx vm.CallFrame, evm *vm.EVM) ([]byte, error) {
return nil, err
}
return method.Outputs.Pack(self.getUndelegations(args))
case "burn":
if !self.isPhalaenopsisHardfork(block_num) {
return nil, abi.MethodNotFoundError(ctx.Input[:4])
}
return nil, nil
default:
}

Expand Down Expand Up @@ -1356,6 +1367,10 @@ func (self *Contract) claimCommissionRewards(ctx vm.CallFrame, block types.Block
if !self.isMagnoliaHardfork(block) || validator.UndelegationsCount == 0 {
self.validators.DeleteValidator(&args.Validator)
self.state_get_and_decrement(args.Validator[:], BlockToBytes(validator.LastUpdated))
} else {
if self.isPhalaenopsisHardfork(block) {
self.validators.ModifyValidatorRewards(&args.Validator, validator_rewards)
}
}
} else {
self.validators.ModifyValidatorRewards(&args.Validator, validator_rewards)
Expand Down Expand Up @@ -1765,6 +1780,10 @@ func (self *Contract) isMagnoliaHardfork(block types.BlockNum) bool {
return self.cfg.Hardforks.IsMagnoliaHardfork(block)
}

func (self *Contract) isPhalaenopsisHardfork(block types.BlockNum) bool {
return self.cfg.Hardforks.IsPhalaenopsisHardfork(block)
}

func (self *Contract) saveTotalSupplyDb() {
self.storage.Put(contract_storage.Stor_k_1(field_total_supply), self.total_supply.Bytes())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,9 @@ interface DposInterface {
external
view
returns (UndelegationData[] memory undelegations, bool end);

/**
* @dev Burns the specified TARA value by transferring them to the dpos contract balance
*/
function burn() external payable;
}

Large diffs are not rendered by default.

27 changes: 24 additions & 3 deletions taraxa/state/contracts/tests/dpos/dpos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"crypto/elliptic"
"crypto/rand"
"fmt"
"log"
"math/big"
"strings"
"testing"
Expand Down Expand Up @@ -1854,8 +1853,6 @@ func TestValidatorsClass(t *testing.T) {
tc, test := test_utils.Init_test(dpos.ContractAddress(), dpos_sol.TaraxaDposClientMetaData, t, CopyDefaultChainConfig())
defer test.End()

log.Println("\n\n********* yu dms")

// Must be here to setup some internal data in evm_state, otherwise it is not possible to write into contract storage
test.St.BeginBlock(&vm.BlockInfo{})

Expand Down Expand Up @@ -2407,3 +2404,27 @@ func TestRedelegateHF(t *testing.T) {
t.Errorf("Balance left %d expected: %d", contractBalance, total_stake)
}
}

func TestPhalaenopsisHF(t *testing.T) {
cfg := CopyDefaultChainConfig()
cfg.Hardforks.PhalaenopsisHfBlockNum = 3
tc, test := test_utils.Init_test(dpos.ContractAddress(), dpos_sol.TaraxaDposClientMetaData, t, cfg)
defer test.End()

testingAccount := addr(1)
testingAccountBalance := test.GetBalance(&testingAccount)
burnAmount := big.NewInt(1000000)
test.ExecuteAndCheck(testingAccount, big.NewInt(1000000), test.Pack("burn"), util.ErrorString("no method with id: 0x44df8e70"), util.ErrorString(""))
tc.Assert.Equal(testingAccountBalance, test.GetBalance(&testingAccount))

test.AdvanceBlock(nil, nil)
test.AdvanceBlock(nil, nil)

dposBalanceBefore := test.GetBalance(dpos.ContractAddress())
test.ExecuteAndCheck(testingAccount, big.NewInt(1000000), test.Pack("burn"), util.ErrorString(""), util.ErrorString(""))
tc.Assert.Equal(testingAccountBalance.Sub(testingAccountBalance, burnAmount), test.GetBalance(&testingAccount))
tc.Assert.Equal(dposBalanceBefore.Add(dposBalanceBefore, burnAmount), test.GetBalance(dpos.ContractAddress()))

// totalBalance := bigutil.Add(total_stake, reward)

}

0 comments on commit 71baa31

Please sign in to comment.