Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
blukat29 committed Jun 7, 2024
2 parents 4c84a4c + 5546d41 commit 0412649
Show file tree
Hide file tree
Showing 39 changed files with 621 additions and 305 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
[![No Maintenance Intended](http://unmaintained.tech/badge.svg)](http://unmaintained.tech/)

# NO LONGER MAINTAINED

Since the launch of Kaia Blockchain this repository has been parked in favour of the new open-source projects in [Kaia's Github](https://github.com/kaiachain). Contributors have now moved there continuing with massive open-source contributions to our blockchain ecosystem. A big thank you to everyone who has contributed to this repository. For more information about Klaytn's chain merge with Finschia blockchain please refer to the launching of Kaia blockchain - [kaia.io](https://kaia.io/).

---

[![Go Report Card](https://goreportcard.com/badge/github.com/klaytn/klaytn)](https://goreportcard.com/report/github.com/klaytn/klaytn)
[![CircleCI](https://circleci.com/gh/klaytn/klaytn/tree/dev.svg?style=svg)](https://circleci.com/gh/klaytn/klaytn/tree/dev)
[![codecov](https://codecov.io/gh/klaytn/klaytn/branch/dev/graph/badge.svg)](https://codecov.io/gh/klaytn/klaytn)
Expand Down
58 changes: 17 additions & 41 deletions api/api_ethereum.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,8 @@ var (
)

// EthereumAPI provides an API to access the Kaia through the `eth` namespace.
// TODO-Kaia: Removed unused variable
type EthereumAPI struct {
publicFilterAPI *filters.PublicFilterAPI
governanceKaiaAPI *governance.GovernanceKaiaAPI

publicFilterAPI *filters.PublicFilterAPI
publicKaiaAPI *PublicKaiaAPI
publicBlockChainAPI *PublicBlockChainAPI
publicTransactionPoolAPI *PublicTransactionPoolAPI
Expand All @@ -80,43 +77,22 @@ type EthereumAPI struct {
// EthereumAPI operates using Kaia's API internally without overriding.
// Therefore, it is necessary to use APIs defined in two different packages(cn and api),
// so those apis will be defined through a setter.
func NewEthereumAPI() *EthereumAPI {
return &EthereumAPI{nil, nil, nil, nil, nil, nil, nil}
}

// SetPublicFilterAPI sets publicFilterAPI
func (api *EthereumAPI) SetPublicFilterAPI(publicFilterAPI *filters.PublicFilterAPI) {
api.publicFilterAPI = publicFilterAPI
}

// SetGovernanceKaiaAPI sets governanceKaiaAPI
func (api *EthereumAPI) SetGovernanceKaiaAPI(governanceKaiaAPI *governance.GovernanceKaiaAPI) {
api.governanceKaiaAPI = governanceKaiaAPI
}

// SetPublicKaiaAPI sets publicKaiaAPI
func (api *EthereumAPI) SetPublicKaiaAPI(publicKaiaAPI *PublicKaiaAPI) {
api.publicKaiaAPI = publicKaiaAPI
}

// SetPublicBlockChainAPI sets publicBlockChainAPI
func (api *EthereumAPI) SetPublicBlockChainAPI(publicBlockChainAPI *PublicBlockChainAPI) {
api.publicBlockChainAPI = publicBlockChainAPI
}

// SetPublicTransactionPoolAPI sets publicTransactionPoolAPI
func (api *EthereumAPI) SetPublicTransactionPoolAPI(publicTransactionPoolAPI *PublicTransactionPoolAPI) {
api.publicTransactionPoolAPI = publicTransactionPoolAPI
}

// SetPublicAccountAPI sets publicAccountAPI
func (api *EthereumAPI) SetPublicAccountAPI(publicAccountAPI *PublicAccountAPI) {
api.publicAccountAPI = publicAccountAPI
}

// SetGovernanceAPI sets governanceAPI
func (api *EthereumAPI) SetGovernanceAPI(governanceAPI *governance.GovernanceAPI) {
api.governanceAPI = governanceAPI
func NewEthereumAPI(
publicFilterAPI *filters.PublicFilterAPI,
publicKaiaAPI *PublicKaiaAPI,
publicBlockChainAPI *PublicBlockChainAPI,
publicTransactionPoolAPI *PublicTransactionPoolAPI,
publicAccountAPI *PublicAccountAPI,
governanceAPI *governance.GovernanceAPI,
) *EthereumAPI {
return &EthereumAPI{
publicFilterAPI,
publicKaiaAPI,
publicBlockChainAPI,
publicTransactionPoolAPI,
publicAccountAPI,
governanceAPI,
}
}

// Etherbase is the address of operating node.
Expand Down
6 changes: 3 additions & 3 deletions api/api_public_blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,10 +405,10 @@ func (s *PublicBlockChainAPI) EstimateGas(ctx context.Context, args CallArgs) (h
if rpcGasCap := s.b.RPCGasCap(); rpcGasCap != nil {
gasCap = rpcGasCap.Uint64()
}
return s.DoEstimateGas(ctx, s.b, args, big.NewInt(int64(gasCap)))
return DoEstimateGas(ctx, s.b, args, s.b.RPCEVMTimeout(), new(big.Int).SetUint64(gasCap))
}

func (s *PublicBlockChainAPI) DoEstimateGas(ctx context.Context, b Backend, args CallArgs, gasCap *big.Int) (hexutil.Uint64, error) {
func DoEstimateGas(ctx context.Context, b Backend, args CallArgs, timeout time.Duration, gasCap *big.Int) (hexutil.Uint64, error) {
var feeCap *big.Int
if args.GasPrice != nil {
feeCap = args.GasPrice.ToInt()
Expand All @@ -425,7 +425,7 @@ func (s *PublicBlockChainAPI) DoEstimateGas(ctx context.Context, b Backend, args
// Create a helper to check if a gas allowance results in an executable transaction
executable := func(gas uint64) (bool, *blockchain.ExecutionResult, error) {
args.Gas = hexutil.Uint64(gas)
result, _, err := DoCall(ctx, b, args, rpc.NewBlockNumberOrHashWithNumber(rpc.LatestBlockNumber), vm.Config{ComputationCostLimit: params.OpcodeComputationCostLimitInfinite}, s.b.RPCEVMTimeout(), gasCap)
result, _, err := DoCall(ctx, b, args, rpc.NewBlockNumberOrHashWithNumber(rpc.LatestBlockNumber), vm.Config{ComputationCostLimit: params.OpcodeComputationCostLimitInfinite}, timeout, gasCap)
if err != nil {
if errors.Is(err, blockchain.ErrIntrinsicGas) {
return true, nil, nil // Special case, raise gas limit
Expand Down
63 changes: 0 additions & 63 deletions api/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,66 +102,3 @@ type Backend interface {
GetBlockReceiptsInCache(blockHash common.Hash) types.Receipts
GetTxLookupInfoAndReceiptInCache(Hash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, *types.Receipt)
}

func GetAPIs(apiBackend Backend, disableUnsafeDebug bool) ([]rpc.API, *EthereumAPI) {
nonceLock := new(AddrLocker)

ethAPI := NewEthereumAPI()

publicKaiaAPI := NewPublicKaiaAPI(apiBackend)
publicBlockChainAPI := NewPublicBlockChainAPI(apiBackend)
publicTransactionPoolAPI := NewPublicTransactionPoolAPI(apiBackend, nonceLock)
publicAccountAPI := NewPublicAccountAPI(apiBackend.AccountManager())

ethAPI.SetPublicKaiaAPI(publicKaiaAPI)
ethAPI.SetPublicBlockChainAPI(publicBlockChainAPI)
ethAPI.SetPublicTransactionPoolAPI(publicTransactionPoolAPI)
ethAPI.SetPublicAccountAPI(publicAccountAPI)

rpcApi := []rpc.API{
{
Namespace: "kaia",
Version: "1.0",
Service: publicKaiaAPI,
Public: true,
}, {
Namespace: "kaia",
Version: "1.0",
Service: publicBlockChainAPI,
Public: true,
}, {
Namespace: "kaia",
Version: "1.0",
Service: publicTransactionPoolAPI,
Public: true,
}, {
Namespace: "txpool",
Version: "1.0",
Service: NewPublicTxPoolAPI(apiBackend),
Public: true,
}, {
Namespace: "debug",
Version: "1.0",
Service: NewPublicDebugAPI(apiBackend),
Public: false,
}, {
Namespace: "kaia",
Version: "1.0",
Service: publicAccountAPI,
Public: true,
}, {
Namespace: "personal",
Version: "1.0",
Service: NewPrivateAccountAPI(apiBackend, nonceLock),
Public: false,
}, {
Namespace: "debug",
Version: "1.0",
Service: NewPrivateDebugAPI(apiBackend),
Public: false,
IPCOnly: disableUnsafeDebug,
},
}

return rpcApi, ethAPI
}
4 changes: 2 additions & 2 deletions api/tx_args.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func (args *SendTxArgs) setDefaults(ctx context.Context, b Backend) error {

// b.SuggestPrice = unitPrice, for before Magma
// = baseFee * 2, for after Magma
// = baseFee * 2 + tip for after Kaia
// = baseFee + tip for after Kaia
price, err := b.SuggestPrice(ctx)
if err != nil {
return err
Expand Down Expand Up @@ -616,7 +616,7 @@ func (args *EthTransactionArgs) setDefaults(ctx context.Context, b Backend) erro

// b.SuggestPrice = unitPrice, for before Magma
// = baseFee * 2, for after Magma
// = baseFee * 2 + tip for
// = baseFee + tip for after Kaia
gasPrice, err := b.SuggestPrice(ctx)
if err != nil {
return err
Expand Down
73 changes: 49 additions & 24 deletions blockchain/system/rebalance.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,19 +133,24 @@ func (caller *Kip103ContractCaller) CallContract(ctx context.Context, call kaia.
return result.Return(), err
}

type rebalanceResult struct {
type rebalanceBalances struct {
Zeroed map[common.Address]*big.Int `json:"zeroed"`
Allocated map[common.Address]*big.Int `json:"allocated"`
Burnt *big.Int `json:"burnt"`
Success bool `json:"success"`
}

type rebalanceResult struct {
Before *rebalanceBalances `json:"before"`
After *rebalanceBalances `json:"after"`
Burnt *big.Int `json:"burnt"`
Success bool `json:"success"`
}

func newRebalanceReceipt() *rebalanceResult {
return &rebalanceResult{
Zeroed: make(map[common.Address]*big.Int),
Allocated: make(map[common.Address]*big.Int),
Burnt: big.NewInt(0),
Success: false,
Before: &rebalanceBalances{make(map[common.Address]*big.Int), make(map[common.Address]*big.Int)},
After: &rebalanceBalances{make(map[common.Address]*big.Int), make(map[common.Address]*big.Int)},
Burnt: big.NewInt(0),
Success: false,
}
}

Expand All @@ -155,13 +160,30 @@ func (result *rebalanceResult) memo(isKip103 bool) []byte {
err error
)
if isKip103 {
type retired struct {
Zeroed common.Address `json:"retired"`
Balance uint64 `json:"balance"`
}
type newbie struct {
Allocated common.Address `json:"newbie"`
FundAllocated uint64 `json:"fundAllocated"`
}
type kip103RebalanceResult struct {
Zeroed map[common.Address]*big.Int `json:"retired"`
Allocated map[common.Address]*big.Int `json:"newbie"`
Burnt *big.Int `json:"burnt"`
Success bool `json:"success"`
Zeroed []retired `json:"retirees"`
Allocated []newbie `json:"newbies"`
Burnt uint64 `json:"burnt"`
Success bool `json:"success"`
}
formattedKip103Result := new(kip103RebalanceResult)
for addr, balance := range result.Before.Zeroed {
formattedKip103Result.Zeroed = append(formattedKip103Result.Zeroed, retired{addr, balance.Uint64()})
}
for addr, fundAllocated := range result.After.Allocated {
formattedKip103Result.Allocated = append(formattedKip103Result.Allocated, newbie{addr, fundAllocated.Uint64()})
}
memo, err = json.Marshal(kip103RebalanceResult{result.Zeroed, result.Allocated, result.Burnt, result.Success})
formattedKip103Result.Burnt = result.Burnt.Uint64()
formattedKip103Result.Success = result.Success
memo, err = json.Marshal(formattedKip103Result)
} else {
memo, err = json.Marshal(result)
}
Expand All @@ -184,12 +206,13 @@ func (result *rebalanceResult) fillZeroed(contract RebalanceCaller, state *state
logger.Error("Failed to get Zeroeds from TreasuryRebalance contract", "err", err)
return err
}
result.Zeroed[ret] = state.GetBalance(ret)
result.Before.Zeroed[ret] = state.GetBalance(ret)
result.After.Zeroed[ret] = state.GetBalance(ret) // will be set as zero if rebalance suceed
}
return nil
}

func (result *rebalanceResult) fillAllocated(contract RebalanceCaller) error {
func (result *rebalanceResult) fillAllocated(contract RebalanceCaller, state *state.StateDB) error {
numNewbieBigInt, err := contract.GetAllocatedCount(nil)
if err != nil {
logger.Error("Failed to get AllocatedCount from TreasuryRebalance contract", "err", err)
Expand All @@ -203,22 +226,23 @@ func (result *rebalanceResult) fillAllocated(contract RebalanceCaller) error {
return err
}

result.Allocated[ret.Addr] = ret.Amount
result.Before.Allocated[ret.Addr] = state.GetBalance(ret.Addr)
result.After.Allocated[ret.Addr] = ret.Amount
}
return nil
}

func (result *rebalanceResult) totalZeroedBalance() *big.Int {
total := big.NewInt(0)
for _, bal := range result.Zeroed {
for _, bal := range result.Before.Zeroed {
total.Add(total, bal)
}
return total
}

func (result *rebalanceResult) totalAllocatedBalance() *big.Int {
total := big.NewInt(0)
for _, bal := range result.Allocated {
for _, bal := range result.After.Allocated {
total.Add(total, bal)
}
return total
Expand Down Expand Up @@ -254,7 +278,7 @@ func RebalanceTreasury(state *state.StateDB, chain backends.BlockChainForCaller,
}

// Retrieve 2) Get Allocated
if err = result.fillAllocated(caller); err != nil {
if err = result.fillAllocated(caller, state); err != nil {
return result, err
}

Expand All @@ -273,20 +297,21 @@ func RebalanceTreasury(state *state.StateDB, chain backends.BlockChainForCaller,
return result, err
}

// Validation 4) Check the total balance of retirees are bigger than the distributing amount
// Validation 4) Check the total balance of zeroeds are bigger than the distributing amount
totalZeroedAmount := result.totalZeroedBalance()
totalAllocatedAmount := result.totalAllocatedBalance()
if isKIP103 && totalZeroedAmount.Cmp(totalAllocatedAmount) < 0 {
return result, ErrRebalanceNotEnoughBalance
}

// Execution 1) Clear all balances of retirees
for addr := range result.Zeroed {
// Execution 1) Clear all balances of zeroeds
for addr := range result.Before.Zeroed {
state.SetBalance(addr, big.NewInt(0))
result.After.Zeroed[addr] = big.NewInt(0)
}
// Execution 2) Distribute KAIA to all newbies
for addr, balance := range result.Allocated {
// if newbie has KAIA before the allocation, it will be burnt
// Execution 2) Distribute KAIA to all allocateds
for addr, balance := range result.After.Allocated {
// if an allocated has KAIA before the allocation, it will be burnt
currentBalance := state.GetBalance(addr)
result.Burnt.Add(result.Burnt, currentBalance)

Expand Down
Loading

0 comments on commit 0412649

Please sign in to comment.