github.com/koko1123/flow-go-1@v0.29.6/engine/execution/state/delta/delta.go (about)

     1  package delta
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  
     7  	"golang.org/x/exp/slices"
     8  
     9  	"github.com/koko1123/flow-go-1/model/flow"
    10  )
    11  
    12  // A Delta is a record of ledger mutations.
    13  type Delta struct {
    14  	Data map[flow.RegisterID]flow.RegisterValue
    15  }
    16  
    17  // NewDelta returns an empty ledger delta.
    18  func NewDelta() Delta {
    19  	return Delta{
    20  		Data: make(map[flow.RegisterID]flow.RegisterValue),
    21  	}
    22  }
    23  
    24  // Get reads a register value from this delta.
    25  //
    26  // This function will return nil if the given key has been deleted in this delta.
    27  // Second return parameters indicated if the value has been set/deleted in this delta
    28  func (d Delta) Get(owner, key string) (flow.RegisterValue, bool) {
    29  	value, set := d.Data[flow.NewRegisterID(owner, key)]
    30  	return value, set
    31  }
    32  
    33  // Set records an update in this delta.
    34  func (d Delta) Set(owner, key string, value flow.RegisterValue) {
    35  	k := flow.NewRegisterID(owner, key)
    36  	d.Data[k] = value
    37  }
    38  
    39  // RegisterUpdates returns all registers that were updated by this delta.
    40  // ids are returned sorted, in ascending order
    41  func (d Delta) RegisterUpdates() ([]flow.RegisterID, []flow.RegisterValue) {
    42  	ids := make([]flow.RegisterID, 0, len(d.Data))
    43  	for k := range d.Data {
    44  		ids = append(ids, k)
    45  	}
    46  
    47  	slices.SortFunc(ids, func(a, b flow.RegisterID) bool {
    48  		return (a.Owner < b.Owner) || (a.Owner == b.Owner && a.Key < b.Key)
    49  	})
    50  
    51  	values := make([]flow.RegisterValue, len(d.Data))
    52  	for i, v := range ids {
    53  		values[i] = d.Data[v]
    54  	}
    55  
    56  	return ids, values
    57  }
    58  
    59  // MergeWith merges this delta with another.
    60  func (d Delta) MergeWith(delta Delta) {
    61  	for key, value := range delta.Data {
    62  		d.Data[key] = value
    63  	}
    64  }
    65  
    66  // RegisterIDs returns the list of registerIDs inside this delta
    67  func (d Delta) RegisterIDs() []flow.RegisterID {
    68  	ids := make([]flow.RegisterID, 0, len(d.Data))
    69  	for k := range d.Data {
    70  		ids = append(ids, k)
    71  	}
    72  	return ids
    73  }
    74  
    75  func (d Delta) MarshalJSON() ([]byte, error) {
    76  	m := make(flow.RegisterEntries, len(d.Data))
    77  	for key, value := range d.Data {
    78  		m = append(m, flow.RegisterEntry{Key: key, Value: value})
    79  	}
    80  	return json.Marshal(m)
    81  }
    82  
    83  func (d *Delta) UnmarshalJSON(data []byte) error {
    84  
    85  	var m flow.RegisterEntries
    86  
    87  	err := json.Unmarshal(data, &m)
    88  	if err != nil {
    89  		return fmt.Errorf("cannot umarshal Delta: %w", err)
    90  	}
    91  	dd := make(map[flow.RegisterID]flow.RegisterValue, len(m))
    92  
    93  	for _, value := range m {
    94  		dd[value.Key] = value.Value
    95  	}
    96  
    97  	d.Data = dd
    98  
    99  	return nil
   100  }