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 (
|
2020-04-19 23:26:23 +00:00
|
|
|
"context"
|
2019-10-24 16:35:39 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
|
2020-04-20 13:14:02 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
2019-10-24 16:35:39 +00:00
|
|
|
"github.com/ethereum/go-ethereum/statediff"
|
|
|
|
)
|
|
|
|
|
|
|
|
// BackFillerClient is a mock client for use in backfiller tests
|
|
|
|
type BackFillerClient struct {
|
|
|
|
MappedStateDiffAt map[uint64][]byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetReturnDiffAt method to set what statediffs the mock client returns
|
|
|
|
func (mc *BackFillerClient) SetReturnDiffAt(height uint64, diffPayload statediff.Payload) error {
|
|
|
|
if mc.MappedStateDiffAt == nil {
|
|
|
|
mc.MappedStateDiffAt = make(map[uint64][]byte)
|
|
|
|
}
|
|
|
|
by, err := json.Marshal(diffPayload)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
mc.MappedStateDiffAt[height] = by
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// BatchCall mockClient method to simulate batch call to geth
|
2020-06-30 00:16:52 +00:00
|
|
|
func (mc *BackFillerClient) BatchCall(batch []rpc.BatchElem) error {
|
2019-10-24 16:35:39 +00:00
|
|
|
if mc.MappedStateDiffAt == nil {
|
|
|
|
return errors.New("mockclient needs to be initialized with statediff payloads and errors")
|
|
|
|
}
|
|
|
|
for _, batchElem := range batch {
|
2020-05-19 20:09:30 +00:00
|
|
|
if len(batchElem.Args) < 1 {
|
|
|
|
return errors.New("expected batch elem to contain an argument(s)")
|
2019-10-24 16:35:39 +00:00
|
|
|
}
|
|
|
|
blockHeight, ok := batchElem.Args[0].(uint64)
|
|
|
|
if !ok {
|
2020-05-19 20:09:30 +00:00
|
|
|
return errors.New("expected first batch elem argument to be a uint64")
|
2019-10-24 16:35:39 +00:00
|
|
|
}
|
|
|
|
err := json.Unmarshal(mc.MappedStateDiffAt[blockHeight], batchElem.Result)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-04-19 23:26:23 +00:00
|
|
|
|
|
|
|
// BatchCallContext mockClient method to simulate batch call to geth
|
2020-04-20 13:14:02 +00:00
|
|
|
func (mc *BackFillerClient) BatchCallContext(ctx context.Context, batch []rpc.BatchElem) error {
|
2020-04-19 23:26:23 +00:00
|
|
|
if mc.MappedStateDiffAt == nil {
|
|
|
|
return errors.New("mockclient needs to be initialized with statediff payloads and errors")
|
|
|
|
}
|
|
|
|
for _, batchElem := range batch {
|
2020-05-19 20:09:30 +00:00
|
|
|
if len(batchElem.Args) < 1 {
|
|
|
|
return errors.New("expected batch elem to contain an argument(s)")
|
2020-04-19 23:26:23 +00:00
|
|
|
}
|
|
|
|
blockHeight, ok := batchElem.Args[0].(uint64)
|
|
|
|
if !ok {
|
2020-05-19 20:09:30 +00:00
|
|
|
return errors.New("expected batch elem first argument to be a uint64")
|
2020-04-19 23:26:23 +00:00
|
|
|
}
|
|
|
|
err := json.Unmarshal(mc.MappedStateDiffAt[blockHeight], batchElem.Result)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|