mirror of
https://github.com/corda/corda.git
synced 2024-12-19 13:08:04 +00:00
Fixed items raised by MH in CRD-CR-58 code review.
This commit is contained in:
parent
ef2ff777a7
commit
c7d98b8c6b
@ -50,7 +50,7 @@ class CashTests {
|
|||||||
val vault: VaultService get() = services.vaultService
|
val vault: VaultService get() = services.vaultService
|
||||||
lateinit var dataSource: Closeable
|
lateinit var dataSource: Closeable
|
||||||
lateinit var database: Database
|
lateinit var database: Database
|
||||||
lateinit var VAULT: Vault
|
lateinit var vaultService: Vault
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
fun setUp() {
|
fun setUp() {
|
||||||
@ -60,7 +60,6 @@ class CashTests {
|
|||||||
database = dataSourceAndDatabase.second
|
database = dataSourceAndDatabase.second
|
||||||
databaseTransaction(database) {
|
databaseTransaction(database) {
|
||||||
services = object : MockServices() {
|
services = object : MockServices() {
|
||||||
|
|
||||||
override val keyManagementService: MockKeyManagementService = MockKeyManagementService(MINI_CORP_KEY, MEGA_CORP_KEY, OUR_KEY)
|
override val keyManagementService: MockKeyManagementService = MockKeyManagementService(MINI_CORP_KEY, MEGA_CORP_KEY, OUR_KEY)
|
||||||
override val vaultService: VaultService = NodeVaultService(this)
|
override val vaultService: VaultService = NodeVaultService(this)
|
||||||
|
|
||||||
@ -82,7 +81,7 @@ class CashTests {
|
|||||||
services.fillWithSomeTestCash(howMuch = 80.SWISS_FRANCS, atLeastThisManyStates = 1, atMostThisManyStates = 1,
|
services.fillWithSomeTestCash(howMuch = 80.SWISS_FRANCS, atLeastThisManyStates = 1, atMostThisManyStates = 1,
|
||||||
issuedBy = MINI_CORP.ref(1), issuerKey = MINI_CORP_KEY, ownedBy = OUR_PUBKEY_1)
|
issuedBy = MINI_CORP.ref(1), issuerKey = MINI_CORP_KEY, ownedBy = OUR_PUBKEY_1)
|
||||||
|
|
||||||
VAULT = services.vaultService.currentVault
|
vaultService = services.vaultService.currentVault
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -557,7 +556,7 @@ class CashTests {
|
|||||||
|
|
||||||
val wtx = makeSpend(100.DOLLARS, THEIR_PUBKEY_1)
|
val wtx = makeSpend(100.DOLLARS, THEIR_PUBKEY_1)
|
||||||
|
|
||||||
val vaultState = VAULT.states.elementAt(0) as StateAndRef<Cash.State>
|
val vaultState = vaultService.states.elementAt(0) as StateAndRef<Cash.State>
|
||||||
assertEquals(vaultState.ref, wtx.inputs[0])
|
assertEquals(vaultState.ref, wtx.inputs[0])
|
||||||
assertEquals(vaultState.state.data.copy(owner = THEIR_PUBKEY_1), wtx.outputs[0].data)
|
assertEquals(vaultState.state.data.copy(owner = THEIR_PUBKEY_1), wtx.outputs[0].data)
|
||||||
assertEquals(OUR_PUBKEY_1, wtx.commands.single { it.value is Cash.Commands.Move }.signers[0])
|
assertEquals(OUR_PUBKEY_1, wtx.commands.single { it.value is Cash.Commands.Move }.signers[0])
|
||||||
@ -572,7 +571,7 @@ class CashTests {
|
|||||||
val tx = TransactionType.General.Builder(DUMMY_NOTARY)
|
val tx = TransactionType.General.Builder(DUMMY_NOTARY)
|
||||||
vault.generateSpend(tx, 80.DOLLARS, ALICE_PUBKEY, setOf(MINI_CORP))
|
vault.generateSpend(tx, 80.DOLLARS, ALICE_PUBKEY, setOf(MINI_CORP))
|
||||||
|
|
||||||
assertEquals(VAULT.states.elementAt(2).ref, tx.inputStates()[0])
|
assertEquals(vaultService.states.elementAt(2).ref, tx.inputStates()[0])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -583,7 +582,7 @@ class CashTests {
|
|||||||
|
|
||||||
val wtx = makeSpend(10.DOLLARS, THEIR_PUBKEY_1)
|
val wtx = makeSpend(10.DOLLARS, THEIR_PUBKEY_1)
|
||||||
|
|
||||||
val vaultState = VAULT.states.elementAt(0) as StateAndRef<Cash.State>
|
val vaultState = vaultService.states.elementAt(0) as StateAndRef<Cash.State>
|
||||||
assertEquals(vaultState.ref, wtx.inputs[0])
|
assertEquals(vaultState.ref, wtx.inputs[0])
|
||||||
assertEquals(vaultState.state.data.copy(owner = THEIR_PUBKEY_1, amount = 10.DOLLARS `issued by` defaultIssuer), wtx.outputs[0].data)
|
assertEquals(vaultState.state.data.copy(owner = THEIR_PUBKEY_1, amount = 10.DOLLARS `issued by` defaultIssuer), wtx.outputs[0].data)
|
||||||
assertEquals(vaultState.state.data.copy(amount = 90.DOLLARS `issued by` defaultIssuer), wtx.outputs[1].data)
|
assertEquals(vaultState.state.data.copy(amount = 90.DOLLARS `issued by` defaultIssuer), wtx.outputs[1].data)
|
||||||
@ -597,8 +596,8 @@ class CashTests {
|
|||||||
databaseTransaction(database) {
|
databaseTransaction(database) {
|
||||||
val wtx = makeSpend(500.DOLLARS, THEIR_PUBKEY_1)
|
val wtx = makeSpend(500.DOLLARS, THEIR_PUBKEY_1)
|
||||||
|
|
||||||
val vaultState0 = VAULT.states.elementAt(0) as StateAndRef<Cash.State>
|
val vaultState0 = vaultService.states.elementAt(0) as StateAndRef<Cash.State>
|
||||||
val vaultState1 = VAULT.states.elementAt(1)
|
val vaultState1 = vaultService.states.elementAt(1)
|
||||||
assertEquals(vaultState0.ref, wtx.inputs[0])
|
assertEquals(vaultState0.ref, wtx.inputs[0])
|
||||||
assertEquals(vaultState1.ref, wtx.inputs[1])
|
assertEquals(vaultState1.ref, wtx.inputs[1])
|
||||||
assertEquals(vaultState0.state.data.copy(owner = THEIR_PUBKEY_1, amount = 500.DOLLARS `issued by` defaultIssuer), wtx.outputs[0].data)
|
assertEquals(vaultState0.state.data.copy(owner = THEIR_PUBKEY_1, amount = 500.DOLLARS `issued by` defaultIssuer), wtx.outputs[0].data)
|
||||||
@ -613,9 +612,9 @@ class CashTests {
|
|||||||
val wtx = makeSpend(580.DOLLARS, THEIR_PUBKEY_1)
|
val wtx = makeSpend(580.DOLLARS, THEIR_PUBKEY_1)
|
||||||
assertEquals(3, wtx.inputs.size)
|
assertEquals(3, wtx.inputs.size)
|
||||||
|
|
||||||
val vaultState0 = VAULT.states.elementAt(0) as StateAndRef<Cash.State>
|
val vaultState0 = vaultService.states.elementAt(0) as StateAndRef<Cash.State>
|
||||||
val vaultState1 = VAULT.states.elementAt(1)
|
val vaultState1 = vaultService.states.elementAt(1)
|
||||||
val vaultState2 = VAULT.states.elementAt(2) as StateAndRef<Cash.State>
|
val vaultState2 = vaultService.states.elementAt(2) as StateAndRef<Cash.State>
|
||||||
assertEquals(vaultState0.ref, wtx.inputs[0])
|
assertEquals(vaultState0.ref, wtx.inputs[0])
|
||||||
assertEquals(vaultState1.ref, wtx.inputs[1])
|
assertEquals(vaultState1.ref, wtx.inputs[1])
|
||||||
assertEquals(vaultState2.ref, wtx.inputs[2])
|
assertEquals(vaultState2.ref, wtx.inputs[2])
|
||||||
|
@ -106,7 +106,15 @@ interface VaultService {
|
|||||||
* Returns a map of how much cash we have in each currency, ignoring details like issuer. Note: currencies for
|
* Returns a map of how much cash we have in each currency, ignoring details like issuer. Note: currencies for
|
||||||
* which we have no cash evaluate to null (not present in map), not 0.
|
* which we have no cash evaluate to null (not present in map), not 0.
|
||||||
*/
|
*/
|
||||||
|
@Suppress("UNCHECKED_CAST")
|
||||||
val cashBalances: Map<Currency, Amount<Currency>>
|
val cashBalances: Map<Currency, Amount<Currency>>
|
||||||
|
get() = currentVault.states.
|
||||||
|
// Select the states we own which are cash, ignore the rest, take the amounts.
|
||||||
|
mapNotNull { (it.state.data as? FungibleAsset<Currency>)?.amount }.
|
||||||
|
// Turn into a Map<Currency, List<Amount>> like { GBP -> (£100, £500, etc), USD -> ($2000, $50) }
|
||||||
|
groupBy { it.token.product }.
|
||||||
|
// Collapse to Map<Currency, Amount> by summing all the amounts of the same currency together.
|
||||||
|
mapValues { it.value.map { Amount(it.quantity, it.token.product) }.sumOrThrow() }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Atomically get the current vault and a stream of updates. Note that the Observable buffers updates until the
|
* Atomically get the current vault and a stream of updates. Note that the Observable buffers updates until the
|
||||||
|
@ -42,16 +42,6 @@ open class InMemoryVaultService(protected val services: ServiceHub) : SingletonS
|
|||||||
override val updates: Observable<Vault.Update>
|
override val updates: Observable<Vault.Update>
|
||||||
get() = mutex.content._updatesPublisher
|
get() = mutex.content._updatesPublisher
|
||||||
|
|
||||||
@Suppress("UNCHECKED_CAST")
|
|
||||||
override val cashBalances: Map<Currency, Amount<Currency>>
|
|
||||||
get() = currentVault.states.
|
|
||||||
// Select the states we own which are cash, ignore the rest, take the amounts.
|
|
||||||
mapNotNull { (it.state.data as? FungibleAsset<Currency>)?.amount }.
|
|
||||||
// Turn into a Map<Currency, List<Amount>> like { GBP -> (£100, £500, etc), USD -> ($2000, $50) }
|
|
||||||
groupBy { it.token.product }.
|
|
||||||
// Collapse to Map<Currency, Amount> by summing all the amounts of the same currency together.
|
|
||||||
mapValues { it.value.map { Amount(it.quantity, it.token.product) }.sumOrThrow() }
|
|
||||||
|
|
||||||
override fun track(): Pair<Vault, Observable<Vault.Update>> {
|
override fun track(): Pair<Vault, Observable<Vault.Update>> {
|
||||||
return mutex.locked {
|
return mutex.locked {
|
||||||
Pair(vault, updates.bufferUntilSubscribed())
|
Pair(vault, updates.bufferUntilSubscribed())
|
||||||
@ -107,7 +97,8 @@ open class InMemoryVaultService(protected val services: ServiceHub) : SingletonS
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun generateSpend(tx: TransactionBuilder, amount: Amount<Currency>, to: PublicKey, onlyFromParties: Set<Party>?): Pair<TransactionBuilder, List<PublicKey>> {
|
override fun generateSpend(tx: TransactionBuilder, amount: Amount<Currency>, to: PublicKey, onlyFromParties: Set<Party>?): Pair<TransactionBuilder, List<PublicKey>> {
|
||||||
throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates.
|
// TODO: decommission entirely this Vault implementation
|
||||||
|
throw UnsupportedOperationException("Should be using NodeVaultService implementation!")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -84,16 +84,6 @@ class NodeVaultService(private val services: ServiceHub) : SingletonSerializeAsT
|
|||||||
override val updates: Observable<Vault.Update>
|
override val updates: Observable<Vault.Update>
|
||||||
get() = mutex.locked { _updatesPublisher }
|
get() = mutex.locked { _updatesPublisher }
|
||||||
|
|
||||||
@Suppress("UNCHECKED_CAST")
|
|
||||||
override val cashBalances: Map<Currency, Amount<Currency>>
|
|
||||||
get() = currentVault.states.
|
|
||||||
// Select the states we own which are cash, ignore the rest, take the amounts.
|
|
||||||
mapNotNull { (it.state.data as? FungibleAsset<Currency>)?.amount }.
|
|
||||||
// Turn into a Map<Currency, List<Amount>> like { GBP -> (£100, £500, etc), USD -> ($2000, $50) }
|
|
||||||
groupBy { it.token.product }.
|
|
||||||
// Collapse to Map<Currency, Amount> by summing all the amounts of the same currency together.
|
|
||||||
mapValues { it.value.map { Amount(it.quantity, it.token.product) }.sumOrThrow() }
|
|
||||||
|
|
||||||
override fun track(): Pair<Vault, Observable<Vault.Update>> {
|
override fun track(): Pair<Vault, Observable<Vault.Update>> {
|
||||||
return mutex.locked {
|
return mutex.locked {
|
||||||
Pair(Vault(allUnconsumedStates()), _updatesPublisher.bufferUntilSubscribed())
|
Pair(Vault(allUnconsumedStates()), _updatesPublisher.bufferUntilSubscribed())
|
||||||
|
Loading…
Reference in New Issue
Block a user