Skip to content

Commit

Permalink
Tests: reproduce issue semuxproject#212
Browse files Browse the repository at this point in the history
  • Loading branch information
semux committed Jul 7, 2019
1 parent adcc820 commit 55e115d
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/main/java/org/semux/core/Transaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.semux.crypto.Hex;
import org.semux.crypto.Key;
import org.semux.crypto.Key.Signature;
import org.semux.util.Bytes;
import org.semux.util.SimpleDecoder;
import org.semux.util.SimpleEncoder;

Expand Down Expand Up @@ -161,6 +162,8 @@ public boolean validate(Network network, boolean verifySignature) {
&& networkId == network.id()
&& type != null
&& to != null && to.length == Key.ADDRESS_LEN
&& (type != TransactionType.CREATE && type != TransactionType.DELEGATE
|| Arrays.equals(to, EMPTY_ADDRESS))
&& value.gte0()
&& fee.gte0()
&& nonce >= 0
Expand Down
53 changes: 53 additions & 0 deletions src/test/java/org/semux/vm/client/VmTransactionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,59 @@ public void testCreate() {
assertEquals(ZERO, as.getAccount(block.getCoinbase()).getAvailable());
}

// pragma solidity ^0.5.7;
//
// contract SimpleStorage {
// uint storedData;
// function set(uint x) public {
// storedData = x;
// }
// function get() public view returns (uint retVal) {
// return storedData;
// }
// }
@Test
public void testCreateAndCall() {
Key key = new Key();

TransactionType type = TransactionType.CREATE;
byte[] from = key.toAddress();
byte[] to = Bytes.EMPTY_ADDRESS;
Amount value = NANO_SEM.of(0);
long nonce = as.getAccount(from).getNonce();
long timestamp = TimeUtil.currentTimeMillis();

// set the contract to a simple program
String code = "0x608060405234801561001057600080fd5b5060c68061001f6000396000f3fe6080604052348015600f57600080fd5b506004361060325760003560e01c806360fe47b11460375780636d4ce63c146062575b600080fd5b606060048036036020811015604b57600080fd5b8101908080359060200190929190505050607e565b005b60686088565b6040518082815260200191505060405180910390f35b8060008190555050565b6000805490509056fea265627a7a7230582040d9036c93b76f14f0505e24ea49d3ca539f2d82eb0acc12f279fd28094a429364736f6c63430005090032";
byte[] data = HexUtil.fromHexString(code);

long gas = 1000000;
Amount gasPrice = NANO_SEM.of(1);

Amount available = SEM.of(1000);
as.adjustAvailable(key.toAddress(), available);

Transaction tx = new Transaction(network, type, to, value, Amount.ZERO, nonce, timestamp, data, gas, gasPrice);
tx.sign(key);
assertTrue(tx.validate(network));

TransactionResult result = exec.execute(tx, as, ds, block, chain, 0);
assertTrue(result.getCode().isSuccess());

byte[] newContractAddress = HashUtil.calcNewAddress(tx.getFrom(), tx.getNonce());

type = TransactionType.CALL;
to = newContractAddress;
nonce += 1;
data = Hex.decode0x("0x60fe47b10000000000000000000000000000000000000000000000000000000000000009");
tx = new Transaction(network, type, to, value, Amount.ZERO, nonce, timestamp, data, gas, gasPrice);
tx.sign(key);
assertTrue(tx.validate(network));

result = exec.execute(tx, as, ds, block, chain, 0);
assertTrue(result.getCode().isSuccess());
}

@Test
public void testTransferToContract() {
Key key = new Key();
Expand Down

0 comments on commit 55e115d

Please sign in to comment.