Skip to content

Commit

Permalink
Merge pull request expanse-org#8 from thanhson1085/dev
Browse files Browse the repository at this point in the history
add validator voting smart contract
  • Loading branch information
ngtuna authored Jun 14, 2018
2 parents ef06236 + 7ad8b48 commit 4c17084
Show file tree
Hide file tree
Showing 14 changed files with 2,692 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ profile.cov
# VS Code
.vscode

# Vim
.*.sw*

# dashboard
/dashboard/assets/flow-typed
/dashboard/assets/node_modules
Expand Down
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ matrix:
script:
- unset -f cd
- brew update
- brew install caskroom/cask/brew-cask
- brew cask install osxfuse
- go run build/ci.go install
- go run build/ci.go test -coverage
Expand Down
3 changes: 0 additions & 3 deletions cmd/tomo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/cmd/utils"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/console"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/eth"
Expand All @@ -47,8 +46,6 @@ const (
var (
// Git SHA1 commit hash of the release (set via linker flags)
gitCommit = ""
// Ethereum address of the Geth release oracle.
relOracle = common.HexToAddress("0xfa7b9770ca4cb04296cac84f37736d4041251cdf")
// The app that holds all commands and flags.
app = utils.NewApp(gitCommit, "the go-ethereum command line interface")
// flags that configure the node
Expand Down
41 changes: 41 additions & 0 deletions contracts/blocksigner/blocksigner.go
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
}
36 changes: 36 additions & 0 deletions contracts/blocksigner/blocksigner_test.go
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()
}
24 changes: 24 additions & 0 deletions contracts/blocksigner/contract/BlockSigner.sol
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];
}
}
Loading

0 comments on commit 4c17084

Please sign in to comment.