use of org.aion.precompiled.contracts.ATB.TokenBridgeContract in project aion by aionnetwork.
the class TokenBridgeContractTest method before.
@Before
public void before() {
RepositoryConfig repoConfig = new RepositoryConfig() {
@Override
public String getDbPath() {
return "";
}
@Override
public PruneConfig getPruneConfig() {
return new CfgPrune(false);
}
@Override
public Properties getDatabaseConfig(String db_name) {
Properties props = new Properties();
props.setProperty(DatabaseFactory.Props.DB_TYPE, DBVendor.MOCKDB.toValue());
return props;
}
};
this.repository = new AionRepositoryCache(AionRepositoryImpl.createForTesting(repoConfig));
// override defaults
this.contract = new TokenBridgeContract(dummyContext(), ExternalStateForTests.usingRepository(this.repository), OWNER_ADDR, CONTRACT_ADDR);
this.connector = this.contract.getConnector();
}
use of org.aion.precompiled.contracts.ATB.TokenBridgeContract in project aion by aionnetwork.
the class TokenBridgeContractTest method testTransferInsufficientValidatorSignatures.
@Test
public void testTransferInsufficientValidatorSignatures() {
// 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);
// only give 3/5 signatures
byte[][] signatures = new byte[3][];
signatures[0] = members[0].sign(payloadHash).toBytes();
signatures[1] = members[1].sign(payloadHash).toBytes();
signatures[2] = members[1].sign(payloadHash).toBytes();
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) {
// add incorrect signatures, copies [0:32] for all chunks
sigChunk1.add(new AddressFVM(Arrays.copyOfRange(sig, 0, 32)));
sigChunk2.add(new AddressFVM(Arrays.copyOfRange(sig, 0, 32)));
sigChunk3.add(new AddressFVM(Arrays.copyOfRange(sig, 0, 32)));
}
callPayload = new AbiEncoder(BridgeFuncSig.SIG_SUBMIT_BUNDLE.getSignature(), new AddressFVM(blockHash), sourceTransactionList, addressList, uintList, sigChunk1, sigChunk2, sigChunk3).encodeBytes();
// input will include 5 transfers and 3 validators with incorrect signatures
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();
}
use of org.aion.precompiled.contracts.ATB.TokenBridgeContract in project aion by aionnetwork.
the class BridgeTransferTest method resetContext.
private void resetContext() {
this.context = dummyContext();
this.contract = new TokenBridgeContract(context, state, OWNER_ADDR, CONTRACT_ADDR);
this.controller = this.contract.getController();
this.controller.initialize();
byte[][] memberList = new byte[members.length][];
for (int i = 0; i < members.length; i++) {
memberList[i] = members[i].getAddress();
}
this.controller.ringInitialize(OWNER_ADDR.toByteArray(), memberList);
}
use of org.aion.precompiled.contracts.ATB.TokenBridgeContract 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 (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());
// 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());
}
use of org.aion.precompiled.contracts.ATB.TokenBridgeContract in project aion by aionnetwork.
the class TokenBridgeContractTest method testRingMap.
@Test
public void testRingMap() {
// 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()));
}
byte[] payload = new AbiEncoder(BridgeFuncSig.SIG_RING_INITIALIZE.getSignature(), encodingList).encodeBytes();
PrecompiledTransactionResult result = this.contract.execute(payload, DEFAULT_NRG);
assertTrue(result.getStatus().isSuccess());
// create input byte[]
byte[] callPayload = new byte[36];
byte[] encodeBytes = new AbiEncoder(BridgeFuncSig.PURE_RING_MAP.getSignature()).encodeBytes();
ECKey newKey = ECKeyFac.inst().create();
byte[] randomAddress = newKey.getAddress();
System.arraycopy(encodeBytes, 0, callPayload, 0, 4);
System.arraycopy(randomAddress, 0, callPayload, 4, 32);
// execute with valid input
PrecompiledTransactionResult transferResult = this.contract.execute(callPayload, DEFAULT_NRG);
assertTrue(transferResult.getStatus().isSuccess());
// execute with invalid input
PrecompiledTransactionResult transferResult2 = this.contract.execute(encodeBytes, DEFAULT_NRG);
assertEquals("FAILURE", transferResult2.getStatus().causeOfError);
}
Aggregations