From 1c691e7f35445d0483a1b1a8325c3d041814b29d Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Tue, 9 May 2023 09:13:17 +0900 Subject: [PATCH] [rosetta-klaytn]_release/v1.0.7 (#161) * update for Kore hardfork * update release version * fix test * add CodeOwners * test: fix go version in github action * update for KIP103 hardfork (#159) * Update CODEOWNERS (#160) --------- Co-authored-by: Jay (Jaeyeong Choi) <102955810+JayChoi1736@users.noreply.github.com> --- .github/CODEOWNERS | 2 +- .github/workflows/DEV.yaml | 11 +- configuration/configuration.go | 2 +- go.mod | 2 +- go.sum | 4 +- klaytn/client.go | 166 +++++++++++++++++++++++++++++-- services/network_service_test.go | 2 +- 7 files changed, 170 insertions(+), 19 deletions(-) diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index cccfffb..455d0b7 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -5,7 +5,7 @@ # the repo. Unless a later match takes precedence, # @global-owner1 and @global-owner2 will be requested for # review when someone opens a pull request. -* @jimni1222 @kjhman21 +* @kjhman21 @kjeom @JayChoi1736 @toniya-klaytn # Order is important; the last matching pattern takes the most # precedence. When someone opens a pull request that only diff --git a/.github/workflows/DEV.yaml b/.github/workflows/DEV.yaml index 28546e6..0f5015a 100644 --- a/.github/workflows/DEV.yaml +++ b/.github/workflows/DEV.yaml @@ -11,7 +11,7 @@ jobs: - uses: actions/checkout@v3 - uses: actions/setup-go@v3 with: - go-version: '>=1.18.0' + go-version: '1.18' - name: download dependencies run: | go get ./... @@ -46,7 +46,7 @@ jobs: - uses: actions/checkout@v3 - uses: actions/setup-go@v3 with: - go-version: '>=1.18.0' + go-version: '1.18' - name: check format run: | make check-format @@ -56,7 +56,7 @@ jobs: - uses: actions/checkout@v3 - uses: actions/setup-go@v3 with: - go-version: '>=1.18.0' + go-version: '1.18' - name: check license run: | make check-license @@ -66,7 +66,7 @@ jobs: - uses: actions/checkout@v3 - uses: actions/setup-go@v3 with: - go-version: '>=1.18.0' + go-version: '1.18' - name: install dependency run: | go mod download github.com/client9/misspell @@ -79,7 +79,8 @@ jobs: - uses: actions/checkout@v3 - uses: actions/setup-go@v3 with: - go-version: '>=1.18.0' + go-version: '1.18' - name: build test run: | + go get -u go4.org/unsafe/assume-no-moving-gc make test diff --git a/configuration/configuration.go b/configuration/configuration.go index c56e5ae..03125d1 100644 --- a/configuration/configuration.go +++ b/configuration/configuration.go @@ -86,7 +86,7 @@ const ( SkipAdminEnv = "SKIP_ADMIN" // MiddlewareVersion is the version of rosetta-klaytn. - MiddlewareVersion = "1.0.5" + MiddlewareVersion = "1.0.7" ) // Configuration determines how diff --git a/go.mod b/go.mod index 18a749c..540bc6f 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,7 @@ module github.com/klaytn/rosetta-klaytn require ( github.com/fatih/color v1.13.0 - github.com/klaytn/klaytn v1.10.1 + github.com/klaytn/klaytn v1.10.2 github.com/klaytn/rosetta-sdk-go-klaytn v0.7.10 github.com/spf13/cobra v1.5.0 github.com/stretchr/testify v1.8.0 diff --git a/go.sum b/go.sum index 22abad5..23eeabc 100644 --- a/go.sum +++ b/go.sum @@ -616,8 +616,8 @@ github.com/klauspost/crc32 v0.0.0-20161016154125-cb6bfca970f6/go.mod h1:+ZoRqAPR github.com/klauspost/pgzip v1.0.2-0.20170402124221-0bf5dcad4ada/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQKQE9RUs= github.com/klauspost/reedsolomon v1.9.2/go.mod h1:CwCi+NUr9pqSVktrkN+Ondf06rkhYZ/pcNv7fu+8Un4= github.com/klaytn/klaytn v1.8.3/go.mod h1:bbf8dqgZn/cLZ+ns+N416knDMKVpyWuUfuOXACpCVH0= -github.com/klaytn/klaytn v1.10.1 h1:XXLP2DYO/0uAg51V0jeJyK+LMOO2vug8qzCSLVIgkSU= -github.com/klaytn/klaytn v1.10.1/go.mod h1:R8eUX9g0sXiiPnQMCDHlWOnDC4oJWWz/1pnudW+1u9c= +github.com/klaytn/klaytn v1.10.2 h1:AAmPn2fcuOTkprMLXkkpvo4uFwqcfAdFpvqBKKqXnNE= +github.com/klaytn/klaytn v1.10.2/go.mod h1:R8eUX9g0sXiiPnQMCDHlWOnDC4oJWWz/1pnudW+1u9c= github.com/klaytn/rosetta-sdk-go-klaytn v0.7.10 h1:ykCtbjVUQQ8NQbWRROXFunmTo81KIdmsujgOPDwPW0M= github.com/klaytn/rosetta-sdk-go-klaytn v0.7.10/go.mod h1:YS9Wj5EvKh7leQJYmtfomC6kMEaqNwjtys+zXbSz0hE= github.com/klaytn/xgo v0.0.0-20220420020737-0d7e3e080122/go.mod h1:1p9sFrvjHQrEknvDHha17xKMZMOqE73LNEGiBt3/3o0= diff --git a/klaytn/client.go b/klaytn/client.go index 79861d2..fc4769c 100644 --- a/klaytn/client.go +++ b/klaytn/client.go @@ -19,6 +19,7 @@ package klaytn import ( "context" + "encoding/hex" "encoding/json" "errors" "fmt" @@ -29,19 +30,20 @@ import ( "strings" "time" + "github.com/klaytn/klaytn/accounts/abi" + "github.com/klaytn/klaytn/contracts/kip103" "github.com/klaytn/klaytn/node/cn/tracers" "github.com/klaytn/klaytn" "github.com/klaytn/klaytn/blockchain/types" "github.com/klaytn/klaytn/blockchain/types/account" "github.com/klaytn/klaytn/common" + "github.com/klaytn/klaytn/common/hexutil" "github.com/klaytn/klaytn/networks/p2p" "github.com/klaytn/klaytn/networks/rpc" "github.com/klaytn/klaytn/params" "github.com/klaytn/klaytn/reward" "github.com/klaytn/klaytn/rlp" - - "github.com/klaytn/klaytn/common/hexutil" RosettaTypes "github.com/klaytn/rosetta-sdk-go-klaytn/types" "golang.org/x/sync/semaphore" ) @@ -1469,6 +1471,14 @@ func (kc *Client) populateTransactions( idx++ } + if block.Number().Cmp(chainConfig.Kip103CompatibleBlock) == 0 { + rebalanceOps, err := kc.rebalanceOperation(ctx, chainConfig, idx) + if err != nil { + return nil, err + } + ops = append(ops, rebalanceOps...) + } + rewardTx = &RosettaTypes.Transaction{ TransactionIdentifier: &RosettaTypes.TransactionIdentifier{ Hash: block.Hash().String(), @@ -1615,7 +1625,7 @@ func (kc *Client) getRewardAndRatioInfo( return nil, nil, nil, fmt.Errorf("could not convert minting amount type from %s", minted) } - // Call `governance_getStakingInfo` to get KIR and KGF addresses. + // Call `governance_getStakingInfo` to get KCF and KFF addresses. var stakingInfo reward.StakingInfo err = kc.c.CallContext(ctx, &stakingInfo, "governance_getStakingInfo", block) if err != nil { @@ -1627,8 +1637,7 @@ func (kc *Client) getRewardAndRatioInfo( rewardAddresses := []string{rewardbase} var kgfAddress, kirAddress string - // TODO-Klaytn: Have to use stakingInfo.KGFAddr instead of stakingInfo.PoCAddr - if common.EmptyAddress(stakingInfo.PoCAddr) { + if common.EmptyAddress(stakingInfo.KFFAddr) { kgfAddress = rewardbase rewardAddresses = append(rewardAddresses, "") } else { @@ -1636,11 +1645,11 @@ func (kc *Client) getRewardAndRatioInfo( // For more info, please check the below source code. // nolint:lll // https://github.com/klaytn/klaytn/blob/7584e71de602ce0367a4fb4e19643b49b076b93c/reward/reward_distributor.go#L116-L121 - kgfAddress = stakingInfo.PoCAddr.String() + kgfAddress = stakingInfo.KFFAddr.String() rewardAddresses = append(rewardAddresses, kgfAddress) } - if common.EmptyAddress(stakingInfo.KIRAddr) { + if common.EmptyAddress(stakingInfo.KCFAddr) { kirAddress = rewardbase rewardAddresses = append(rewardAddresses, "") } else { @@ -1648,7 +1657,7 @@ func (kc *Client) getRewardAndRatioInfo( // For more info, please check the below source code. // nolint:lll // https://github.com/klaytn/klaytn/blob/7584e71de602ce0367a4fb4e19643b49b076b93c/reward/reward_distributor.go#L123-L127 - kirAddress = stakingInfo.KIRAddr.String() + kirAddress = stakingInfo.KCFAddr.String() rewardAddresses = append(rewardAddresses, kirAddress) } @@ -1758,6 +1767,147 @@ func (kc *Client) blockRewardTransaction( }, rewardAddresses, rewardRatioMap, nil } +/* + type for kip103 treasury-rebalance contract's memo + { + "retirees": [ + { + "retired": "0x2bcf9d3e4a846015e7e3152a614c684de16f37c6", + "balance": 423137197918247524183438005 + }, + { + "retired": "0x716f89d9bc333286c79db4ebb05516897c8d208a", + "balance": 125112416844433105491822174 + }, + { + "retired": "0x571f50dFD1c92C46CD4CECC540e9214Ff5B3421e", + "balance": 100000000000000000000 + } + ], + "newbies": [ + { + "newbie": "0xaa8d19a5e17e9e1bA693f13aB0E079d274a7e51E", + "fundAllocated": 300000000000000000000000000 + }, + { + "newbie": "0x8B537f5BC7d176a94D7bF63BeFB81586EB3D1c0E", + "fundAllocated": 50000000000000000000000000 + }, + { + "newbie": "0x47E3DbB8c1602BdB0DAeeE89Ce59452c4746CA1C", + "fundAllocated": 70000000000000000000000000 + } + ], + "burnt": 128249714762680629675260179, + "success": true +} +*/ +type kip103Memo struct { + Retirees []struct { + Retired string `json:"retired"` + Balance *big.Int `json:"balance"` + } `json:"retirees"` + Newbies []struct { + Newbie string `json:"newbie"` + FundAllocated *big.Int `json:"fundAllocated"` + } `json:"newbies"` + Burnt *big.Int `json:"burnt"` + Success bool `json:"success"` +} + +/* + get memo() from treasuray rebalance contract + https://github.com/klaytn/treasury-rebalance/blob/main/contracts/TreasuryRebalance.sol + parse the memo and make it to rosetta operation of the block reward transaction +*/ + +func (kc *Client) rebalanceOperation( + ctx context.Context, + chainConfig params.ChainConfig, + idx int64, +) ([]*RosettaTypes.Operation, error) { + var ops []*RosettaTypes.Operation + params := map[string]interface{}{ + "to": chainConfig.Kip103ContractAddress, + "input": "0x58c3b870", // Function Signature for memo() + } + + // ABI for Treasury Rebalance Contract + abi, err := abi.JSON(strings.NewReader(kip103.TreasuryRebalanceABI)) + if err != nil { + return nil, err + } + + // contract call for Treasury Rebalance Contract + // convert response -> string -> abi unpack -> JSON + resp, err := kc.contractCall(ctx, params) + if err != nil { + return nil, err + } + + result, ok := resp["result"].(string) + if !ok { + return nil, errors.New("could not convert result to String") + } + + encb, err := hex.DecodeString(result) + if err != nil { + return nil, err + } + + var memoStr string + err = abi.Unpack(&memoStr, "memo", encb) + if err != nil { + return nil, err + } + + var memoResult kip103Memo + err = json.Unmarshal([]byte(memoStr), &memoResult) + if err != nil { + return nil, err + } + + for _, retiree := range memoResult.Retirees { + retiredBalanceOp := &RosettaTypes.Operation{ + OperationIdentifier: &RosettaTypes.OperationIdentifier{ + Index: idx, + }, + Type: BlockRewardOpType, + Status: RosettaTypes.String(SuccessStatus), + Account: &RosettaTypes.AccountIdentifier{ + Address: MustChecksum(retiree.Retired), + }, + Amount: &RosettaTypes.Amount{ + Value: new(big.Int).Neg(retiree.Balance).String(), + Currency: Currency, + }, + } + ops = append(ops, retiredBalanceOp) + idx++ + } + + for _, newbie := range memoResult.Newbies { + newbieFundOp := &RosettaTypes.Operation{ + OperationIdentifier: &RosettaTypes.OperationIdentifier{ + Index: idx, + }, + Type: BlockRewardOpType, + Status: RosettaTypes.String(SuccessStatus), + Account: &RosettaTypes.AccountIdentifier{ + Address: MustChecksum(newbie.Newbie), + }, + Amount: &RosettaTypes.Amount{ + Value: newbie.FundAllocated.String(), + Currency: Currency, + }, + } + ops = append(ops, newbieFundOp) + idx++ + } + return ops, nil + +} + type rpcProgress struct { StartingBlock hexutil.Uint64 CurrentBlock hexutil.Uint64 diff --git a/services/network_service_test.go b/services/network_service_test.go index 7911043..3de8a3a 100644 --- a/services/network_service_test.go +++ b/services/network_service_test.go @@ -32,7 +32,7 @@ import ( var ( // rosetta-klaytn version - middlewareVersion = "1.0.5" + middlewareVersion = "1.0.7" defaultNetworkOptions = &types.NetworkOptionsResponse{ Version: &types.Version{