Skip to content

Commit

Permalink
Impove check transaction command (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
KonradStaniec authored Aug 21, 2024
1 parent 1606ca1 commit 18b5b71
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 21 deletions.
49 changes: 35 additions & 14 deletions cmd/stakercli/transaction/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,21 +76,26 @@ type StakingTxData struct {
FinalityProviderPublicKeyHex string `json:"finality_provider_public_key_hex"`
StakingAmount int64 `json:"staking_amount"`
StakingTimeBlocks int64 `json:"staking_time_blocks"`
// ParamsVersion is the version of the global parameters aginst which is valid
ParamsVersion int64 `json:"params_version"`
}

type ValidityInfo struct {
ParametersVersion uint64 `json:"parameters_version"`
IsValid bool `json:"is_valid"`
ErrMsg string `json:"err_msg,omitempty"`
StakingData *StakingTxData `json:"staking_data,omitempty"`
}

type CheckPhase1StakingTxResponse struct {
IsValid bool `json:"is_valid"`
// StakingData will only be populated if the transaction is valid
StakingData *StakingTxData `json:"staking_data"`
ValidityInfo []*ValidityInfo `json:"validity_info"`
}

func validateTxAgainstParams(
tx *wire.MsgTx,
globalParams *parser.ParsedGlobalParams,
net *chaincfg.Params) *CheckPhase1StakingTxResponse {

var info []*ValidityInfo

for i := len(globalParams.Versions) - 1; i >= 0; i-- {
params := globalParams.Versions[i]

Expand All @@ -102,32 +107,48 @@ func validateTxAgainstParams(
net,
)
if err != nil {
info = append(info, &ValidityInfo{
ParametersVersion: params.Version,
IsValid: false,
ErrMsg: fmt.Sprintf("error parsing tx: %s", err.Error()),
})
continue
}

if parsed.OpReturnData.StakingTime < params.MinStakingTime || parsed.OpReturnData.StakingTime > params.MaxStakingTime {
info = append(info, &ValidityInfo{
ParametersVersion: params.Version,
IsValid: false,
ErrMsg: fmt.Sprintf("staking time %d is out of bounds", parsed.OpReturnData.StakingTime),
})
continue
}

if btcutil.Amount(parsed.StakingOutput.Value) < params.MinStakingAmount || btcutil.Amount(parsed.StakingOutput.Value) > params.MaxStakingAmount {
info = append(info, &ValidityInfo{
ParametersVersion: params.Version,
IsValid: false,
ErrMsg: fmt.Sprintf("staking amount %d is out of bounds", parsed.StakingOutput.Value),
})
continue
}

// At this point we know staking transaction is valid against this version of global params
return &CheckPhase1StakingTxResponse{
IsValid: true,
info = append(info, &ValidityInfo{
ParametersVersion: params.Version,
IsValid: true,
StakingData: &StakingTxData{
StakerPublicKeyHex: hex.EncodeToString(parsed.OpReturnData.StakerPublicKey.Marshall()),
FinalityProviderPublicKeyHex: hex.EncodeToString(parsed.OpReturnData.FinalityProviderPublicKey.Marshall()),
StakerPublicKeyHex: hex.EncodeToString(schnorr.SerializePubKey(parsed.OpReturnData.StakerPublicKey.PubKey)),
FinalityProviderPublicKeyHex: hex.EncodeToString(schnorr.SerializePubKey(parsed.OpReturnData.FinalityProviderPublicKey.PubKey)),
StakingAmount: parsed.StakingOutput.Value,
StakingTimeBlocks: int64(parsed.OpReturnData.StakingTime),
ParamsVersion: int64(params.Version),
},
}
}})

// We found latest valid version, no need to check further
break
}

return &CheckPhase1StakingTxResponse{
IsValid: false,
ValidityInfo: info,
}
}

Expand Down
16 changes: 9 additions & 7 deletions cmd/stakercli/transaction/transactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,13 +434,15 @@ func FuzzCheckPhase1Tx(f *testing.F) {
r, t, app, checkArgs,
)
require.NotNil(t, resCheck)
require.True(t, resCheck.IsValid)
require.NotNil(t, resCheck.StakingData)
require.Equal(t, globalParams.Versions[0].Version, uint64(resCheck.StakingData.ParamsVersion))
require.Equal(t, stakerParams.StakingAmount, btcutil.Amount(resCheck.StakingData.StakingAmount))
require.Equal(t, stakerParams.StakingTime, uint16(resCheck.StakingData.StakingTimeBlocks))
require.Equal(t, keyToSchnorrHex(stakerParams.StakerPk), resCheck.StakingData.StakerPublicKeyHex)
require.Equal(t, keyToSchnorrHex(stakerParams.FinalityProviderPk), resCheck.StakingData.FinalityProviderPublicKeyHex)
require.Len(t, resCheck.ValidityInfo, 1)

require.True(t, resCheck.ValidityInfo[0].IsValid)
require.NotNil(t, resCheck.ValidityInfo[0].StakingData)
require.Equal(t, globalParams.Versions[0].Version, resCheck.ValidityInfo[0].ParametersVersion)
require.Equal(t, stakerParams.StakingAmount, btcutil.Amount(resCheck.ValidityInfo[0].StakingData.StakingAmount))
require.Equal(t, stakerParams.StakingTime, uint16(resCheck.ValidityInfo[0].StakingData.StakingTimeBlocks))
require.Equal(t, keyToSchnorrHex(stakerParams.StakerPk), resCheck.ValidityInfo[0].StakingData.StakerPublicKeyHex)
require.Equal(t, keyToSchnorrHex(stakerParams.FinalityProviderPk), resCheck.ValidityInfo[0].StakingData.FinalityProviderPublicKeyHex)
})
}

Expand Down

0 comments on commit 18b5b71

Please sign in to comment.