2020-01-17 23:16:01 +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 eth
|
|
|
|
|
|
|
|
import (
|
2020-04-19 23:26:23 +00:00
|
|
|
"context"
|
2020-01-17 23:16:01 +00:00
|
|
|
"fmt"
|
2020-04-19 23:26:23 +00:00
|
|
|
"time"
|
2020-01-31 18:03:37 +00:00
|
|
|
|
2020-04-20 13:14:02 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
2020-01-17 23:16:01 +00:00
|
|
|
"github.com/ethereum/go-ethereum/statediff"
|
|
|
|
|
2020-04-19 23:26:23 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/super_node/shared"
|
2020-01-17 23:16:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// BatchClient is an interface to a batch-fetching geth rpc client; created to allow mock insertion
|
|
|
|
type BatchClient interface {
|
2020-04-20 13:14:02 +00:00
|
|
|
BatchCallContext(ctx context.Context, batch []rpc.BatchElem) error
|
2020-01-17 23:16:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PayloadFetcher satisfies the PayloadFetcher interface for ethereum
|
|
|
|
type PayloadFetcher struct {
|
|
|
|
// PayloadFetcher is thread-safe as long as the underlying client is thread-safe, since it has/modifies no other state
|
|
|
|
// http.Client is thread-safe
|
2020-04-19 23:26:23 +00:00
|
|
|
client BatchClient
|
|
|
|
timeout time.Duration
|
2020-05-19 20:09:30 +00:00
|
|
|
params statediff.Params
|
2020-01-17 23:16:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const method = "statediff_stateDiffAt"
|
|
|
|
|
2020-05-19 20:09:30 +00:00
|
|
|
// NewPayloadFetcher returns a PayloadFetcher
|
2020-04-19 23:26:23 +00:00
|
|
|
func NewPayloadFetcher(bc BatchClient, timeout time.Duration) *PayloadFetcher {
|
2020-01-17 23:16:01 +00:00
|
|
|
return &PayloadFetcher{
|
2020-04-19 23:26:23 +00:00
|
|
|
client: bc,
|
|
|
|
timeout: timeout,
|
2020-05-19 20:09:30 +00:00
|
|
|
params: statediff.Params{
|
|
|
|
IncludeReceipts: true,
|
|
|
|
IncludeTD: true,
|
|
|
|
IncludeBlock: true,
|
|
|
|
IntermediateStateNodes: true,
|
|
|
|
IntermediateStorageNodes: true,
|
|
|
|
},
|
2020-01-17 23:16:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FetchAt fetches the statediff payloads at the given block heights
|
2020-05-19 20:09:30 +00:00
|
|
|
// Calls StateDiffAt(ctx context.Context, blockNumber uint64, params Params) (*Payload, error)
|
2020-01-31 18:03:37 +00:00
|
|
|
func (fetcher *PayloadFetcher) FetchAt(blockHeights []uint64) ([]shared.RawChainData, error) {
|
2020-04-20 13:14:02 +00:00
|
|
|
batch := make([]rpc.BatchElem, 0)
|
2020-01-17 23:16:01 +00:00
|
|
|
for _, height := range blockHeights {
|
2020-04-20 13:14:02 +00:00
|
|
|
batch = append(batch, rpc.BatchElem{
|
2020-01-17 23:16:01 +00:00
|
|
|
Method: method,
|
2020-05-19 20:09:30 +00:00
|
|
|
Args: []interface{}{height, fetcher.params},
|
2020-01-17 23:16:01 +00:00
|
|
|
Result: new(statediff.Payload),
|
|
|
|
})
|
|
|
|
}
|
2020-04-19 23:26:23 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), fetcher.timeout)
|
|
|
|
defer cancel()
|
2020-05-19 20:09:30 +00:00
|
|
|
if err := fetcher.client.BatchCallContext(ctx, batch); err != nil {
|
|
|
|
return nil, fmt.Errorf("ethereum PayloadFetcher batch err for block range %d-%d: %s", blockHeights[0], blockHeights[len(blockHeights)-1], err.Error())
|
2020-01-17 23:16:01 +00:00
|
|
|
}
|
2020-01-31 18:03:37 +00:00
|
|
|
results := make([]shared.RawChainData, 0, len(blockHeights))
|
2020-01-17 23:16:01 +00:00
|
|
|
for _, batchElem := range batch {
|
|
|
|
if batchElem.Error != nil {
|
2020-04-19 23:26:23 +00:00
|
|
|
return nil, fmt.Errorf("ethereum PayloadFetcher err at blockheight %d: %s", batchElem.Args[0].(uint64), batchElem.Error.Error())
|
2020-01-17 23:16:01 +00:00
|
|
|
}
|
|
|
|
payload, ok := batchElem.Result.(*statediff.Payload)
|
|
|
|
if ok {
|
|
|
|
results = append(results, *payload)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return results, nil
|
|
|
|
}
|