good morning!!!!

Skip to content
Snippets Groups Projects
Commit f7e58d60 authored by atvanguard's avatar atvanguard
Browse files

Test for IsValidatorAction

parent 1d5b2633
Branches
Tags
No related merge requests found
...@@ -163,6 +163,11 @@ Ethereum nodes with exposed APIs! Further, all browser tabs can access locally ...@@ -163,6 +163,11 @@ Ethereum nodes with exposed APIs! Further, all browser tabs can access locally
running web servers, so malicious web pages could try to subvert locally available running web servers, so malicious web pages could try to subvert locally available
APIs!** APIs!**
### Tests
```
go test github.com/maticnetwork/bor/consensus/bor_test
```
## License ## License
The bor library (i.e. all code outside of the `cmd` directory) is licensed under the The bor library (i.e. all code outside of the `cmd` directory) is licensed under the
......
...@@ -40,8 +40,10 @@ import ( ...@@ -40,8 +40,10 @@ import (
"golang.org/x/crypto/sha3" "golang.org/x/crypto/sha3"
) )
const validatorsetABI = `[{"constant":true,"inputs":[{"name":"span","type":"uint256"}],"name":"getSpan","outputs":[{"name":"number","type":"uint256"},{"name":"startBlock","type":"uint256"},{"name":"endBlock","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"number","type":"uint256"}],"name":"getBorValidators","outputs":[{"name":"","type":"address[]"},{"name":"","type":"uint256[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"span","type":"uint256"},{"name":"signer","type":"address"}],"name":"isProducer","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"newSpan","type":"uint256"},{"name":"startBlock","type":"uint256"},{"name":"endBlock","type":"uint256"},{"name":"validatorBytes","type":"bytes"},{"name":"producerBytes","type":"bytes"}],"name":"commitSpan","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"span","type":"uint256"},{"name":"signer","type":"address"}],"name":"isValidator","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"proposeSpan","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"currentSpanNumber","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"getNextSpan","outputs":[{"name":"number","type":"uint256"},{"name":"startBlock","type":"uint256"},{"name":"endBlock","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"getInitialValidators","outputs":[{"name":"","type":"address[]"},{"name":"","type":"uint256[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"spanProposalPending","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"getCurrentSpan","outputs":[{"name":"number","type":"uint256"},{"name":"startBlock","type":"uint256"},{"name":"endBlock","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"number","type":"uint256"}],"name":"getSpanByBlock","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"getValidators","outputs":[{"name":"","type":"address[]"},{"name":"","type":"uint256[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"vote","type":"bytes"},{"name":"sigs","type":"bytes"},{"name":"txBytes","type":"bytes"},{"name":"proof","type":"bytes"}],"name":"validateValidatorSet","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"}]` const (
const stateReceiverABI = `[{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"states","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"recordBytes","type":"bytes"}],"name":"commitState","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"getPendingStates","outputs":[{"name":"","type":"uint256[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"SYSTEM_ADDRESS","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"validatorSet","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"vote","type":"bytes"},{"name":"sigs","type":"bytes"},{"name":"txBytes","type":"bytes"},{"name":"proof","type":"bytes"}],"name":"validateValidatorSet","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"isValidatorSetContract","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"stateId","type":"uint256"}],"name":"proposeState","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"signer","type":"address"}],"name":"isProducer","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"signer","type":"address"}],"name":"isValidator","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"}]` validatorsetABI = `[{"constant":true,"inputs":[{"name":"span","type":"uint256"}],"name":"getSpan","outputs":[{"name":"number","type":"uint256"},{"name":"startBlock","type":"uint256"},{"name":"endBlock","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"number","type":"uint256"}],"name":"getBorValidators","outputs":[{"name":"","type":"address[]"},{"name":"","type":"uint256[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"span","type":"uint256"},{"name":"signer","type":"address"}],"name":"isProducer","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"newSpan","type":"uint256"},{"name":"startBlock","type":"uint256"},{"name":"endBlock","type":"uint256"},{"name":"validatorBytes","type":"bytes"},{"name":"producerBytes","type":"bytes"}],"name":"commitSpan","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"span","type":"uint256"},{"name":"signer","type":"address"}],"name":"isValidator","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"proposeSpan","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"currentSpanNumber","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"getNextSpan","outputs":[{"name":"number","type":"uint256"},{"name":"startBlock","type":"uint256"},{"name":"endBlock","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"getInitialValidators","outputs":[{"name":"","type":"address[]"},{"name":"","type":"uint256[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"spanProposalPending","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"getCurrentSpan","outputs":[{"name":"number","type":"uint256"},{"name":"startBlock","type":"uint256"},{"name":"endBlock","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"number","type":"uint256"}],"name":"getSpanByBlock","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"getValidators","outputs":[{"name":"","type":"address[]"},{"name":"","type":"uint256[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"vote","type":"bytes"},{"name":"sigs","type":"bytes"},{"name":"txBytes","type":"bytes"},{"name":"proof","type":"bytes"}],"name":"validateValidatorSet","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"}]`
stateReceiverABI = `[{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"states","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"recordBytes","type":"bytes"}],"name":"commitState","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"getPendingStates","outputs":[{"name":"","type":"uint256[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"SYSTEM_ADDRESS","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"validatorSet","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"vote","type":"bytes"},{"name":"sigs","type":"bytes"},{"name":"txBytes","type":"bytes"},{"name":"proof","type":"bytes"}],"name":"validateValidatorSet","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"isValidatorSetContract","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"stateId","type":"uint256"}],"name":"proposeState","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"signer","type":"address"}],"name":"isProducer","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"signer","type":"address"}],"name":"isValidator","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"}]`
)
const ( const (
checkpointInterval = 1024 // Number of blocks after which to save the vote snapshot to the database checkpointInterval = 1024 // Number of blocks after which to save the vote snapshot to the database
...@@ -1225,16 +1227,30 @@ func (c *Bor) IsValidatorAction(chain consensus.ChainReader, from common.Address ...@@ -1225,16 +1227,30 @@ func (c *Bor) IsValidatorAction(chain consensus.ChainReader, from common.Address
log.Error("Failed fetching snapshot", err) log.Error("Failed fetching snapshot", err)
} }
_IsValidatorAction := false isValidator := false
for _, validator := range snap.ValidatorSet.Validators { for _, validator := range snap.ValidatorSet.Validators {
if bytes.Compare(validator.Address.Bytes(), from.Bytes()) == 0 { if bytes.Compare(validator.Address.Bytes(), from.Bytes()) == 0 {
_IsValidatorAction = true isValidator = true
break break
} }
} }
// @todo only either of proposeState and proposeSpan should pass this check return isValidator && (isProposeSpanAction(tx, chain.Config().Bor.ValidatorContract) ||
fmt.Println("tx.Data", tx.Data()) isProposeStateAction(tx, chain.Config().Bor.StateReceiverContract))
return _IsValidatorAction }
func isProposeSpanAction(tx *types.Transaction, validatorContract string) bool {
// keccak256('proposeSpan()').slice(0, 4)
proposeSpanSig, _ := hex.DecodeString("4b0e4d17")
return bytes.Compare(proposeSpanSig, tx.Data()[:4]) == 0 &&
tx.To().String() == validatorContract
}
func isProposeStateAction(tx *types.Transaction, stateReceiverContract string) bool {
// keccak256('proposeState(uint256)').slice(0, 4)
proposeStateSig, _ := hex.DecodeString("ede01f17")
fmt.Println(bytes.Compare(proposeStateSig, tx.Data()[:4]) == 0, tx.To().String(), stateReceiverContract)
return bytes.Compare(proposeStateSig, tx.Data()[:4]) == 0 &&
tx.To().String() == stateReceiverContract
} }
// //
......
package bor_test package bor_test
import ( import (
"encoding/hex"
"encoding/json" "encoding/json"
"fmt"
"io/ioutil" "io/ioutil"
"math/big" "math/big"
"testing" "testing"
"github.com/stretchr/testify/assert"
"github.com/maticnetwork/bor/common"
"github.com/maticnetwork/bor/consensus/bor" "github.com/maticnetwork/bor/consensus/bor"
"github.com/maticnetwork/bor/core" "github.com/maticnetwork/bor/core"
"github.com/maticnetwork/bor/core/rawdb" "github.com/maticnetwork/bor/core/rawdb"
"github.com/maticnetwork/bor/core/types" "github.com/maticnetwork/bor/core/types"
"github.com/maticnetwork/bor/core/vm"
"github.com/maticnetwork/bor/crypto" "github.com/maticnetwork/bor/crypto"
"github.com/maticnetwork/bor/eth" "github.com/maticnetwork/bor/eth"
"github.com/maticnetwork/bor/internal/ethapi"
// "github.com/maticnetwork/bor/node" "github.com/maticnetwork/bor/ethdb"
// "github.com/maticnetwork/bor/params" "github.com/maticnetwork/bor/node"
) )
func TestIsValidatorAction(t *testing.T) { func TestIsValidatorAction(t *testing.T) {
...@@ -24,59 +26,77 @@ func TestIsValidatorAction(t *testing.T) { ...@@ -24,59 +26,77 @@ func TestIsValidatorAction(t *testing.T) {
db = rawdb.NewMemoryDatabase() db = rawdb.NewMemoryDatabase()
key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
addr = crypto.PubkeyToAddress(key.PublicKey) addr = crypto.PubkeyToAddress(key.PublicKey)
// signer = new(types.HomesteadSigner)
) )
data, err := ioutil.ReadFile("genesis.json") ethereum := buildEthereumInstance(t, db)
chain := ethereum.BlockChain()
engine := ethereum.Engine()
bor := engine.(*bor.Bor)
// proposeState
data, _ := hex.DecodeString("ede01f170000000000000000000000000000000000000000000000000000000000000000")
tx := types.NewTransaction(
0,
common.HexToAddress(chain.Config().Bor.StateReceiverContract),
big.NewInt(0), 0, big.NewInt(0),
data,
)
assert.True(t, bor.IsValidatorAction(chain, addr, tx))
// proposeSpan
data, _ = hex.DecodeString("4b0e4d17")
tx = types.NewTransaction(
0,
common.HexToAddress(chain.Config().Bor.ValidatorContract),
big.NewInt(0), 0, big.NewInt(0),
data,
)
assert.True(t, bor.IsValidatorAction(chain, addr, tx))
}
func buildEthereumInstance(t *testing.T, db ethdb.Database) *eth.Ethereum {
genesisData, err := ioutil.ReadFile("genesis.json")
if err != nil { if err != nil {
t.Fatalf("%s", err) t.Fatalf("%s", err)
} }
config := eth.Config gen := &core.Genesis{}
Genesis: &core.Genesis{}, if err := json.Unmarshal(genesisData, gen); err != nil {
}
// var genesis core.Genesis
if err := json.Unmarshal(data, config.Genesis); err != nil {
t.Fatalf("%s", err) t.Fatalf("%s", err)
} }
// fmt.Println(genesis) // chainConfig, _, err := core.SetupGenesisBlock(db, gen)
chainConfig, _, err := core.SetupGenesisBlock(db, config.Genesis) ethConf := &eth.Config{
fmt.Printf("Chain config: %v\n", chainConfig) Genesis: gen,
}
ethConf.Genesis.MustCommit(db)
// copy(genspec.ExtraData[extraVanity:], addr[:]) // Create a temporary storage for the node keys and initialize it
config.Genesis.MustCommit(db) workspace, err := ioutil.TempDir("", "console-tester-")
if err != nil {
t.Fatalf("failed to create temporary keystore: %v", err)
}
_eth := &eth.Ethereum{ // Create a networkless protocol stack and start an Ethereum service within
// config: config, stack, err := node.New(&node.Config{DataDir: workspace, UseLightweightKDF: true, Name: "console-tester"})
// chainDb: chainDb, if err != nil {
// eventMux: ctx.EventMux, t.Fatalf("failed to create node: %v", err)
// accountManager: ctx.AccountManager, }
// engine: nil, err = stack.Register(func(ctx *node.ServiceContext) (node.Service, error) {
// shutdownChan: make(chan bool), s, err := eth.New(ctx, ethConf)
// networkID: config.NetworkId, return s, err
// gasPrice: config.Miner.GasPrice, })
// etherbase: config.Miner.Etherbase, if err != nil {
// bloomRequests: make(chan chan *bloombits.Retrieval), t.Fatalf("failed to register Ethereum protocol: %v", err)
// bloomIndexer: NewBloomIndexer(chainDb, params.BloomBitsBlocks, params.BloomConfirms),
} }
_eth.APIBackend = &eth.EthAPIBackend{false, _eth, nil} // Start the node and assemble the JavaScript console around it
ethAPI := ethapi.NewPublicBlockChainAPI(_eth.APIBackend) if err = stack.Start(); err != nil {
// ctx := &node.ServiceContext{ t.Fatalf("failed to start test stack: %v", err)
// config: &node.Config{}, }
// // services: make(map[reflect.Type]Service), _, err = stack.Attach()
// // EventMux: n.eventmux,
// // AccountManager: n.accman,
// }
engine := bor.New(chainConfig, db, &ethapi.PublicBlockChainAPI{})
chain, err := core.NewBlockChain(db, nil, chainConfig, engine, vm.Config{}, nil)
if err != nil { if err != nil {
t.Fatalf("%s", err) t.Fatalf("failed to attach to node: %v", err)
} }
tx := types.NewTransaction( var ethereum *eth.Ethereum
0, stack.Service(&ethereum)
addr, // to - Just a place holder return ethereum
big.NewInt(0), 0 /* fix gas limit */, big.NewInt(0),
nil, // data
)
engine.IsValidatorAction(chain, addr, tx)
} }
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment