@@ -20,10 +20,12 @@ import (
2020 "bytes"
2121 "crypto/ecdsa"
2222 "math/big"
23+ "reflect"
2324 "testing"
2425
2526 "github.com/ethereum/go-ethereum/common"
2627 "github.com/ethereum/go-ethereum/consensus/istanbul"
28+ "github.com/ethereum/go-ethereum/consensus/istanbul/validator"
2729 "github.com/ethereum/go-ethereum/core"
2830 "github.com/ethereum/go-ethereum/core/types"
2931 "github.com/ethereum/go-ethereum/core/vm"
@@ -400,3 +402,54 @@ func TestVoting(t *testing.T) {
400402 }
401403 }
402404}
405+
406+ func TestSaveAndLoad (t * testing.T ) {
407+ snap := & Snapshot {
408+ Epoch : 5 ,
409+ Number : 10 ,
410+ Hash : common .HexToHash ("1234567890" ),
411+ Votes : []* Vote {
412+ {
413+ Validator : common .StringToAddress ("1234567891" ),
414+ Block : 15 ,
415+ Address : common .StringToAddress ("1234567892" ),
416+ Authorize : false ,
417+ },
418+ },
419+ Tally : map [common.Address ]Tally {
420+ common .StringToAddress ("1234567893" ): Tally {
421+ Authorize : false ,
422+ Votes : 20 ,
423+ },
424+ },
425+ ValSet : validator .NewSet ([]common.Address {
426+ common .StringToAddress ("1234567894" ),
427+ common .StringToAddress ("1234567895" ),
428+ }, istanbul .RoundRobin ),
429+ }
430+ db , _ := ethdb .NewMemDatabase ()
431+ err := snap .store (db )
432+ if err != nil {
433+ t .Errorf ("store snapshot failed: %v" , err )
434+ }
435+
436+ snap1 , err := loadSnapshot (snap .Epoch , db , snap .Hash )
437+ if err != nil {
438+ t .Errorf ("load snapshot failed: %v" , err )
439+ }
440+ if snap .Epoch != snap1 .Epoch {
441+ t .Errorf ("epoch mismatch: have %v, want %v" , snap1 .Epoch , snap .Epoch )
442+ }
443+ if snap .Hash != snap1 .Hash {
444+ t .Errorf ("hash mismatch: have %v, want %v" , snap1 .Number , snap .Number )
445+ }
446+ if ! reflect .DeepEqual (snap .Votes , snap .Votes ) {
447+ t .Errorf ("votes mismatch: have %v, want %v" , snap1 .Votes , snap .Votes )
448+ }
449+ if ! reflect .DeepEqual (snap .Tally , snap .Tally ) {
450+ t .Errorf ("tally mismatch: have %v, want %v" , snap1 .Tally , snap .Tally )
451+ }
452+ if ! reflect .DeepEqual (snap .ValSet , snap .ValSet ) {
453+ t .Errorf ("validator set mismatch: have %v, want %v" , snap1 .ValSet , snap .ValSet )
454+ }
455+ }
0 commit comments