use of org.aion.precompiled.encoding.ListFVM in project aion by aionnetwork.
the class TokenBridgeContractTest method testTransfersGreaterThanMaxListSize.
@Test
public void testTransfersGreaterThanMaxListSize() {
// 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());
// 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
PrecompiledTransactionContext submitBundleContext = context(new AionAddress(members[0]), CONTRACT_ADDR);
this.repository.addBalance(CONTRACT_ADDR, BigInteger.valueOf(1024));
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());
// place a value greater than the maximum 1024
BridgeTransfer[] transfers = new BridgeTransfer[1024];
for (int i = 0; i < 1024; 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);
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();
transferResult = this.contract.execute(callPayload, DEFAULT_NRG);
// / VERIFICATION
assertEquals("FAILURE", transferResult.getStatus().causeOfError);
for (BridgeTransfer b : transfers) {
assertThat(this.repository.getBalance(new AionAddress(b.getRecipient()))).isEqualTo(BigInteger.ZERO);
}
assertThat(this.repository.getBalance(CONTRACT_ADDR)).isEqualTo(BigInteger.valueOf(1024));
assertThat(submitBundleContext.getInternalTransactions()).isEmpty();
assertThat(submitBundleContext.getLogs()).isEmpty();
}
use of org.aion.precompiled.encoding.ListFVM 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 (byte[] k : members) {
encodingList.add(new AddressFVM(k));
}
// 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 = getRandomAddr();
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);
assertEquals(result.getStatus(), TransactionStatus.successful());
// caller not owner - fail
AionAddress address1 = new AionAddress(getRandomAddr());
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 = getRandomAddr();
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);
}
use of org.aion.precompiled.encoding.ListFVM in project aion by aionnetwork.
the class TokenBridgeContractTest method testInitializeRing.
@Test
public void testInitializeRing() {
// 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());
// pull results from controller
for (byte[] k : members) {
assertThat(this.connector.getActiveMember(k)).isTrue();
}
}
Aggregations