Search in sources :

Example 1 with ListFVM

use of org.aion.precompiled.encoding.ListFVM in project aion by aionnetwork.

the class TokenBridgeContractTest method testMemberCount.

@Test
public void testMemberCount() {
    // override defaults
    this.contract = new TokenBridgeContract(context(OWNER_ADDR, CONTRACT_ADDR), ExternalStateForTests.usingRepository(this.repository), OWNER_ADDR, CONTRACT_ADDR);
    this.connector = this.contract.getConnector();
    ListFVM encodingList = new ListFVM();
    for (byte[] k : members) {
        encodingList.add(new AddressFVM(k));
    }
    byte[] payload = new AbiEncoder(BridgeFuncSig.SIG_RING_INITIALIZE.getSignature(), encodingList).encodeBytes();
    PrecompiledTransactionResult result = this.contract.execute(payload, DEFAULT_NRG);
    assertEquals(result.getStatus(), TransactionStatus.successful());
    // try before
    byte[] callPayload = new AbiEncoder(BridgeFuncSig.PURE_MEMBER_COUNT.getSignature(), encodingList).encodeBytes();
    PrecompiledTransactionResult transferResult = this.contract.execute(callPayload, DEFAULT_NRG);
    assertTrue(transferResult.getStatus().isSuccess());
    assertThat(transferResult.getReturnData()).isEqualTo(new DataWord(new BigInteger("5")).getData());
    // explicitly set the member count to 10
    this.connector.setMemberCount(10);
    // try after
    byte[] callPayload2 = new AbiEncoder(BridgeFuncSig.PURE_MEMBER_COUNT.getSignature(), encodingList).encodeBytes();
    PrecompiledTransactionResult transferResult2 = this.contract.execute(callPayload2, DEFAULT_NRG);
    assertTrue(transferResult2.getStatus().isSuccess());
    assertThat(transferResult2.getReturnData()).isEqualTo(new DataWord(new BigInteger("10")).getData());
}
Also used : PrecompiledTransactionResult(org.aion.precompiled.PrecompiledTransactionResult) AddressFVM(org.aion.precompiled.encoding.AddressFVM) AbiEncoder(org.aion.precompiled.encoding.AbiEncoder) BigInteger(java.math.BigInteger) DataWord(org.aion.precompiled.util.DataWord) ListFVM(org.aion.precompiled.encoding.ListFVM) Test(org.junit.Test)

Example 2 with ListFVM

use of org.aion.precompiled.encoding.ListFVM in project aion by aionnetwork.

the class TokenBridgeContractTest method testTransferNotRelayer.

