-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
consortium/mock: implement consortiumv2 mocking logics which mocks al…
…l contract interaction code - This commit also adds `--mock.validators` and `--mock.blspublickeys` to simulate validators set and bls voting public keys
- Loading branch information
Showing
8 changed files
with
181 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package common | ||
|
||
import ( | ||
"errors" | ||
"math/big" | ||
"strings" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/crypto/bls/blst" | ||
blsCommon "github.com/ethereum/go-ethereum/crypto/bls/common" | ||
"github.com/ethereum/go-ethereum/log" | ||
) | ||
|
||
var Validators *MockValidators | ||
|
||
type MockValidators struct { | ||
validators []common.Address | ||
blsPublicKeys map[common.Address]blsCommon.PublicKey | ||
} | ||
|
||
func SetMockValidators(validators, publicKeys string) error { | ||
vals := strings.Split(validators, ",") | ||
pubs := strings.Split(publicKeys, ",") | ||
if len(vals) != len(pubs) { | ||
return errors.New("mismatch length between mock validators and mock blsPubKey") | ||
} | ||
Validators = &MockValidators{ | ||
validators: make([]common.Address, len(vals)), | ||
blsPublicKeys: make(map[common.Address]blsCommon.PublicKey), | ||
} | ||
for i, val := range vals { | ||
Validators.validators[i] = common.HexToAddress(val) | ||
pubKey, err := blst.PublicKeyFromBytes(common.Hex2Bytes(pubs[i])) | ||
if err != nil { | ||
return err | ||
} | ||
Validators.blsPublicKeys[Validators.validators[i]] = pubKey | ||
} | ||
return nil | ||
} | ||
|
||
func (m *MockValidators) GetValidators() []common.Address { | ||
return m.validators | ||
} | ||
|
||
func (m *MockValidators) GetPublicKey(addr common.Address) (blsCommon.PublicKey, error) { | ||
if key, ok := m.blsPublicKeys[addr]; ok { | ||
return key, nil | ||
} | ||
return nil, errors.New("public key not found") | ||
} | ||
|
||
type MockContract struct { | ||
} | ||
|
||
func (contract *MockContract) GetValidators(*big.Int) ([]common.Address, error) { | ||
return Validators.GetValidators(), nil | ||
} | ||
|
||
func (contract *MockContract) WrapUpEpoch(*ApplyTransactOpts) error { | ||
log.Info("WrapUpEpoch") | ||
return nil | ||
} | ||
|
||
func (contract *MockContract) SubmitBlockReward(*ApplyTransactOpts) error { | ||
log.Info("SubmitBlockReward") | ||
return nil | ||
} | ||
|
||
func (contract *MockContract) Slash(*ApplyTransactOpts, common.Address) error { | ||
return nil | ||
} | ||
|
||
func (contract *MockContract) FinalityReward(*ApplyTransactOpts, []common.Address) error { | ||
return nil | ||
} | ||
|
||
func (contract *MockContract) GetBlsPublicKey(_ *big.Int, addr common.Address) (blsCommon.PublicKey, error) { | ||
return Validators.GetPublicKey(addr) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters