This commit is contained in:
atvanguard 2020-03-04 15:43:29 +05:30
parent a3e01638a4
commit 1d5b263362
5 changed files with 135 additions and 63 deletions

View file

@ -1218,23 +1218,23 @@ func (c *Bor) CommitStates(
return nil
}
func (c *Bor) isValidatorAction(chain consensus.ChainReader, from common.Address, tx *types.Transaction) bool {
func (c *Bor) IsValidatorAction(chain consensus.ChainReader, from common.Address, tx *types.Transaction) bool {
header := chain.CurrentHeader()
snap, err := c.snapshot(chain, header.Number.Uint64(), header.Hash(), nil)
if err != nil {
log.Error("Failed fetching snapshot", err)
}
_isValidatorAction := false
_IsValidatorAction := false
for _, validator := range snap.ValidatorSet.Validators {
if bytes.Compare(validator.Address.Bytes(), from.Bytes()) == 0 {
_isValidatorAction = true
_IsValidatorAction = true
break
}
}
// @todo only either of proposeState and proposeSpan should pass this check
fmt.Println("tx.Data", tx.Data())
return _isValidatorAction
return _IsValidatorAction
}
//

View file

@ -1,57 +0,0 @@
package bor
import (
"math/big"
"testing"
"github.com/maticnetwork/bor/common"
"github.com/maticnetwork/bor/core"
"github.com/maticnetwork/bor/core/rawdb"
"github.com/maticnetwork/bor/core/types"
"github.com/maticnetwork/bor/core/vm"
"github.com/maticnetwork/bor/crypto"
"github.com/maticnetwork/bor/internal/ethapi"
"github.com/maticnetwork/bor/params"
)
type MockEthAPI struct{}
// func (ethapi *MockEthAPI) call()
func TestIsValidatorAction(t *testing.T) {
var (
db = rawdb.NewMemoryDatabase()
key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
addr = crypto.PubkeyToAddress(key.PublicKey)
// bor = New(&params.ChainConfig{}, db, &ethapi.PublicBlockChainAPI{})
// signer = new(types.HomesteadSigner)
)
genspec := &core.Genesis{
ExtraData: make([]byte, extraVanity+common.AddressLength+extraSeal),
Alloc: map[common.Address]core.GenesisAccount{
addr: {Balance: big.NewInt(1)},
},
}
copy(genspec.ExtraData[extraVanity:], addr[:])
genspec.MustCommit(db)
// genesis := genspec.MustCommit(db)
config := &params.ChainConfig{
Bor: &params.BorConfig{
ValidatorContract: "0x0000000000000000000000000000000000001000",
StateReceiverContract: "0x0000000000000000000000000000000000001001",
},
}
bor := New(config, db, &ethapi.PublicBlockChainAPI{})
chain, err := core.NewBlockChain(db, nil, config, bor, vm.Config{}, nil)
if err != nil {
t.Fatalf("%s", err)
}
tx := types.NewTransaction(
0,
addr, // to - Just a place holder
big.NewInt(0), 0 /* fix gas limit */, big.NewInt(0),
nil, // data
)
bor.isValidatorAction(chain, addr, tx)
}

View file

@ -0,0 +1,82 @@
package bor_test
import (
"encoding/json"
"fmt"
"io/ioutil"
"math/big"
"testing"
"github.com/maticnetwork/bor/consensus/bor"
"github.com/maticnetwork/bor/core"
"github.com/maticnetwork/bor/core/rawdb"
"github.com/maticnetwork/bor/core/types"
"github.com/maticnetwork/bor/core/vm"
"github.com/maticnetwork/bor/crypto"
"github.com/maticnetwork/bor/eth"
"github.com/maticnetwork/bor/internal/ethapi"
// "github.com/maticnetwork/bor/node"
// "github.com/maticnetwork/bor/params"
)
func TestIsValidatorAction(t *testing.T) {
var (
db = rawdb.NewMemoryDatabase()
key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
addr = crypto.PubkeyToAddress(key.PublicKey)
// signer = new(types.HomesteadSigner)
)
data, err := ioutil.ReadFile("genesis.json")
if err != nil {
t.Fatalf("%s", err)
}
config := eth.Config
Genesis: &core.Genesis{},
}
// var genesis core.Genesis
if err := json.Unmarshal(data, config.Genesis); err != nil {
t.Fatalf("%s", err)
}
// fmt.Println(genesis)
chainConfig, _, err := core.SetupGenesisBlock(db, config.Genesis)
fmt.Printf("Chain config: %v\n", chainConfig)
// copy(genspec.ExtraData[extraVanity:], addr[:])
config.Genesis.MustCommit(db)
_eth := &eth.Ethereum{
// config: config,
// chainDb: chainDb,
// eventMux: ctx.EventMux,
// accountManager: ctx.AccountManager,
// engine: nil,
// shutdownChan: make(chan bool),
// networkID: config.NetworkId,
// gasPrice: config.Miner.GasPrice,
// etherbase: config.Miner.Etherbase,
// bloomRequests: make(chan chan *bloombits.Retrieval),
// bloomIndexer: NewBloomIndexer(chainDb, params.BloomBitsBlocks, params.BloomConfirms),
}
_eth.APIBackend = &eth.EthAPIBackend{false, _eth, nil}
ethAPI := ethapi.NewPublicBlockChainAPI(_eth.APIBackend)
// ctx := &node.ServiceContext{
// config: &node.Config{},
// // services: make(map[reflect.Type]Service),
// // 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 {
t.Fatalf("%s", err)
}
tx := types.NewTransaction(
0,
addr, // to - Just a place holder
big.NewInt(0), 0 /* fix gas limit */, big.NewInt(0),
nil, // data
)
engine.IsValidatorAction(chain, addr, tx)
}

File diff suppressed because one or more lines are too long

View file

@ -120,7 +120,7 @@ const (
// bor acts as a way to be able to type cast consensus.Engine;
// since importing "github.com/maticnetwork/bor/consensus/bor" results in a cyclic dependency
type bor interface {
isValidatorAction(chain consensus.ChainReader, from common.Address, tx *types.Transaction) bool
IsValidatorAction(chain consensus.ChainReader, from common.Address, tx *types.Transaction) bool
}
// blockChain provides the state of blockchain and current gas limit to do
@ -537,7 +537,7 @@ func (pool *TxPool) validateTx(tx *types.Transaction, local bool) error {
// Drop non-local transactions under our own minimal accepted gas price
local = local || pool.locals.contains(from) // account may be local even if the transaction arrived from the network
if !local &&
!pool.chain.Engine().(bor).isValidatorAction(pool.chain.(consensus.ChainReader), from, tx) &&
!pool.chain.Engine().(bor).IsValidatorAction(pool.chain.(consensus.ChainReader), from, tx) &&
pool.gasPrice.Cmp(tx.GasPrice()) > 0 {
return ErrUnderpriced
}