@Test
public void testTransferNotRelayer() {
    // override defaults
    PrecompiledTransactionContext initializationContext = context(OWNER_ADDR, CONTRACT_ADDR);
    this.contract = new TokenBridgeContract(initializationContext, ExternalStateForTests.usingRepository(this.repository), OWNER_ADDR, CONTRACT_ADDR);
    this.connector = this.contract.getConnector();
    ListFVM encodingList = new ListFVM();
    for (byte[] k : members) {
        encodingList.add(new AddressFVM(k));
    }
    byte[] payload = new AbiEncoder(BridgeFuncSig.SIG_RING_INITIALIZE.getSignature(), encodingList).encodeBytes();
    PrecompiledTransactionResult result = this.contract.execute(payload, DEFAULT_NRG);
    assertEquals(result.getStatus(), TransactionStatus.successful());
    // set relayer
    byte[] callPayload = new AbiEncoder(BridgeFuncSig.SIG_SET_RELAYER.getSignature(), new AddressFVM(members[0])).encodeBytes();
    PrecompiledTransactionResult transferResult = this.contract.execute(callPayload, DEFAULT_NRG);
    assertTrue(transferResult.getStatus().isSuccess());
    // override defaults
    this.repository.addBalance(CONTRACT_ADDR, BigInteger.TEN);
    // we create a new token bridge contract here because we
    // need to change the execution context
    PrecompiledTransactionContext submitBundleContext = context(new AionAddress(members[0]), CONTRACT_ADDR);
    this.contract = new TokenBridgeContract(submitBundleContext, ExternalStateForTests.usingRepository(this.repository), OWNER_ADDR, CONTRACT_ADDR);
    this.connector = this.contract.getConnector();
    // assemble the payload
    byte[] blockHash = capabilities.blake2b("blockHash".getBytes());
    BridgeTransfer[] transfers = new BridgeTransfer[10];
    for (int i = 0; i < 10; i++) {
        // generate a unique sourceTransactionHash for each transfer
        byte[] sourceTransactionHash = capabilities.blake2b(Integer.toString(i).getBytes());
        transfers[i] = BridgeTransfer.getInstance(BigInteger.ONE, capabilities.computeA0Address(capabilities.blake2b(Integer.toHexString(i).getBytes())), sourceTransactionHash);
    }
    byte[] payloadHash = BridgeUtilities.computeBundleHash(blockHash, transfers);
    // ATB-4, do one assert here to check that transactionHash is not set
    assertThat(this.contract.execute(ByteUtil.merge(BridgeFuncSig.PURE_ACTION_MAP.getBytes(), payloadHash), 21000L).getReturnData()).isEqualTo(ByteUtil.EMPTY_WORD);
    byte[][] signatures = new byte[members.length][];
    int i = 0;
    for (byte[] k : members) {
        signatures[i] = capabilities.sign(k, payloadHash);
        i++;
    }
    ListFVM sourceTransactionList = new ListFVM();
    ListFVM addressList = new ListFVM();
    ListFVM uintList = new ListFVM();
    for (BridgeTransfer b : transfers) {
        sourceTransactionList.add(new AddressFVM(b.getSourceTransactionHash()));
        addressList.add(new AddressFVM(b.getRecipient()));
        uintList.add(new Uint128FVM(PrecompiledUtilities.pad(b.getTransferValue().toByteArray(), 16)));
    }
    ListFVM sigChunk1 = new ListFVM();
    ListFVM sigChunk2 = new ListFVM();
    ListFVM sigChunk3 = new ListFVM();
    for (byte[] sig : signatures) {
        sigChunk1.add(new AddressFVM(Arrays.copyOfRange(sig, 0, 32)));
        sigChunk2.add(new AddressFVM(Arrays.copyOfRange(sig, 32, 64)));
        sigChunk3.add(new AddressFVM(Arrays.copyOfRange(sig, 64, 96)));
    }
    // we create a new token bridge contract here because we
    // need to change the execution context
    PrecompiledTransactionContext incorrectRelaySubmitBundleContext = context(AddressUtils.ZERO_ADDRESS, CONTRACT_ADDR);
    this.contract = new TokenBridgeContract(incorrectRelaySubmitBundleContext, ExternalStateForTests.usingRepository(this.repository), OWNER_ADDR, CONTRACT_ADDR);
    this.connector = this.contract.getConnector();
    callPayload = new AbiEncoder(BridgeFuncSig.SIG_SUBMIT_BUNDLE.getSignature(), new AddressFVM(blockHash), sourceTransactionList, addressList, uintList, sigChunk1, sigChunk2, sigChunk3).encodeBytes();
    transferResult = this.contract.execute(callPayload, DEFAULT_NRG);
    assertEquals("FAILURE", transferResult.getStatus().causeOfError);
}
Also used : Uint128FVM(org.aion.precompiled.encoding.Uint128FVM) AionAddress(org.aion.types.AionAddress) AddressFVM(org.aion.precompiled.encoding.AddressFVM) AbiEncoder(org.aion.precompiled.encoding.AbiEncoder) ListFVM(org.aion.precompiled.encoding.ListFVM) PrecompiledTransactionResult(org.aion.precompiled.PrecompiledTransactionResult) PrecompiledTransactionContext(org.aion.precompiled.type.PrecompiledTransactionContext) Test(org.junit.Test)

Example 3 with ListFVM

use of org.aion.precompiled.encoding.ListFVM in project aion by aionnetwork.

the class TokenBridgeContractTest method testMinThreshold.

