util

package
v0.0.28 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 5, 2023 License: MIT Imports: 6 Imported by: 4

Documentation

Overview

Package util contains eth type utils

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func LogPointer added in v0.0.5

func LogPointer(log types.Log) *types.Log

LogPointer wraps a log in a pointer.

func LogsPointer added in v0.0.5

func LogsPointer(logs []types.Log) (res []*types.Log)

LogsPointer wraps logs in a pointer TODO: consider abstracting this out into a generic.

func TxToCall

func TxToCall(transaction *types.Transaction) (*ethereum.CallMsg, error)

TxToCall converts a transaction to a call.

Types

type Chunk

type Chunk struct {
	// StartBlock for this chunk. It is less then end block in ascending txes and greater in descending.
	StartBlock *big.Int
	// EndBlock for this chunk
	EndBlock *big.Int
	// contains filtered or unexported fields
}

Chunk represents an individual chunk of startBlock-startBlock.

Example

ExampleChunk demonstrates ascending and descending chunking.

// a ascending chunk iterator
chunkIterator := util.NewChunkIterator(big.NewInt(1), big.NewInt(10), 1, true)
var minMaxChunk, startEndChunks string
for {
	nextChunk := chunkIterator.NextChunk()
	if nextChunk == nil {
		break
	}

	minMaxChunk += fmt.Sprintf("[%d-%d],", nextChunk.MinBlock(), nextChunk.MaxBlock())
	startEndChunks += fmt.Sprintf("[%d-%d],", nextChunk.StartBlock, nextChunk.EndBlock)
}
fmt.Println(strings.TrimSuffix(minMaxChunk, ","))
fmt.Println(strings.TrimSuffix(startEndChunks, ","))

// a descending chunk iterator
chunkIterator = util.NewChunkIterator(big.NewInt(1), big.NewInt(10), 1, false)
minMaxChunk = ""
startEndChunks = ""

for {
	nextChunk := chunkIterator.NextChunk()
	if nextChunk == nil {
		break
	}

	minMaxChunk += fmt.Sprintf("[%d-%d],", nextChunk.MinBlock(), nextChunk.MaxBlock())
	startEndChunks += fmt.Sprintf("[%d-%d],", nextChunk.StartBlock, nextChunk.EndBlock)
}
fmt.Println(strings.TrimSuffix(minMaxChunk, ","))
fmt.Println(strings.TrimSuffix(startEndChunks, ","))
Output:

[1-2],[3-4],[5-6],[7-8],[9-10]
[1-2],[3-4],[5-6],[7-8],[9-10]
[9-10],[7-8],[5-6],[3-4],[1-2]
[10-9],[8-7],[6-5],[4-3],[2-1]

func (*Chunk) MaxBlock

func (c *Chunk) MaxBlock() *big.Int

MaxBlock returns the minimum of start block and end block. This is useful because start/end change based on the ordering of the chunk.

func (*Chunk) MinBlock

func (c *Chunk) MinBlock() *big.Int

MinBlock returns the minimum of start block and end block. This is useful because start/end change based on the ordering of the chunk.

type ChunkIterator

type ChunkIterator interface {
	// NextChunk gets the next chunk. If the iterator has completed null will be returned.
	NextChunk() *Chunk
}

ChunkIterator is an instantiation of a stateful iterator that groups startBlock-endBlock blocks into length chunkSize.

func NewChunkIterator

func NewChunkIterator(startBlock, endBlock *big.Int, chunkSize int, ascending bool) ChunkIterator

NewChunkIterator creates an iterator for a range of elements (startBlock-endBlock) split into groups the length of chunkSize the final chunk has any remaining elements. An iterator is used here as opposed to a slice to save memory while iterating over *very large* chunks.This follows the stateful iterator pattern: https://ewencp.org/blog/golang-iterators/index.html

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL