aboutsummaryrefslogtreecommitdiffstats
path: root/core/vm
diff options
context:
space:
mode:
authorFelix Lange <fjl@twurst.com>2016-08-04 09:55:33 +0800
committerFelix Lange <fjl@twurst.com>2016-08-04 09:55:33 +0800
commit704fde01e8ed28877fe0f69fc8059b1f0c850d04 (patch)
treeb57e6dd9b93806d5803b6a45c5d4c9ac8d382481 /core/vm
parent0c9a858f2d8094d48371deee97493a1b1412e7de (diff)
downloadgo-tangerine-704fde01e8ed28877fe0f69fc8059b1f0c850d04.tar
go-tangerine-704fde01e8ed28877fe0f69fc8059b1f0c850d04.tar.gz
go-tangerine-704fde01e8ed28877fe0f69fc8059b1f0c850d04.tar.bz2
go-tangerine-704fde01e8ed28877fe0f69fc8059b1f0c850d04.tar.lz
go-tangerine-704fde01e8ed28877fe0f69fc8059b1f0c850d04.tar.xz
go-tangerine-704fde01e8ed28877fe0f69fc8059b1f0c850d04.tar.zst
go-tangerine-704fde01e8ed28877fe0f69fc8059b1f0c850d04.zip
core/types, core/vm: improve docs, add JSON marshaling methods
In this commit, core/types's types learn how to encode and decode themselves as JSON. The encoding is very similar to what the RPC API uses. The RPC API is missing some output fields (e.g. transaction signature values) which will be added to the API in a later commit. Some fields that the API generates are ignored by the decoder methods here.
Diffstat (limited to 'core/vm')
-rw-r--r--core/vm/log.go89
-rw-r--r--core/vm/log_test.go59
2 files changed, 127 insertions, 21 deletions
diff --git a/core/vm/log.go b/core/vm/log.go
index e4cc6021b..b292f5f43 100644
--- a/core/vm/log.go
+++ b/core/vm/log.go
@@ -18,6 +18,7 @@ package vm
import (
"encoding/json"
+ "errors"
"fmt"
"io"
@@ -25,18 +26,33 @@ import (
"github.com/ethereum/go-ethereum/rlp"
)
+var errMissingLogFields = errors.New("missing required JSON log fields")
+
+// Log represents a contract log event. These events are generated by the LOG
+// opcode and stored/indexed by the node.
type Log struct {
- // Consensus fields
- Address common.Address
- Topics []common.Hash
- Data []byte
+ // Consensus fields.
+ Address common.Address // address of the contract that generated the event
+ Topics []common.Hash // list of topics provided by the contract.
+ Data []byte // supplied by the contract, usually ABI-encoded
+
+ // Derived fields (don't reorder!).
+ BlockNumber uint64 // block in which the transaction was included
+ TxHash common.Hash // hash of the transaction
+ TxIndex uint // index of the transaction in the block
+ BlockHash common.Hash // hash of the block in which the transaction was included
+ Index uint // index of the log in the receipt
+}
- // Derived fields (don't reorder!)
- BlockNumber uint64
- TxHash common.Hash
- TxIndex uint
- BlockHash common.Hash
- Index uint
+type jsonLog struct {
+ Address *common.Address `json:"address"`
+ Topics *[]common.Hash `json:"topics"`
+ Data string `json:"data"`
+ BlockNumber string `json:"blockNumber"`
+ TxIndex string `json:"transactionIndex"`
+ TxHash *common.Hash `json:"transactionHash"`
+ BlockHash *common.Hash `json:"blockHash"`
+ Index string `json:"logIndex"`
}
func NewLog(address common.Address, topics []common.Hash, data []byte, number uint64) *Log {
@@ -64,19 +80,50 @@ func (l *Log) String() string {
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)
}
+// MarshalJSON implements json.Marshaler.
func (r *Log) MarshalJSON() ([]byte, error) {
- fields := map[string]interface{}{
- "address": r.Address,
- "data": fmt.Sprintf("%#x", r.Data),
- "blockNumber": fmt.Sprintf("%#x", r.BlockNumber),
- "logIndex": fmt.Sprintf("%#x", r.Index),
- "blockHash": r.BlockHash,
- "transactionHash": r.TxHash,
- "transactionIndex": fmt.Sprintf("%#x", r.TxIndex),
- "topics": r.Topics,
- }
+ return json.Marshal(&jsonLog{
+ Address: &r.Address,
+ Topics: &r.Topics,
+ Data: fmt.Sprintf("0x%x", r.Data),
+ BlockNumber: fmt.Sprintf("0x%x", r.BlockNumber),
+ TxIndex: fmt.Sprintf("0x%x", r.TxIndex),
+ TxHash: &r.TxHash,
+ BlockHash: &r.BlockHash,
+ Index: fmt.Sprintf("0x%x", r.Index),
+ })
+}
- return json.Marshal(fields)
+// UnmarshalJSON implements json.Umarshaler.
+func (r *Log) UnmarshalJSON(input []byte) error {
+ var dec jsonLog
+ if err := json.Unmarshal(input, &dec); err != nil {
+ return err
+ }
+ if dec.Address == nil || dec.Topics == nil || dec.Data == "" || dec.BlockNumber == "" ||
+ dec.TxIndex == "" || dec.TxHash == nil || dec.BlockHash == nil || dec.Index == "" {
+ return errMissingLogFields
+ }
+ declog := Log{
+ Address: *dec.Address,
+ Topics: *dec.Topics,
+ TxHash: *dec.TxHash,
+ BlockHash: *dec.BlockHash,
+ }
+ if _, err := fmt.Sscanf(dec.Data, "0x%x", &declog.Data); err != nil {
+ return fmt.Errorf("invalid hex log data")
+ }
+ if _, err := fmt.Sscanf(dec.BlockNumber, "0x%x", &declog.BlockNumber); err != nil {
+ return fmt.Errorf("invalid hex log block number")
+ }
+ if _, err := fmt.Sscanf(dec.TxIndex, "0x%x", &declog.TxIndex); err != nil {
+ return fmt.Errorf("invalid hex log tx index")
+ }
+ if _, err := fmt.Sscanf(dec.Index, "0x%x", &declog.Index); err != nil {
+ return fmt.Errorf("invalid hex log index")
+ }
+ *r = declog
+ return nil
}
type Logs []*Log
diff --git a/core/vm/log_test.go b/core/vm/log_test.go
new file mode 100644
index 000000000..775016f9c
--- /dev/null
+++ b/core/vm/log_test.go
@@ -0,0 +1,59 @@
+// Copyright 2016 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package vm
+
+import (
+ "encoding/json"
+ "testing"
+)
+
+var unmarshalLogTests = map[string]struct {
+ input string
+ wantError error
+}{
+ "ok": {
+ input: `{"address":"0xecf8f87f810ecf450940c9f60066b4a7a501d6a7","blockHash":"0x656c34545f90a730a19008c0e7a7cd4fb3895064b48d6d69761bd5abad681056","blockNumber":"0x1ecfa4","data":"0x000000000000000000000000000000000000000000000001a055690d9db80000","logIndex":"0x2","topics":["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef","0x00000000000000000000000080b2c9d7cbbf30a1b0fc8983c647d754c6525615","0x000000000000000000000000f9dff387dcb5cc4cca5b91adb07a95f54e9f1bb6"],"transactionHash":"0x3b198bfd5d2907285af009e9ae84a0ecd63677110d89d7e030251acb87f6487e","transactionIndex":"0x3"}`,
+ },
+ "missing data": {
+ input: `{"address":"0xecf8f87f810ecf450940c9f60066b4a7a501d6a7","blockHash":"0x656c34545f90a730a19008c0e7a7cd4fb3895064b48d6d69761bd5abad681056","blockNumber":"0x1ecfa4","logIndex":"0x2","topics":["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef","0x00000000000000000000000080b2c9d7cbbf30a1b0fc8983c647d754c6525615","0x000000000000000000000000f9dff387dcb5cc4cca5b91adb07a95f54e9f1bb6"],"transactionHash":"0x3b198bfd5d2907285af009e9ae84a0ecd63677110d89d7e030251acb87f6487e","transactionIndex":"0x3"}`,
+ wantError: errMissingLogFields,
+ },
+}
+
+func TestUnmarshalLog(t *testing.T) {
+ for name, test := range unmarshalLogTests {
+ var log *Log
+ err := json.Unmarshal([]byte(test.input), &log)
+ checkError(t, name, err, test.wantError)
+ }
+}
+
+func checkError(t *testing.T, testname string, got, want error) bool {
+ if got == nil {
+ if want != nil {
+ t.Errorf("test %q: got no error, want %q", testname, want)
+ return false
+ }
+ return true
+ }
+ if want == nil {
+ t.Errorf("test %q: unexpected error %q", testname, got)
+ } else if got.Error() != want.Error() {
+ t.Errorf("test %q: got error %q, want %q", testname, got, want)
+ }
+ return false
+}