@Test
public void testMinThreshold() {
    // override defaults
    this.contract = new TokenBridgeContract(context(OWNER_ADDR, CONTRACT_ADDR), ExternalStateForTests.usingRepository(this.repository), OWNER_ADDR, CONTRACT_ADDR);
    this.connector = this.contract.getConnector();
    ListFVM encodingList = new ListFVM();
    for (byte[] k : members) {
        encodingList.add(new AddressFVM(k));
    }
    byte[] payload = new AbiEncoder(BridgeFuncSig.SIG_RING_INITIALIZE.getSignature(), encodingList).encodeBytes();
    PrecompiledTransactionResult result = this.contract.execute(payload, DEFAULT_NRG);
    assertEquals(result.getStatus(), TransactionStatus.successful());
    // try before
    byte[] callPayload = new AbiEncoder(BridgeFuncSig.PURE_MIN_THRESH.getSignature(), encodingList).encodeBytes();
    PrecompiledTransactionResult transferResult = this.contract.execute(callPayload, DEFAULT_NRG);
    assertTrue(transferResult.getStatus().isSuccess());
    assertThat(transferResult.getReturnData()).isEqualTo(new DataWord(new BigInteger("3")).getData());
    // explicitly set the min threshold to 5
    this.connector.setMinThresh(5);
    // try after
    byte[] callPayload2 = new AbiEncoder(BridgeFuncSig.PURE_MIN_THRESH.getSignature(), encodingList).encodeBytes();
    PrecompiledTransactionResult transferResult2 = this.contract.execute(callPayload2, DEFAULT_NRG);
    assertTrue(transferResult2.getStatus().isSuccess());
    assertThat(transferResult2.getReturnData()).isEqualTo(new DataWord(new BigInteger("5")).getData());
    // try setting threshold greater than number of validator members
    this.connector.setMinThresh(10);
    // try after
    byte[] callPayload3 = new AbiEncoder(BridgeFuncSig.PURE_MIN_THRESH.getSignature(), encodingList).encodeBytes();
    PrecompiledTransactionResult transferResult3 = this.contract.execute(callPayload3, DEFAULT_NRG);
    assertTrue(transferResult3.getStatus().isSuccess());
    assertThat(transferResult3.getReturnData()).isEqualTo(new DataWord(new BigInteger("10")).getData());
}
Also used : PrecompiledTransactionResult(org.aion.precompiled.PrecompiledTransactionResult) AddressFVM(org.aion.precompiled.encoding.AddressFVM) AbiEncoder(org.aion.precompiled.encoding.AbiEncoder) BigInteger(java.math.BigInteger) DataWord(org.aion.precompiled.util.DataWord) ListFVM(org.aion.precompiled.encoding.ListFVM) Test(org.junit.Test)

Example 4 with ListFVM

use of org.aion.precompiled.encoding.ListFVM in project aion by aionnetwork.

the class TokenBridgeContractTest method testFailRingInitialize.

@Test
public void testFailRingInitialize() {
    // override defaults
    this.contract = new TokenBridgeContract(context(OWNER_ADDR, CONTRACT_ADDR), ExternalStateForTests.usingRepository(this.repository), OWNER_ADDR, CONTRACT_ADDR);
    this.connector = this.contract.getConnector();
    ListFVM encodingList = new ListFVM();
    for (byte[] k : members) {
        encodingList.add(new AddressFVM(k));
    }
    // address null
    byte[] payload = new AbiEncoder(BridgeFuncSig.SIG_RING_INITIALIZE.getSignature()).encodeBytes();
    PrecompiledTransactionResult result = this.contract.execute(payload, DEFAULT_NRG);
    assertEquals("FAILURE", result.getStatus().causeOfError);
    this.connector.setRingLocked(true);
    // failed to initialize due to locked ring
    byte[] payload2 = new AbiEncoder(BridgeFuncSig.SIG_RING_INITIALIZE.getSignature(), encodingList).encodeBytes();
    PrecompiledTransactionResult result2 = this.contract.execute(payload2, DEFAULT_NRG);
    assertEquals("FAILURE", result2.getStatus().causeOfError);
}
Also used : PrecompiledTransactionResult(org.aion.precompiled.PrecompiledTransactionResult) AddressFVM(org.aion.precompiled.encoding.AddressFVM) AbiEncoder(org.aion.precompiled.encoding.AbiEncoder) ListFVM(org.aion.precompiled.encoding.ListFVM) Test(org.junit.Test)

Example 5 with ListFVM

use of org.aion.precompiled.encoding.ListFVM in project aion by aionnetwork.

the class TokenBridgeContractTest method setupForTest.

