Skip to content

Commit

Permalink
Feature/add dao coin limit order transaction (deso-protocol#307)
Browse files Browse the repository at this point in the history
* Add DAO Coin Limit Order network skeleton. (deso-protocol#239)

* Add DAO Coin Limit Order constants. (deso-protocol#240)

* Add DAO Coin Limit Order mempool skeleton. (deso-protocol#243)

* Add DAO Coin Limit Order mempool skeleton.

* Restore removed imports.

* Side-step unused var error.

* Add DAO Coin Limit Order blockchain skeleton. (deso-protocol#242)

* Add DAO Coin Limit Order blockchain skeleton.

* Restore removed imports.

* Add DAO Coin Limit Order block view skeleton. (deso-protocol#241)

* Add DAO Coin Limit Order block view skeleton.

* Restore removed import.

* Side-step unused var error.

* Add DAO Coin Limit Order block view flush skeleton. (deso-protocol#244)

* Add DAO coin limit order db prefixes. (deso-protocol#245)

* Mf/add dao coin limit order key builders (deso-protocol#246)

* Add DAO coin limit order key builders.

* Use uint32s for enums.

* Remove unnecessary line.

* Mf/add dao coin limit order db utils (deso-protocol#247)

* Add DAO coin limit order db utils.
* Add FromBytes skeleton method.
* Fix FromBytes operation happens in-place.

* Mf/add dao coin limit order postgres skeleton (deso-protocol#248)

* Add DAO coin limit order postgres skeleton.

* Fix spacing/tabs.

* Fix method call capitalization.

* Convert uint256 types to strings.

* Add DAO coin limit order network metadata. (deso-protocol#249)

* Revert DAO coin limit order enums to uint8s. (deso-protocol#250)

* Add DAO coin limit order connect transaction logic. (deso-protocol#259)

* Mf/add dao coin limit order txn testing (deso-protocol#263)

* Switch DAO coin order limit quantity to pointer. (deso-protocol#265)

* Add more DAO coin limit order testing scenarios. (deso-protocol#267)

* Convert DAO coin limit order price from BigFloat to Uint256. (deso-protocol#266)

* Convert DAO coin price to uint256.

* Complete conversion of DAO limit order price to uint256.

* Use Lt, Gt instead of Cmp.

* Start inverting price comparison logic.

* add function to calculate total cost of an order (deso-protocol#268)

* add function to calculate total cost of an order

* Remove Conditional from DBGetMatchingDAOCoinBidOrders that didn't apply, Fix IsBetterAskThan and IsBetterBidThan (deso-protocol#269)

* add check that order is better than last seen order when getting next orders to fill, fix txn metadata for m1's txn in test

* use total cost calculating function to accurately get total cost of orders in blockchain.go, set prefixKey correctly for DBGetMatchingDAOCoinAskOrders

Co-authored-by: Lazy Nina <[email protected]>

* Mf/track dao coin limit order utxos (deso-protocol#270)

* Track DAO coin limit order UTXOs.

* Add disconnect logic.

* Fix failing test.

* Update lib/block_view_dao_coin_limit_order.go

* Add DAO coin limit order postgres structs. (deso-protocol#271)

* Add DAO coin limit order postgres structs.

* Update dao coin limit order postgres migration.

* Fill out LeftPaddedHexToUint256.

* Add DAO coin limit order postgres tests. (deso-protocol#273)

* Remove logging before flags error. (deso-protocol#274)

* Fix DAO coin down migration. (deso-protocol#275)

* Fix some postgres testing issues. (deso-protocol#277)

* Add additional DAO coin limit order test and resolve bugs it surfaces. (deso-protocol#278)

* Add additional DAO coin limit order testing.

* Refactor block view to use db adapter for DAO coin limit orders.

* Set up multi-order test.

* Give m0 more initial  in testing.

* Fix failing txn spend amt test.

* Fix updating order's quantity bug.

* Add helper function to query for all orders.

* Fix by transactor PKID prefix bug.

* Mf/better dao coin limit order var naming (deso-protocol#279)

* Refactor DAO coin limit order variable naming.

* Rename order to matchingOrder.

* Test DAO coin limit order total cost doesnt overflow validation. (deso-protocol#280)

* Mf/allow dao coin limit order cancellations (deso-protocol#281)

* Allow DAO coin limit order cancellations.

* Fix failing postgres test.

* Add more comments.

* Add failing test to show no transactorPKID in index bug.

* Update helper utils to new order schema.

* Checkpoint. Store progress updating connect logic.

* Another checkpoint. Stash progress in connect logic.

* Commit checkpoint. Updated all of block view.

* Start rebuilding tests.

* Checkpoint. Continue updating tests.

* Add back endpoint to query open orders by transactor PKID.

* Add function to get all limit orders for a DAO coin pair.

* Clean up postgres.

* Convert price from uint256 to big.Float.

* Add uint256 ScaledPrice field.

* Continue turning back-on more tests.

* Fix more tests.

* Checkpoint. Continue fighting tests.

* Continue fixing tests.

* Fix last two tests.

* Revised a lot of things, but mainly _connect logic.

Let's have a meeting to discuss.

* Convert PKID vars to PublicKeys.

* Start fixing tests.

* Fix tests by removing reflect.DeepEqual.

* Switch buying and selling DAO coins back to PKIDs.

* Fix a few more failing tests.

* Must compare transactor buying qty to matching selling qty.

* Continue fixing tests.

* Get cancellation tests passing.

* Only update orders within the same block height.

* Fix small postgres bug.

* Ln/dao coin limit order txindex (deso-protocol#283)

* initial check in for txindex for dao coin limit orders

* Use new struct to track fulfilled orders

* update naming convention

* Mf/add safe math uint256 utils (deso-protocol#285)

* Add SafeMathUint256 utils.

* Convert some DAO coin limit order math to safe math.

* Save me some whitespace.

* Mf/add safe math uint64 utils (deso-protocol#287)

* Add SafeMathUint256 utils.

* Convert some DAO coin limit order math to safe math.

* Save me some whitespace.

* Add SafeMathUint64 utils.

* Update DAOCoinLimitOrderMetadata type so it can be serialized to json (deso-protocol#286)

* Update DAOCoinLimitOrderMetadata type so it can be serialized to json

* Re-autoformat comments

* Address feedback on sorting slices

* Address feedback in connect transaction function

* Mf/add dao coin transfer restriction validations (deso-protocol#288)

* Add DAO coin limit order transfer restriction validations.

* Make a bit cleaner.

* Keep full error message.

* Add UTXO view API DAO coin limit order getters. (deso-protocol#290)

* Add UTXO view API DAO coin limit order getters.

* Make merging database and UTXO values more consistent.

* Only compute order map key once.

* Ln/dao coin limit order disconnect (deso-protocol#289)

* Add temporary postgres DAO coin limit order solution. (deso-protocol#292)

* Initial check in for spending limits on DAO Coin Limit Orders (deso-protocol#293)

* Initial check in for spending limits on DAO Coin Limit Orders

* add comments, rename key to DAOCoinLimitOrderLimitKey, initialize map in copy, add tests for spending limit on dao coin limit orders

* Add additional DAO coin limit order tests. (deso-protocol#291)

* Ln/dao coin limit order cancellation validations (deso-protocol#294)

* Add additional DAO coin limit order tests.

* fix infinite loop with last seen order, fix validations around cancellations, update tests

Co-authored-by: mattfoley8 <[email protected]>

* Address fixmes: add sanity checks and comments. Add isDeleted checks and new RuleErrors (deso-protocol#295)

* Ln/dao coin limit order loop till filled (deso-protocol#296)

* Loop until order is filled, fix db utils for getting next order, add more tests, fix copy DAOCoinLimitOrderEntry to not return error

* leave comment in test for diamondhands

* add balance checks in latest test

* Diamondhands touch-ups

* Add postgres CI whitelisted testing. (deso-protocol#298)

* Add postgres CI whitelisted testing.

* Run migrations before testing.

* Map postgres port to localhost.

* Empty commit to test caching dependencies.

* Test migration rollbacks as well.

* Add newline at EOF.

* Mf/add additional dao coin order book tests (deso-protocol#299)

* Add additional DAO coin order book tests.

* Dont say bid/ask.

* Add go-fmt to CI. (deso-protocol#300)

* Add go-fmt to CI.

* Fix failing gofmt error.

* Address DH fixmes (deso-protocol#301)

* Add another test. (deso-protocol#302)

* Fix inputs from transactor to account for fee AND DESO spent purchasing DAO Coins, fix postgres, and add FeeNanos to txn metadata (deso-protocol#303)

* Fix inputs from transactor to account for fee AND DESO spent purchasing DAO Coins, fix postgres, and add FeeNanos to txn metadata

* Clone quantity to be safe

* go fmt blockchain.go

* always write utxo ops to badger (deso-protocol#304)

* always write utxo ops to badger

* run disconnect for pg

* fix table name for dao coin limit order metadata

* Add local postgres testing setup. (deso-protocol#305)

* Make derived key txn construction accept a spending limit hex, not object (deso-protocol#297) (deso-protocol#306)

* Make derived key txn construction accept a spending limit hex, not object

* Update lib/block_view_derived_key.go

* add exists declaration back.

Co-authored-by: Lazy Nina <[email protected]>

Co-authored-by: diamondhands0 <[email protected]>

* Add fee nanos to the to bytes and from bytes logic for DAO Coin Limit Orders (deso-protocol#308)

* Error if matching own order. (deso-protocol#309)

* Make API getter functions public (deso-protocol#310)

* Mf/add operation type column (deso-protocol#311)

* Rename quantity to buy -> quantity to fill.

* Checkpoint passing tests.

* Get postgres tests working.

* Add helper function and tests.

* Continue adding tests for operation type quantity math.

* Complete BID-BID calculation logic.

* Build out ask-ask case.

* Use named return values instead of a struct.

* Allow for param specifying different transactor quantity to fill.

* Implement logic in db_utils.

* Refactor calculate test into own test.

* Update blockchain.go w/ new quantity calculation.

* Throw error if updating order w/ different operation type.

* Update connection logic.

* Fix failing derived key test.

* Document off-by-one bug.

* Add check on FeeNanos in DAO Coin Limit Order connect logic

* Check for overflow before mutliplying fee nanos

* revert changes to UpdateGlobalParams txn construction

* Update w/ PR review feedback.

* Make price calculators var naming a bit more consistent.

* Remove DAO prefix from filled order struct qty fields.

* Empty commit to trigger CI.

* Fix off-by-one bug.

* Fix floating point issues in tests, and implement fixed-point conversion scheme

* Some touch-ups to the order matching logic.

* Add a FIXME for an ultimate sanity-check in _connectDAOCoinLimitOrder

* Add money printer prevention validation.

* WIP - fix prevBalances, but sanity-check fails

* Fix prevBalances bug and fix sanity-check after

* Resolve merge conflicts.

* Fix tests with a workaround on fee computation

* diamondhands holy tidy pass

* Fix failing postgres test.

Co-authored-by: lazynina <[email protected]>
Co-authored-by: diamondhands <[email protected]>

* Add DAO coin limit order blockheight validation. (deso-protocol#314)

* Add DAO coin limit order blockheight validation.

* Empty commit to retrigger CI.

* Set DAO coin limit order block height in derived key test.

* Fix get next orders to fill qty check. (deso-protocol#315)

* Add some cosmetic updates. (deso-protocol#316)

* don't return error from functions that don't generate errors (deso-protocol#318)

* TotalInput and TotalOutput should consider inputs and outputs to/from bidders and difference should equal fee (deso-protocol#317)

* TotalInput and TotalOutput should consider inputs and outputs to/from bidders and difference should equal fee

* Update lib/block_view_dao_coin_limit_order.go

Co-authored-by: Matt Foley <[email protected]>

* Update lib/block_view_dao_coin_limit_order.go

Co-authored-by: Matt Foley <[email protected]>

Co-authored-by: Matt Foley <[email protected]>

* Test submitting ASK order and matching existing BID. (deso-protocol#319)

Co-authored-by: Lazy Nina <[email protected]>
Co-authored-by: diamondhands <[email protected]>
Co-authored-by: iamsofonias <[email protected]>
Co-authored-by: diamondhands0 <[email protected]>
Co-authored-by: lazynina <[email protected]>
  • Loading branch information
6 people authored Apr 12, 2022
1 parent d3b5472 commit ca30aa9
Show file tree
Hide file tree
Showing 29 changed files with 6,409 additions and 52 deletions.
File renamed without changes.
86 changes: 86 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
name: ci

on:
pull_request: {}
push:
branches:
- main

jobs:
go-fmt:
name: go-fmt
runs-on: ubuntu-20.04

steps:
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: "1.16"

- name: Checkout branch
uses: actions/checkout@v3

# Fail if go-fmt recommends any changes.
- name: Run go fmt
run: if [ "$(gofmt -s -l . | wc -l)" -gt 0 ]; then exit 1; fi

go-test-postgres:
name: go-test-postgres
runs-on: ubuntu-20.04
env:
# These dummy values are used for testing purposes only.
POSTGRES_URI: postgresql://admin:admin@localhost:5432/admin

services:
postgres:
image: postgres:14.2-alpine3.15
env:
# These dummy values are used for testing purposes only.
POSTGRES_DB: admin
POSTGRES_PASSWORD: admin
POSTGRES_USER: admin
# Set health checks to wait until postgres has started.
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432

steps:
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: "1.16"

- name: Checkout branch
uses: actions/checkout@v3

- name: Cache dependencies
id: cache-dependencies
uses: actions/cache@v3
with:
path: |
~/.cache/go-build
~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
# Install dependencies if not found in cache.
- name: Install dependencies
if: steps.cache-dependencies.outputs.cache-hit != 'true'
run: go mod download && go install

- name: Build package
run: go build

- name: Run migrations
run: go run scripts/migrate.go migrate

- name: Run go test
run: go test -run TestDAOCoinLimitOrder -v ./lib

- name: Rollback migrations
run: go run scripts/migrate.go rollback
13 changes: 13 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
postgres-start:
@docker-compose --file docker-compose.test.yml up --detach
@sleep 3
@go run scripts/migrate.go migrate

postgres-reset:
@go run scripts/migrate.go rollback
@go run scripts/migrate.go migrate

postgres-restart: postgres-stop postgres-start

postgres-stop:
@docker-compose --file docker-compose.test.yml down --remove-orphans --volumes
14 changes: 14 additions & 0 deletions docker-compose.test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
version: "3"

services:
postgres:
image: postgres:14.2-alpine3.15
container_name: postgres
environment:
# These dummy values are used for local testing purposes only.
POSTGRES_HOST_AUTH_METHOD: trust
POSTGRES_DB: admin
POSTGRES_PASSWORD: ""
POSTGRES_USER: admin
ports:
- "5432:5432"
120 changes: 115 additions & 5 deletions lib/block_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ type UtxoView struct {
// Derived Key entries. Map key is a combination of owner and derived public keys.
DerivedKeyToDerivedEntry map[DerivedKeyMapKey]*DerivedKeyEntry

// DAO coin limit order entry mapping.
DAOCoinLimitOrderMapKeyToDAOCoinLimitOrderEntry map[DAOCoinLimitOrderMapKey]*DAOCoinLimitOrderEntry

// The hash of the tip the view is currently referencing. Mainly used
// for error-checking when doing a bulk operation on the view.
TipHash *BlockHash
Expand Down Expand Up @@ -151,6 +154,9 @@ func (bav *UtxoView) _ResetViewMappingsAfterFlush() {

// Derived Key entries
bav.DerivedKeyToDerivedEntry = make(map[DerivedKeyMapKey]*DerivedKeyEntry)

// DAO Coin Limit Order Entries
bav.DAOCoinLimitOrderMapKeyToDAOCoinLimitOrderEntry = make(map[DAOCoinLimitOrderMapKey]*DAOCoinLimitOrderEntry)
}

func (bav *UtxoView) CopyUtxoView() (*UtxoView, error) {
Expand Down Expand Up @@ -338,6 +344,13 @@ func (bav *UtxoView) CopyUtxoView() (*UtxoView, error) {
newView.DerivedKeyToDerivedEntry[entryKey] = &newEntry
}

// Copy the DAO Coin Limit Order Entries
newView.DAOCoinLimitOrderMapKeyToDAOCoinLimitOrderEntry = make(map[DAOCoinLimitOrderMapKey]*DAOCoinLimitOrderEntry,
len(bav.DAOCoinLimitOrderMapKeyToDAOCoinLimitOrderEntry))
for entryKey, entry := range bav.DAOCoinLimitOrderMapKeyToDAOCoinLimitOrderEntry {
newEntry := *entry
newView.DAOCoinLimitOrderMapKeyToDAOCoinLimitOrderEntry[entryKey] = &newEntry
}
return newView, nil
}

Expand Down Expand Up @@ -947,6 +960,10 @@ func (bav *UtxoView) DisconnectTransaction(currentTxn *MsgDeSoTxn, txnHash *Bloc
return bav._disconnectDAOCoinTransfer(
OperationTypeDAOCoinTransfer, currentTxn, txnHash, utxoOpsForTxn, blockHeight)

} else if currentTxn.TxnMeta.GetTxnType() == TxnTypeDAOCoinLimitOrder {
return bav._disconnectDAOCoinLimitOrder(
OperationTypeDAOCoinLimitOrder, currentTxn, txnHash, utxoOpsForTxn, blockHeight)

} else if currentTxn.TxnMeta.GetTxnType() == TxnTypeSwapIdentity {
return bav._disconnectSwapIdentity(
OperationTypeSwapIdentity, currentTxn, txnHash, utxoOpsForTxn, blockHeight)
Expand Down Expand Up @@ -1015,6 +1032,15 @@ func (bav *UtxoView) DisconnectBlock(
if txn.TxnMeta.GetTxnType() == TxnTypeAcceptNFTBid {
numInputs += len(txn.TxnMeta.(*AcceptNFTBidMetadata).BidderInputs)
}
if txn.TxnMeta.GetTxnType() == TxnTypeDAOCoinLimitOrder {
numMatchingOrderInputs := 0

for _, transactor := range txn.TxnMeta.(*DAOCoinLimitOrderMetadata).BidderInputs {
numMatchingOrderInputs += len(transactor.Inputs)
}

numInputs += numMatchingOrderInputs
}
numOutputs += len(txn.TxOutputs)
}
numSpendOps := 0
Expand Down Expand Up @@ -1569,6 +1595,13 @@ func (bav *UtxoView) _checkDerivedKeySpendingLimit(
derivedKeyEntry, txnMeta.ProfilePublicKey, TransferDAOCoinOperation); err != nil {
return utxoOpsForTxn, err
}
case TxnTypeDAOCoinLimitOrder:
txnMeta := txn.TxnMeta.(*DAOCoinLimitOrderMetadata)
if derivedKeyEntry, err = bav._checkDAOCoinLimitOrderLimitAndUpdateDerivedKeyEntry(
derivedKeyEntry, txnMeta.BuyingDAOCoinCreatorPublicKey.ToBytes(),
txnMeta.SellingDAOCoinCreatorPublicKey.ToBytes()); err != nil {
return utxoOpsForTxn, err
}
case TxnTypeUpdateNFT:
txnMeta := txn.TxnMeta.(*UpdateNFTMetadata)
if derivedKeyEntry, err = _checkNFTLimitAndUpdateDerivedKeyEntry(
Expand Down Expand Up @@ -1832,6 +1865,71 @@ func (bav *UtxoView) _checkDAOCoinLimitAndUpdateDerivedKeyEntry(
return derivedKeyEntry, RuleErrorDerivedKeyDAOCoinOperationNotAuthorized
}

// _checkDAOCoinLimitOrderLimitKeyAndUpdateDerivedKeyEntry checks if the DAOCoinLimitOrderLimitKey is present
// in the DerivedKeyEntry's TransactionSpendingLimitTracker's DAOCoinLimitOrderLimitMap.
// If the key is present, the operation is allowed and we decrement the number of operations remaining.
// If there are no operations remaining after this one, we delete the key.
// Returns true if the key was found and the derived key entry was updated.
//
// TODO: Right now, the "buy" and "sell" DAO coins that the user is transacting must be
// specified explicitly. There is no way to specify "any" DAO coins in the spending limit
// because ZeroPKID, which we use to specify "any" in other spending limits, corresponds
// to DESO for order book operations. We should fix this down the road.
func _checkDAOCoinLimitOrderLimitKeyAndUpdateDerivedKeyEntry(
key DAOCoinLimitOrderLimitKey, derivedKeyEntry DerivedKeyEntry) bool {

// Check if the key is present in the DAOCoinLimitOrderLimitMap...
daoCoinLimitOrderLimit, daoCoinLimitOrderLimitExists :=
derivedKeyEntry.TransactionSpendingLimitTracker.DAOCoinLimitOrderLimitMap[key]
// If the key doesn't exist or the value is <= 0, return false.
if !daoCoinLimitOrderLimitExists || daoCoinLimitOrderLimit <= 0 {
return false
}
// If this is the last operation allowed for this key, we delete the key from the map.
if daoCoinLimitOrderLimit == 1 {
delete(derivedKeyEntry.TransactionSpendingLimitTracker.DAOCoinLimitOrderLimitMap, key)
} else {
// Otherwise, we decrement the number of operations remaining for this key
derivedKeyEntry.TransactionSpendingLimitTracker.DAOCoinLimitOrderLimitMap[key]--
}
// Return true because we found the key and decremented the remaining operations
return true
}

// _checkDAOCoinLimitOrderLimitAndUpdateDerivedKeyEntry checks that the DAO Coin Limit Order
// being performed has been authorized for this derived key.
//
// TODO: Right now, the "buy" and "sell" DAO coins that the user is transacting must be
// specified explicitly. There is no way to specify "any" DAO coins in the spending limit
// because ZeroPKID, which we use to specify "any" in other spending limits, corresponds
// to DESO for order book operations. We should fix this down the road.
func (bav *UtxoView) _checkDAOCoinLimitOrderLimitAndUpdateDerivedKeyEntry(
derivedKeyEntry DerivedKeyEntry, buyingDAOCoinCreatorPublicKey []byte, sellingDAOCoinCreatorPublicKey []byte) (
_derivedKeyEntry DerivedKeyEntry, _err error) {
buyingPKIDEntry := bav.GetPKIDForPublicKey(buyingDAOCoinCreatorPublicKey)
if buyingPKIDEntry == nil || buyingPKIDEntry.isDeleted {
return derivedKeyEntry, fmt.Errorf(
"_checkDAOCoinLimitOrderLimitAndUpdateDerivedKeyEntry: buying pkid is deleted")
}
sellingPKIDEntry := bav.GetPKIDForPublicKey(sellingDAOCoinCreatorPublicKey)
if sellingPKIDEntry == nil || sellingPKIDEntry.isDeleted {
return derivedKeyEntry, fmt.Errorf(
"_checkDAOCoinLimitOrderLimitAndUpdateDerivedKeyEntry: selling pkid is deleted")
}

// Check (buying DAO Creator PKID || selling DAO Creator PKID) key
buyingAndSellingKey := MakeDAOCoinLimitOrderLimitKey(*buyingPKIDEntry.PKID, *sellingPKIDEntry.PKID)
if _checkDAOCoinLimitOrderLimitKeyAndUpdateDerivedKeyEntry(buyingAndSellingKey, derivedKeyEntry) {
return derivedKeyEntry, nil
}

// TODO: How do we want to account for buying ANY creator or selling ANY creator given that we
// use the ZeroPKID / ZeroPublicKey to represent buying/selling DESO.

return derivedKeyEntry, errors.Wrapf(RuleErrorDerivedKeyDAOCoinLimitOrderNotAuthorized,
"_checkDAOCoinLimitOrderLimitAndUpdateDerivedKeyEntr: DAO Coin limit order not authorized: ")
}

func (bav *UtxoView) _connectUpdateGlobalParams(
txn *MsgDeSoTxn, txHash *BlockHash, blockHeight uint32, verifySignatures bool) (
_totalInput uint64, _totalOutput uint64, _utxoOps []*UtxoOperation, _err error) {
Expand Down Expand Up @@ -2136,6 +2234,11 @@ func (bav *UtxoView) _connectTransaction(txn *MsgDeSoTxn, txHash *BlockHash,
bav._connectDAOCoinTransfer(
txn, txHash, blockHeight, verifySignatures)

} else if txn.TxnMeta.GetTxnType() == TxnTypeDAOCoinLimitOrder {
totalInput, totalOutput, utxoOpsForTxn, err =
bav._connectDAOCoinLimitOrder(
txn, txHash, blockHeight, verifySignatures)

} else if txn.TxnMeta.GetTxnType() == TxnTypeSwapIdentity {
totalInput, totalOutput, utxoOpsForTxn, err =
bav._connectSwapIdentity(
Expand Down Expand Up @@ -2200,6 +2303,12 @@ func (bav *UtxoView) _connectTransaction(txn *MsgDeSoTxn, txHash *BlockHash,
}
fees = totalInput - totalOutput
}
// Validate that totalInput - totalOutput is equal to the fee specified in the transaction metadata.
if txn.TxnMeta.GetTxnType() == TxnTypeDAOCoinLimitOrder {
if totalInput-totalOutput != txn.TxnMeta.(*DAOCoinLimitOrderMetadata).FeeNanos {
return nil, 0, 0, 0, RuleErrorDAOCoinLimitOrderTotalInputMinusTotalOutputNotEqualToFee
}
}

// BitcoinExchange transactions have their own special fee that is computed as a function of how much
// DeSo is being minted. They do not need to abide by the global minimum fee check, since if they had
Expand Down Expand Up @@ -2632,11 +2741,12 @@ func (bav *UtxoView) GetSpendableDeSoBalanceNanosForPublicKey(pkBytes []byte,
immatureBlockRewards := uint64(0)

if bav.Postgres != nil {
// TODO: Filter out immature block rewards in postgres. UtxoType needs to be set correctly when importing blocks
//outputs := bav.Postgres.GetBlockRewardsForPublicKey(NewPublicKey(pkBytes), tipHeight-numImmatureBlocks, tipHeight)
//for _, output := range outputs {
// immatureBlockRewards += output.AmountNanos
//}
// Filter out immature block rewards in postgres. UtxoType needs to be set correctly when importing blocks
outputs := bav.Postgres.GetBlockRewardsForPublicKey(NewPublicKey(pkBytes), tipHeight-numImmatureBlocks, tipHeight)

for _, output := range outputs {
immatureBlockRewards += output.AmountNanos
}
} else {
for ii := uint64(1); ii < uint64(numImmatureBlocks); ii++ {
// Don't look up the genesis block since it isn't in the DB.
Expand Down
Loading

0 comments on commit ca30aa9

Please sign in to comment.