Search in sources :

Example 26 with TokenBridgeContract

use of org.aion.precompiled.contracts.ATB.TokenBridgeContract 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 (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());
    BridgeTransfer[] transfers = new BridgeTransfer[10];
    for (int i = 0; i < 10; 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)));
    }
    // 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.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 27 with TokenBridgeContract

use of org.aion.precompiled.contracts.ATB.TokenBridgeContract in project aion by aionnetwork.

the class TokenBridgeContractTest method testTransferOutOfBoundsListMeta.

@Test
public void testTransferOutOfBoundsListMeta() {
    // 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());
    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();
    callPayload[36] = (byte) 0x172;
    callPayload[37] = (byte) 0x172;
    callPayload[38] = (byte) 0x172;
    transferResult = this.contract.execute(callPayload, DEFAULT_NRG);
    // / VERIFICATION
    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 changed 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 28 with TokenBridgeContract

use of org.aion.precompiled.contracts.ATB.TokenBridgeContract in project aion by aionnetwork.

the class TokenBridgeContractTest method testSetReplayer.

@Test
public void testSetReplayer() {
    // 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 (ECKey k : members) {
        encodingList.add(new AddressFVM(k.getAddress()));
    }
    // address null
    byte[] nullInput = new AbiEncoder(BridgeFuncSig.SIG_SET_RELAYER.getSignature()).encodeBytes();
    PrecompiledTransactionResult res = this.contract.execute(nullInput, DEFAULT_NRG);
    assertEquals("FAILURE", res.getStatus().causeOfError);
    // address valid
    byte[] sig = new AbiEncoder(BridgeFuncSig.SIG_SET_RELAYER.getSignature()).encodeBytes();
    byte[] newReplayer = ECKeyFac.inst().create().getAddress();
    byte[] payload = new byte[4 + 32];
    System.arraycopy(sig, 0, payload, 0, 4);
    System.arraycopy(newReplayer, 0, payload, 4, 32);
    PrecompiledTransactionResult result = this.contract.execute(payload, DEFAULT_NRG);
    assertTrue(result.getStatus().isSuccess());
    // caller not owner - fail
    AionAddress address1 = new AionAddress(ECKeyFac.inst().create().getAddress());
    this.contract = new TokenBridgeContract(context(address1, CONTRACT_ADDR), ExternalStateForTests.usingRepository(this.repository), OWNER_ADDR, CONTRACT_ADDR);
    this.connector = this.contract.getConnector();
    byte[] sig2 = new AbiEncoder(BridgeFuncSig.SIG_SET_RELAYER.getSignature()).encodeBytes();
    byte[] newReplayer2 = ECKeyFac.inst().create().getAddress();
    byte[] payload2 = new byte[4 + 32];
    System.arraycopy(sig2, 0, payload2, 0, 4);
    System.arraycopy(newReplayer2, 0, payload2, 4, 32);
    PrecompiledTransactionResult result2 = this.contract.execute(payload2, DEFAULT_NRG);
    assertEquals("FAILURE", result2.getStatus().causeOfError);
}
Also used : PrecompiledTransactionResult(org.aion.precompiled.PrecompiledTransactionResult) 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) ListFVM(org.aion.zero.impl.precompiled.encoding.ListFVM) Test(org.junit.Test)

Example 29 with TokenBridgeContract

use of org.aion.precompiled.contracts.ATB.TokenBridgeContract in project aion by aionnetwork.

the class ContractFactory method getPrecompiledContract.

/**
 * Returns a new pre-compiled contract such that the address of the new contract is address.
 * Returns null if address does not map to any known contracts.
 *
 * @param context Passed in execution context.
 * @param externalState The current state of the world.
 * @return the specified pre-compiled address.
 */
public PrecompiledContract getPrecompiledContract(PrecompiledTransactionContext context, IExternalStateForPrecompiled externalState) {
    boolean fork_032 = externalState.isFork032Enabled();
    // TODO: need to provide a real solution for the repository here ....
    AionAddress destination = context.destinationAddress;
    if (destination.equals(ContractInfo.TOKEN_BRIDGE.contractAddress)) {
        TokenBridgeContract contract = new TokenBridgeContract(context, externalState, ContractInfo.TOKEN_BRIDGE.ownerAddress, ContractInfo.TOKEN_BRIDGE.contractAddress);
        if (!context.originAddress.equals(ContractInfo.TOKEN_BRIDGE.ownerAddress) && !contract.isInitialized()) {
            return null;
        }
        return contract;
    } else if (destination.equals(ContractInfo.ED_VERIFY.contractAddress)) {
        return fork_032 ? new EDVerifyContract() : null;
    } else if (destination.equals(ContractInfo.BLAKE_2B.contractAddress)) {
        return fork_032 ? new Blake2bHashContract() : null;
    } else if (destination.equals(ContractInfo.TRANSACTION_HASH.contractAddress)) {
        return fork_032 ? new TXHashContract(context) : null;
    } else if (destination.equals(ContractInfo.TOTAL_CURRENCY.contractAddress)) {
        return fork_032 ? null : new TotalCurrencyContract(externalState, context.senderAddress, ContractInfo.TOTAL_CURRENCY.ownerAddress);
    } else {
        return null;
    }
}
Also used : Blake2bHashContract(org.aion.precompiled.contracts.Blake2bHashContract) AionAddress(org.aion.types.AionAddress) TokenBridgeContract(org.aion.precompiled.contracts.ATB.TokenBridgeContract) EDVerifyContract(org.aion.precompiled.contracts.EDVerifyContract) TotalCurrencyContract(org.aion.precompiled.contracts.TotalCurrencyContract) TXHashContract(org.aion.precompiled.contracts.TXHashContract)

Aggregations

TokenBridgeContract (org.aion.precompiled.contracts.ATB.TokenBridgeContract)29 PrecompiledTransactionResult (org.aion.precompiled.PrecompiledTransactionResult)26 AbiEncoder (org.aion.zero.impl.precompiled.encoding.AbiEncoder)25 AddressFVM (org.aion.zero.impl.precompiled.encoding.AddressFVM)25 Test (org.junit.Test)25 ECKey (org.aion.crypto.ECKey)23 ListFVM (org.aion.zero.impl.precompiled.encoding.ListFVM)23 AionAddress (org.aion.types.AionAddress)13 BridgeTransfer (org.aion.precompiled.contracts.ATB.BridgeTransfer)11 PrecompiledTransactionContext (org.aion.precompiled.type.PrecompiledTransactionContext)11 Uint128FVM (org.aion.zero.impl.precompiled.encoding.Uint128FVM)11 BigInteger (java.math.BigInteger)4 InternalTransaction (org.aion.types.InternalTransaction)2 Log (org.aion.types.Log)2 DataWord (org.aion.util.types.DataWord)2 Properties (java.util.Properties)1 Blake2bHashContract (org.aion.precompiled.contracts.Blake2bHashContract)1 EDVerifyContract (org.aion.precompiled.contracts.EDVerifyContract)1 TXHashContract (org.aion.precompiled.contracts.TXHashContract)1 TotalCurrencyContract (org.aion.precompiled.contracts.TotalCurrencyContract)1