github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/swarm/storage/mru/lookup.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 12:09:50</date>
    10  //</624342683082559488>
    11  
    12  //
    13  //
    14  //
    15  //
    16  //
    17  //
    18  //
    19  //
    20  //
    21  //
    22  //
    23  //
    24  //
    25  //
    26  //
    27  
    28  package mru
    29  
    30  import (
    31  	"encoding/binary"
    32  	"hash"
    33  
    34  	"github.com/ethereum/go-ethereum/swarm/storage"
    35  )
    36  
    37  //
    38  //
    39  //
    40  type LookupParams struct {
    41  	UpdateLookup
    42  	Limit uint32
    43  }
    44  
    45  //
    46  func (r *LookupParams) RootAddr() storage.Address {
    47  	return r.rootAddr
    48  }
    49  
    50  func NewLookupParams(rootAddr storage.Address, period, version uint32, limit uint32) *LookupParams {
    51  	return &LookupParams{
    52  		UpdateLookup: UpdateLookup{
    53  			period:   period,
    54  			version:  version,
    55  			rootAddr: rootAddr,
    56  		},
    57  		Limit: limit,
    58  	}
    59  }
    60  
    61  //
    62  func LookupLatest(rootAddr storage.Address) *LookupParams {
    63  	return NewLookupParams(rootAddr, 0, 0, 0)
    64  }
    65  
    66  //
    67  func LookupLatestVersionInPeriod(rootAddr storage.Address, period uint32) *LookupParams {
    68  	return NewLookupParams(rootAddr, period, 0, 0)
    69  }
    70  
    71  //
    72  func LookupVersion(rootAddr storage.Address, period, version uint32) *LookupParams {
    73  	return NewLookupParams(rootAddr, period, version, 0)
    74  }
    75  
    76  //
    77  type UpdateLookup struct {
    78  	period   uint32
    79  	version  uint32
    80  	rootAddr storage.Address
    81  }
    82  
    83  //
    84  //
    85  //
    86  const updateLookupLength = 4 + 4 + storage.KeyLength
    87  
    88  //
    89  func (u *UpdateLookup) UpdateAddr() (updateAddr storage.Address) {
    90  	serializedData := make([]byte, updateLookupLength)
    91  	u.binaryPut(serializedData)
    92  	hasher := hashPool.Get().(hash.Hash)
    93  	defer hashPool.Put(hasher)
    94  	hasher.Reset()
    95  	hasher.Write(serializedData)
    96  	return hasher.Sum(nil)
    97  }
    98  
    99  //
   100  func (u *UpdateLookup) binaryPut(serializedData []byte) error {
   101  	if len(serializedData) != updateLookupLength {
   102  		return NewErrorf(ErrInvalidValue, "Incorrect slice size to serialize UpdateLookup. Expected %d, got %d", updateLookupLength, len(serializedData))
   103  	}
   104  	if len(u.rootAddr) != storage.KeyLength {
   105  		return NewError(ErrInvalidValue, "UpdateLookup.binaryPut called without rootAddr set")
   106  	}
   107  	binary.LittleEndian.PutUint32(serializedData[:4], u.period)
   108  	binary.LittleEndian.PutUint32(serializedData[4:8], u.version)
   109  	copy(serializedData[8:], u.rootAddr[:])
   110  	return nil
   111  }
   112  
   113  //
   114  func (u *UpdateLookup) binaryLength() int {
   115  	return updateLookupLength
   116  }
   117  
   118  //
   119  func (u *UpdateLookup) binaryGet(serializedData []byte) error {
   120  	if len(serializedData) != updateLookupLength {
   121  		return NewErrorf(ErrInvalidValue, "Incorrect slice size to read UpdateLookup. Expected %d, got %d", updateLookupLength, len(serializedData))
   122  	}
   123  	u.period = binary.LittleEndian.Uint32(serializedData[:4])
   124  	u.version = binary.LittleEndian.Uint32(serializedData[4:8])
   125  	u.rootAddr = storage.Address(make([]byte, storage.KeyLength))
   126  	copy(u.rootAddr[:], serializedData[8:])
   127  	return nil
   128  }
   129