github.com/datachainlab/burrow@v0.25.0/execution/exec/log_event.go (about)

     1  // Copyright 2017 Monax Industries Limited
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package exec
    16  
    17  import (
    18  	"strings"
    19  
    20  	"fmt"
    21  
    22  	. "github.com/hyperledger/burrow/binary"
    23  	"github.com/hyperledger/burrow/event"
    24  	"github.com/hyperledger/burrow/event/query"
    25  	hex "github.com/tmthrgd/go-hex"
    26  )
    27  
    28  const logNTextTopicCutset = "\x00"
    29  const LogNKeyPrefix = "Log"
    30  
    31  func LogNKey(topic int) string {
    32  	return fmt.Sprintf("%s%d", LogNKeyPrefix, topic)
    33  }
    34  
    35  func LogNTextKey(topic int) string {
    36  	return fmt.Sprintf("%s%dText", LogNKeyPrefix, topic)
    37  }
    38  
    39  var logTagKeys []string
    40  var logNTopicIndex = make(map[string]int, 5)
    41  var logNTextTopicIndex = make(map[string]int, 5)
    42  
    43  func init() {
    44  	for i := 0; i <= 4; i++ {
    45  		logN := LogNKey(i)
    46  		logTagKeys = append(logTagKeys, LogNKey(i))
    47  		logNText := LogNTextKey(i)
    48  		logTagKeys = append(logTagKeys, logNText)
    49  		logNTopicIndex[logN] = i
    50  		logNTextTopicIndex[logNText] = i
    51  	}
    52  	logTagKeys = append(logTagKeys, event.AddressKey)
    53  }
    54  
    55  func (log *LogEvent) Get(key string) (string, bool) {
    56  	if log == nil {
    57  		return "", false
    58  	}
    59  	var value interface{}
    60  	switch key {
    61  	case event.AddressKey:
    62  		value = log.Address
    63  	default:
    64  		if i, ok := logNTopicIndex[key]; ok {
    65  			return hex.EncodeUpperToString(log.GetTopic(i).Bytes()), true
    66  		}
    67  		if i, ok := logNTextTopicIndex[key]; ok {
    68  			return strings.Trim(string(log.GetTopic(i).Bytes()), logNTextTopicCutset), true
    69  		}
    70  		return "", false
    71  	}
    72  	return query.StringFromValue(value), true
    73  }
    74  
    75  func (log *LogEvent) GetTopic(i int) Word256 {
    76  	if i < len(log.Topics) {
    77  		return log.Topics[i]
    78  	}
    79  	return Word256{}
    80  }
    81  
    82  func (log *LogEvent) Len() int {
    83  	return len(logTagKeys)
    84  }
    85  
    86  func (log *LogEvent) Keys() []string {
    87  	return logTagKeys
    88  }