Skip to content

Commit

Permalink
[rosetta-klaytn]_release/v1.0.7 (#161)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
github-actions[bot] and JayChoi1736 authored May 9, 2023
1 parent ccce1a0 commit 1c691e7
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 6 additions & 5 deletions .github/workflows/DEV.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 ./...
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
2 changes: 1 addition & 1 deletion configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
166 changes: 158 additions & 8 deletions klaytn/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package klaytn

import (
"context"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
Expand All @@ -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"
)
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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 {
Expand All @@ -1627,28 +1637,27 @@ 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 {
// If PoC(=KGF) address is empty, reward must be given to reward base(= block proposer).
// 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 {
// If KIR address is empty, reward must be given to reward base(= block proposer).
// 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)
}

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion services/network_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (

var (
// rosetta-klaytn version
middlewareVersion = "1.0.5"
middlewareVersion = "1.0.7"

defaultNetworkOptions = &types.NetworkOptionsResponse{
Version: &types.Version{
Expand Down

0 comments on commit 1c691e7

Please sign in to comment.