forked from expanse-org/go-expanse
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request expanse-org#8 from thanhson1085/dev
add validator voting smart contract
- Loading branch information
Showing
14 changed files
with
2,692 additions
and
4 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package blocksigner | ||
|
||
import ( | ||
"github.com/ethereum/go-ethereum/accounts/abi/bind" | ||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/contracts/blocksigner/contract" | ||
) | ||
|
||
type BlockSigner struct { | ||
*contract.BlockSignerSession | ||
contractBackend bind.ContractBackend | ||
} | ||
|
||
func NewBlockSigner(transactOpts *bind.TransactOpts, contractAddr common.Address, contractBackend bind.ContractBackend) (*BlockSigner, error) { | ||
blockSigner, err := contract.NewBlockSigner(contractAddr, contractBackend) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &BlockSigner{ | ||
&contract.BlockSignerSession{ | ||
Contract: blockSigner, | ||
TransactOpts: *transactOpts, | ||
}, | ||
contractBackend, | ||
}, nil | ||
} | ||
|
||
func DeployBlockSigner(transactOpts *bind.TransactOpts, contractBackend bind.ContractBackend) (common.Address, *BlockSigner, error) { | ||
blockSignerAddr, _, _, err := contract.DeployBlockSigner(transactOpts, contractBackend) | ||
if err != nil { | ||
return blockSignerAddr, nil, err | ||
} | ||
|
||
blockSigner, err := NewBlockSigner(transactOpts, blockSignerAddr, contractBackend) | ||
if err != nil { | ||
return blockSignerAddr, nil, err | ||
} | ||
|
||
return blockSignerAddr, blockSigner, nil | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package blocksigner | ||
|
||
import ( | ||
"math/big" | ||
"testing" | ||
|
||
"github.com/ethereum/go-ethereum/accounts/abi/bind" | ||
"github.com/ethereum/go-ethereum/accounts/abi/bind/backends" | ||
"github.com/ethereum/go-ethereum/core" | ||
"github.com/ethereum/go-ethereum/crypto" | ||
) | ||
|
||
var ( | ||
key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") | ||
addr = crypto.PubkeyToAddress(key.PublicKey) | ||
) | ||
|
||
func TestBlockSigner(t *testing.T) { | ||
contractBackend := backends.NewSimulatedBackend(core.GenesisAlloc{addr: {Balance: big.NewInt(1000000000)}}) | ||
transactOpts := bind.NewKeyedTransactor(key) | ||
|
||
_, blockSigner, err := DeployBlockSigner(transactOpts, contractBackend) | ||
if err != nil { | ||
t.Fatalf("can't deploy root registry: %v", err) | ||
} | ||
contractBackend.Commit() | ||
|
||
signers, err := blockSigner.GetSigners(big.NewInt(0)) | ||
if err != nil { | ||
t.Fatalf("can't get candidates: %v", err) | ||
} | ||
for _, it := range signers { | ||
t.Log("signer", it.String()) | ||
} | ||
contractBackend.Commit() | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
pragma solidity ^0.4.21; | ||
|
||
import "./libs/SafeMath.sol"; | ||
|
||
contract BlockSigner { | ||
using SafeMath for uint256; | ||
|
||
event Sign(address _signer, uint256 blockNumber); | ||
|
||
mapping(uint256 => address[]) blockSigners; | ||
|
||
function sign(uint256 _blockNumber) external { | ||
// consensus should validate all senders are validators, gas = 0 | ||
require(block.number >= _blockNumber); | ||
require(block.number <= _blockNumber.add(990 * 2)); | ||
blockSigners[_blockNumber].push(msg.sender); | ||
|
||
emit Sign(msg.sender, _blockNumber); | ||
} | ||
|
||
function getSigners(uint256 _blockNumber) public view returns(address[]) { | ||
return blockSigners[_blockNumber]; | ||
} | ||
} |
Oops, something went wrong.