Skip to content

Commit

Permalink
chore: Rename jury to covenant (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
gitferry authored Nov 9, 2023
1 parent 4a3cc44 commit a680fcc
Show file tree
Hide file tree
Showing 34 changed files with 693 additions and 692 deletions.
38 changes: 19 additions & 19 deletions btcstaking/staking.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func makeScriptNum(v []byte, requireMinimal bool, scriptNumLen int) (int64, erro
type StakingScriptData struct {
StakerKey *btcec.PublicKey
ValidatorKey *btcec.PublicKey
JuryKey *btcec.PublicKey
CovenantKey *btcec.PublicKey
StakingTime uint16
}

Expand All @@ -126,17 +126,17 @@ type StakingOutputInfo struct {
func NewStakingScriptData(
stakerKey,
validatorKey,
juryKey *btcec.PublicKey,
covenantKey *btcec.PublicKey,
stakingTime uint16) (*StakingScriptData, error) {

if stakerKey == nil || validatorKey == nil || juryKey == nil {
return nil, fmt.Errorf("staker, validator and jury keys cannot be nil")
if stakerKey == nil || validatorKey == nil || covenantKey == nil {
return nil, fmt.Errorf("staker, validator and covenant keys cannot be nil")
}

return &StakingScriptData{
StakerKey: stakerKey,
ValidatorKey: validatorKey,
JuryKey: juryKey,
CovenantKey: covenantKey,
StakingTime: stakingTime,
}, nil
}
Expand All @@ -145,7 +145,7 @@ func NewStakingScriptData(
// <StakerKey> OP_CHECKSIG
// OP_NOTIF
//
// <StakerKey> OP_CHECKSIG <ValidatorKey> OP_CHECKSIGADD <JuryKey> OP_CHECKSIGADD 3 OP_NUMEQUAL
// <StakerKey> OP_CHECKSIG <ValidatorKey> OP_CHECKSIGADD <CovenantKey> OP_CHECKSIGADD 3 OP_NUMEQUAL
//
// OP_ELSE
//
Expand All @@ -161,7 +161,7 @@ func (sd *StakingScriptData) BuildStakingScript() ([]byte, error) {
builder.AddOp(txscript.OP_CHECKSIG)
builder.AddData(schnorr.SerializePubKey(sd.ValidatorKey))
builder.AddOp(txscript.OP_CHECKSIGADD)
builder.AddData(schnorr.SerializePubKey(sd.JuryKey))
builder.AddData(schnorr.SerializePubKey(sd.CovenantKey))
builder.AddOp(txscript.OP_CHECKSIGADD)
builder.AddInt64(expectedMultiSigSigners)
builder.AddOp(txscript.OP_NUMEQUAL)
Expand All @@ -181,7 +181,7 @@ func ParseStakingTransactionScript(script []byte) (*StakingScriptData, error) {
// <StakerKey> OP_CHECKSIG
// OP_NOTIF
//
// <StakerKey> OP_CHECKSIG <ValidatorKey> OP_CHECKSIGADD <JuryKey> OP_CHECKSIGADD 3 OP_NUMEQUAL
// <StakerKey> OP_CHECKSIG <ValidatorKey> OP_CHECKSIGADD <CovenantKey> OP_CHECKSIGADD 3 OP_NUMEQUAL
//
// OP_ELSE
//
Expand Down Expand Up @@ -284,8 +284,8 @@ func ParseStakingTransactionScript(script []byte) (*StakingScriptData, error) {
return nil, err
}

// Jury public key
juryPk, err := schnorr.ParsePubKey(template[7].extractedData)
// Covenant public key
covenantPk, err := schnorr.ParsePubKey(template[7].extractedData)

if err != nil {
return nil, err
Expand All @@ -305,7 +305,7 @@ func ParseStakingTransactionScript(script []byte) (*StakingScriptData, error) {
scriptData, err := NewStakingScriptData(
stakerPk1,
validatorPk,
juryPk,
covenantPk,
uint16(template[12].extractedInt),
)

Expand Down Expand Up @@ -376,12 +376,12 @@ func BuildUnspendableTaprootPkScript(rawScript []byte, net *chaincfg.Params) ([]
func BuildStakingOutput(
stakerKey,
validatorKey,
juryKey *btcec.PublicKey,
covenantKey *btcec.PublicKey,
stTime uint16,
stAmount btcutil.Amount,
net *chaincfg.Params) (*wire.TxOut, []byte, error) {

sd, err := NewStakingScriptData(stakerKey, validatorKey, juryKey, stTime)
sd, err := NewStakingScriptData(stakerKey, validatorKey, covenantKey, stTime)

if err != nil {
return nil, nil, err
Expand Down Expand Up @@ -705,39 +705,39 @@ func CheckTransactions(
return nil, fmt.Errorf("slashing transaction min fee must be larger than 0")
}

//1. Check slashing tx
// 1. Check slashing tx
if err := IsSlashingTx(slashingTx, slashingAddress); err != nil {
return nil, err
}

//2. Check staking script.
// 2. Check staking script.
scriptData, err := ParseStakingTransactionScript(script)

if err != nil {
return nil, err
}

//3. Check that staking transaction has output committing to the provided script
// 3. Check that staking transaction has output committing to the provided script
stakingOutputIdx, err := GetIdxOutputCommitingToScript(fundingTransaction, script, net)

if err != nil {
return nil, err
}

//4. Check that slashing transaction input is pointing to staking transaction
// 4. Check that slashing transaction input is pointing to staking transaction
stakingTxHash := fundingTransaction.TxHash()
if !slashingTx.TxIn[0].PreviousOutPoint.Hash.IsEqual(&stakingTxHash) {
return nil, fmt.Errorf("slashing transaction must spend staking output")
}

//5. Check that index of the fund output matches index of the input in slashing transaction
// 5. Check that index of the fund output matches index of the input in slashing transaction
if slashingTx.TxIn[0].PreviousOutPoint.Index != uint32(stakingOutputIdx) {
return nil, fmt.Errorf("slashing transaction input must spend staking output")
}

stakingOutput := fundingTransaction.TxOut[stakingOutputIdx]

//6. Check fees
// 6. Check fees
if slashingTx.TxOut[0].Value <= 0 || stakingOutput.Value <= 0 {
return nil, fmt.Errorf("values of slashing and staking transaction must be larger than 0")
}
Expand Down
26 changes: 13 additions & 13 deletions btcstaking/staking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ import (
func genValidStakingScriptData(t *testing.T, r *rand.Rand) *btcstaking.StakingScriptData {
stakerPrivKeyBytes := datagen.GenRandomByteArray(r, 32)
validatorPrivKeyBytes := datagen.GenRandomByteArray(r, 32)
juryPrivKeyBytes := datagen.GenRandomByteArray(r, 32)
covenantPrivKeyBytes := datagen.GenRandomByteArray(r, 32)
stakingTime := uint16(r.Intn(math.MaxUint16))

_, stakerPublicKey := btcec.PrivKeyFromBytes(stakerPrivKeyBytes)
_, validatorPublicKey := btcec.PrivKeyFromBytes(validatorPrivKeyBytes)
_, juryPublicKey := btcec.PrivKeyFromBytes(juryPrivKeyBytes)
_, covenantPublicKey := btcec.PrivKeyFromBytes(covenantPrivKeyBytes)

sd, _ := btcstaking.NewStakingScriptData(stakerPublicKey, validatorPublicKey, juryPublicKey, stakingTime)
sd, _ := btcstaking.NewStakingScriptData(stakerPublicKey, validatorPublicKey, covenantPublicKey, stakingTime)

return sd
}
Expand All @@ -49,7 +49,7 @@ func FuzzGeneratingParsingValidStakingScript(f *testing.F) {
require.Equal(t, parsedScript.StakingTime, sd.StakingTime)
require.Equal(t, schnorr.SerializePubKey(sd.StakerKey), schnorr.SerializePubKey(parsedScript.StakerKey))
require.Equal(t, schnorr.SerializePubKey(sd.ValidatorKey), schnorr.SerializePubKey(parsedScript.ValidatorKey))
require.Equal(t, schnorr.SerializePubKey(sd.JuryKey), schnorr.SerializePubKey(parsedScript.JuryKey))
require.Equal(t, schnorr.SerializePubKey(sd.CovenantKey), schnorr.SerializePubKey(parsedScript.CovenantKey))
})
}

Expand All @@ -76,7 +76,7 @@ func FuzzGeneratingValidStakingSlashingTx(f *testing.F) {
stakingOutput, _, err := btcstaking.BuildStakingOutput(
sd.StakerKey,
sd.ValidatorKey,
sd.JuryKey,
sd.CovenantKey,
sd.StakingTime,
btcutil.Amount(r.Intn(5000)+minStakingValue),
&chaincfg.MainNetParams,
Expand Down Expand Up @@ -196,7 +196,7 @@ func TestStakingScriptExecutionSingleStaker(t *testing.T) {
validatorPrivKey, err := btcec.NewPrivateKey()
require.NoError(t, err)

juryPrivKey, err := btcec.NewPrivateKey()
covenantPrivKey, err := btcec.NewPrivateKey()
require.NoError(t, err)

txid, err := chainhash.NewHash(datagen.GenRandomByteArray(r, 32))
Expand All @@ -210,7 +210,7 @@ func TestStakingScriptExecutionSingleStaker(t *testing.T) {
stakingOutput, stakingScript, err := btcstaking.BuildStakingOutput(
stakerPrivKey.PubKey(),
validatorPrivKey.PubKey(),
juryPrivKey.PubKey(),
covenantPrivKey.PubKey(),
stakingTimeBlocks,
stakingValue,
&chaincfg.MainNetParams,
Expand Down Expand Up @@ -272,7 +272,7 @@ func TestStakingScriptExecutionMulitSig(t *testing.T) {
validatorPrivKey, err := btcec.NewPrivateKey()
require.NoError(t, err)

juryPrivKey, err := btcec.NewPrivateKey()
covenantPrivKey, err := btcec.NewPrivateKey()
require.NoError(t, err)

txid, err := chainhash.NewHash(datagen.GenRandomByteArray(r, 32))
Expand All @@ -286,7 +286,7 @@ func TestStakingScriptExecutionMulitSig(t *testing.T) {
stakingOutput, stakingScript, err := btcstaking.BuildStakingOutput(
stakerPrivKey.PubKey(),
validatorPrivKey.PubKey(),
juryPrivKey.PubKey(),
covenantPrivKey.PubKey(),
stakingTimeBlocks,
stakingValue,
&chaincfg.MainNetParams,
Expand Down Expand Up @@ -322,25 +322,25 @@ func TestStakingScriptExecutionMulitSig(t *testing.T) {

require.NoError(t, err)

witnessJury, err := btcstaking.BuildWitnessToSpendStakingOutput(
witnessCovenant, err := btcstaking.BuildWitnessToSpendStakingOutput(
spendStakeTx,
stakingOutput,
stakingScript,
juryPrivKey,
covenantPrivKey,
)

require.NoError(t, err)

// To Construct valid witness, for multisig case we need:
// - jury signature - witnessJury[0]
// - covenant signature - witnessCovenant[0]
// - validator signature - witnessValidator[0]
// - staker signature - witnessStaker[0]
// - empty signature - which is just an empty byte array which signals we are going to use multisig.
// This must be signagure on top of the stack.
// - whole script - witnessStaker[1] (any other wittness[1] will work as well)
// - control block - witnessStaker[2] (any other wittness[2] will work as well)
spendStakeTx.TxIn[0].Witness = [][]byte{
witnessJury[0], witnessValidator[0], witnessStaker[0], []byte{}, witnessStaker[1], witnessStaker[2],
witnessCovenant[0], witnessValidator[0], witnessStaker[0], []byte{}, witnessStaker[1], witnessStaker[2],
}

prevOutputFetcher := txscript.NewCannedPrevOutputFetcher(
Expand Down
10 changes: 5 additions & 5 deletions cmd/babylond/cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const (
flagBlocksPerYear = "blocks-per-year"
flagGenesisTime = "genesis-time"
flagBlockGasLimit = "block-gas-limit"
flagJuryPk = "jury-pk"
flagCovenantPk = "covenant-pk"
flagSlashingAddress = "slashing-address"
flagMinSlashingFee = "min-slashing-fee-sat"
flagSlashingRate = "slashing-rate"
Expand All @@ -52,7 +52,7 @@ type GenesisCLIArgs struct {
BlocksPerYear uint64
GenesisTime time.Time
BlockGasLimit int64
JuryPK string
CovenantPK string
SlashingAddress string
MinSlashingTransactionFeeSat int64
SlashingRate sdk.Dec
Expand All @@ -75,7 +75,7 @@ func addGenesisFlags(cmd *cobra.Command) {
cmd.Flags().String(flagBaseBtcHeaderHex, "0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a45068653ffff7f2002000000", "Hex of the base Bitcoin header.")
cmd.Flags().Uint64(flagBaseBtcHeaderHeight, 0, "Height of the base Bitcoin header.")
// btcstaking args
cmd.Flags().String(flagJuryPk, btcstypes.DefaultParams().JuryPk.MarshalHex(), "Bitcoin staking jury public key")
cmd.Flags().String(flagCovenantPk, btcstypes.DefaultParams().CovenantPk.MarshalHex(), "Bitcoin staking covenant public key")
cmd.Flags().String(flagSlashingAddress, btcstypes.DefaultParams().SlashingAddress, "Bitcoin staking slashing address")
cmd.Flags().Int64(flagMinSlashingFee, 1000, "Bitcoin staking minimum slashing fee")
cmd.Flags().String(flagMinCommissionRate, "0", "Bitcoin staking validator minimum commission rate")
Expand Down Expand Up @@ -103,7 +103,7 @@ func parseGenesisFlags(cmd *cobra.Command) *GenesisCLIArgs {
epochInterval, _ := cmd.Flags().GetUint64(flagEpochInterval)
baseBtcHeaderHex, _ := cmd.Flags().GetString(flagBaseBtcHeaderHex)
baseBtcHeaderHeight, _ := cmd.Flags().GetUint64(flagBaseBtcHeaderHeight)
juryPk, _ := cmd.Flags().GetString(flagJuryPk)
covenantPk, _ := cmd.Flags().GetString(flagCovenantPk)
slashingAddress, _ := cmd.Flags().GetString(flagSlashingAddress)
minSlashingFee, _ := cmd.Flags().GetInt64(flagMinSlashingFee)
minCommissionRate, _ := cmd.Flags().GetString(flagMinCommissionRate)
Expand Down Expand Up @@ -132,7 +132,7 @@ func parseGenesisFlags(cmd *cobra.Command) *GenesisCLIArgs {
EpochInterval: epochInterval,
BaseBtcHeaderHeight: baseBtcHeaderHeight,
BaseBtcHeaderHex: baseBtcHeaderHex,
JuryPK: juryPk,
CovenantPK: covenantPk,
SlashingAddress: slashingAddress,
MinSlashingTransactionFeeSat: minSlashingFee,
MinCommissionRate: sdk.MustNewDecFromStr(minCommissionRate),
Expand Down
6 changes: 3 additions & 3 deletions cmd/babylond/cmd/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ Example:
genesisParams = TestnetGenesisParams(genesisCliArgs.MaxActiveValidators,
genesisCliArgs.BtcConfirmationDepth, genesisCliArgs.BtcFinalizationTimeout, genesisCliArgs.CheckpointTag,
genesisCliArgs.EpochInterval, genesisCliArgs.BaseBtcHeaderHex,
genesisCliArgs.BaseBtcHeaderHeight, genesisCliArgs.JuryPK,
genesisCliArgs.BaseBtcHeaderHeight, genesisCliArgs.CovenantPK,
genesisCliArgs.SlashingAddress, genesisCliArgs.MinSlashingTransactionFeeSat,
genesisCliArgs.MinCommissionRate, genesisCliArgs.SlashingRate,
genesisCliArgs.MinPubRand, genesisCliArgs.InflationRateChange,
Expand Down Expand Up @@ -230,7 +230,7 @@ type GenesisParams struct {

func TestnetGenesisParams(maxActiveValidators uint32, btcConfirmationDepth uint64,
btcFinalizationTimeout uint64, checkpointTag string, epochInterval uint64, baseBtcHeaderHex string,
baseBtcHeaderHeight uint64, juryPk string, slashingAddress string, minSlashingFee int64,
baseBtcHeaderHeight uint64, covenantPk string, slashingAddress string, minSlashingFee int64,
minCommissionRate sdk.Dec, slashingRate sdk.Dec, minPubRand uint64, inflationRateChange float64,
inflationMin float64, inflationMax float64, goalBonded float64,
blocksPerYear uint64, genesisTime time.Time, blockGasLimit int64) GenesisParams {
Expand Down Expand Up @@ -305,7 +305,7 @@ func TestnetGenesisParams(maxActiveValidators uint32, btcConfirmationDepth uint6
genParams.BtclightclientBaseBtcHeader = *baseBtcHeaderInfo

genParams.BtcstakingParams = btcstakingtypes.DefaultParams()
genParams.BtcstakingParams.JuryPk, err = bbn.NewBIP340PubKeyFromHex(juryPk)
genParams.BtcstakingParams.CovenantPk, err = bbn.NewBIP340PubKeyFromHex(covenantPk)
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/babylond/cmd/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ Example:
genesisParams := TestnetGenesisParams(genesisCliArgs.MaxActiveValidators,
genesisCliArgs.BtcConfirmationDepth, genesisCliArgs.BtcFinalizationTimeout, genesisCliArgs.CheckpointTag,
genesisCliArgs.EpochInterval, genesisCliArgs.BaseBtcHeaderHex, genesisCliArgs.BaseBtcHeaderHeight,
genesisCliArgs.JuryPK, genesisCliArgs.SlashingAddress, genesisCliArgs.MinSlashingTransactionFeeSat,
genesisCliArgs.CovenantPK, genesisCliArgs.SlashingAddress, genesisCliArgs.MinSlashingTransactionFeeSat,
genesisCliArgs.MinCommissionRate, genesisCliArgs.SlashingRate, genesisCliArgs.MinPubRand,
genesisCliArgs.InflationRateChange, genesisCliArgs.InflationMin,
genesisCliArgs.InflationMax, genesisCliArgs.GoalBonded, genesisCliArgs.BlocksPerYear,
Expand Down
Loading

0 comments on commit a680fcc

Please sign in to comment.