Skip to content

Commit

Permalink
Merge main into feature pos (#886)
Browse files Browse the repository at this point in the history
* Add extra data to max spend and diamond txn construction (#883)

* Only encode state change metadata if flag is set (#884)

Co-authored-by: Lazy Nina <>

---------

Co-authored-by: Lazy Nina <>
  • Loading branch information
lazynina authored Dec 22, 2023
1 parent 882e94b commit 70c7888
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 6 deletions.
3 changes: 2 additions & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ func SetupRunFlags(cmd *cobra.Command) {
cmd.PersistentFlags().Bool("log-db-summary-snapshots", false, "The node will log a snapshot of all DB keys every 30s.")
cmd.PersistentFlags().Bool("datadog-profiler", false, "Enable the DataDog profiler for performance testing")
cmd.PersistentFlags().Bool("time-events", false, "Enable simple event timer, helpful in hands-on performance testing")

cmd.PersistentFlags().String("state-change-dir", "", "The directory for state change logs. WARNING: Changing this "+
"from an empty string to a non-empty string (or from a non-empty string to the empty string) requires a resync.")
cmd.PersistentFlags().VisitAll(func(flag *pflag.Flag) {
viper.BindPFlag(flag.Name, flag)
})
Expand Down
1 change: 1 addition & 0 deletions lib/block_view_post_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ func _giveDeSoDiamonds(t *testing.T, chain *Blockchain, db *badger.DB, params *D
senderPkBytes,
diamondPostHash,
diamondLevel,
nil,
feeRateNanosPerKB,
nil,
[]*DeSoOutput{})
Expand Down
28 changes: 26 additions & 2 deletions lib/block_view_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"github.com/spf13/viper"
"io"
"math"
"math/big"
Expand Down Expand Up @@ -997,6 +998,17 @@ type UtxoOperation struct {
StakeAmountNanosDiff uint64
}

// FIXME: This hackIsRunningStateSyncer() call is a hack to get around the fact that
// we don't have a way to not require a resync while introducing the state change
// metadata to the utxo operation struct. We don't want to use a block height to gate
// this because we want to be able to get state change metadata for ALL transactions.
// We should replace this with a more elegant solution, a better hack, or bundle it
// in with a release that requires a resync anyway. We should remove this function
// when we have a better solution in place.
func hackIsRunningStateSyncer() bool {
return viper.GetString("state-change-dir") != ""
}

func (op *UtxoOperation) RawEncodeWithoutMetadata(blockHeight uint64, skipMetadata ...bool) []byte {
var data []byte
// Type
Expand Down Expand Up @@ -1341,7 +1353,13 @@ func (op *UtxoOperation) RawEncodeWithoutMetadata(blockHeight uint64, skipMetada
}

// StateChangeMetadata
if op.StateChangeMetadata != nil {
// FIXME: This hackIsRunningStateSyncer() call is a hack to get around the fact that
// we don't have a way to not require a resync while introducing the state change
// metadata to the utxo operation struct. We don't want to use a block height to gate
// this because we want to be able to get state change metadata for ALL transactions.
// We should replace this with a more elegant solution, a better hack, or bundle it
// in with a release that requires a resync anyway.
if hackIsRunningStateSyncer() && op.StateChangeMetadata != nil {
data = append(data, EncodeToBytes(blockHeight, op.StateChangeMetadata, skipMetadata...)...)
}

Expand Down Expand Up @@ -2012,7 +2030,13 @@ func (op *UtxoOperation) RawDecodeWithoutMetadata(blockHeight uint64, rr *bytes.

// DeSoEncoder
stateChangeMetadata := GetStateChangeMetadataFromOpType(op.Type)
if stateChangeMetadata != nil {
// FIXME: This hackIsRunningStateSyncer() call is a hack to get around the fact that
// we don't have a way to not require a resync while introducing the state change
// metadata to the utxo operation struct. We don't want to use a block height to gate
// this because we want to be able to get state change metadata for ALL transactions.
// We should replace this with a more elegant solution, a better hack, or bundle it
// in with a release that requires a resync anyway.
if hackIsRunningStateSyncer() && stateChangeMetadata != nil {
if exist, err := DecodeFromBytes(stateChangeMetadata, rr); exist && err == nil {
op.StateChangeMetadata = stateChangeMetadata
} else if err != nil {
Expand Down
14 changes: 11 additions & 3 deletions lib/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -4772,6 +4772,7 @@ func (bc *Blockchain) CreateBasicTransferTxnWithDiamonds(
SenderPublicKey []byte,
DiamondPostHash *BlockHash,
DiamondLevel int64,
ExtraData map[string][]byte,
// Standard transaction fields
minFeeRateNanosPerKB uint64, mempool Mempool, additionalOutputs []*DeSoOutput) (
_txn *MsgDeSoTxn, _totalInput uint64, _spendAmount uint64, _changeAmount uint64, _fees uint64, _err error) {
Expand Down Expand Up @@ -4821,11 +4822,14 @@ func (bc *Blockchain) CreateBasicTransferTxnWithDiamonds(
// This function does not compute a signature.
}

delete(ExtraData, DiamondLevelKey)
delete(ExtraData, DiamondPostHashKey)

// Make a map for the diamond extra data and add it.
diamondsExtraData := make(map[string][]byte)
diamondsExtraData[DiamondLevelKey] = IntToBuf(DiamondLevel)
diamondsExtraData[DiamondPostHashKey] = DiamondPostHash[:]
txn.ExtraData = diamondsExtraData
txn.ExtraData = mergeExtraData(ExtraData, diamondsExtraData)

// We don't need to make any tweaks to the amount because it's basically
// a standard "pay per kilobyte" transaction.
Expand All @@ -4851,8 +4855,8 @@ func (bc *Blockchain) CreateBasicTransferTxnWithDiamonds(
}

func (bc *Blockchain) CreateMaxSpend(
senderPkBytes []byte, recipientPkBytes []byte, minFeeRateNanosPerKB uint64,
mempool Mempool, additionalOutputs []*DeSoOutput) (
senderPkBytes []byte, recipientPkBytes []byte, extraData map[string][]byte, minFeeRateNanosPerKB uint64,
mempool *DeSoMempool, additionalOutputs []*DeSoOutput) (
_txn *MsgDeSoTxn, _totalInputAdded uint64, _spendAmount uint64, _fee uint64, _err error) {

txn := &MsgDeSoTxn{
Expand All @@ -4869,6 +4873,10 @@ func (bc *Blockchain) CreateMaxSpend(
// This function does not compute a signature.
}

if len(extraData) > 0 {
txn.ExtraData = extraData
}

if bc.BlockTip().Height >= bc.params.ForkHeights.BalanceModelBlockHeight {
var utxoView *UtxoView
var err error
Expand Down

0 comments on commit 70c7888

Please sign in to comment.