本文整理汇总了Java中org.ethereum.util.blockchain.SolidityContract类的典型用法代码示例。如果您正苦于以下问题:Java SolidityContract类的具体用法?Java SolidityContract怎么用?Java SolidityContract使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
SolidityContract类属于org.ethereum.util.blockchain包,在下文中一共展示了SolidityContract类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: constructorTest
import org.ethereum.util.blockchain.SolidityContract; //导入依赖的package包/类
@Test
public void constructorTest() {
StandaloneBlockchain sb = new StandaloneBlockchain().withAutoblock(true);
SolidityContract a = sb.submitNewContract(
"contract A {" +
" uint public a;" +
" uint public b;" +
" function A(uint a_, uint b_) {a = a_; b = b_; }" +
"}",
"A", 555, 777
);
Assert.assertEquals(BigInteger.valueOf(555), a.callConstFunction("a")[0]);
Assert.assertEquals(BigInteger.valueOf(777), a.callConstFunction("b")[0]);
SolidityContract b = sb.submitNewContract(
"contract A {" +
" string public a;" +
" uint public b;" +
" function A(string a_, uint b_) {a = a_; b = b_; }" +
"}",
"A", "This string is longer than 32 bytes...", 777
);
Assert.assertEquals("This string is longer than 32 bytes...", b.callConstFunction("a")[0]);
Assert.assertEquals(BigInteger.valueOf(777), b.callConstFunction("b")[0]);
}
示例2: branchTest
import org.ethereum.util.blockchain.SolidityContract; //导入依赖的package包/类
@Test
public void branchTest() throws Exception {
final int pruneCount = 3;
SystemProperties.getDefault().overrideParams(
"database.prune.enabled", "true",
"database.prune.maxDepth", "" + pruneCount);
StandaloneBlockchain bc = new StandaloneBlockchain();
SolidityContract contr = bc.submitNewContract(
"contract Simple {" +
" uint public n;" +
" function set(uint _n) { n = _n; } " +
"}");
Block b1 = bc.createBlock();
contr.callFunction("set", 0xaaaaaaaaaaaaL);
Block b2 = bc.createBlock();
contr.callFunction("set", 0xbbbbbbbbbbbbL);
Block b2_ = bc.createForkBlock(b1);
bc.createForkBlock(b2);
bc.createBlock();
bc.createBlock();
bc.createBlock();
Assert.assertEquals(BigInteger.valueOf(0xaaaaaaaaaaaaL), contr.callConstFunction("n")[0]);
}
示例3: main
import org.ethereum.util.blockchain.SolidityContract; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
// Creating a blockchain which generates a new block for each transaction
// just not to call createBlock() after each call transaction
StandaloneBlockchain bc = new StandaloneBlockchain().withAutoblock(true);
System.out.println("Creating first empty block (need some time to generate DAG)...");
// warning up the block miner just to understand how long
// the initial miner dataset is generated
bc.createBlock();
System.out.println("Creating a contract...");
// This compiles our Solidity contract, submits it to the blockchain
// internally generates the block with this transaction and returns the
// contract interface
SolidityContract calc = bc.submitNewContract(contractSrc);
System.out.println("Calculating...");
// Creates the contract call transaction, submits it to the blockchain
// and generates a new block which includes this transaction
// After new block is generated the contract state is changed
calc.callFunction("add", 100);
// Check the contract state with a constant call which returns result
// but doesn't generate any transactions and remain the contract state unchanged
assertEqual(BigInteger.valueOf(100), (BigInteger) calc.callConstFunction("result")[0]);
calc.callFunction("add", 200);
assertEqual(BigInteger.valueOf(300), (BigInteger) calc.callConstFunction("result")[0]);
calc.callFunction("mul", 10);
assertEqual(BigInteger.valueOf(3000), (BigInteger) calc.callConstFunction("result")[0]);
calc.callFunction("div", 5);
assertEqual(BigInteger.valueOf(600), (BigInteger) calc.callConstFunction("result")[0]);
System.out.println("Clearing...");
calc.callFunction("clear");
assertEqual(BigInteger.valueOf(0), (BigInteger) calc.callConstFunction("result")[0]);
// We are done - the Solidity contract worked as expected.
System.out.println("Done.");
}
示例4: simpleTest
import org.ethereum.util.blockchain.SolidityContract; //导入依赖的package包/类
@Test
public void simpleTest() {
String contractSrc =
"contract Adder {" +
" function add(int a, int b) returns (int) {return a + b;}" +
"}";
HashMapDB<byte[]> txDb = new HashMapDB<>();
StandaloneBlockchain bc = new StandaloneBlockchain();
bc.getBlockchain().withTransactionStore(new TransactionStore(txDb));
SolidityContract contract = bc.submitNewContract(contractSrc);
bc.createBlock();
contract.callFunction("add", 555, 222);
Block b2 = bc.createBlock();
contract.callFunction("add", 333, 333);
Block b3 = bc.createBlock();
Transaction tx1 = b2.getTransactionsList().get(0);
TransactionInfo tx1Info = bc.getBlockchain().getTransactionInfo(tx1.getHash());
byte[] executionResult = tx1Info.getReceipt().getExecutionResult();
Assert.assertArrayEquals(new DataWord(777).getData(), executionResult);
System.out.println(txDb.keys().size());
bc.getBlockchain().flush();
System.out.println(txDb.keys().size());
TransactionStore txStore = new TransactionStore(txDb);
TransactionInfo tx1Info_ = txStore.get(tx1.getHash()).get(0);
executionResult = tx1Info_.getReceipt().getExecutionResult();
Assert.assertArrayEquals(new DataWord(777).getData(), executionResult);
TransactionInfo highIndex = new TransactionInfo(tx1Info.getReceipt(), tx1Info.getBlockHash(), 255);
TransactionInfo highIndexCopy = new TransactionInfo(highIndex.getEncoded());
Assert.assertArrayEquals(highIndex.getBlockHash(), highIndexCopy.getBlockHash());
Assert.assertEquals(highIndex.getIndex(), highIndexCopy.getIndex());
}
示例5: forkTest
import org.ethereum.util.blockchain.SolidityContract; //导入依赖的package包/类
@Test
public void forkTest() {
// check that TransactionInfo is always returned from the main chain for
// transaction which included into blocks from different forks
String contractSrc =
"contract Adder {" +
" int public lastResult;" +
" function add(int a, int b) returns (int) {lastResult = a + b; return lastResult; }" +
"}";
HashMapDB txDb = new HashMapDB();
StandaloneBlockchain bc = new StandaloneBlockchain();
TransactionStore transactionStore = new TransactionStore(txDb);
bc.getBlockchain().withTransactionStore(transactionStore);
SolidityContract contract = bc.submitNewContract(contractSrc);
Block b1 = bc.createBlock();
contract.callFunction("add", 555, 222);
Block b2 = bc.createBlock();
Transaction tx1 = b2.getTransactionsList().get(0);
TransactionInfo txInfo = bc.getBlockchain().getTransactionInfo(tx1.getHash());
Assert.assertTrue(Arrays.equals(txInfo.getBlockHash(), b2.getHash()));
Block b2_ = bc.createForkBlock(b1);
contract.callFunction("add", 555, 222); // tx with the same hash as before
Block b3_ = bc.createForkBlock(b2_);
TransactionInfo txInfo_ = bc.getBlockchain().getTransactionInfo(tx1.getHash());
Assert.assertTrue(Arrays.equals(txInfo_.getBlockHash(), b3_.getHash()));
Block b3 = bc.createForkBlock(b2);
Block b4 = bc.createForkBlock(b3);
txInfo = bc.getBlockchain().getTransactionInfo(tx1.getHash());
Assert.assertTrue(Arrays.equals(txInfo.getBlockHash(), b2.getHash()));
}
示例6: encodeTest1
import org.ethereum.util.blockchain.SolidityContract; //导入依赖的package包/类
@Test
public void encodeTest1() {
StandaloneBlockchain sb = new StandaloneBlockchain().withAutoblock(true);
SolidityContract a = sb.submitNewContract(
"contract A {" +
" uint public a;" +
" function f(uint a_) {a = a_;}" +
"}");
a.callFunction("f", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
BigInteger r = (BigInteger) a.callConstFunction("a")[0];
System.out.println(r.toString(16));
Assert.assertEquals(new BigInteger(Hex.decode("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")), r);
}
示例7: putZeroValue
import org.ethereum.util.blockchain.SolidityContract; //导入依赖的package包/类
@Ignore // periodically get different roots ?
@Test
public void putZeroValue() {
StandaloneBlockchain sb = new StandaloneBlockchain();
SolidityContract a = sb.submitNewContract("contract A { uint public a; function set() { a = 0;}}");
a.callFunction("set");
Block block = sb.createBlock();
System.out.println(Hex.toHexString(block.getStateRoot()));
Assert.assertEquals("cad42169cafc7855c25b8889df83faf38e493fb6e95b2c9c8e155dbc340160d6", Hex.toHexString(block.getStateRoot()));
}
示例8: simpleDbTest
import org.ethereum.util.blockchain.SolidityContract; //导入依赖的package包/类
@Test
public void simpleDbTest() {
StandaloneBlockchain bc = new StandaloneBlockchain();
SolidityContract parent = bc.submitNewContract("contract A {" +
" uint public a;" +
" function set(uint a_) { a = a_;}" +
"}");
bc.createBlock();
parent.callFunction("set", 123);
bc.createBlock();
Object ret = parent.callConstFunction("a")[0];
System.out.println("Ret = " + ret);
}
示例9: createContractFork1
import org.ethereum.util.blockchain.SolidityContract; //导入依赖的package包/类
@Test
public void createContractFork1() throws Exception {
// Test creation of the contract on forked branch with different storage
String contractSrc =
"contract A {" +
" int public a;" +
" function A() {" +
" a = 333;" +
" }" +
"}" +
"contract B {" +
" int public a;" +
" function B() {" +
" a = 111;" +
" }" +
"}";
{
StandaloneBlockchain bc = new StandaloneBlockchain();
Block b1 = bc.createBlock();
Block b2 = bc.createBlock();
SolidityContract a = bc.submitNewContract(contractSrc, "A");
Block b3 = bc.createBlock();
SolidityContract b = bc.submitNewContract(contractSrc, "B");
Block b2_ = bc.createForkBlock(b1);
Assert.assertEquals(BigInteger.valueOf(333), a.callConstFunction("a")[0]);
Assert.assertEquals(BigInteger.valueOf(111), b.callConstFunction(b2_, "a")[0]);
Block b3_ = bc.createForkBlock(b2_);
Block b4_ = bc.createForkBlock(b3_);
Assert.assertEquals(BigInteger.valueOf(111), a.callConstFunction("a")[0]);
Assert.assertEquals(BigInteger.valueOf(333), a.callConstFunction(b3, "a")[0]);
}
}
示例10: contractCodeForkTest
import org.ethereum.util.blockchain.SolidityContract; //导入依赖的package包/类
@Test
public void contractCodeForkTest() throws IOException, InterruptedException {
String contractA =
"contract A {" +
" function call() returns (uint) {" +
" return 111;" +
" }" +
"}";
String contractB =
"contract B {" +
" function call() returns (uint) {" +
" return 222222;" +
" }" +
"}";
StandaloneBlockchain bc = new StandaloneBlockchain();
Block b1 = bc.createBlock();
SolidityContract a = bc.submitNewContract(contractA);
Block b2 = bc.createBlock();
Assert.assertEquals(BigInteger.valueOf(111), a.callConstFunction("call")[0]);
SolidityContract b = bc.submitNewContract(contractB);
Block b2_ = bc.createForkBlock(b1);
Block b3 = bc.createForkBlock(b2);
Assert.assertEquals(BigInteger.valueOf(111), a.callConstFunction("call")[0]);
Assert.assertEquals(BigInteger.valueOf(111), a.callConstFunction(b2, "call")[0]);
Assert.assertEquals(BigInteger.valueOf(222222), b.callConstFunction(b2_, "call")[0]);
}
示例11: prevBlockHashOnFork
import org.ethereum.util.blockchain.SolidityContract; //导入依赖的package包/类
@Test
public void prevBlockHashOnFork() throws Exception {
String contractA =
"contract A {" +
" bytes32 public blockHash;" +
" function a(){" +
" blockHash = block.blockhash(block.number - 1);" +
" }" +
"}";
StandaloneBlockchain bc = new StandaloneBlockchain();
SolidityContract a = bc.submitNewContract(contractA);
Block b1 = bc.createBlock();
Block b2 = bc.createBlock();
Block b3 = bc.createBlock();
Block b4 = bc.createBlock();
Block b5 = bc.createBlock();
Block b6 = bc.createBlock();
Block b2_ = bc.createForkBlock(b1);
a.callFunction("a");
Block b3_ = bc.createForkBlock(b2_);
Object hash = a.callConstFunction(b3_, "blockHash")[0];
Assert.assertArrayEquals((byte[]) hash, b2_.getHash());
// no StackOverflowException
}
示例12: functionTypeTest
import org.ethereum.util.blockchain.SolidityContract; //导入依赖的package包/类
@Test
public void functionTypeTest() throws IOException, InterruptedException {
String contractA =
"contract A {" +
" int public res;" +
" function calc(int b, function (int a) external returns (int) f) external returns (int) {" +
" return f(b);" +
" }" +
" function fInc(int a) external returns (int) { return a + 1;}" +
" function fDec(int a) external returns (int) { return a - 1;}" +
" function test() {" +
" res = this.calc(111, this.fInc);" +
" }" +
"}";
StandaloneBlockchain bc = new StandaloneBlockchain();
SolidityContract a = bc.submitNewContract(contractA);
bc.createBlock();
a.callFunction("test");
bc.createBlock();
Assert.assertEquals(a.callConstFunction("res")[0], BigInteger.valueOf(112));
BigInteger r1 = (BigInteger) a.callConstFunction("calc", 222, a.getFunction("fInc"))[0];
Assert.assertEquals(223, r1.intValue());
BigInteger r2 = (BigInteger) a.callConstFunction("calc", 222, a.getFunction("fDec"))[0];
Assert.assertEquals(221, r2.intValue());
}
示例13: testPrevBlock
import org.ethereum.util.blockchain.SolidityContract; //导入依赖的package包/类
@Test
public void testPrevBlock() throws InterruptedException {
StandaloneBlockchain bc = new StandaloneBlockchain();
PendingStateImpl pendingState = (PendingStateImpl) bc.getBlockchain().getPendingState();
ECKey alice = new ECKey();
ECKey bob = new ECKey();
SolidityContract contract = bc.submitNewContract("contract A {" +
" function getPrevBlockHash() returns (bytes32) {" +
" return block.blockhash(block.number - 1);" +
" }" +
"}");
bc.sendEther(bob.getAddress(), convert(100, ETHER));
Block b1 = bc.createBlock();
Block b2 = bc.createBlock();
Block b3 = bc.createBlock();
PendingListener l = new PendingListener();
bc.addEthereumListener(l);
Triple<TransactionReceipt, EthereumListener.PendingTransactionState, Block> txUpd;
contract.callFunction("getPrevBlockHash");
bc.generatePendingTransactions();
txUpd = l.onPendingTransactionUpdate.values().iterator().next().poll();
Assert.assertArrayEquals(txUpd.getLeft().getExecutionResult(), b3.getHash());
}
示例14: rewriteSameTrieNode
import org.ethereum.util.blockchain.SolidityContract; //导入依赖的package包/类
@Ignore
@Test
public void rewriteSameTrieNode() throws Exception {
final int pruneCount = 3;
SystemProperties.getDefault().overrideParams(
"database.prune.enabled", "true",
"database.prune.maxDepth", "" + pruneCount);
StandaloneBlockchain bc = new StandaloneBlockchain();
byte[] receiver = Hex.decode("0000000000000000000000000000000000000000");
bc.sendEther(receiver, BigInteger.valueOf(0x77777777));
bc.createBlock();
for (int i = 0; i < 100; i++) {
bc.sendEther(new ECKey().getAddress(), BigInteger.valueOf(i));
}
SolidityContract contr = bc.submitNewContract(
"contract Stupid {" +
" function wrongAddress() { " +
" address addr = 0x0000000000000000000000000000000000000000; " +
" addr.call();" +
" } " +
"}");
Block b1 = bc.createBlock();
contr.callFunction("wrongAddress");
Block b2 = bc.createBlock();
contr.callFunction("wrongAddress");
Block b3 = bc.createBlock();
Assert.assertEquals(BigInteger.valueOf(0xaaaaaaaaaaaaL), contr.callConstFunction("n")[0]);
}
示例15: createContractFork
import org.ethereum.util.blockchain.SolidityContract; //导入依赖的package包/类
@Test
public void createContractFork() throws Exception {
// #1 (Parent) --> #2 --> #3 (Child) ----------------------> #4 (call Child)
// \-------------------------------> #2' (forked Child)
//
// Testing the situation when the Child contract is created by the Parent contract
// first on the main chain with one parameter (#3) and then on the fork with another parameter (#2')
// so their storages are different. Check that original Child storage is not broken
// on the main chain (#4)
String contractSrc =
"contract Child {" +
" int a;" +
" int b;" +
" int public c;" +
" function Child(int i) {" +
" a = 333 + i;" +
" b = 444 + i;" +
" }" +
" function sum() {" +
" c = a + b;" +
" }" +
"}" +
"contract Parent {" +
" address public child;" +
" function createChild(int a) returns (address) {" +
" child = new Child(a);" +
" return child;" +
" }" +
"}";
StandaloneBlockchain bc = new StandaloneBlockchain();
SolidityContract parent = bc.submitNewContract(contractSrc, "Parent");
Block b1 = bc.createBlock();
Block b2 = bc.createBlock();
parent.callFunction("createChild", 100);
Block b3 = bc.createBlock();
byte[] childAddress = (byte[]) parent.callConstFunction("child")[0];
parent.callFunction("createChild", 200);
Block b2_ = bc.createForkBlock(b1);
SolidityContract child = bc.createExistingContractFromSrc(contractSrc, "Child", childAddress);
child.callFunction("sum");
Block b4 = bc.createBlock();
Assert.assertEquals(BigInteger.valueOf(100 + 333 + 100 + 444), child.callConstFunction("c")[0]);
}