log.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright 2014 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package vm
  17. import (
  18. "fmt"
  19. "io"
  20. "github.com/ethereum/go-ethereum/common"
  21. "github.com/ethereum/go-ethereum/rlp"
  22. )
  23. type Log struct {
  24. // Consensus fields
  25. Address common.Address
  26. Topics []common.Hash
  27. Data []byte
  28. // Derived fields (don't reorder!)
  29. BlockNumber uint64
  30. TxHash common.Hash
  31. TxIndex uint
  32. BlockHash common.Hash
  33. Index uint
  34. }
  35. func NewLog(address common.Address, topics []common.Hash, data []byte, number uint64) *Log {
  36. return &Log{Address: address, Topics: topics, Data: data, BlockNumber: number}
  37. }
  38. func (l *Log) EncodeRLP(w io.Writer) error {
  39. return rlp.Encode(w, []interface{}{l.Address, l.Topics, l.Data})
  40. }
  41. func (l *Log) DecodeRLP(s *rlp.Stream) error {
  42. var log struct {
  43. Address common.Address
  44. Topics []common.Hash
  45. Data []byte
  46. }
  47. if err := s.Decode(&log); err != nil {
  48. return err
  49. }
  50. l.Address, l.Topics, l.Data = log.Address, log.Topics, log.Data
  51. return nil
  52. }
  53. func (l *Log) String() string {
  54. return fmt.Sprintf(`log: %x %x %x %x %d %x %d`, l.Address, l.Topics, l.Data, l.TxHash, l.TxIndex, l.BlockHash, l.Index)
  55. }
  56. type Logs []*Log
  57. // LogForStorage is a wrapper around a Log that flattens and parses the entire
  58. // content of a log, as opposed to only the consensus fields originally (by hiding
  59. // the rlp interface methods).
  60. type LogForStorage Log