mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 18:32:23 +00:00
add validation to randomize smc
This commit is contained in:
parent
b7fdf73a35
commit
61f70d07b1
5 changed files with 16 additions and 41 deletions
|
|
@ -222,7 +222,7 @@ func (w *wizard) makeGenesis() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Randomize Smart Contract Code
|
// Randomize Smart Contract Code
|
||||||
randomizeAddress, _, err := randomizeContract.DeployRandomize(transactOpts, contractBackend, big.NewInt(90))
|
randomizeAddress, _, err := randomizeContract.DeployRandomize(transactOpts, contractBackend)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Can't deploy root registry")
|
fmt.Println("Can't deploy root registry")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,20 +4,23 @@ import "./libs/SafeMath.sol";
|
||||||
|
|
||||||
contract TomoRandomize {
|
contract TomoRandomize {
|
||||||
using SafeMath for uint256;
|
using SafeMath for uint256;
|
||||||
uint256 public randomNumber;
|
|
||||||
|
|
||||||
mapping (address=>bytes32[]) randomSecret;
|
mapping (address=>bytes32[]) randomSecret;
|
||||||
mapping (address=>bytes32) randomOpening;
|
mapping (address=>bytes32) randomOpening;
|
||||||
|
|
||||||
function TomoRandomize (uint256 _randomNumber) public {
|
function TomoRandomize () public {
|
||||||
randomNumber = _randomNumber;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function setSecret(bytes32[] _secret) public {
|
function setSecret(bytes32[] _secret) public {
|
||||||
|
uint secretPoint = block.number % 900;
|
||||||
|
require(secretPoint >= 800);
|
||||||
|
require(secretPoint < 850);
|
||||||
randomSecret[msg.sender] = _secret;
|
randomSecret[msg.sender] = _secret;
|
||||||
}
|
}
|
||||||
|
|
||||||
function setOpening(bytes32 _opening) public {
|
function setOpening(bytes32 _opening) public {
|
||||||
|
uint openingPoint = block.number % 900;
|
||||||
|
require(openingPoint >= 850);
|
||||||
randomOpening[msg.sender] = _opening;
|
randomOpening[msg.sender] = _opening;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,6 @@
|
||||||
package contract
|
package contract
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"math/big"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/accounts/abi"
|
"github.com/ethereum/go-ethereum/accounts/abi"
|
||||||
|
|
@ -17,7 +16,7 @@ import (
|
||||||
const SafeMathABI = "[]"
|
const SafeMathABI = "[]"
|
||||||
|
|
||||||
// SafeMathBin is the compiled bytecode used for deploying new contracts.
|
// SafeMathBin is the compiled bytecode used for deploying new contracts.
|
||||||
const SafeMathBin = `0x604c602c600b82828239805160001a60731460008114601c57601e565bfe5b5030600052607381538281f30073000000000000000000000000000000000000000030146080604052600080fd00a165627a7a72305820a3f63b465e1cf25f306b1eb1efefc8dac3c38993a7340f69d8b470c3bf599ff30029`
|
const SafeMathBin = `0x604c602c600b82828239805160001a60731460008114601c57601e565bfe5b5030600052607381538281f30073000000000000000000000000000000000000000030146080604052600080fd00a165627a7a72305820ea3f7b50706b29b7324f2b7bc6c5eec464df491d2c006d76decd3a344ef24b5b0029`
|
||||||
|
|
||||||
// DeploySafeMath deploys a new Ethereum contract, binding an instance of SafeMath to it.
|
// DeploySafeMath deploys a new Ethereum contract, binding an instance of SafeMath to it.
|
||||||
func DeploySafeMath(auth *bind.TransactOpts, backend bind.ContractBackend) (common.Address, *types.Transaction, *SafeMath, error) {
|
func DeploySafeMath(auth *bind.TransactOpts, backend bind.ContractBackend) (common.Address, *types.Transaction, *SafeMath, error) {
|
||||||
|
|
@ -175,18 +174,18 @@ func (_SafeMath *SafeMathTransactorRaw) Transact(opts *bind.TransactOpts, method
|
||||||
}
|
}
|
||||||
|
|
||||||
// TomoRandomizeABI is the input ABI used to generate the binding from.
|
// TomoRandomizeABI is the input ABI used to generate the binding from.
|
||||||
const TomoRandomizeABI = "[{\"constant\":true,\"inputs\":[{\"name\":\"_validator\",\"type\":\"address\"}],\"name\":\"getSecret\",\"outputs\":[{\"name\":\"\",\"type\":\"bytes32[]\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_secret\",\"type\":\"bytes32[]\"}],\"name\":\"setSecret\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"randomNumber\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"_validator\",\"type\":\"address\"}],\"name\":\"getOpening\",\"outputs\":[{\"name\":\"\",\"type\":\"bytes32\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_opening\",\"type\":\"bytes32\"}],\"name\":\"setOpening\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"name\":\"_randomNumber\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"}]"
|
const TomoRandomizeABI = "[{\"constant\":true,\"inputs\":[{\"name\":\"_validator\",\"type\":\"address\"}],\"name\":\"getSecret\",\"outputs\":[{\"name\":\"\",\"type\":\"bytes32[]\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_secret\",\"type\":\"bytes32[]\"}],\"name\":\"setSecret\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"_validator\",\"type\":\"address\"}],\"name\":\"getOpening\",\"outputs\":[{\"name\":\"\",\"type\":\"bytes32\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_opening\",\"type\":\"bytes32\"}],\"name\":\"setOpening\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"}]"
|
||||||
|
|
||||||
// TomoRandomizeBin is the compiled bytecode used for deploying new contracts.
|
// TomoRandomizeBin is the compiled bytecode used for deploying new contracts.
|
||||||
const TomoRandomizeBin = `0x608060405234801561001057600080fd5b5060405160208061035b8339810160405251600055610327806100346000396000f30060806040526004361061006c5763ffffffff7c0100000000000000000000000000000000000000000000000000000000600035041663284180fc811461007157806334d38600146100ef578063ccbac9f514610146578063d442d6cc1461016d578063e11f5ba21461019b575b600080fd5b34801561007d57600080fd5b5061009f73ffffffffffffffffffffffffffffffffffffffff600435166101b3565b60408051602080825283518183015283519192839290830191858101910280838360005b838110156100db5781810151838201526020016100c3565b505050509050019250505060405180910390f35b3480156100fb57600080fd5b50604080516020600480358082013583810280860185019096528085526101449536959394602494938501929182918501908490808284375094975061022d9650505050505050565b005b34801561015257600080fd5b5061015b610251565b60408051918252519081900360200190f35b34801561017957600080fd5b5061015b73ffffffffffffffffffffffffffffffffffffffff60043516610257565b3480156101a757600080fd5b5061014460043561027f565b73ffffffffffffffffffffffffffffffffffffffff811660009081526001602090815260409182902080548351818402810184019094528084526060939283018282801561022157602002820191906000526020600020905b8154815260019091019060200180831161020c575b50505050509050919050565b336000908152600160209081526040909120825161024d92840190610291565b5050565b60005481565b73ffffffffffffffffffffffffffffffffffffffff1660009081526002602052604090205490565b33600090815260026020526040902055565b8280548282559060005260206000209081019282156102ce579160200282015b828111156102ce57825182556020909201916001909101906102b1565b506102da9291506102de565b5090565b6102f891905b808211156102da57600081556001016102e4565b905600a165627a7a7230582027c1c755e5d546967a2057a4338eb36cf7ec5972758d7f7a8427abdfc890bd590029`
|
const TomoRandomizeBin = `0x608060405234801561001057600080fd5b50610335806100206000396000f3006080604052600436106100615763ffffffff7c0100000000000000000000000000000000000000000000000000000000600035041663284180fc811461006657806334d38600146100e4578063d442d6cc1461013b578063e11f5ba21461017b575b600080fd5b34801561007257600080fd5b5061009473ffffffffffffffffffffffffffffffffffffffff60043516610193565b60408051602080825283518183015283519192839290830191858101910280838360005b838110156100d05781810151838201526020016100b8565b505050509050019250505060405180910390f35b3480156100f057600080fd5b50604080516020600480358082013583810280860185019096528085526101399536959394602494938501929182918501908490808284375094975061020b9650505050505050565b005b34801561014757600080fd5b5061016973ffffffffffffffffffffffffffffffffffffffff60043516610250565b60408051918252519081900360200190f35b34801561018757600080fd5b50610139600435610278565b73ffffffffffffffffffffffffffffffffffffffff8116600090815260208181526040918290208054835181840281018401909452808452606093928301828280156101ff57602002820191906000526020600020905b815481526001909101906020018083116101ea575b50505050509050919050565b610384430661032081101561021f57600080fd5b610352811061022d57600080fd5b33600090815260208181526040909120835161024b9285019061029f565b505050565b73ffffffffffffffffffffffffffffffffffffffff1660009081526001602052604090205490565b610384430661035281101561028c57600080fd5b5033600090815260016020526040902055565b8280548282559060005260206000209081019282156102dc579160200282015b828111156102dc57825182556020909201916001909101906102bf565b506102e89291506102ec565b5090565b61030691905b808211156102e857600081556001016102f2565b905600a165627a7a72305820524cb2eeb0cc4214180425f822fd315cd15fd8352a830bbd9846b46133730a100029`
|
||||||
|
|
||||||
// DeployTomoRandomize deploys a new Ethereum contract, binding an instance of TomoRandomize to it.
|
// DeployTomoRandomize deploys a new Ethereum contract, binding an instance of TomoRandomize to it.
|
||||||
func DeployTomoRandomize(auth *bind.TransactOpts, backend bind.ContractBackend, _randomNumber *big.Int) (common.Address, *types.Transaction, *TomoRandomize, error) {
|
func DeployTomoRandomize(auth *bind.TransactOpts, backend bind.ContractBackend) (common.Address, *types.Transaction, *TomoRandomize, error) {
|
||||||
parsed, err := abi.JSON(strings.NewReader(TomoRandomizeABI))
|
parsed, err := abi.JSON(strings.NewReader(TomoRandomizeABI))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return common.Address{}, nil, nil, err
|
return common.Address{}, nil, nil, err
|
||||||
}
|
}
|
||||||
address, tx, contract, err := bind.DeployContract(auth, parsed, common.FromHex(TomoRandomizeBin), backend, _randomNumber)
|
address, tx, contract, err := bind.DeployContract(auth, parsed, common.FromHex(TomoRandomizeBin), backend)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return common.Address{}, nil, nil, err
|
return common.Address{}, nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
@ -387,32 +386,6 @@ func (_TomoRandomize *TomoRandomizeCallerSession) GetSecret(_validator common.Ad
|
||||||
return _TomoRandomize.Contract.GetSecret(&_TomoRandomize.CallOpts, _validator)
|
return _TomoRandomize.Contract.GetSecret(&_TomoRandomize.CallOpts, _validator)
|
||||||
}
|
}
|
||||||
|
|
||||||
// RandomNumber is a free data retrieval call binding the contract method 0xccbac9f5.
|
|
||||||
//
|
|
||||||
// Solidity: function randomNumber() constant returns(uint256)
|
|
||||||
func (_TomoRandomize *TomoRandomizeCaller) RandomNumber(opts *bind.CallOpts) (*big.Int, error) {
|
|
||||||
var (
|
|
||||||
ret0 = new(*big.Int)
|
|
||||||
)
|
|
||||||
out := ret0
|
|
||||||
err := _TomoRandomize.contract.Call(opts, out, "randomNumber")
|
|
||||||
return *ret0, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// RandomNumber is a free data retrieval call binding the contract method 0xccbac9f5.
|
|
||||||
//
|
|
||||||
// Solidity: function randomNumber() constant returns(uint256)
|
|
||||||
func (_TomoRandomize *TomoRandomizeSession) RandomNumber() (*big.Int, error) {
|
|
||||||
return _TomoRandomize.Contract.RandomNumber(&_TomoRandomize.CallOpts)
|
|
||||||
}
|
|
||||||
|
|
||||||
// RandomNumber is a free data retrieval call binding the contract method 0xccbac9f5.
|
|
||||||
//
|
|
||||||
// Solidity: function randomNumber() constant returns(uint256)
|
|
||||||
func (_TomoRandomize *TomoRandomizeCallerSession) RandomNumber() (*big.Int, error) {
|
|
||||||
return _TomoRandomize.Contract.RandomNumber(&_TomoRandomize.CallOpts)
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetOpening is a paid mutator transaction binding the contract method 0xe11f5ba2.
|
// SetOpening is a paid mutator transaction binding the contract method 0xe11f5ba2.
|
||||||
//
|
//
|
||||||
// Solidity: function setOpening(_opening bytes32) returns()
|
// Solidity: function setOpening(_opening bytes32) returns()
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,6 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/ethereum/go-ethereum/contracts/randomize/contract"
|
"github.com/ethereum/go-ethereum/contracts/randomize/contract"
|
||||||
"math/big"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Randomize struct {
|
type Randomize struct {
|
||||||
|
|
@ -42,8 +41,8 @@ func NewRandomize(transactOpts *bind.TransactOpts, contractAddr common.Address,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func DeployRandomize(transactOpts *bind.TransactOpts, contractBackend bind.ContractBackend, randomNumber *big.Int) (common.Address, *Randomize, error) {
|
func DeployRandomize(transactOpts *bind.TransactOpts, contractBackend bind.ContractBackend) (common.Address, *Randomize, error) {
|
||||||
randomizeAddr, _, _, err := contract.DeployTomoRandomize(transactOpts, contractBackend, randomNumber)
|
randomizeAddr, _, _, err := contract.DeployTomoRandomize(transactOpts, contractBackend)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return randomizeAddr, nil, err
|
return randomizeAddr, nil, err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -44,7 +44,7 @@ func TestRandomize(t *testing.T) {
|
||||||
transactOpts := bind.NewKeyedTransactor(key)
|
transactOpts := bind.NewKeyedTransactor(key)
|
||||||
transactOpts.GasLimit = 1000000
|
transactOpts.GasLimit = 1000000
|
||||||
|
|
||||||
randomizeAddress, randomize, err := DeployRandomize(transactOpts, contractBackend, big.NewInt(2))
|
randomizeAddress, randomize, err := DeployRandomize(transactOpts, contractBackend)
|
||||||
t.Log("contract address", randomizeAddress.String())
|
t.Log("contract address", randomizeAddress.String())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("can't deploy root registry: %v", err)
|
t.Fatalf("can't deploy root registry: %v", err)
|
||||||
|
|
@ -79,7 +79,7 @@ func TestSendTxRandomizeSecretAndOpening(t *testing.T) {
|
||||||
transactOpts := bind.NewKeyedTransactor(acc1Key)
|
transactOpts := bind.NewKeyedTransactor(acc1Key)
|
||||||
transactOpts.GasLimit = 4200000
|
transactOpts.GasLimit = 4200000
|
||||||
epocNumber := uint64(900)
|
epocNumber := uint64(900)
|
||||||
randomizeAddr, randomizeContract, err := DeployRandomize(transactOpts, backend, new(big.Int).SetInt64(0))
|
randomizeAddr, randomizeContract, err := DeployRandomize(transactOpts, backend)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Can't deploy randomize SC: %v", err)
|
t.Fatalf("Can't deploy randomize SC: %v", err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue