Skip to content

Commit

Permalink
Merge pull request ethereum#89 from dinhln89/master
Browse files Browse the repository at this point in the history
BlockSigners Smart Contract - Move from BlockNumber to BlockHash
  • Loading branch information
dinhln89 authored Jul 26, 2018
2 parents f807412 + 275d76c commit 08ec57c
Show file tree
Hide file tree
Showing 8 changed files with 199 additions and 105 deletions.
13 changes: 13 additions & 0 deletions accounts/abi/bind/backends/simulated.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,19 @@ func (b *SimulatedBackend) StorageAt(ctx context.Context, contract common.Addres
return val[:], nil
}

// ForEachStorageAt returns func to read all keys, values in the storage
func (b *SimulatedBackend) ForEachStorageAt(ctx context.Context, contract common.Address, blockNumber *big.Int, f func(key, val common.Hash) bool) error {
b.mu.Lock()
defer b.mu.Unlock()

if blockNumber != nil && blockNumber.Cmp(b.blockchain.CurrentBlock().Number()) != 0 {
return errBlockNumberUnsupported
}
statedb, _ := b.blockchain.State()
statedb.ForEachStorage(contract, f)
return nil
}

// TransactionReceipt returns the receipt of a transaction.
func (b *SimulatedBackend) TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error) {
receipt, _, _, _ := core.GetReceipt(b.database, txHash)
Expand Down
3 changes: 2 additions & 1 deletion contracts/blocksigner/blocksigner.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/contracts/blocksigner/contract"
"math/big"
)

type BlockSigner struct {
Expand All @@ -27,7 +28,7 @@ func NewBlockSigner(transactOpts *bind.TransactOpts, contractAddr common.Address
}

func DeployBlockSigner(transactOpts *bind.TransactOpts, contractBackend bind.ContractBackend) (common.Address, *BlockSigner, error) {
blockSignerAddr, _, _, err := contract.DeployBlockSigner(transactOpts, contractBackend)
blockSignerAddr, _, _, err := contract.DeployBlockSigner(transactOpts, contractBackend, big.NewInt(99))
if err != nil {
return blockSignerAddr, nil, err
}
Expand Down
41 changes: 38 additions & 3 deletions contracts/blocksigner/blocksigner_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package blocksigner

import (
"context"
"math/big"
"testing"
"time"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/accounts/abi/bind/backends"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/crypto"
"math/rand"
)

var (
Expand All @@ -19,18 +23,49 @@ func TestBlockSigner(t *testing.T) {
contractBackend := backends.NewSimulatedBackend(core.GenesisAlloc{addr: {Balance: big.NewInt(1000000000)}})
transactOpts := bind.NewKeyedTransactor(key)

_, blockSigner, err := DeployBlockSigner(transactOpts, contractBackend)
blockSignerAddress, 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))
d := time.Now().Add(1000 * time.Millisecond)
ctx, cancel := context.WithDeadline(context.Background(), d)
defer cancel()
code, _ := contractBackend.CodeAt(ctx, blockSignerAddress, nil)
t.Log("contract code", common.ToHex(code))
f := func(key, val common.Hash) bool {
t.Log(key.Hex(), val.Hex())
return true
}
contractBackend.ForEachStorageAt(ctx, blockSignerAddress, nil, f)

byte0 := randomHash()

// Test sign.
tx, err := blockSigner.Sign(big.NewInt(50), byte0)
if err != nil {
t.Fatalf("can't sign: %v", err)
}
contractBackend.Commit()
t.Log("tx", tx)

signers, err := blockSigner.GetSigners(byte0)
if err != nil {
t.Fatalf("can't get candidates: %v", err)
}
for _, it := range signers {
t.Log("signer", it.String())
}
contractBackend.Commit()
}

// Generate random string.
func randomHash() common.Hash {
letterBytes := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789"
var b common.Hash
for i := range b {
rand.Seed(time.Now().UnixNano())
b[i] = letterBytes[rand.Intn(len(letterBytes))]
}
return b
}
25 changes: 16 additions & 9 deletions contracts/blocksigner/contract/BlockSigner.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,27 @@ import "./libs/SafeMath.sol";
contract BlockSigner {
using SafeMath for uint256;

event Sign(address _signer, uint256 _blockNumber);
event Sign(address _signer, uint256 _blockNumber, bytes32 _blockHash);

mapping(uint256 => address[]) blockSigners;
mapping(bytes32 => address[]) blockSigners;
mapping(uint256 => bytes32[]) blocks;
uint256 public epochNumber;

function sign(uint256 _blockNumber) external {
function BlockSigner(uint256 _epochNumber) public {
epochNumber = _epochNumber;
}

function sign(uint256 _blockNumber, bytes32 _blockHash) 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);
//require(block.number >= _blockNumber);
//require(block.number <= _blockNumber.add(epochNumber * 2));
blocks[_blockNumber].push(_blockHash);
blockSigners[_blockHash].push(msg.sender);

emit Sign(msg.sender, _blockNumber);
emit Sign(msg.sender, _blockNumber, _blockHash);
}

function getSigners(uint256 _blockNumber) public view returns(address[]) {
return blockSigners[_blockNumber];
function getSigners(bytes32 _blockHash) public view returns(address[]) {
return blockSigners[_blockHash];
}
}
91 changes: 59 additions & 32 deletions contracts/blocksigner/contract/blocksigner.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 08ec57c

Please sign in to comment.