mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 10:22:23 +00:00
contracts: add registrar wrapper and unittests
This commit is contained in:
parent
8f0e1537bb
commit
1f2f7ea18e
5 changed files with 267 additions and 1 deletions
|
|
@ -405,6 +405,28 @@ func (b *SimulatedBackend) AdjustTime(adjustment time.Duration) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ShiftBlocks inserts a batch of empty blocks to blockchain.
|
||||||
|
func (b *SimulatedBackend) ShiftBlocks(number int) error {
|
||||||
|
b.mu.Lock()
|
||||||
|
defer b.mu.Unlock()
|
||||||
|
// Insert a batch of empty blocks and commit to the database
|
||||||
|
blocks, _ := core.GenerateChain(b.config, b.blockchain.CurrentBlock(), ethash.NewFaker(), b.database, number, func(i int, block *core.BlockGen) {})
|
||||||
|
if _, err := b.blockchain.InsertChain(blocks); err != nil {
|
||||||
|
panic(err) // This cannot happen unless the simulator is wrong, fail in that case
|
||||||
|
}
|
||||||
|
// Apply all pending transactions to new pending blocks.
|
||||||
|
blocks, _ = core.GenerateChain(b.config, b.blockchain.CurrentBlock(), ethash.NewFaker(), b.database, 1, func(number int, block *core.BlockGen) {
|
||||||
|
for _, tx := range b.pendingBlock.Transactions() {
|
||||||
|
block.AddTx(tx)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
statedb, _ := b.blockchain.State()
|
||||||
|
b.pendingBlock = blocks[0]
|
||||||
|
b.pendingState, _ = state.New(b.pendingBlock.Root(), statedb.Database())
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// callmsg implements core.Message to allow passing it as a transaction simulator.
|
// callmsg implements core.Message to allow passing it as a transaction simulator.
|
||||||
type callmsg struct {
|
type callmsg struct {
|
||||||
ethereum.CallMsg
|
ethereum.CallMsg
|
||||||
|
|
|
||||||
File diff suppressed because one or more lines are too long
|
|
@ -51,6 +51,9 @@ contract Registrar {
|
||||||
Public Functions
|
Public Functions
|
||||||
*/
|
*/
|
||||||
constructor(address[] _adminlist) public {
|
constructor(address[] _adminlist) public {
|
||||||
|
// regard contract creator as a default admin.
|
||||||
|
admins[msg.sender] = 1;
|
||||||
|
adminList.push(msg.sender);
|
||||||
for (uint i = 0; i < _adminlist.length; i++) {
|
for (uint i = 0; i < _adminlist.length; i++) {
|
||||||
admins[_adminlist[i]] = 1;
|
admins[_adminlist[i]] = 1;
|
||||||
adminList.push(_adminlist[i]);
|
adminList.push(_adminlist[i]);
|
||||||
|
|
@ -113,6 +116,11 @@ contract Registrar {
|
||||||
if (_sectionIndex != latest + 1 && latest != 0) {
|
if (_sectionIndex != latest + 1 && latest != 0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
// Ensure the checkpoint is stable enough to be registered.
|
||||||
|
if (block.number < (_sectionIndex+1)*sectionSize+confirmations) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
checkpoints[_sectionIndex] = Checkpoint({
|
checkpoints[_sectionIndex] = Checkpoint({
|
||||||
sectionHead: _sectionHead,
|
sectionHead: _sectionHead,
|
||||||
chtRoot: _chtRoot,
|
chtRoot: _chtRoot,
|
||||||
|
|
@ -208,5 +216,11 @@ contract Registrar {
|
||||||
// Latest stored section id
|
// Latest stored section id
|
||||||
// Note all registered checkpoint information should continuous with previous one.
|
// Note all registered checkpoint information should continuous with previous one.
|
||||||
uint latest;
|
uint latest;
|
||||||
|
|
||||||
|
// The frequency for creating a checkpoint
|
||||||
|
uint constant sectionSize = 32768;
|
||||||
|
|
||||||
|
// The number of confirmations needed before a checkpoint can be registered.
|
||||||
|
uint constant confirmations = 10000;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
88
contracts/registrar/registrar.go
Normal file
88
contracts/registrar/registrar.go
Normal file
|
|
@ -0,0 +1,88 @@
|
||||||
|
// Copyright 2018 The go-ethereum Authors
|
||||||
|
// This file is part of the go-ethereum library.
|
||||||
|
//
|
||||||
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU Lesser General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Lesser General Public License
|
||||||
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
package registrar
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"math/big"
|
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
||||||
|
"github.com/ethereum/go-ethereum/common"
|
||||||
|
"github.com/ethereum/go-ethereum/contracts/registrar/contract"
|
||||||
|
"github.com/ethereum/go-ethereum/eth"
|
||||||
|
"github.com/ethereum/go-ethereum/event"
|
||||||
|
"github.com/ethereum/go-ethereum/internal/ethapi"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
MainNetAddr = common.HexToAddress("")
|
||||||
|
TestNetAddr = common.HexToAddress("0x3b934494985d17bcb49557671e1bc8ec32cccdd5") // Rinkeby
|
||||||
|
)
|
||||||
|
|
||||||
|
var errEventNotFound = errors.New("contract event not found")
|
||||||
|
|
||||||
|
const (
|
||||||
|
sectionSize = 32768 // The frequency for creating a checkpoint
|
||||||
|
checkpointConfirmation = 10000 // The number of confirmations needed before a checkpoint becoming stable.
|
||||||
|
)
|
||||||
|
|
||||||
|
// Checkpoint represents a set of post-processed trie roots (CHT and BloomTrie) associated with
|
||||||
|
// the appropriate section index and head hash.
|
||||||
|
//
|
||||||
|
// It is used to start light syncing from this checkpoint and avoid downloading the entire header chain
|
||||||
|
// while still being able to securely access old headers/logs.
|
||||||
|
type Checkpoint struct {
|
||||||
|
SectionIndex uint64
|
||||||
|
SectionHead common.Hash // Block Hash for the last block in the section
|
||||||
|
ChtRoot common.Hash // CHT(Canonical Hash Trie) root associated to the section
|
||||||
|
BloomTrieRoot common.Hash // Bloom Trie root associated to the section
|
||||||
|
}
|
||||||
|
|
||||||
|
type Registrar struct {
|
||||||
|
contract *contract.Contract
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewRegistrar binds checkpoint contract and returns a registrar instance.
|
||||||
|
func NewRegistrar(contractAddr common.Address, backend ethapi.Backend, lightMode bool) (*Registrar, error) {
|
||||||
|
contract, err := contract.NewContract(contractAddr, eth.NewContractBackend(backend, lightMode))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &Registrar{
|
||||||
|
contract: contract,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// WatchNewCheckpointEvent watches new fired NewCheckpointEvent and delivers all matching events by result channel.
|
||||||
|
func (registrar *Registrar) WatchNewCheckpointEvent(sink chan<- *contract.ContractNewCheckpointEvent) (event.Subscription, error) {
|
||||||
|
return registrar.contract.WatchNewCheckpointEvent(nil, sink, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FilterNewCheckpointEvent filters out NewCheckpointEvent for specific section number.
|
||||||
|
func (registrar *Registrar) FilterNewCheckpointEvent(head uint64, section uint64) (*contract.ContractNewCheckpointEventIterator, error) {
|
||||||
|
start := (section + 1) * sectionSize
|
||||||
|
end := head - checkpointConfirmation
|
||||||
|
if end < start {
|
||||||
|
return nil, errEventNotFound
|
||||||
|
}
|
||||||
|
opt := &bind.FilterOpts{
|
||||||
|
Start: start,
|
||||||
|
End: &end,
|
||||||
|
}
|
||||||
|
return registrar.contract.FilterNewCheckpointEvent(opt, []*big.Int{big.NewInt(int64(section))})
|
||||||
|
}
|
||||||
142
contracts/registrar/registrar_test.go
Normal file
142
contracts/registrar/registrar_test.go
Normal file
|
|
@ -0,0 +1,142 @@
|
||||||
|
// Copyright 2018 The go-ethereum Authors
|
||||||
|
// This file is part of the go-ethereum library.
|
||||||
|
//
|
||||||
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU Lesser General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Lesser General Public License
|
||||||
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
package registrar
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math/big"
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"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/registrar/contract"
|
||||||
|
"github.com/ethereum/go-ethereum/core"
|
||||||
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
|
||||||
|
addr = crypto.PubkeyToAddress(key.PublicKey)
|
||||||
|
emptyHash = [32]byte{}
|
||||||
|
|
||||||
|
trustedCheckpoint = Checkpoint{
|
||||||
|
SectionIndex: 0,
|
||||||
|
SectionHead: common.HexToHash("14c8639dfc32812ed20839f5a11993cd59b22e5226cb2179640ba5c1f0c08f87"),
|
||||||
|
ChtRoot: common.HexToHash("cf92fd2a79464354e8dae4d589ae92acdf90a3a4f8f7d8a3ec5fb9c114ae81cd"),
|
||||||
|
BloomTrieRoot: common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestAdminManagement(t *testing.T) {
|
||||||
|
var (
|
||||||
|
adminCandidate = common.HexToAddress("0x123")
|
||||||
|
adminCandidate2 = common.HexToAddress("0x456")
|
||||||
|
)
|
||||||
|
|
||||||
|
// Deploy registrar contract
|
||||||
|
transactOpts := bind.NewKeyedTransactor(key)
|
||||||
|
contractBackend := backends.NewSimulatedBackend(core.GenesisAlloc{addr: {Balance: big.NewInt(1000000000)}})
|
||||||
|
_, _, contract, err := contract.DeployContract(transactOpts, contractBackend, nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Error("deploy registrar contract failed", err)
|
||||||
|
}
|
||||||
|
contractBackend.Commit()
|
||||||
|
|
||||||
|
// Test AddAdmin function
|
||||||
|
contract.AddAdmin(transactOpts, addr) // Contract should ignore the duplicate registration
|
||||||
|
contract.AddAdmin(transactOpts, adminCandidate)
|
||||||
|
contract.AddAdmin(transactOpts, adminCandidate2)
|
||||||
|
contractBackend.Commit()
|
||||||
|
adminList, err := contract.GetAllAdmin(nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Error("fetch admin list failed", err)
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(adminList, []common.Address{addr, adminCandidate, adminCandidate2}) {
|
||||||
|
t.Error("expect the returned admin list contain 3 address")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test RemoveAdmin function (remove at the middle)
|
||||||
|
contract.RemoveAdmin(transactOpts, adminCandidate)
|
||||||
|
contractBackend.Commit()
|
||||||
|
adminList, err = contract.GetAllAdmin(nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Error("fetch admin list failed", err)
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(adminList, []common.Address{addr, adminCandidate2}) {
|
||||||
|
t.Error("expect the returned admin list contain 3 address")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test RemoveAdmin function (remove at the head)
|
||||||
|
contract.RemoveAdmin(transactOpts, addr)
|
||||||
|
contractBackend.Commit()
|
||||||
|
adminList, err = contract.GetAllAdmin(nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Error("fetch admin list failed", err)
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(adminList, []common.Address{adminCandidate2}) {
|
||||||
|
t.Error("expect the returned admin list contain 3 address")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test unauthorized operation
|
||||||
|
contract.AddAdmin(transactOpts, adminCandidate)
|
||||||
|
contractBackend.Commit()
|
||||||
|
adminList, err = contract.GetAllAdmin(nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Error("fetch admin list failed", err)
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(adminList, []common.Address{adminCandidate2}) {
|
||||||
|
t.Error("expect the returned admin list contain 3 address")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCheckpointRegister(t *testing.T) {
|
||||||
|
// Deploy registrar contract
|
||||||
|
transactOpts := bind.NewKeyedTransactor(key)
|
||||||
|
contractBackend := backends.NewSimulatedBackend(core.GenesisAlloc{addr: {Balance: big.NewInt(1000000000)}})
|
||||||
|
_, _, contract, err := contract.DeployContract(transactOpts, contractBackend, []common.Address{addr})
|
||||||
|
if err != nil {
|
||||||
|
t.Error("deploy registrar contract failed", err)
|
||||||
|
}
|
||||||
|
contractBackend.Commit()
|
||||||
|
|
||||||
|
// Register an unstable checkpoint
|
||||||
|
contract.SetCheckpoint(transactOpts, big.NewInt(int64(trustedCheckpoint.SectionIndex)), trustedCheckpoint.SectionHead,
|
||||||
|
trustedCheckpoint.ChtRoot, trustedCheckpoint.BloomTrieRoot)
|
||||||
|
contractBackend.Commit()
|
||||||
|
head, chtRoot, bloomTrieRoot, err := contract.GetCheckpoint(nil, big.NewInt(int64(trustedCheckpoint.SectionIndex)))
|
||||||
|
if err != nil {
|
||||||
|
t.Error("fetch checkpoint failed", err)
|
||||||
|
}
|
||||||
|
if head != emptyHash || chtRoot != emptyHash || bloomTrieRoot != emptyHash {
|
||||||
|
t.Error("the unstable checkpoint is not allowed to be registered")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Register a stable checkpoint
|
||||||
|
contractBackend.ShiftBlocks(sectionSize + checkpointConfirmation)
|
||||||
|
contract.SetCheckpoint(transactOpts, big.NewInt(int64(trustedCheckpoint.SectionIndex)), trustedCheckpoint.SectionHead,
|
||||||
|
trustedCheckpoint.ChtRoot, trustedCheckpoint.BloomTrieRoot)
|
||||||
|
contractBackend.Commit()
|
||||||
|
head, chtRoot, bloomTrieRoot, err = contract.GetCheckpoint(nil, big.NewInt(int64(trustedCheckpoint.SectionIndex)))
|
||||||
|
if err != nil {
|
||||||
|
t.Error("fetch checkpoint failed", err)
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(head[:], trustedCheckpoint.SectionHead.Bytes()) || !reflect.DeepEqual(chtRoot[:], trustedCheckpoint.ChtRoot.Bytes()) ||
|
||||||
|
!reflect.DeepEqual(bloomTrieRoot[:], trustedCheckpoint.BloomTrieRoot.Bytes()) {
|
||||||
|
t.Error("expect the returned checkpoint should be same with the given one")
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue