2019-10-24 16:35:39 +00:00
|
|
|
// VulcanizeDB
|
|
|
|
// Copyright © 2019 Vulcanize
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// This program 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 Affero General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package mocks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2019-11-01 05:29:57 +00:00
|
|
|
"sync/atomic"
|
2019-10-24 16:35:39 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/statediff"
|
|
|
|
)
|
|
|
|
|
|
|
|
// StateDiffFetcher mock for tests
|
|
|
|
type StateDiffFetcher struct {
|
2019-11-01 05:29:57 +00:00
|
|
|
PayloadsToReturn map[uint64]statediff.Payload
|
|
|
|
FetchErrs map[uint64]error
|
2019-10-24 16:35:39 +00:00
|
|
|
CalledAtBlockHeights [][]uint64
|
2019-11-01 05:29:57 +00:00
|
|
|
CalledTimes int64
|
2019-10-24 16:35:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FetchStateDiffsAt mock method
|
2019-11-01 05:29:57 +00:00
|
|
|
func (fetcher *StateDiffFetcher) FetchStateDiffsAt(blockHeights []uint64) ([]statediff.Payload, error) {
|
2019-10-24 16:35:39 +00:00
|
|
|
if fetcher.PayloadsToReturn == nil {
|
2019-11-01 05:29:57 +00:00
|
|
|
return nil, errors.New("mock StateDiffFetcher needs to be initialized with payloads to return")
|
2019-10-24 16:35:39 +00:00
|
|
|
}
|
2019-11-01 05:29:57 +00:00
|
|
|
atomic.AddInt64(&fetcher.CalledTimes, 1) // thread-safe increment
|
|
|
|
fetcher.CalledAtBlockHeights = append(fetcher.CalledAtBlockHeights, blockHeights)
|
|
|
|
results := make([]statediff.Payload, 0, len(blockHeights))
|
2019-10-24 16:35:39 +00:00
|
|
|
for _, height := range blockHeights {
|
|
|
|
results = append(results, fetcher.PayloadsToReturn[height])
|
2019-11-01 05:29:57 +00:00
|
|
|
err, ok := fetcher.FetchErrs[height]
|
|
|
|
if ok && err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-10-24 16:35:39 +00:00
|
|
|
}
|
|
|
|
return results, nil
|
|
|
|
}
|