Search in sources :

Example 1 with PrecompiledTransactionContext

use of org.aion.precompiled.type.PrecompiledTransactionContext in project aion by aionnetwork.

the class TokenBridgeContractTest method testTransferRingLocked.

// william test
@Test
public void testTransferRingLocked() {
    // 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 (ECKey k : members) {
        encodingList.add(new AddressFVM(k.getAddress()));
    }
    // not initializing the ring
    // set relayer
    byte[] callPayload = new AbiEncoder(BridgeFuncSig.SIG_SET_RELAYER.getSignature(), new AddressFVM(members[0].getAddress())).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].getAddress()), 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 = HashUtil.h256("blockHash".getBytes());
    BridgeTransfer[] transfers = new BridgeTransfer[5];
    for (int i = 0; i < 5; i++) {
        // generate a unique sourceTransactionHash for each transfer
        byte[] sourceTransactionHash = HashUtil.h256(Integer.toString(i).getBytes());
        transfers[i] = BridgeTransfer.getInstance(BigInteger.ONE, AddressSpecs.computeA0Address(HashUtil.h256(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 (ECKey k : members) {
        signatures[i] = k.sign(payloadHash).toBytes();
        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();
    transferResult = this.contract.execute(callPayload, DEFAULT_NRG);
    // VERIFICATION - failure
    assertThat(this.contract.execute(ByteUtil.merge(BridgeFuncSig.PURE_ACTION_MAP.getBytes(), payloadHash), 21000L).getReturnData()).isEqualTo(new byte[32]);
    assertEquals("FAILURE", transferResult.getStatus().causeOfError);
    // check that nothing has been modified from the failed transfer
    for (BridgeTransfer b : transfers) {
        assertThat(this.repository.getBalance(new AionAddress(b.getRecipient()))).isEqualTo(BigInteger.ZERO);
    }
    assertThat(this.repository.getBalance(CONTRACT_ADDR)).isEqualTo(BigInteger.valueOf(10));
    assertThat(submitBundleContext.getInternalTransactions()).isEmpty();
    assertThat(submitBundleContext.getLogs()).isEmpty();
}
Also used : Uint128FVM(org.aion.zero.impl.precompiled.encoding.Uint128FVM) AionAddress(org.aion.types.AionAddress) TokenBridgeContract(org.aion.precompiled.contracts.ATB.TokenBridgeContract) AddressFVM(org.aion.zero.impl.precompiled.encoding.AddressFVM) AbiEncoder(org.aion.zero.impl.precompiled.encoding.AbiEncoder) ECKey(org.aion.crypto.ECKey) BridgeTransfer(org.aion.precompiled.contracts.ATB.BridgeTransfer) ListFVM(org.aion.zero.impl.precompiled.encoding.ListFVM) PrecompiledTransactionResult(org.aion.precompiled.PrecompiledTransactionResult) PrecompiledTransactionContext(org.aion.precompiled.type.PrecompiledTransactionContext) Test(org.junit.Test)

Example 2 with PrecompiledTransactionContext

use of org.aion.precompiled.type.PrecompiledTransactionContext in project aion by aionnetwork.

the class TokenBridgeContractTest method setupForTest.

private ReturnDataFromSetup setupForTest(BridgeTransfer[] transfers, ECKey[] 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 (ECKey k : members) {
        encodingList.add(new AddressFVM(k.getAddress()));
    }
    byte[] payload = new AbiEncoder(BridgeFuncSig.SIG_RING_INITIALIZE.getSignature(), encodingList).encodeBytes();
    PrecompiledTransactionResult result = this.contract.execute(payload, DEFAULT_NRG);
    assertTrue(result.getStatus().isSuccess());
    // set relayer
    byte[] callPayload = new AbiEncoder(BridgeFuncSig.SIG_SET_RELAYER.getSignature(), new AddressFVM(members[0].getAddress())).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].getAddress()), 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 = HashUtil.h256("blockHash".getBytes());
    byte[] payloadHash = BridgeUtilities.computeBundleHash(blockHash, transfers);
    byte[][] signatures = new byte[members.length][];
    int i = 0;
    for (ECKey k : members) {
        signatures[i] = k.sign(payloadHash).toBytes();
        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.zero.impl.precompiled.encoding.Uint128FVM) AionAddress(org.aion.types.AionAddress) TokenBridgeContract(org.aion.precompiled.contracts.ATB.TokenBridgeContract) AddressFVM(org.aion.zero.impl.precompiled.encoding.AddressFVM) AbiEncoder(org.aion.zero.impl.precompiled.encoding.AbiEncoder) ECKey(org.aion.crypto.ECKey) BridgeTransfer(org.aion.precompiled.contracts.ATB.BridgeTransfer) ListFVM(org.aion.zero.impl.precompiled.encoding.ListFVM) PrecompiledTransactionResult(org.aion.precompiled.PrecompiledTransactionResult) PrecompiledTransactionContext(org.aion.precompiled.type.PrecompiledTransactionContext)

Example 3 with PrecompiledTransactionContext

use of org.aion.precompiled.type.PrecompiledTransactionContext in project aion by aionnetwork.

the class EDVerifyContractTest method invalidInputLengthTest.

@Test
public void invalidInputLengthTest() {
    // note the length is 129
    byte[] input = new byte[129];
    input[128] = 0x1;
    PrecompiledTransactionContext ctx = new PrecompiledTransactionContext(ContractInfo.ED_VERIFY.contractAddress, origin, caller, Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), txHash, txHash, blockNumber, nrgLimit, depth);
    PrecompiledContract contract = new ContractFactory().getPrecompiledContract(ctx, externalStateForTests);
    PrecompiledTransactionResult result = contract.execute(input, 21000L);
    assertEquals("FAILURE", result.getStatus().causeOfError);
}
Also used : PrecompiledContract(org.aion.precompiled.type.PrecompiledContract) PrecompiledTransactionResult(org.aion.precompiled.PrecompiledTransactionResult) PrecompiledTransactionContext(org.aion.precompiled.type.PrecompiledTransactionContext) ContractFactory(org.aion.precompiled.ContractFactory) Test(org.junit.Test)

Example 4 with PrecompiledTransactionContext

use of org.aion.precompiled.type.PrecompiledTransactionContext in project aion by aionnetwork.

the class EDVerifyContractTest method shouldReturnSuccessTestingWith256.

@Test
public void shouldReturnSuccessTestingWith256() {
    byte[] input = setupInput();
    PrecompiledTransactionContext ctx = new PrecompiledTransactionContext(ContractInfo.ED_VERIFY.contractAddress, origin, caller, Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), txHash, txHash, blockNumber, nrgLimit, depth);
    PrecompiledContract contract = new ContractFactory().getPrecompiledContract(ctx, externalStateForTests);
    assertNotNull(contract);
    PrecompiledTransactionResult result = contract.execute(input, 21000L);
    assertThat(result.getStatus().isSuccess());
    assertThat(Arrays.equals(result.getReturnData(), pubKey));
}
Also used : PrecompiledContract(org.aion.precompiled.type.PrecompiledContract) PrecompiledTransactionResult(org.aion.precompiled.PrecompiledTransactionResult) PrecompiledTransactionContext(org.aion.precompiled.type.PrecompiledTransactionContext) ContractFactory(org.aion.precompiled.ContractFactory) Test(org.junit.Test)

Example 5 with PrecompiledTransactionContext

use of org.aion.precompiled.type.PrecompiledTransactionContext in project aion by aionnetwork.

the class EDVerifyContractTest method incorrectInputTest.

@Test
public void incorrectInputTest() {
    byte[] input = setupInput();
    // modify sig
    input[22] = (byte) ((int) (input[32]) - 10);
    // modify sig
    input[33] = (byte) ((int) (input[33]) + 4);
    // modify sig
    input[99] = (byte) ((int) (input[33]) - 40);
    PrecompiledTransactionContext ctx = new PrecompiledTransactionContext(ContractInfo.ED_VERIFY.contractAddress, origin, caller, Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), txHash, txHash, blockNumber, nrgLimit, depth);
    PrecompiledContract contract = new ContractFactory().getPrecompiledContract(ctx, externalStateForTests);
    assertNotNull(contract);
    PrecompiledTransactionResult result = contract.execute(input, 21000L);
    assertThat(result.getStatus().isSuccess());
    assertThat(Arrays.equals(result.getReturnData(), AddressUtils.ZERO_ADDRESS.toByteArray()));
}
Also used : PrecompiledContract(org.aion.precompiled.type.PrecompiledContract) PrecompiledTransactionResult(org.aion.precompiled.PrecompiledTransactionResult) PrecompiledTransactionContext(org.aion.precompiled.type.PrecompiledTransactionContext) ContractFactory(org.aion.precompiled.ContractFactory) Test(org.junit.Test)

