refactor: change some format and coding style base on the review suggetion

pull/3649/head
xiaohuo 4 years ago committed by Leo Chen
parent 3140f75d16
commit 1af411a227
  1. 51
      rosetta/services/account.go
  2. 2
      rosetta/services/block_side_effect.go
  3. 83
      rosetta/services/tx_operation.go
  4. 2
      rosetta/services/tx_operation_test.go

@ -93,37 +93,39 @@ func (s *AccountAPI) getStakingBalance(
) (*big.Int, *types.Error) {
balance := new(big.Int)
ty, exist := subAccount.Metadata["type"]
if exist {
switch ty.(string) {
case Delegation:
validatorAddr := subAccount.Address
validators, delegations := s.hmy.GetDelegationsByDelegatorByBlock(addr, block)
for index, validator := range validators {
if validatorAddr == internalCommon.MustAddressToBech32(validator) {
balance = new(big.Int).Add(balance, delegations[index].Amount)
}
if !exist {
return nil, common.NewError(common.SanityCheckError, map[string]interface{}{
"message": "invalid sub account",
})
}
switch ty.(string) {
case Delegation:
validatorAddr := subAccount.Address
validators, delegations := s.hmy.GetDelegationsByDelegatorByBlock(addr, block)
for index, validator := range validators {
if validatorAddr == internalCommon.MustAddressToBech32(validator) {
balance = new(big.Int).Add(balance, delegations[index].Amount)
}
case UnDelegation:
validatorAddr := subAccount.Address
validators, delegations := s.hmy.GetDelegationsByDelegatorByBlock(addr, block)
for index, validator := range validators {
if validatorAddr == internalCommon.MustAddressToBech32(validator) {
undelegations := delegations[index].Undelegations
for _, undelegate := range undelegations {
balance = new(big.Int).Add(balance, undelegate.Amount)
}
}
case UnDelegation:
validatorAddr := subAccount.Address
validators, delegations := s.hmy.GetDelegationsByDelegatorByBlock(addr, block)
for index, validator := range validators {
if validatorAddr == internalCommon.MustAddressToBech32(validator) {
undelegations := delegations[index].Undelegations
for _, undelegate := range undelegations {
balance = new(big.Int).Add(balance, undelegate.Amount)
}
}
default:
return nil, common.NewError(common.SanityCheckError, map[string]interface{}{
"message": "invalid sub account type",
})
}
} else {
default:
return nil, common.NewError(common.SanityCheckError, map[string]interface{}{
"message": "invalid sub account",
"message": "invalid sub account type",
})
}
return balance, nil
}
@ -155,7 +157,6 @@ func newAccountIdentifier(
}, nil
}
// newSubAccountIdentifier ..
func newSubAccountIdentifier(
address ethCommon.Address, metadata map[string]interface{},
) (*types.SubAccountIdentifier, *types.Error) {

@ -113,7 +113,7 @@ func (s *BlockAPI) getSideEffectTransaction(
"message": err.Error(),
})
}
ops, rosettaError := GetSideEffectOperationsFromUndelegationPayouts(payouts, startingOpIndex)
ops, rosettaError := getSideEffectOperationsFromUndelegationPayouts(payouts, startingOpIndex)
if rosettaError != nil {
return nil, rosettaError
}

@ -138,40 +138,12 @@ func GetNativeOperationsFromStakingTransaction(
// expose delegated balance
if tx.StakingType() == stakingTypes.DirectiveDelegate {
op2 := GetDelegateOperationForSubAccount(tx, operations[1])
op2 := getDelegateOperationForSubAccount(tx, operations[1])
return append(operations, op2), nil
}
if tx.StakingType() == stakingTypes.DirectiveUndelegate {
// set sub account
validatorAddress := operations[1].Metadata["validatorAddress"]
operations[1].Account.SubAccount = &types.SubAccountIdentifier{
Address: validatorAddress.(string),
Metadata: map[string]interface{}{
SubAccountMetadataKey: Delegation,
},
}
op2 := &types.Operation{
OperationIdentifier: &types.OperationIdentifier{
Index: operations[1].OperationIdentifier.Index + 1,
},
Type: tx.StakingType().String(),
Status: GetTransactionStatus(tx, receipt),
Account: &types.AccountIdentifier{
Address: operations[1].Account.Address,
SubAccount: &types.SubAccountIdentifier{
Address: validatorAddress.(string),
Metadata: map[string]interface{}{
SubAccountMetadataKey: UnDelegation,
},
},
Metadata: operations[1].Account.Metadata,
},
Amount: operations[1].Amount,
Metadata: operations[1].Metadata,
}
op2 := getUndelegateOperationForSubAccount(tx, operations[1], receipt)
return append(operations, op2), nil
}
@ -179,7 +151,44 @@ func GetNativeOperationsFromStakingTransaction(
}
func GetDelegateOperationForSubAccount(tx *stakingTypes.StakingTransaction, delegateOperation *types.Operation) *types.Operation {
func getUndelegateOperationForSubAccount(
tx *stakingTypes.StakingTransaction, delegateOperation *types.Operation, receipt *hmytypes.Receipt,
) *types.Operation {
// set sub account
validatorAddress := delegateOperation.Metadata["validatorAddress"]
delegateOperation.Account.SubAccount = &types.SubAccountIdentifier{
Address: validatorAddress.(string),
Metadata: map[string]interface{}{
SubAccountMetadataKey: Delegation,
},
}
undelegateion := &types.Operation{
OperationIdentifier: &types.OperationIdentifier{
Index: delegateOperation.OperationIdentifier.Index + 1,
},
Type: tx.StakingType().String(),
Status: GetTransactionStatus(tx, receipt),
Account: &types.AccountIdentifier{
Address: delegateOperation.Account.Address,
SubAccount: &types.SubAccountIdentifier{
Address: validatorAddress.(string),
Metadata: map[string]interface{}{
SubAccountMetadataKey: UnDelegation,
},
},
Metadata: delegateOperation.Account.Metadata,
},
Amount: delegateOperation.Amount,
Metadata: delegateOperation.Metadata,
}
return undelegateion
}
func getDelegateOperationForSubAccount(
tx *stakingTypes.StakingTransaction, delegateOperation *types.Operation,
) *types.Operation {
amt, _ := new(big.Int).SetString(delegateOperation.Amount.Value, 10)
delegateAmt := new(big.Int).Sub(new(big.Int).SetUint64(0), amt)
validatorAddress := delegateOperation.Metadata["validatorAddress"]
@ -216,12 +225,12 @@ func GetDelegateOperationForSubAccount(tx *stakingTypes.StakingTransaction, dele
}
// GetSideEffectOperationsFromUndelegationPayouts from the given payouts.
// getSideEffectOperationsFromUndelegationPayouts from the given payouts.
// If the startingOperationIndex is provided, all operations will be indexed starting from the given operation index.
func GetSideEffectOperationsFromUndelegationPayouts(
func getSideEffectOperationsFromUndelegationPayouts(
payouts *hmy.UndelegationPayouts, startingOperationIndex *int64,
) ([]*types.Operation, *types.Error) {
return getSideEffectOperationsFromUndelegationPayouts(
return getSideEffectOperationsFromUndelegationPayoutsMap(
payouts, common.UndelegationPayoutOperation, startingOperationIndex,
)
}
@ -429,8 +438,6 @@ func getCrossShardSenderTransferNativeOperations(
var opIndex int64
if startingOperationIndex != nil {
opIndex = *startingOperationIndex
} else {
opIndex = 0
}
return []*types.Operation{
@ -451,7 +458,7 @@ func getCrossShardSenderTransferNativeOperations(
}
// delegator address => validator address => amount
func getSideEffectOperationsFromUndelegationPayouts(
func getSideEffectOperationsFromUndelegationPayoutsMap(
undelegationPayouts *hmy.UndelegationPayouts, opType string, startingOperationIndex *int64,
) ([]*types.Operation, *types.Error) {
var opIndex int64
@ -498,7 +505,7 @@ func getSideEffectOperationsFromUndelegationPayouts(
return operations, nil
}
// getOperationAndTotalAmountFromUndelegationMap is a helper for getSideEffectOperationsFromUndelegationPayouts which actually
// getOperationAndTotalAmountFromUndelegationMap is a helper for getSideEffectOperationsFromUndelegationPayoutsMap which actually
// has some side effect(opIndex will be increased by this function) so be careful while using for other purpose
func getOperationAndTotalAmountFromUndelegationMap(
delegator ethcommon.Address, opIndex *int64, relatedOpIdentifier *types.OperationIdentifier, opType string,

@ -249,7 +249,7 @@ func TestGetSideEffectOperationsFromUndelegationPayouts(t *testing.T) {
delegator := ethcommon.HexToAddress("0xB5f440B5c6215eEDc1b2E12b4b964fa31f7afa7d")
validator := ethcommon.HexToAddress("0x3b8DE43c8F30D3C387840681FED67783f93f1F94")
undelegationPayouts.SetPayoutByDelegatorAddrAndValidatorAddr(delegator, validator, new(big.Int).SetInt64(4000))
operations, err := GetSideEffectOperationsFromUndelegationPayouts(undelegationPayouts, &startingOperationIndex)
operations, err := getSideEffectOperationsFromUndelegationPayouts(undelegationPayouts, &startingOperationIndex)
if err != nil {
t.Fatal(err)
}

Loading…
Cancel
Save