-
Notifications
You must be signed in to change notification settings - Fork 324
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[staking] implement staking abi v3 #4273
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package v3 | ||
|
||
import ( | ||
"encoding/hex" | ||
|
||
"github.com/iotexproject/iotex-core/action/protocol" | ||
stakingComm "github.com/iotexproject/iotex-core/action/protocol/staking/ethabi/common" | ||
) | ||
|
||
func BuildReadStateRequest(data []byte) (protocol.StateContext, error) { | ||
switch methodSig := hex.EncodeToString(data[:4]); methodSig { | ||
case hex.EncodeToString(_compositeBucketsMethod.ID): | ||
return newCompositeBucketsStateContext(data[4:]) | ||
case hex.EncodeToString(_compositeBucketsByCandidateMethod.ID): | ||
return newCompositeBucketsByCandidateStateContext(data[4:]) | ||
case hex.EncodeToString(_compositeBucketsByIndexesMethod.ID): | ||
return newCompositeBucketsByIndexesStateContext(data[4:]) | ||
case hex.EncodeToString(_compositeBucketsByVoterMethod.ID): | ||
return newCompositeBucketsByVoterStateContext(data[4:]) | ||
default: | ||
return nil, stakingComm.ErrInvalidCallSig | ||
} | ||
} |
129 changes: 129 additions & 0 deletions
129
action/protocol/staking/ethabi/v3/stake_composite_buckets.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package v3 | ||
|
||
import ( | ||
"github.com/ethereum/go-ethereum/accounts/abi" | ||
"github.com/iotexproject/iotex-proto/golang/iotexapi" | ||
|
||
"github.com/iotexproject/iotex-core/action/protocol" | ||
"github.com/iotexproject/iotex-core/action/protocol/abiutil" | ||
stakingComm "github.com/iotexproject/iotex-core/action/protocol/staking/ethabi/common" | ||
) | ||
|
||
const _compositeBucketsInterfaceABI = `[ | ||
{ | ||
"inputs": [ | ||
{ | ||
"internalType": "uint32", | ||
"name": "offset", | ||
"type": "uint32" | ||
}, | ||
{ | ||
"internalType": "uint32", | ||
"name": "limit", | ||
"type": "uint32" | ||
} | ||
], | ||
"name": "compositeBucketsV3", | ||
"outputs": [ | ||
{ | ||
"components": [ | ||
{ | ||
"internalType": "uint64", | ||
"name": "index", | ||
"type": "uint64" | ||
}, | ||
{ | ||
"internalType": "address", | ||
"name": "candidateAddress", | ||
"type": "address" | ||
}, | ||
{ | ||
"internalType": "uint256", | ||
"name": "stakedAmount", | ||
"type": "uint256" | ||
}, | ||
{ | ||
"internalType": "uint32", | ||
"name": "stakedDuration", | ||
"type": "uint32" | ||
}, | ||
{ | ||
"internalType": "int64", | ||
"name": "createTime", | ||
"type": "int64" | ||
}, | ||
{ | ||
"internalType": "int64", | ||
"name": "stakeStartTime", | ||
"type": "int64" | ||
}, | ||
{ | ||
"internalType": "int64", | ||
"name": "unstakeStartTime", | ||
"type": "int64" | ||
}, | ||
{ | ||
"internalType": "bool", | ||
"name": "autoStake", | ||
"type": "bool" | ||
}, | ||
{ | ||
"internalType": "address", | ||
"name": "owner", | ||
"type": "address" | ||
}, | ||
{ | ||
"internalType": "address", | ||
"name": "contractAddress", | ||
"type": "address" | ||
}, | ||
{ | ||
"internalType": "uint64", | ||
"name": "stakedDurationBlockNumber", | ||
"type": "uint64" | ||
}, | ||
{ | ||
"internalType": "uint64", | ||
"name": "createBlockHeight", | ||
"type": "uint64" | ||
}, | ||
{ | ||
"internalType": "uint64", | ||
"name": "stakeStartBlockHeight", | ||
"type": "uint64" | ||
}, | ||
{ | ||
"internalType": "uint64", | ||
"name": "unstakeStartBlockHeight", | ||
"type": "uint64" | ||
}, | ||
{ | ||
"internalType": "uint64", | ||
"name": "endorsementExpireBlockHeight", | ||
"type": "uint64" | ||
} | ||
], | ||
"internalType": "struct IStaking.CompositeVoteBucket[]", | ||
"name": "", | ||
"type": "tuple[]" | ||
} | ||
], | ||
"stateMutability": "view", | ||
"type": "function" | ||
} | ||
]` | ||
|
||
var _compositeBucketsMethod abi.Method | ||
|
||
func init() { | ||
_compositeBucketsMethod = abiutil.MustLoadMethod(_compositeBucketsInterfaceABI, "compositeBucketsV3") | ||
} | ||
|
||
// CompositeBucketsStateContext context for Composite Buckets | ||
type CompositeBucketsStateContext struct { | ||
*protocol.BaseStateContext | ||
} | ||
|
||
func newCompositeBucketsStateContext(data []byte) (*stakingComm.BucketsStateContext, error) { | ||
return stakingComm.NewBucketsStateContext(data, &_compositeBucketsMethod, iotexapi.ReadStakingDataMethod_COMPOSITE_BUCKETS) | ||
} |
100 changes: 100 additions & 0 deletions
100
action/protocol/staking/ethabi/v3/stake_composite_buckets_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package v3 | ||
|
||
import ( | ||
"encoding/hex" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/iotexproject/iotex-proto/golang/iotexapi" | ||
"github.com/iotexproject/iotex-proto/golang/iotextypes" | ||
"github.com/stretchr/testify/require" | ||
"google.golang.org/protobuf/proto" | ||
"google.golang.org/protobuf/types/known/timestamppb" | ||
|
||
stakingComm "github.com/iotexproject/iotex-core/action/protocol/staking/ethabi/common" | ||
) | ||
|
||
func TestBuildReadStateRequestCompositeBuckets(t *testing.T) { | ||
r := require.New(t) | ||
|
||
// data, err := _compositeBucketsMethod.Inputs.Pack(uint32(1), uint32(5)) | ||
// r.NoError(err) | ||
// data = append(_compositeBucketsMethod.ID, data...) | ||
// t.Logf("data: %s", hex.EncodeToString(data)) | ||
|
||
data, _ := hex.DecodeString("520f1bdc00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000005") | ||
req, err := BuildReadStateRequest(data) | ||
|
||
r.Nil(err) | ||
r.EqualValues("*common.BucketsStateContext", reflect.TypeOf(req).String()) | ||
|
||
method := &iotexapi.ReadStakingDataMethod{ | ||
Method: iotexapi.ReadStakingDataMethod_COMPOSITE_BUCKETS, | ||
} | ||
methodBytes, _ := proto.Marshal(method) | ||
r.EqualValues(methodBytes, req.Parameters().MethodName) | ||
|
||
arguments := &iotexapi.ReadStakingDataRequest{ | ||
Request: &iotexapi.ReadStakingDataRequest_Buckets{ | ||
Buckets: &iotexapi.ReadStakingDataRequest_VoteBuckets{ | ||
Pagination: &iotexapi.PaginationParam{ | ||
Offset: 1, | ||
Limit: 5, | ||
}, | ||
}, | ||
}, | ||
} | ||
argumentsBytes, _ := proto.Marshal(arguments) | ||
r.EqualValues([][]byte{argumentsBytes}, req.Parameters().Arguments) | ||
} | ||
|
||
func TestEncodeCompositeVoteBucketListToEth(t *testing.T) { | ||
r := require.New(t) | ||
|
||
buckets := make([]*iotextypes.VoteBucket, 3) | ||
|
||
buckets[0] = &iotextypes.VoteBucket{ | ||
Index: 1, | ||
CandidateAddress: "io1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqryn4k9fw", | ||
StakedAmount: "1000000000000000000", | ||
StakedDuration: 1_000_000, | ||
CreateTime: ×tamppb.Timestamp{Seconds: 1_000_000_000}, | ||
StakeStartTime: ×tamppb.Timestamp{Seconds: 1_000_000_001}, | ||
UnstakeStartTime: ×tamppb.Timestamp{Seconds: 1_000_000_002}, | ||
AutoStake: true, | ||
Owner: "io1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqxgce2xkh", | ||
ContractAddress: "", | ||
} | ||
buckets[1] = &iotextypes.VoteBucket{ | ||
Index: 2, | ||
CandidateAddress: "io1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqr9wrzs5u", | ||
StakedAmount: "2000000000000000000", | ||
AutoStake: false, | ||
Owner: "io1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqxf907nt9", | ||
ContractAddress: "io1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqxf907nt9", | ||
StakedDurationBlockNumber: 1_000_000, | ||
CreateBlockHeight: 1_000_000_000, | ||
StakeStartBlockHeight: 1_000_000_001, | ||
UnstakeStartBlockHeight: 1_000_000_002, | ||
} | ||
buckets[2] = &iotextypes.VoteBucket{ | ||
Index: 2, | ||
CandidateAddress: "io1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqr9wrzs5u", | ||
StakedAmount: "2000000000000000000", | ||
AutoStake: false, | ||
Owner: "io1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqxf907nt9", | ||
ContractAddress: "io1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqxf907nt9", | ||
StakedDurationBlockNumber: 1_000_000, | ||
CreateBlockHeight: 1_000_000_000, | ||
StakeStartBlockHeight: 1_000_000_001, | ||
UnstakeStartBlockHeight: 1_000_000_002, | ||
EndorsementExpireBlockHeight: 300_000_000, | ||
} | ||
|
||
data, err := stakingComm.EncodeVoteBucketListToEth(_compositeBucketsMethod.Outputs, &iotextypes.VoteBucketList{ | ||
Buckets: buckets, | ||
}) | ||
// t.Logf("data: %s\n", data) | ||
r.Nil(err) | ||
r.EqualValues("00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000640000000000000000000000000000000000000000000000000de0b6b3a764000000000000000000000000000000000000000000000000000000000000000f4240000000000000000000000000000000000000000000000000000000003b9aca00000000000000000000000000000000000000000000000000000000003b9aca01000000000000000000000000000000000000000000000000000000003b9aca02000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000c8000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000650000000000000000000000000000000000000000000000001bc16d674ec800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c900000000000000000000000000000000000000000000000000000000000000c900000000000000000000000000000000000000000000000000000000000f4240000000000000000000000000000000000000000000000000000000003b9aca00000000000000000000000000000000000000000000000000000000003b9aca01000000000000000000000000000000000000000000000000000000003b9aca020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000650000000000000000000000000000000000000000000000001bc16d674ec800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c900000000000000000000000000000000000000000000000000000000000000c900000000000000000000000000000000000000000000000000000000000f4240000000000000000000000000000000000000000000000000000000003b9aca00000000000000000000000000000000000000000000000000000000003b9aca01000000000000000000000000000000000000000000000000000000003b9aca020000000000000000000000000000000000000000000000000000000000000000", data) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BuildReadStateContext
is a better name?also seems this func only used in test code in this PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is used at next commit
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
change the name? it's returning different new contexts