Aggregations

PrecompiledTransactionContext (org.aion.precompiled.type.PrecompiledTransactionContext)42 PrecompiledTransactionResult (org.aion.precompiled.PrecompiledTransactionResult)39 Test (org.junit.Test)37 AionAddress (org.aion.types.AionAddress)28 BridgeTransfer (org.aion.precompiled.contracts.ATB.BridgeTransfer)14 ECKey (org.aion.crypto.ECKey)11 TokenBridgeContract (org.aion.precompiled.contracts.ATB.TokenBridgeContract)11 AbiEncoder (org.aion.precompiled.encoding.AbiEncoder)11 AddressFVM (org.aion.precompiled.encoding.AddressFVM)11 ListFVM (org.aion.precompiled.encoding.ListFVM)11 Uint128FVM (org.aion.precompiled.encoding.Uint128FVM)11 PrecompiledContract (org.aion.precompiled.type.PrecompiledContract)11 AbiEncoder (org.aion.zero.impl.precompiled.encoding.AbiEncoder)11 AddressFVM (org.aion.zero.impl.precompiled.encoding.AddressFVM)11 ListFVM (org.aion.zero.impl.precompiled.encoding.ListFVM)11 Uint128FVM (org.aion.zero.impl.precompiled.encoding.Uint128FVM)11 ContractFactory (org.aion.precompiled.ContractFactory)10 BigInteger (java.math.BigInteger)6 InternalTransaction (org.aion.types.InternalTransaction)6 Log (org.aion.types.Log)6