Пример #1
0
func (db *BoltDB) Get(bucket []byte, key []byte, destination interfaces.BinaryMarshallable) (interfaces.BinaryMarshallable, error) {
	db.Sem.RLock()
	defer db.Sem.RUnlock()

	var v []byte
	db.db.View(func(tx *bolt.Tx) error {
		b := tx.Bucket(bucket)
		if b == nil {
			return nil
		}
		v = b.Get(key)
		if v == nil {
			return nil
		}
		return nil
	})
	if v == nil { // If the value is undefined, return nil
		return nil, nil
	}

	_, err := destination.UnmarshalBinaryData(v)
	if err != nil {
		return nil, err
	}
	return destination, nil
}
Пример #2
0
// Make this stuff easier to read.
func Unmarshall(thing interfaces.BinaryMarshallable, err error, data []byte) ([]byte, error) {
	if err != nil {
		return nil, err
	}
	newdata, err := thing.UnmarshalBinaryData(data)
	return newdata, err
}
Пример #3
0
func (db *MapDB) Get(bucket, key []byte, destination interfaces.BinaryMarshallable) (interfaces.BinaryMarshallable, error) {
	db.createCache(bucket)

	db.Sem.RLock()
	defer db.Sem.RUnlock()

	if db.Cache == nil {
		db.Cache = map[string]map[string][]byte{}
	}
	_, ok := db.Cache[string(bucket)]
	if ok == false {
		db.Cache[string(bucket)] = map[string][]byte{}
	}
	v, ok := db.Cache[string(bucket)][string(key)]
	if ok == false {
		return nil, nil
	}
	if v == nil {
		return nil, nil
	}
	_, err := destination.UnmarshalBinaryData(v)
	if err != nil {
		return nil, err
	}
	return destination, nil
}
Пример #4
0
func TestCommitMarshalUnmarshal(t *testing.T) {
	blocks := testHelper.CreateFullTestBlockSet()
	for _, block := range blocks {
		for _, tx := range block.ECBlock.GetEntries() {
			h1, err := tx.MarshalBinary()
			if err != nil {
				t.Errorf("Error marshalling - %v", err)
			}
			var h2 []byte
			var e interfaces.BinaryMarshallable
			switch tx.ECID() {
			case ECIDChainCommit:
				e = new(CommitChain)
				break
			case ECIDEntryCommit:
				e = new(CommitEntry)
				break
			case ECIDBalanceIncrease:
				e = new(IncreaseBalance)
				break
			case ECIDMinuteNumber:
				e = new(MinuteNumber)
				break
			case ECIDServerIndexNumber:
				e = new(ServerIndexNumber)
				break
			default:
				t.Error("Wrong ECID")
				break
			}

			h2, err = e.UnmarshalBinaryData(h1)
			if err != nil {
				t.Logf("Tried to unmarshal %x", h1)
				t.Errorf("Error unmarshalling - %v", err)
				continue
			}
			if len(h2) > 0 {
				t.Errorf("Leftovers from unmarshalling - %x", h2)
			}
			h2, err = e.MarshalBinary()
			if err != nil {
				t.Errorf("Error marshalling2 - %v", err)
				continue
			}

			if primitives.AreBytesEqual(h1, h2) == false {
				t.Error("ECEntries are not identical - %x vs %x", h1, h2)
			}
		}
	}
}
Пример #5
0
func (d *BoltDB) Put(bucket []byte, key []byte, data interfaces.BinaryMarshallable) error {
	hex, err := data.MarshalBinary()
	if err != nil {
		return err
	}
	err = d.db.Update(func(tx *bolt.Tx) error {
		_, err := tx.CreateBucketIfNotExists(bucket)
		if err != nil {
			return err
		}
		b := tx.Bucket(bucket)
		err = b.Put(key, hex)
		return err
	})
	return err
}
Пример #6
0
func (db *MapDB) rawPut(bucket, key []byte, data interfaces.BinaryMarshallable) error {
	if db.Cache == nil {
		db.Cache = map[string]map[string][]byte{}
	}
	_, ok := db.Cache[string(bucket)]
	if ok == false {
		db.Cache[string(bucket)] = map[string][]byte{}
	}
	var hex []byte
	var err error
	if data != nil {
		hex, err = data.MarshalBinary()
		if err != nil {
			return err
		}
	}
	db.Cache[string(bucket)][string(key)] = hex
	return nil
}
Пример #7
0
func (db *LevelDB) Put(bucket []byte, key []byte, data interfaces.BinaryMarshallable) error {
	if db.lbatch == nil {
		db.lbatch = new(leveldb.Batch)
	}

	defer db.lbatch.Reset()

	ldbKey := append(bucket, key...)
	hex, err := data.MarshalBinary()
	if err != nil {
		return err
	}
	db.lbatch.Put(ldbKey, hex)

	err = db.lDB.Write(db.lbatch, db.wo)
	if err != nil {
		return err
	}
	return nil
}
Пример #8
0
func (db *LevelDB) Get(bucket []byte, key []byte, destination interfaces.BinaryMarshallable) (interfaces.BinaryMarshallable, error) {
	db.dbLock.Lock()
	defer db.dbLock.Unlock()

	ldbKey := append(bucket, key...)
	data, err := db.lDB.Get(ldbKey, db.ro)
	if err != nil {
		if strings.Contains(err.Error(), "not found") {
			return nil, nil
		}
		return nil, err
	}

	_, err = destination.UnmarshalBinaryData(data)
	if err != nil {
		return nil, err
	}

	return destination, nil
}
Пример #9
0
func AreBinaryMarshallablesEqual(b1, b2 interfaces.BinaryMarshallable) (bool, error) {
	if b1 == nil {
		if b2 == nil {
			return true, nil
		}
		return false, nil
	}
	if b2 == nil {
		return false, nil
	}

	bytes1, err := b1.MarshalBinary()
	if err != nil {
		return false, err
	}
	bytes2, err := b2.MarshalBinary()
	if err != nil {
		return false, err
	}
	return AreBytesEqual(bytes1, bytes2), nil
}
Пример #10
0
func HandleV2RawData(state interfaces.IState, params interface{}) (interface{}, *primitives.JSONError) {
	hashkey := new(HashRequest)
	err := MapToObject(params, hashkey)
	if err != nil {
		panic(reflect.TypeOf(params))
		return nil, NewInvalidParamsError()
	}

	h, err := primitives.HexToHash(hashkey.Hash)
	if err != nil {
		return nil, NewInvalidHashError()
	}

	var block interfaces.BinaryMarshallable
	var b []byte

	if block, _ = state.FetchECTransactionByHash(h); block != nil {
		b, _ = block.MarshalBinary()
	} else if block, _ = state.FetchFactoidTransactionByHash(h); block != nil {
		b, _ = block.MarshalBinary()
	} else if block, _ = state.FetchEntryByHash(h); block != nil {
		b, _ = block.MarshalBinary()
	}

	if b == nil {
		dbase := state.GetAndLockDB()
		defer state.UnlockDB()

		// try to find the block data in db and return the first one found
		if block, _ = dbase.FetchFBlock(h); block != nil {
			b, _ = block.MarshalBinary()
		} else if block, _ = dbase.FetchDBlock(h); block != nil {
			b, _ = block.MarshalBinary()
		} else if block, _ = dbase.FetchABlock(h); block != nil {
			b, _ = block.MarshalBinary()
		} else if block, _ = dbase.FetchEBlock(h); block != nil {
			b, _ = block.MarshalBinary()
		} else if block, _ = dbase.FetchECBlock(h); block != nil {
			b, _ = block.MarshalBinary()
		} else if block, _ = dbase.FetchFBlock(h); block != nil {
			b, _ = block.MarshalBinary()
		} else if block, _ = dbase.FetchEntry(h); block != nil {
			b, _ = block.MarshalBinary()
		} else {
			return nil, NewEntryNotFoundError()
		}
	}

	d := new(RawDataResponse)
	d.Data = hex.EncodeToString(b)
	return d, nil
}
Пример #11
0
// Sign signs msg with PrivateKey and return Signature
func (pk *PrivateKey) MarshalSign(msg interfaces.BinaryMarshallable) (sig interfaces.IFullSignature) {
	data, _ := msg.MarshalBinary()
	return pk.Sign(data)
}