github.com/koko1123/flow-go-1@v0.29.6/model/flow/chunk.go (about)

     1  package flow
     2  
     3  type ChunkBody struct {
     4  	CollectionIndex uint
     5  
     6  	// execution info
     7  	StartState      StateCommitment // start state when starting executing this chunk
     8  	EventCollection Identifier      // Events generated by executing results
     9  	BlockID         Identifier      // Block id of the execution result this chunk belongs to
    10  
    11  	// Computation consumption info
    12  	TotalComputationUsed uint64 // total amount of computation used by running all txs in this chunk
    13  	NumberOfTransactions uint64 // number of transactions inside the collection
    14  }
    15  
    16  type Chunk struct {
    17  	ChunkBody
    18  
    19  	Index uint64 // chunk index inside the ER (starts from zero)
    20  	// EndState inferred from next chunk or from the ER
    21  	EndState StateCommitment
    22  }
    23  
    24  // ID returns a unique id for this entity
    25  func (ch *Chunk) ID() Identifier {
    26  	return MakeID(ch.ChunkBody)
    27  }
    28  
    29  // Checksum provides a cryptographic commitment for a chunk content
    30  func (ch *Chunk) Checksum() Identifier {
    31  	return MakeID(ch)
    32  }
    33  
    34  // ChunkDataPack holds all register touches (any read, or write).
    35  //
    36  // Note that we have to capture a read proof for each write before updating the registers.
    37  // `Proof` includes proofs for all registers read to execute the chunck.
    38  // Register proofs order must not be correlated to the order of register reads during
    39  // the chunk execution in order to enforce the SPoCK secret high entropy.
    40  type ChunkDataPack struct {
    41  	ChunkID    Identifier
    42  	StartState StateCommitment
    43  	Proof      StorageProof
    44  	Collection *Collection
    45  }
    46  
    47  // ID returns the unique identifier for the concrete view, which is the ID of
    48  // the chunk the view is for.
    49  func (c *ChunkDataPack) ID() Identifier {
    50  	return c.ChunkID
    51  }
    52  
    53  // Checksum returns the checksum of the chunk data pack.
    54  func (c *ChunkDataPack) Checksum() Identifier {
    55  	return MakeID(c)
    56  }
    57  
    58  // TODO: This is the basic version of the list, we need to substitute it with something like Merkle tree at some point
    59  type ChunkList []*Chunk
    60  
    61  func (cl ChunkList) Fingerprint() Identifier {
    62  	return MerkleRoot(GetIDs(cl)...)
    63  }
    64  
    65  func (cl *ChunkList) Insert(ch *Chunk) {
    66  	*cl = append(*cl, ch)
    67  }
    68  
    69  func (cl ChunkList) Items() []*Chunk {
    70  	return cl
    71  }
    72  
    73  // Empty returns true if the chunk list is empty. Otherwise it returns false.
    74  func (cl ChunkList) Empty() bool {
    75  	return len(cl) == 0
    76  }
    77  
    78  func (cl ChunkList) Indices() []uint64 {
    79  	indices := make([]uint64, len(cl))
    80  	for i, chunk := range cl {
    81  		indices[i] = chunk.Index
    82  	}
    83  
    84  	return indices
    85  }
    86  
    87  // ByChecksum returns an entity from the list by entity fingerprint
    88  func (cl ChunkList) ByChecksum(cs Identifier) (*Chunk, bool) {
    89  	for _, ch := range cl {
    90  		if ch.Checksum() == cs {
    91  			return ch, true
    92  		}
    93  	}
    94  	return nil, false
    95  }
    96  
    97  // ByIndex returns an entity from the list by index
    98  // if requested chunk is within range of list, it returns chunk and true
    99  // if requested chunk is out of the range, it returns nil and false
   100  // boolean return value indicates whether requested chunk is within range
   101  func (cl ChunkList) ByIndex(i uint64) (*Chunk, bool) {
   102  	if i >= uint64(len(cl)) {
   103  		// index out of range
   104  		return nil, false
   105  	}
   106  	return cl[i], true
   107  }
   108  
   109  // Len returns the number of Chunks in the list. It is also part of the sort
   110  // interface that makes ChunkList sortable
   111  func (cl ChunkList) Len() int {
   112  	return len(cl)
   113  }