github.com/aswedchain/aswed@v1.0.1/contracts/checkpointoracle/oracle_test.go (about)

     1  // Copyright 2019 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package checkpointoracle
    18  
    19  import (
    20  	"bytes"
    21  	"crypto/ecdsa"
    22  	"encoding/binary"
    23  	"errors"
    24  	"math/big"
    25  	"reflect"
    26  	"sort"
    27  	"testing"
    28  	"time"
    29  
    30  	"github.com/aswedchain/aswed/accounts/abi/bind"
    31  	"github.com/aswedchain/aswed/accounts/abi/bind/backends"
    32  	"github.com/aswedchain/aswed/common"
    33  	"github.com/aswedchain/aswed/contracts/checkpointoracle/contract"
    34  	"github.com/aswedchain/aswed/core"
    35  	"github.com/aswedchain/aswed/crypto"
    36  	"github.com/aswedchain/aswed/params"
    37  )
    38  
    39  var (
    40  	emptyHash = [32]byte{}
    41  
    42  	checkpoint0 = params.TrustedCheckpoint{
    43  		SectionIndex: 0,
    44  		SectionHead:  common.HexToHash("0x7fa3c32f996c2bfb41a1a65b3d8ea3e0a33a1674cde43678ad6f4235e764d17d"),
    45  		CHTRoot:      common.HexToHash("0x98fc5d3de23a0fecebad236f6655533c157d26a1aedcd0852a514dc1169e6350"),
    46  		BloomRoot:    common.HexToHash("0x99b5adb52b337fe25e74c1c6d3835b896bd638611b3aebddb2317cce27a3f9fa"),
    47  	}
    48  	checkpoint1 = params.TrustedCheckpoint{
    49  		SectionIndex: 1,
    50  		SectionHead:  common.HexToHash("0x2d4dee68102125e59b0cc61b176bd89f0d12b3b91cfaf52ef8c2c82fb920c2d2"),
    51  		CHTRoot:      common.HexToHash("0x7d428008ece3b4c4ef5439f071930aad0bb75108d381308df73beadcd01ded95"),
    52  		BloomRoot:    common.HexToHash("0x652571f7736de17e7bbb427ac881474da684c6988a88bf51b10cca9a2ee148f4"),
    53  	}
    54  	checkpoint2 = params.TrustedCheckpoint{
    55  		SectionIndex: 2,
    56  		SectionHead:  common.HexToHash("0x61c0de578c0115b1dff8ef39aa600588c7c6ecb8a2f102003d7cf4c4146e9291"),
    57  		CHTRoot:      common.HexToHash("0x407a08a407a2bc3838b74ca3eb206903c9c8a186ccf5ef14af07794efff1970b"),
    58  		BloomRoot:    common.HexToHash("0x058b4161f558ce295a92925efc57f34f9210d5a30088d7475c183e0d3e58f5ac"),
    59  	}
    60  )
    61  
    62  var (
    63  	// The block frequency for creating checkpoint(only used in test)
    64  	sectionSize = big.NewInt(512)
    65  
    66  	// The number of confirmations needed to generate a checkpoint(only used in test).
    67  	processConfirms = big.NewInt(4)
    68  )
    69  
    70  // validateOperation executes the operation, watches and delivers all events fired by the backend and ensures the
    71  // correctness by assert function.
    72  func validateOperation(t *testing.T, c *contract.CheckpointOracle, backend *backends.SimulatedBackend, operation func(),
    73  	assert func(<-chan *contract.CheckpointOracleNewCheckpointVote) error, opName string) {
    74  	// Watch all events and deliver them to assert function
    75  	var (
    76  		sink   = make(chan *contract.CheckpointOracleNewCheckpointVote)
    77  		sub, _ = c.WatchNewCheckpointVote(nil, sink, nil)
    78  	)
    79  	defer func() {
    80  		// Close all subscribers
    81  		sub.Unsubscribe()
    82  	}()
    83  	operation()
    84  
    85  	// flush pending block
    86  	backend.Commit()
    87  	if err := assert(sink); err != nil {
    88  		t.Errorf("operation {%s} failed, err %s", opName, err)
    89  	}
    90  }
    91  
    92  // validateEvents checks that the correct number of contract events
    93  // fired by contract backend.
    94  func validateEvents(target int, sink interface{}) (bool, []reflect.Value) {
    95  	chanval := reflect.ValueOf(sink)
    96  	chantyp := chanval.Type()
    97  	if chantyp.Kind() != reflect.Chan || chantyp.ChanDir()&reflect.RecvDir == 0 {
    98  		return false, nil
    99  	}
   100  	count := 0
   101  	var recv []reflect.Value
   102  	timeout := time.After(1 * time.Second)
   103  	cases := []reflect.SelectCase{{Chan: chanval, Dir: reflect.SelectRecv}, {Chan: reflect.ValueOf(timeout), Dir: reflect.SelectRecv}}
   104  	for {
   105  		chose, v, _ := reflect.Select(cases)
   106  		if chose == 1 {
   107  			// Not enough event received
   108  			return false, nil
   109  		}
   110  		count += 1
   111  		recv = append(recv, v)
   112  		if count == target {
   113  			break
   114  		}
   115  	}
   116  	done := time.After(50 * time.Millisecond)
   117  	cases = cases[:1]
   118  	cases = append(cases, reflect.SelectCase{Chan: reflect.ValueOf(done), Dir: reflect.SelectRecv})
   119  	chose, _, _ := reflect.Select(cases)
   120  	// If chose equal 0, it means receiving redundant events.
   121  	return chose == 1, recv
   122  }
   123  
   124  func signCheckpoint(addr common.Address, privateKey *ecdsa.PrivateKey, index uint64, hash common.Hash) []byte {
   125  	// EIP 191 style signatures
   126  	//
   127  	// Arguments when calculating hash to validate
   128  	// 1: byte(0x19) - the initial 0x19 byte
   129  	// 2: byte(0) - the version byte (data with intended validator)
   130  	// 3: this - the validator address
   131  	// --  Application specific data
   132  	// 4 : checkpoint section_index(uint64)
   133  	// 5 : checkpoint hash (bytes32)
   134  	//     hash = keccak256(checkpoint_index, section_head, cht_root, bloom_root)
   135  	buf := make([]byte, 8)
   136  	binary.BigEndian.PutUint64(buf, index)
   137  	data := append([]byte{0x19, 0x00}, append(addr.Bytes(), append(buf, hash.Bytes()...)...)...)
   138  	sig, _ := crypto.Sign(crypto.Keccak256(data), privateKey)
   139  	sig[64] += 27 // Transform V from 0/1 to 27/28 according to the yellow paper
   140  	return sig
   141  }
   142  
   143  // assertSignature verifies whether the recovered signers are equal with expected.
   144  func assertSignature(addr common.Address, index uint64, hash [32]byte, r, s [32]byte, v uint8, expect common.Address) bool {
   145  	buf := make([]byte, 8)
   146  	binary.BigEndian.PutUint64(buf, index)
   147  	data := append([]byte{0x19, 0x00}, append(addr.Bytes(), append(buf, hash[:]...)...)...)
   148  	pubkey, err := crypto.Ecrecover(crypto.Keccak256(data), append(r[:], append(s[:], v-27)...))
   149  	if err != nil {
   150  		return false
   151  	}
   152  	var signer common.Address
   153  	copy(signer[:], crypto.Keccak256(pubkey[1:])[12:])
   154  	return bytes.Equal(signer.Bytes(), expect.Bytes())
   155  }
   156  
   157  type Account struct {
   158  	key  *ecdsa.PrivateKey
   159  	addr common.Address
   160  }
   161  type Accounts []Account
   162  
   163  func (a Accounts) Len() int           { return len(a) }
   164  func (a Accounts) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
   165  func (a Accounts) Less(i, j int) bool { return bytes.Compare(a[i].addr.Bytes(), a[j].addr.Bytes()) < 0 }
   166  
   167  func TestCheckpointRegister(t *testing.T) {
   168  	// Initialize test accounts
   169  	var accounts Accounts
   170  	for i := 0; i < 3; i++ {
   171  		key, _ := crypto.GenerateKey()
   172  		addr := crypto.PubkeyToAddress(key.PublicKey)
   173  		accounts = append(accounts, Account{key: key, addr: addr})
   174  	}
   175  	sort.Sort(accounts)
   176  
   177  	// Deploy registrar contract
   178  	contractBackend := backends.NewSimulatedBackend(core.GenesisAlloc{accounts[0].addr: {Balance: big.NewInt(1000000000)}, accounts[1].addr: {Balance: big.NewInt(1000000000)}, accounts[2].addr: {Balance: big.NewInt(1000000000)}}, 10000000)
   179  	defer contractBackend.Close()
   180  
   181  	transactOpts := bind.NewKeyedTransactor(accounts[0].key)
   182  
   183  	// 3 trusted signers, threshold 2
   184  	contractAddr, _, c, err := contract.DeployCheckpointOracle(transactOpts, contractBackend, []common.Address{accounts[0].addr, accounts[1].addr, accounts[2].addr}, sectionSize, processConfirms, big.NewInt(2))
   185  	if err != nil {
   186  		t.Error("Failed to deploy registrar contract", err)
   187  	}
   188  	contractBackend.Commit()
   189  
   190  	// getRecent returns block height and hash of the head parent.
   191  	getRecent := func() (*big.Int, common.Hash) {
   192  		parentNumber := new(big.Int).Sub(contractBackend.Blockchain().CurrentHeader().Number, big.NewInt(1))
   193  		parentHash := contractBackend.Blockchain().CurrentHeader().ParentHash
   194  		return parentNumber, parentHash
   195  	}
   196  	// collectSig generates specified number signatures.
   197  	collectSig := func(index uint64, hash common.Hash, n int, unauthorized *ecdsa.PrivateKey) (v []uint8, r [][32]byte, s [][32]byte) {
   198  		for i := 0; i < n; i++ {
   199  			sig := signCheckpoint(contractAddr, accounts[i].key, index, hash)
   200  			if unauthorized != nil {
   201  				sig = signCheckpoint(contractAddr, unauthorized, index, hash)
   202  			}
   203  			r = append(r, common.BytesToHash(sig[:32]))
   204  			s = append(s, common.BytesToHash(sig[32:64]))
   205  			v = append(v, sig[64])
   206  		}
   207  		return v, r, s
   208  	}
   209  	// insertEmptyBlocks inserts a batch of empty blocks to blockchain.
   210  	insertEmptyBlocks := func(number int) {
   211  		for i := 0; i < number; i++ {
   212  			contractBackend.Commit()
   213  		}
   214  	}
   215  	// assert checks whether the current contract status is same with
   216  	// the expected.
   217  	assert := func(index uint64, hash [32]byte, height *big.Int) error {
   218  		lindex, lhash, lheight, err := c.GetLatestCheckpoint(nil)
   219  		if err != nil {
   220  			return err
   221  		}
   222  		if lindex != index {
   223  			return errors.New("latest checkpoint index mismatch")
   224  		}
   225  		if !bytes.Equal(lhash[:], hash[:]) {
   226  			return errors.New("latest checkpoint hash mismatch")
   227  		}
   228  		if lheight.Cmp(height) != 0 {
   229  			return errors.New("latest checkpoint height mismatch")
   230  		}
   231  		return nil
   232  	}
   233  
   234  	// Test future checkpoint registration
   235  	validateOperation(t, c, contractBackend, func() {
   236  		number, hash := getRecent()
   237  		v, r, s := collectSig(0, checkpoint0.Hash(), 2, nil)
   238  		c.SetCheckpoint(transactOpts, number, hash, checkpoint0.Hash(), 0, v, r, s)
   239  	}, func(events <-chan *contract.CheckpointOracleNewCheckpointVote) error {
   240  		return assert(0, emptyHash, big.NewInt(0))
   241  	}, "test future checkpoint registration")
   242  
   243  	insertEmptyBlocks(int(sectionSize.Uint64() + processConfirms.Uint64()))
   244  
   245  	// Test transaction replay protection
   246  	validateOperation(t, c, contractBackend, func() {
   247  		number, _ := getRecent()
   248  		v, r, s := collectSig(0, checkpoint0.Hash(), 2, nil)
   249  		hash := common.HexToHash("deadbeef")
   250  		c.SetCheckpoint(transactOpts, number, hash, checkpoint0.Hash(), 0, v, r, s)
   251  	}, func(events <-chan *contract.CheckpointOracleNewCheckpointVote) error {
   252  		return assert(0, emptyHash, big.NewInt(0))
   253  	}, "test transaction replay protection")
   254  
   255  	// Test unauthorized signature checking
   256  	validateOperation(t, c, contractBackend, func() {
   257  		number, hash := getRecent()
   258  		u, _ := crypto.GenerateKey()
   259  		v, r, s := collectSig(0, checkpoint0.Hash(), 2, u)
   260  		c.SetCheckpoint(transactOpts, number, hash, checkpoint0.Hash(), 0, v, r, s)
   261  	}, func(events <-chan *contract.CheckpointOracleNewCheckpointVote) error {
   262  		return assert(0, emptyHash, big.NewInt(0))
   263  	}, "test unauthorized signature checking")
   264  
   265  	// Test un-multi-signature checkpoint registration
   266  	validateOperation(t, c, contractBackend, func() {
   267  		number, hash := getRecent()
   268  		v, r, s := collectSig(0, checkpoint0.Hash(), 1, nil)
   269  		c.SetCheckpoint(transactOpts, number, hash, checkpoint0.Hash(), 0, v, r, s)
   270  	}, func(events <-chan *contract.CheckpointOracleNewCheckpointVote) error {
   271  		return assert(0, emptyHash, big.NewInt(0))
   272  	}, "test un-multi-signature checkpoint registration")
   273  
   274  	// Test valid checkpoint registration
   275  	validateOperation(t, c, contractBackend, func() {
   276  		number, hash := getRecent()
   277  		v, r, s := collectSig(0, checkpoint0.Hash(), 2, nil)
   278  		c.SetCheckpoint(transactOpts, number, hash, checkpoint0.Hash(), 0, v, r, s)
   279  	}, func(events <-chan *contract.CheckpointOracleNewCheckpointVote) error {
   280  		if valid, recv := validateEvents(2, events); !valid {
   281  			return errors.New("receive incorrect number of events")
   282  		} else {
   283  			for i := 0; i < len(recv); i++ {
   284  				event := recv[i].Interface().(*contract.CheckpointOracleNewCheckpointVote)
   285  				if !assertSignature(contractAddr, event.Index, event.CheckpointHash, event.R, event.S, event.V, accounts[i].addr) {
   286  					return errors.New("recover signer failed")
   287  				}
   288  			}
   289  		}
   290  		number, _ := getRecent()
   291  		return assert(0, checkpoint0.Hash(), number.Add(number, big.NewInt(1)))
   292  	}, "test valid checkpoint registration")
   293  
   294  	distance := 3*sectionSize.Uint64() + processConfirms.Uint64() - contractBackend.Blockchain().CurrentHeader().Number.Uint64()
   295  	insertEmptyBlocks(int(distance))
   296  
   297  	// Test uncontinuous checkpoint registration
   298  	validateOperation(t, c, contractBackend, func() {
   299  		number, hash := getRecent()
   300  		v, r, s := collectSig(2, checkpoint2.Hash(), 2, nil)
   301  		c.SetCheckpoint(transactOpts, number, hash, checkpoint2.Hash(), 2, v, r, s)
   302  	}, func(events <-chan *contract.CheckpointOracleNewCheckpointVote) error {
   303  		if valid, recv := validateEvents(2, events); !valid {
   304  			return errors.New("receive incorrect number of events")
   305  		} else {
   306  			for i := 0; i < len(recv); i++ {
   307  				event := recv[i].Interface().(*contract.CheckpointOracleNewCheckpointVote)
   308  				if !assertSignature(contractAddr, event.Index, event.CheckpointHash, event.R, event.S, event.V, accounts[i].addr) {
   309  					return errors.New("recover signer failed")
   310  				}
   311  			}
   312  		}
   313  		number, _ := getRecent()
   314  		return assert(2, checkpoint2.Hash(), number.Add(number, big.NewInt(1)))
   315  	}, "test uncontinuous checkpoint registration")
   316  
   317  	// Test old checkpoint registration
   318  	validateOperation(t, c, contractBackend, func() {
   319  		number, hash := getRecent()
   320  		v, r, s := collectSig(1, checkpoint1.Hash(), 2, nil)
   321  		c.SetCheckpoint(transactOpts, number, hash, checkpoint1.Hash(), 1, v, r, s)
   322  	}, func(events <-chan *contract.CheckpointOracleNewCheckpointVote) error {
   323  		number, _ := getRecent()
   324  		return assert(2, checkpoint2.Hash(), number)
   325  	}, "test uncontinuous checkpoint registration")
   326  
   327  	// Test stale checkpoint registration
   328  	validateOperation(t, c, contractBackend, func() {
   329  		number, hash := getRecent()
   330  		v, r, s := collectSig(2, checkpoint2.Hash(), 2, nil)
   331  		c.SetCheckpoint(transactOpts, number, hash, checkpoint2.Hash(), 2, v, r, s)
   332  	}, func(events <-chan *contract.CheckpointOracleNewCheckpointVote) error {
   333  		number, _ := getRecent()
   334  		return assert(2, checkpoint2.Hash(), number.Sub(number, big.NewInt(1)))
   335  	}, "test stale checkpoint registration")
   336  }