Skip to content

Commit

Permalink
Get rid of manual free ports finding (#374)
Browse files Browse the repository at this point in the history
  • Loading branch information
arndey authored Oct 11, 2023
1 parent 007a9ac commit 7797bba
Show file tree
Hide file tree
Showing 20 changed files with 180 additions and 132 deletions.
Binary file modified modules/block/src/test/resources/validator.wasm
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package jp.co.soramitsu.iroha2
import jp.co.soramitsu.iroha2.generated.Name

enum class Permissions(val type: Name) {
CanBurnUserTrigger("CanBurnUserTrigger".asName()),
CanUnregisterAccount("CanUnregisterAccount".asName()),
CanMintUserPublicKeys("CanMintUserPublicKeys".asName()),
CanBurnUserPublicKeys("CanBurnUserPublicKeys".asName()),
Expand Down
35 changes: 0 additions & 35 deletions modules/client/src/main/kotlin/jp/co/soramitsu/iroha2/Utils.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import kotlin.Pair;
import kotlin.coroutines.*;
import kotlinx.coroutines.flow.*;
import static org.apache.commons.lang3.RandomStringUtils.random;
import org.jetbrains.annotations.*;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
Expand All @@ -31,6 +30,7 @@
import static jp.co.soramitsu.iroha2.testengine.TestConstsKt.DEFAULT_ASSET_ID;
import static jp.co.soramitsu.iroha2.testengine.TestConstsKt.DEFAULT_DOMAIN_ID;
import org.junit.jupiter.api.parallel.ResourceLock;
import static org.testcontainers.shaded.org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;

public class JavaTest extends IrohaTest<Iroha2AsyncClient> {

Expand Down Expand Up @@ -182,7 +182,7 @@ public void blockStreaming() throws ExecutionException, InterruptedException {
for (int i = 0; i <= count + 1; i++) {
final VersionedSignedTransaction transaction = TransactionBuilder.Companion.builder()
.account(ALICE_ACCOUNT_ID)
.setKeyValue(ALICE_ACCOUNT_ID, new Name(random(10)), new Value.String(random(10)))
.setKeyValue(ALICE_ACCOUNT_ID, new Name(randomAlphabetic(10)), new Value.String(randomAlphabetic(10)))
.buildSigned(ALICE_KEYPAIR);
client.sendTransactionAsync(transaction);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import jp.co.soramitsu.iroha2.testengine.WithIroha
import kotlinx.coroutines.runBlocking
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.parallel.ResourceLock
import org.testcontainers.shaded.org.apache.commons.lang3.RandomStringUtils.random
import org.testcontainers.shaded.org.apache.commons.lang3.RandomStringUtils.randomAlphabetic
import java.math.BigInteger
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
Expand Down Expand Up @@ -107,7 +107,7 @@ class BlockStreamTest : IrohaTest<Iroha2Client>() {
subscription.receive<BigInteger>(initialActionId) { heightSum += it }

repeat(repeatTimes + shift) {
client.tx { setKeyValue(ALICE_ACCOUNT_ID, random(16).asName(), random(16).asValue()) }
client.tx { setKeyValue(ALICE_ACCOUNT_ID, randomAlphabetic(16).asName(), randomAlphabetic(16).asValue()) }
}
assertEquals((1..repeatTimes.toLong()).sum(), heightSum.toLong())

Expand All @@ -121,8 +121,8 @@ class BlockStreamTest : IrohaTest<Iroha2Client>() {

lateinit var lastValue: String
repeat(repeatTimes * 2) {
lastValue = random(16)
client.tx { setKeyValue(ALICE_ACCOUNT_ID, random(16).asName(), lastValue.asValue()) }
lastValue = randomAlphabetic(16)
client.tx { setKeyValue(ALICE_ACCOUNT_ID, randomAlphabetic(16).asName(), lastValue.asValue()) }
}
assertEquals(lastValue, isi.last().cast<InstructionBox.SetKeyValue>().extractValueString())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import jp.co.soramitsu.iroha2.query.QueryBuilder
import jp.co.soramitsu.iroha2.testengine.ALICE_ACCOUNT_ID
import jp.co.soramitsu.iroha2.testengine.ALICE_KEYPAIR
import jp.co.soramitsu.iroha2.testengine.DefaultGenesis
import jp.co.soramitsu.iroha2.testengine.IrohaConfig
import jp.co.soramitsu.iroha2.testengine.IrohaContainer
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.time.withTimeout
Expand All @@ -23,12 +22,8 @@ class ExampleTest {
*/
@Test
fun `register domain instruction committed`(): Unit = runBlocking {
val ports = findFreePorts(3)
val p2pPort = ports[IrohaConfig.P2P_PORT_IDX]

val container = IrohaContainer {
this.ports = ports
this.alias = "iroha$p2pPort"
this.alias = "iroha$DEFAULT_P2P_PORT"
this.genesis = DefaultGenesis::class.createInstance()
}.also { it.start() }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import jp.co.soramitsu.iroha2.client.Iroha2Client
import jp.co.soramitsu.iroha2.query.QueryBuilder
import jp.co.soramitsu.iroha2.testengine.ALICE_ACCOUNT_ID
import jp.co.soramitsu.iroha2.testengine.BOB_ACCOUNT_ID
import jp.co.soramitsu.iroha2.testengine.IrohaConfig
import jp.co.soramitsu.iroha2.testengine.IrohaContainer
import jp.co.soramitsu.iroha2.testengine.IrohaTest
import jp.co.soramitsu.iroha2.testengine.WithIroha
Expand All @@ -27,13 +26,9 @@ class GenesisTest : IrohaTest<Iroha2Client>() {

@Test
fun `manual IrohaContainer initialization`(): Unit = runBlocking {
val ports = findFreePorts(3)
val p2pPort = ports[IrohaConfig.P2P_PORT_IDX]

val path = javaClass.getResource("../../genesis.json")!!.path
val container = IrohaContainer {
this.ports = ports
this.alias = "iroha$p2pPort"
this.alias = "iroha$DEFAULT_P2P_PORT"
this.genesisPath = path
}.also { it.start() }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ import jp.co.soramitsu.iroha2.transaction.Instructions.fail
import jp.co.soramitsu.iroha2.transaction.TransactionBuilder
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.time.withTimeout
import org.apache.commons.lang3.RandomStringUtils.randomAlphabetic
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import org.testcontainers.shaded.org.apache.commons.lang3.RandomStringUtils
import java.math.BigDecimal
import java.math.MathContext
import java.math.RoundingMode
Expand Down Expand Up @@ -402,7 +402,7 @@ class InstructionsTest : IrohaTest<Iroha2Client>() {
fun `account metadata limit increased`(): Unit = runBlocking {
client.tx {
// 5000 characters string would be rejected by Iroha with default WSV_ACCOUNT_METADATA_LIMITS config
setKeyValue(ALICE_ACCOUNT_ID, "key".asName(), RandomStringUtils.random(5000).asValue())
setKeyValue(ALICE_ACCOUNT_ID, "key".asName(), randomAlphabetic(5000).asValue())
}
}

Expand Down Expand Up @@ -626,7 +626,7 @@ class InstructionsTest : IrohaTest<Iroha2Client>() {
}

// add/update salt value in Bob's account metadata
val salt = RandomStringUtils.random(10).asValue()
val salt = randomAlphabetic(10).asValue()
client.tx { setKeyValue(BOB_ACCOUNT_ID, saltKey.asName(), salt) }

// check new metadata in Bob's account
Expand Down
20 changes: 8 additions & 12 deletions modules/client/src/test/kotlin/jp/co/soramitsu/iroha2/PeerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ import jp.co.soramitsu.iroha2.testengine.ALICE_KEYPAIR
import jp.co.soramitsu.iroha2.testengine.AliceCanUnregisterAnyPeer
import jp.co.soramitsu.iroha2.testengine.DEFAULT_DOMAIN_ID
import jp.co.soramitsu.iroha2.testengine.DefaultGenesis
import jp.co.soramitsu.iroha2.testengine.IrohaConfig
import jp.co.soramitsu.iroha2.testengine.IrohaContainer
import jp.co.soramitsu.iroha2.testengine.IrohaTest
import jp.co.soramitsu.iroha2.testengine.WithIroha
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.time.withTimeout
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.Timeout
import java.security.KeyPair
Expand All @@ -49,33 +49,32 @@ class PeerTest : IrohaTest<AdminIroha2Client>() {
@Permission("no_permission_required")
@SdkTestId("register_peer")
fun `register peer`(): Unit = runBlocking {
val ports = findFreePorts(3)
val p2pPort = ports[IrohaConfig.P2P_PORT_IDX]
val p2pPort = DEFAULT_P2P_PORT
val alias = "iroha$p2pPort"
val address = "$alias:$p2pPort"
val keyPair = generateKeyPair()
val payload = keyPair.public.bytes()

startNewContainer(keyPair, alias, ports).use {
startNewContainer(keyPair, alias).use {
registerPeer(address, payload)
assertTrue(isPeerAvailable(address, payload))
}
}

@Disabled // https://app.zenhub.com/workspaces/iroha-v2-60ddb820813b9100181fc060/issues/gh/hyperledger/iroha-java/372
@Test
@WithIroha([AliceCanUnregisterAnyPeer::class], amount = PEER_AMOUNT)
@Story("Account unregisters a peer")
@Permission("no_permission_required")
@SdkTestId("unregister_peer")
fun `unregister peer`(): Unit = runBlocking {
val ports = findFreePorts(3)
val p2pPort = ports[IrohaConfig.P2P_PORT_IDX]
val p2pPort = DEFAULT_P2P_PORT
val alias = "iroha$p2pPort"
val address = "$alias:$p2pPort"
val keyPair = generateKeyPair()
val payload = keyPair.public.bytes()

startNewContainer(keyPair, alias, ports).use {
startNewContainer(keyPair, alias).use {
registerPeer(address, payload)
repeat(PEER_AMOUNT) { assertTrue(isPeerAvailable(address, payload)) }

Expand All @@ -87,14 +86,13 @@ class PeerTest : IrohaTest<AdminIroha2Client>() {
@Test
@WithIroha([DefaultGenesis::class], amount = PEER_AMOUNT)
fun `registered peer should return consistent data`(): Unit = runBlocking {
val ports = findFreePorts(3)
val p2pPort = ports[IrohaConfig.P2P_PORT_IDX]
val p2pPort = DEFAULT_P2P_PORT
val alias = "iroha$p2pPort"
val address = "$alias:$p2pPort"
val keyPair = generateKeyPair()
val payload = keyPair.public.bytes()

startNewContainer(keyPair, alias, ports).use { container ->
startNewContainer(keyPair, alias).use { container ->
registerPeer(address, payload)
assertTrue(isPeerAvailable(address, payload))

Expand Down Expand Up @@ -137,12 +135,10 @@ class PeerTest : IrohaTest<AdminIroha2Client>() {
private fun startNewContainer(
keyPair: KeyPair,
alias: String,
ports: List<Int>,
): IrohaContainer {
return IrohaContainer {
this.waitStrategy = false
this.keyPair = keyPair
this.ports = ports
this.alias = alias
this.networkToJoin = containers.first().network ?: throw IrohaSdkException("Container network not found")
this.genesis = DefaultGenesis::class.createInstance()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -904,7 +904,7 @@ class QueriesTest : IrohaTest<Iroha2Client>() {
.account(ALICE_ACCOUNT_ID)
.buildSigned(ALICE_KEYPAIR)
.let { query -> client.sendQuery(query) }
assertEquals(Permissions.values().size, permissionTokenSchema.tokenIds.size)
assertEquals(permissionTokenSchema.tokenIds.size, Permissions.values().size)

val expectedPermissions = Permissions.values().map { it.type }.toList()
assertTrue(permissionTokenSchema.tokenIds.containsAll(expectedPermissions))
Expand Down
Binary file modified modules/client/src/test/resources/validator.wasm
Binary file not shown.
31 changes: 28 additions & 3 deletions modules/codegen/src/main/resources/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,10 @@
{
"name": "metadata",
"type": "Metadata"
},
{
"name": "owned_by",
"type": "AccountId"
}
]
},
Expand Down Expand Up @@ -1004,6 +1008,11 @@
"tag": "MetadataRemoved",
"discriminant": 5,
"type": "MetadataChanged<DomainId>"
},
{
"tag": "OwnerChanged",
"discriminant": 6,
"type": "DomainOwnerChanged"
}
]
},
Expand All @@ -1025,14 +1034,18 @@
"tag": "ByMetadataRemoved",
"discriminant": 3
},
{
"tag": "ByOwnerChanged",
"discriminant": 4
},
{
"tag": "ByAccount",
"discriminant": 4,
"discriminant": 5,
"type": "FilterOpt<AccountFilter>"
},
{
"tag": "ByAssetDefinition",
"discriminant": 5,
"discriminant": 6,
"type": "FilterOpt<AssetDefinitionFilter>"
}
]
Expand All @@ -1057,6 +1070,18 @@
}
]
},
"DomainOwnerChanged": {
"Struct": [
{
"name": "domain_id",
"type": "DomainId"
},
{
"name": "new_owner",
"type": "AccountId"
}
]
},
"Duration": {
"Tuple": [
"u64",
Expand Down Expand Up @@ -4919,4 +4944,4 @@
"u8": {
"Int": "FixedWidth"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public data class Domain(
public val assetTotalQuantities: Map<AssetDefinitionId, NumericValue>,
public val logo: IpfsPath? = null,
public val metadata: Metadata,
public val ownedBy: AccountId,
) {
public companion object : ScaleReader<Domain>, ScaleWriter<Domain> {
override fun read(reader: ScaleCodecReader): Domain = try {
Expand All @@ -42,6 +43,7 @@ public data class Domain(
),
reader.readNullable(IpfsPath) as IpfsPath?,
Metadata.read(reader),
AccountId.read(reader),
)
} catch (ex: Exception) {
throw wrapException(ex)
Expand Down Expand Up @@ -72,6 +74,7 @@ public data class Domain(
}
writer.writeNullable(IpfsPath, instance.logo)
Metadata.write(writer, instance.metadata)
AccountId.write(writer, instance.ownedBy)
} catch (ex: Exception) {
throw wrapException(ex)
}
Expand Down
Loading

0 comments on commit 7797bba

Please sign in to comment.