Skip to content

Commit

Permalink
Merge pull request ethereum#153 from dinhln89/randomize
Browse files Browse the repository at this point in the history
Randomization implementation
  • Loading branch information
ngtuna authored Oct 4, 2018
2 parents 9fa830c + 911663d commit dacfcc7
Show file tree
Hide file tree
Showing 15,771 changed files with 6,035,910 additions and 68 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

5 changes: 3 additions & 2 deletions cmd/tomo/dao_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ var daoProForkGenesis = `{
}
}`

var daoGenesisHash = common.HexToHash("5e1fc79cb4ffa4739177b5408045cd5d51c6cf766133f23f7cd72ee1f8d790e0")
var daoGenesisHash = common.HexToHash("f2ea0466bf5a07cb7407474d9fbaae6e275127f038ca57a673b833234204f4fd")
var daoGenesisForkBlock = big.NewInt(314)

// TestDAOForkBlockNewChain tests that the DAO hard-fork number and the nodes support/opposition is correctly
Expand Down Expand Up @@ -127,10 +127,11 @@ func testDAOForkBlockNewChain(t *testing.T, test int, genesis string, expectBloc
}
defer db.Close()

genesisHash := common.HexToHash("0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3")
genesisHash := common.HexToHash("2efa267fef46877ac2659209e2299f97b0afc2a797ee8672db21920a5151e0aa")
if genesis != "" {
genesisHash = daoGenesisHash
}
t.Log("genesisHash", genesisHash.String())
config, err := core.GetChainConfig(db, genesisHash)
if err != nil {
t.Errorf("test %d: failed to retrieve chain config: %v", test, err)
Expand Down
14 changes: 14 additions & 0 deletions common/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package common

const (
RewardMasterPercent = 40
RewardVoterPercent = 50
RewardFoundationPercent = 10
HexSignMethod = "e341eaa4"
HexSetSecret = "34d38600"
HexSetOpening = "e11f5ba2"
EpocBlockSecret = 800
EpocBlockOpening = 850
EpocBlockRandomize = 900
MaxMasternodes = 150
)
4 changes: 2 additions & 2 deletions contracts/randomize/contract/randomize.go

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

87 changes: 81 additions & 6 deletions contracts/randomize/randomize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,25 @@ import (
"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/contracts"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
)

var (
key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
addr = crypto.PubkeyToAddress(key.PublicKey)
byte0 = make([][32]byte, 2)
epocNumber = int64(12)
key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
addr = crypto.PubkeyToAddress(key.PublicKey)
byte0 = make([][32]byte, epocNumber)
acc1Key, _ = crypto.HexToECDSA("8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a")
acc1Addr = crypto.PubkeyToAddress(acc1Key.PublicKey)
)

func TestRandomize(t *testing.T) {
contractBackend := backends.NewSimulatedBackend(core.GenesisAlloc{addr: {Balance: big.NewInt(1000000000)}})
contractBackend := backends.NewSimulatedBackend(core.GenesisAlloc{addr: {Balance: big.NewInt(100000000000000)}})
transactOpts := bind.NewKeyedTransactor(key)
transactOpts.GasLimit = 1000000

randomizeAddress, randomize, err := DeployRandomize(transactOpts, contractBackend, big.NewInt(2))
t.Log("contract address", randomizeAddress.String())
Expand All @@ -55,11 +61,80 @@ func TestRandomize(t *testing.T) {
return true
}
contractBackend.ForEachStorageAt(ctx, randomizeAddress, nil, f)

s, err := randomize.SetSecret(byte0)
if err != nil {
t.Fatalf("can't get secret: %v", err)
t.Fatalf("can't set secret: %v", err)
}
t.Log("tx data", s)
contractBackend.Commit()
}

func TestSendTxRandomizeSecretAndOpening(t *testing.T) {
genesis := core.GenesisAlloc{acc1Addr: {Balance: big.NewInt(1000000000000)}}
backend := backends.NewSimulatedBackend(genesis)
backend.Commit()
signer := types.HomesteadSigner{}
ctx := context.Background()

transactOpts := bind.NewKeyedTransactor(acc1Key)
transactOpts.GasLimit = 4200000
epocNumber := uint64(900)
randomizeAddr, randomizeContract, err := DeployRandomize(transactOpts, backend, new(big.Int).SetInt64(0))
if err != nil {
t.Fatalf("Can't deploy randomize SC: %v", err)
}
backend.Commit()

nonce := uint64(1)
randomizeKeyValue := contracts.RandStringByte(32)
tx, err := contracts.BuildTxSecretRandomize(nonce, randomizeAddr, epocNumber, randomizeKeyValue)
if err != nil {
t.Fatalf("Can't create tx randomize secret: %v", err)
}
tx, err = types.SignTx(tx, signer, acc1Key)
if err != nil {
t.Fatalf("Can't sign tx randomize secret: %v", err)
}

err = backend.SendTransaction(ctx, tx)
if err != nil {
t.Fatalf("Can't send tx for create randomize secret: %v", err)
}
backend.Commit()
// Increment nonce.
nonce++
// Set opening.
tx, err = contracts.BuildTxOpeningRandomize(nonce, randomizeAddr, randomizeKeyValue)
if err != nil {
t.Fatalf("Can't create tx randomize opening: %v", err)
}
tx, err = types.SignTx(tx, signer, acc1Key)
if err != nil {
t.Fatalf("Can't sign tx randomize opening: %v", err)
}

err = backend.SendTransaction(ctx, tx)
if err != nil {
t.Fatalf("Can't send tx for create randomize opening: %v", err)
}
backend.Commit()

// Get randomize secret from SC.
secrets, err := randomizeContract.GetSecret(acc1Addr)
if err != nil {
t.Error("Fail get secrets from randomize", err)
}
if len(secrets) <= 0 {
t.Error("Empty get secrets from SC", err)
}
// Decrypt randomize from SC.
opening, err := randomizeContract.GetOpening(acc1Addr)
if err != nil {
t.Fatalf("Can't get secret from SC: %v", err)
}
randomize, err := contracts.DecryptRandomizeFromSecretsAndOpening(secrets, opening)
t.Log("randomize", randomize)
if err != nil {
t.Error("Can't decrypt secret and opening", err)
}
}
Loading

0 comments on commit dacfcc7

Please sign in to comment.