private ReturnDataFromSetup setupForTest(BridgeTransfer[] transfers, byte[][] members) {
    // override defaults
    PrecompiledTransactionContext initializationContext = context(OWNER_ADDR, CONTRACT_ADDR);
    this.contract = new TokenBridgeContract(initializationContext, ExternalStateForTests.usingRepository(this.repository), OWNER_ADDR, CONTRACT_ADDR);
    this.connector = this.contract.getConnector();
    ListFVM encodingList = new ListFVM();
    for (byte[] k : members) {
        encodingList.add(new AddressFVM(k));
    }
    byte[] payload = new AbiEncoder(BridgeFuncSig.SIG_RING_INITIALIZE.getSignature(), encodingList).encodeBytes();
    PrecompiledTransactionResult result = this.contract.execute(payload, DEFAULT_NRG);
    assertEquals(result.getStatus(), TransactionStatus.successful());
    // set relayer
    byte[] callPayload = new AbiEncoder(BridgeFuncSig.SIG_SET_RELAYER.getSignature(), new AddressFVM(members[0])).encodeBytes();
    PrecompiledTransactionResult transferResult = this.contract.execute(callPayload, DEFAULT_NRG);
    assertTrue(transferResult.getStatus().isSuccess());
    // override defaults
    this.repository.addBalance(CONTRACT_ADDR, BigInteger.TEN);
    // we create a new token bridge contract here because we
    // need to change the execution context
    PrecompiledTransactionContext submitBundleContext = context(new AionAddress(members[0]), CONTRACT_ADDR);
    this.contract = new TokenBridgeContract(submitBundleContext, ExternalStateForTests.usingRepository(this.repository), OWNER_ADDR, CONTRACT_ADDR);
    this.connector = this.contract.getConnector();
    // assemble the payload
    byte[] blockHash = capabilities.blake2b("blockHash".getBytes());
    byte[] payloadHash = BridgeUtilities.computeBundleHash(blockHash, transfers);
    byte[][] signatures = new byte[members.length][];
    int i = 0;
    for (byte[] k : members) {
        signatures[i] = capabilities.sign(k, payloadHash);
        i++;
    }
    ListFVM sourceTransactionList = new ListFVM();
    ListFVM addressList = new ListFVM();
    ListFVM uintList = new ListFVM();
    for (BridgeTransfer b : transfers) {
        sourceTransactionList.add(new AddressFVM(b.getSourceTransactionHash()));
        addressList.add(new AddressFVM(b.getRecipient()));
        uintList.add(new Uint128FVM(PrecompiledUtilities.pad(b.getTransferValue().toByteArray(), 16)));
    }
    ListFVM sigChunk1 = new ListFVM();
    ListFVM sigChunk2 = new ListFVM();
    ListFVM sigChunk3 = new ListFVM();
    for (byte[] sig : signatures) {
        sigChunk1.add(new AddressFVM(Arrays.copyOfRange(sig, 0, 32)));
        sigChunk2.add(new AddressFVM(Arrays.copyOfRange(sig, 32, 64)));
        sigChunk3.add(new AddressFVM(Arrays.copyOfRange(sig, 64, 96)));
    }
    callPayload = new AbiEncoder(BridgeFuncSig.SIG_SUBMIT_BUNDLE.getSignature(), new AddressFVM(blockHash), sourceTransactionList, addressList, uintList, sigChunk1, sigChunk2, sigChunk3).encodeBytes();
    return new ReturnDataFromSetup(submitBundleContext, blockHash, payloadHash, callPayload);
}
Also used : Uint128FVM(org.aion.precompiled.encoding.Uint128FVM) AionAddress(org.aion.types.AionAddress) AddressFVM(org.aion.precompiled.encoding.AddressFVM) AbiEncoder(org.aion.precompiled.encoding.AbiEncoder) ListFVM(org.aion.precompiled.encoding.ListFVM) PrecompiledTransactionResult(org.aion.precompiled.PrecompiledTransactionResult) PrecompiledTransactionContext(org.aion.precompiled.type.PrecompiledTransactionContext)

Aggregations

PrecompiledTransactionResult (org.aion.precompiled.PrecompiledTransactionResult)23 AbiEncoder (org.aion.precompiled.encoding.AbiEncoder)23 AddressFVM (org.aion.precompiled.encoding.AddressFVM)23 ListFVM (org.aion.precompiled.encoding.ListFVM)23 Test (org.junit.Test)22 AionAddress (org.aion.types.AionAddress)12 Uint128FVM (org.aion.precompiled.encoding.Uint128FVM)11 PrecompiledTransactionContext (org.aion.precompiled.type.PrecompiledTransactionContext)11 BigInteger (java.math.BigInteger)4 DataWord (org.aion.precompiled.util.DataWord)2 InternalTransaction (org.aion.types.InternalTransaction)2 Log (org.aion.types.Log)2